1use super::mounting::ContentMount;
3use super::*;
4
5pub trait Content: sealed::Content {
6 fn validate(&self, scope: &Scope) -> Result<()>;
7 #[doc(hidden)]
8 fn install(self, scope: &Scope, parent: &impl ContentMount) -> glory_ui::Result;
9}
10impl sealed::Content for glory_ui::Children {}
11impl Content for glory_ui::Children {
12 fn validate(&self, _: &Scope) -> Result<()> {
13 Ok(())
14 }
15 fn install(self, _: &Scope, parent: &impl ContentMount) -> glory_ui::Result {
16 for child in self.into_widgets() {
17 parent.one(move || child)?;
18 }
19 Ok(())
20 }
21}
22impl<A: Actions + 'static> sealed::Content for View<A> {}
23impl<A: Actions + 'static> Content for View<A> {
24 fn validate(&self, scope: &Scope) -> Result<()> {
25 self.validate(scope)
26 }
27 fn install(self, _: &Scope, parent: &impl ContentMount) -> glory_ui::Result {
28 parent.one(move || self)
29 }
30}
31impl<C: Content> sealed::Content for Option<C> {}
32impl<C: Content> Content for Option<C> {
33 fn validate(&self, scope: &Scope) -> Result<()> {
34 self.as_ref()
35 .map_or(Ok(()), |content| content.validate(scope))
36 }
37 fn install(self, scope: &Scope, parent: &impl ContentMount) -> glory_ui::Result {
38 self.map_or(Ok(()), |content| content.install(scope, parent))
39 }
40}
41impl<C: Content> sealed::Content for Vec<C> {}
42impl<C: Content> Content for Vec<C> {
43 fn validate(&self, scope: &Scope) -> Result<()> {
44 for content in self {
45 content.validate(scope)?;
46 }
47 Ok(())
48 }
49 fn install(self, scope: &Scope, parent: &impl ContentMount) -> glory_ui::Result {
50 for content in self {
51 content.install(scope, parent)?;
52 }
53 Ok(())
54 }
55}
56impl<S, F, W> sealed::Content for glory_ui::Each<S, F>
57where
58 S: glory_ui::each::source::Source,
59 F: FnMut(S::Row) -> W + 'static,
60 W: Widget,
61{
62}
63impl<S, F, W> Content for glory_ui::Each<S, F>
64where
65 S: glory_ui::each::source::Source,
66 F: FnMut(S::Row) -> W + 'static,
67 W: Widget,
68{
69 fn validate(&self, scope: &Scope) -> Result<()> {
70 glory_ui::Each::validate(self, scope).map_err(error)
71 }
72 fn install(self, scope: &Scope, parent: &impl ContentMount) -> glory_ui::Result {
73 glory_ui::Each::install(self, scope, parent.clone())
74 }
75}
76pub struct Show<C, F, G> {
77 condition: C,
78 yes: F,
79 no: G,
80}
81pub fn show<C, F, G, V, W>(condition: C, yes: F, no: G) -> Show<C, F, G>
82where
83 C: FnMut() -> bool + 'static,
84 F: FnMut() -> V + 'static,
85 G: FnMut() -> W + 'static,
86 V: Widget,
87 W: Widget,
88{
89 Show { condition, yes, no }
90}
91impl<C, F, G> sealed::Content for Show<C, F, G> {}
92impl<C, F, G, V, W> Content for Show<C, F, G>
93where
94 C: FnMut() -> bool + 'static,
95 F: FnMut() -> V + 'static,
96 G: FnMut() -> W + 'static,
97 V: Widget,
98 W: Widget,
99{
100 fn validate(&self, _: &Scope) -> Result<()> {
101 Ok(())
102 }
103 fn install(mut self, scope: &Scope, parent: &impl ContentMount) -> glory_ui::Result {
104 let parent = parent.clone();
105 let mut branch = glory_ui::branch::Branch::default();
106 scope.structural(move || {
107 let result = branch.update((self.condition)(), |selected| {
108 let child = if selected {
109 parent.mount_widget(&mut self.yes)?
110 } else {
111 parent.mount_widget(&mut self.no)?
112 };
113 parent.attach(&child)?;
114 Ok::<_, glory_ui::Error>(Some(child))
115 });
116 if let Err(error) = result {
117 parent.report_error(error);
118 }
119 });
120 Ok(())
121 }
122}
123static RANGE: Template = Template::new(&[glory_html::view::outlet(Outlet::new("content"))]);
124pub fn content<C: Content + 'static>(value: C) -> impl Widget {
125 view(&RANGE).child(Outlet::new("content"), value)
126}
127pub fn empty() -> impl Widget {
128 content(None::<View>)
129}
130pub struct Lazy<F>(F);
131pub fn lazy<F, V>(factory: F) -> Lazy<F>
132where
133 F: FnOnce() -> V,
134 V: Widget,
135{
136 Lazy(factory)
137}
138impl<F> sealed::Content for Lazy<F> {}
139impl<F, V> Content for Lazy<F>
140where
141 F: FnOnce() -> V + 'static,
142 V: Widget,
143{
144 fn validate(&self, _: &Scope) -> Result<()> {
145 Ok(())
146 }
147 fn install(self, _: &Scope, parent: &impl ContentMount) -> glory_ui::Result {
148 parent.one(self.0)
149 }
150}