1use std::cell::RefCell;
4use std::collections::HashMap;
5use std::hash::Hash;
6use std::rc::Rc;
7
8use mitsuami_reactive::{IntoValue, Owner, Value, computed, effect, on_cleanup, untrack};
9
10use crate::ui::Ui;
11use crate::view::{AnyView, View};
12use crate::widget::{NodeId, WidgetKind};
13
14type Branch = Rc<dyn Fn() -> AnyView>;
15
16pub struct Show {
20 when: Value<bool>,
21 then: Branch,
22 fallback: Option<Branch>,
23}
24
25impl Show {
26 pub fn new<V: View>(when: impl IntoValue<bool>, then: impl Fn() -> V + 'static) -> Show {
27 Show { when: when.into_value(), then: Rc::new(move || AnyView::new(then())), fallback: None }
28 }
29
30 pub fn fallback<V: View>(mut self, fallback: impl Fn() -> V + 'static) -> Show {
31 self.fallback = Some(Rc::new(move || AnyView::new(fallback())));
32 self
33 }
34}
35
36impl View for Show {
37 fn build(self, ui: &Ui) -> NodeId {
38 let fragment = ui.create(WidgetKind::Fragment, Vec::new());
39 let Show { when, then, fallback } = self;
40 let ui = ui.clone();
41 let render = move |condition: bool| {
42 let branch = if condition { Some(&then) } else { fallback.as_ref() };
43 if let Some(branch) = branch {
44 let child = branch().build(&ui);
45 ui.append_child(fragment, child);
46 let ui = ui.clone();
47 on_cleanup(move || ui.destroy(child));
48 }
49 };
50 match when {
51 Value::Static(condition) => render(condition),
52 Value::Dynamic(f) => {
53 let condition = computed(move || f());
54 effect(move || {
55 let c = condition.get();
56 untrack(|| render(c));
57 });
58 }
59 }
60 fragment
61 }
62}
63
64type KeyFn<T, K> = Rc<dyn Fn(&T) -> K>;
65type RenderFn<T> = Rc<dyn Fn(T) -> AnyView>;
66
67pub struct For<T: 'static, K: 'static> {
74 each: Value<Vec<T>>,
75 key: KeyFn<T, K>,
76 render: RenderFn<T>,
77}
78
79impl<T: Clone + 'static, K: Eq + Hash + 'static> For<T, K> {
80 pub fn new<V: View>(
81 each: impl IntoValue<Vec<T>>,
82 key: impl Fn(&T) -> K + 'static,
83 render: impl Fn(T) -> V + 'static,
84 ) -> For<T, K> {
85 For { each: each.into_value(), key: Rc::new(key), render: Rc::new(move |item| AnyView::new(render(item))) }
86 }
87}
88
89impl<T: Clone + 'static, K: Eq + Hash + 'static> View for For<T, K> {
90 fn build(self, ui: &Ui) -> NodeId {
91 let fragment = ui.create(WidgetKind::Fragment, Vec::new());
92 let rows_scope = Owner::current().map(|o| o.child()).unwrap_or_else(Owner::new_root);
95 let rows: Rc<RefCell<Vec<(K, NodeId, Owner)>>> = Rc::default();
96 let For { each, key, render } = self;
97 let ui = ui.clone();
98 effect(move || {
99 let items = each.get();
100 untrack(|| {
101 let mut old: HashMap<K, (NodeId, Owner)> =
102 rows.borrow_mut().drain(..).map(|(k, id, owner)| (k, (id, owner))).collect();
103 let mut next = Vec::with_capacity(items.len());
104 for item in items {
105 let k = key(&item);
106 let (id, owner) = match old.remove(&k) {
107 Some(existing) => existing,
108 None => {
109 let owner = rows_scope.child();
110 let id = owner.with(|| render(item).build(&ui));
111 (id, owner)
112 }
113 };
114 next.push((k, id, owner));
115 }
116 for (_, (id, owner)) in old {
117 owner.dispose();
118 ui.destroy(id);
119 }
120 ui.set_children(fragment, next.iter().map(|(_, id, _)| *id).collect());
121 *rows.borrow_mut() = next;
122 });
123 });
124 fragment
125 }
126}
127
128impl Show {
133 #[doc(hidden)]
134 pub fn __tag() -> ShowWithoutWhen {
135 ShowWithoutWhen { fallback: None }
136 }
137}
138
139#[doc(hidden)]
140pub struct ShowWithoutWhen {
141 fallback: Option<Branch>,
142}
143
144impl ShowWithoutWhen {
145 pub fn when(self, when: impl IntoValue<bool>) -> ShowWhen {
146 ShowWhen { when: when.into_value(), fallback: self.fallback }
147 }
148
149 pub fn fallback<V: View>(mut self, fallback: impl Fn() -> V + 'static) -> ShowWithoutWhen {
150 self.fallback = Some(Rc::new(move || AnyView::new(fallback())));
151 self
152 }
153}
154
155#[doc(hidden)]
156pub struct ShowWhen {
157 when: Value<bool>,
158 fallback: Option<Branch>,
159}
160
161impl ShowWhen {
162 pub fn fallback<V: View>(mut self, fallback: impl Fn() -> V + 'static) -> ShowWhen {
163 self.fallback = Some(Rc::new(move || AnyView::new(fallback())));
164 self
165 }
166
167 pub fn __children<V: View>(self, then: impl Fn() -> V + 'static) -> Show {
168 Show { when: self.when, then: Rc::new(move || AnyView::new(then())), fallback: self.fallback }
169 }
170}
171
172impl For<(), ()> {
173 #[doc(hidden)]
174 pub fn __tag() -> ForWithoutEach {
175 ForWithoutEach
176 }
177}
178
179#[doc(hidden)]
180pub struct ForWithoutEach;
181
182impl ForWithoutEach {
183 pub fn each<T: Clone + 'static>(self, each: impl IntoValue<Vec<T>>) -> ForWithoutKey<T> {
184 ForWithoutKey { each: each.into_value() }
185 }
186}
187
188#[doc(hidden)]
189pub struct ForWithoutKey<T: 'static> {
190 each: Value<Vec<T>>,
191}
192
193impl<T: Clone + 'static> ForWithoutKey<T> {
194 pub fn key<K: Eq + Hash + 'static>(self, key: impl Fn(&T) -> K + 'static) -> ForKeyed<T, K> {
195 ForKeyed { each: self.each, key: Rc::new(key) }
196 }
197}
198
199#[doc(hidden)]
200pub struct ForKeyed<T: 'static, K: 'static> {
201 each: Value<Vec<T>>,
202 key: KeyFn<T, K>,
203}
204
205impl<T: Clone + 'static, K: Eq + Hash + 'static> ForKeyed<T, K> {
206 pub fn __children<V: View>(self, render: impl Fn(T) -> V + 'static) -> For<T, K> {
207 For { each: self.each, key: self.key, render: Rc::new(move |item| AnyView::new(render(item))) }
208 }
209}