Skip to main content

tui_lipan/widgets/
context_provider.rs

1//! Typed context provider widget.
2
3use crate::core::context_value::ContextValue;
4use crate::core::element::{ContextProviderElement, Element, ElementKind};
5
6/// Provide a typed context value for a subtree.
7#[derive(Clone)]
8pub struct ContextProvider<T>
9where
10    T: ContextValue,
11{
12    value: T,
13    child: Element,
14}
15
16impl<T> ContextProvider<T>
17where
18    T: ContextValue,
19{
20    /// Create a new typed context provider.
21    pub fn new(value: T) -> Self {
22        Self {
23            value,
24            child: crate::widgets::Spacer::new().into(),
25        }
26    }
27
28    /// Set child content.
29    pub fn child(mut self, child: impl Into<Element>) -> Self {
30        self.child = child.into();
31        self
32    }
33}
34
35impl<T> From<ContextProvider<T>> for Element
36where
37    T: ContextValue,
38{
39    fn from(provider: ContextProvider<T>) -> Self {
40        Element::new(ElementKind::ContextProvider(Box::new(
41            ContextProviderElement::new(provider.value, provider.child),
42        )))
43    }
44}