Skip to main content

fission_core/ui/widgets/
provider.rs

1use crate::Widget;
2
3/// Installs a typed value for descendants during widget construction.
4///
5/// Provider values are read with [`crate::build::read`] or
6/// [`crate::build::try_read`]. The child closure is evaluated while the
7/// provider is active, so custom components below it can resolve the nearest
8/// value of `T`.
9pub struct Provider<T, F> {
10    pub value: T,
11    pub child: F,
12}
13
14impl<T, F> Provider<T, F> {
15    pub fn new(value: T, child: F) -> Self {
16        Self { value, child }
17    }
18}
19
20impl<T, F, R> From<Provider<T, F>> for Widget
21where
22    T: Clone + Send + Sync + 'static,
23    F: FnOnce() -> R,
24    R: Into<Widget>,
25{
26    fn from(provider: Provider<T, F>) -> Self {
27        crate::build::provide(provider.value, || (provider.child)().into())
28    }
29}
30
31pub fn provider<T, F>(value: T, child: F) -> Provider<T, F>
32where
33    T: Clone + Send + Sync + 'static,
34{
35    Provider::new(value, child)
36}