1use crate::{Result, Widget};
3use glory_core::Scope as CoreScope;
4use std::hash::Hash;
5pub mod source;
6use source::{Initial, IntoSource, Seeds, Source, Unkeyed, Values};
7mod mounting;
8#[doc(hidden)]
9pub use mounting::{Direct, Ownership, Reactive, UiMount, UiRow};
10
11pub struct EachBuilder<S> {
12 source: S,
13}
14pub fn each<S: IntoSource>(source: S) -> EachBuilder<S::Source> {
15 EachBuilder {
16 source: source.into_source(),
17 }
18}
19impl<T: Clone + 'static> EachBuilder<Unkeyed<T>> {
20 pub fn key<K, F>(self, key: F) -> EachBuilder<Values<T, K, F>>
21 where
22 K: Clone + Eq + Hash + 'static,
23 F: Fn(&T) -> K + 'static,
24 {
25 EachBuilder {
26 source: Values {
27 source: self.source.0,
28 key,
29 items: Vec::new(),
30 marker: std::marker::PhantomData,
31 },
32 }
33 }
34}
35impl<S: Source> EachBuilder<S> {
36 pub fn children<F, W>(self, factory: F) -> Each<S, F>
37 where
38 F: FnMut(S::Row) -> W + 'static,
39 W: Widget,
40 {
41 Each {
42 source: Some(self.source),
43 factory: Some(factory),
44 }
45 }
46}
47impl<S: Initial> EachBuilder<S> {
48 pub fn seed<F, W>(self, factory: F) -> Each<Seeds<S>, F>
51 where
52 F: FnMut(S::Value) -> W + 'static,
53 W: Widget,
54 {
55 Each {
56 source: Some(Seeds(self.source)),
57 factory: Some(factory),
58 }
59 }
60}
61
62pub struct Each<S, F> {
63 source: Option<S>,
64 factory: Option<F>,
65}
66
67#[doc(hidden)]
69pub trait Mount: Clone + 'static {
70 type Row<O: Ownership>: 'static;
71 fn mount<O: Ownership, W: Widget>(
72 &self,
73 factory: impl FnOnce(&CoreScope) -> W,
74 ) -> Result<Self::Row<O>>;
75 fn place<O: Ownership>(&self, row: &Self::Row<O>, before: Option<&Self::Row<O>>) -> Result;
76 fn has_content<O: Ownership>(&self, row: &Self::Row<O>) -> bool;
77 fn report(&self, error: crate::Error);
78 fn clear_on_error(&self) -> bool {
79 false
80 }
81}
82struct Row<M, D> {
83 mounted: M,
84 data: D,
85}
86struct Host<S: Source, F, M: Mount> {
87 source: S,
88 factory: F,
89 parent: M,
90 fault: Option<crate::Error>,
91}
92impl<S, F, W, M> crate::list::RowHost for Host<S, F, M>
93where
94 S: Source,
95 F: FnMut(S::Row) -> W + 'static,
96 W: Widget,
97 M: Mount,
98{
99 type Key = S::Key;
100 type Row = Option<Row<M::Row<S::Ownership>, S::Data>>;
101 fn key(&mut self, index: usize) -> S::Key {
102 self.source.key(index)
103 }
104 fn mount(&mut self, index: usize, key: &S::Key) -> Self::Row {
105 if self.fault.is_some() {
106 return None;
107 }
108 let seed = match self.source.prepare(index, key) {
109 Ok(seed) => seed,
110 Err(error) => {
111 self.fault = Some(error);
112 return None;
113 }
114 };
115 let mut data = None;
116 let result = self.parent.mount::<S::Ownership, _>(|owner| {
117 let (row, state) = self.source.row(seed, owner);
118 data = Some(state);
119 (self.factory)(row)
120 });
121 match result {
122 Ok(mounted) => Some(Row {
123 mounted,
124 data: data.expect("row factory completed"),
125 }),
126 Err(error) => {
127 self.fault = Some(error);
128 None
129 }
130 }
131 }
132 fn update(&mut self, index: usize, _: &S::Key, row: &mut Self::Row) {
133 if let Some(row) = row {
134 self.source.update(index, &mut row.data);
135 }
136 }
137 fn has_content(&self, row: &Self::Row) -> bool {
138 row.as_ref()
139 .is_some_and(|row| self.parent.has_content(&row.mounted))
140 }
141 fn place(&mut self, row: &Self::Row, before: Option<&Self::Row>) {
142 if self.fault.is_none()
143 && let Some(row) = row
144 && let Err(error) = self.parent.place(
145 &row.mounted,
146 before.and_then(Option::as_ref).map(|row| &row.mounted),
147 )
148 {
149 self.fault = Some(error);
150 }
151 }
152 fn dispose(&mut self, row: Self::Row) {
153 drop(row);
154 }
155}
156impl<S, F, W> Each<S, F>
157where
158 S: Source,
159 F: FnMut(S::Row) -> W + 'static,
160 W: Widget,
161{
162 #[doc(hidden)]
163 pub fn validate(&self, scope: &CoreScope) -> Result {
164 self.source
165 .as_ref()
166 .ok_or(crate::Error::Gone)?
167 .validate(scope.runtime())
168 }
169 #[doc(hidden)]
170 pub fn install<M: Mount>(mut self, scope: &CoreScope, parent: M) -> Result {
171 self.install_in(scope, parent)
172 }
173 fn install_in<M: Mount>(&mut self, scope: &CoreScope, parent: M) -> Result {
174 self.validate(scope)?;
175 let mut host = Host {
176 source: self.source.take().ok_or(crate::Error::Gone)?,
177 factory: self.factory.take().ok_or(crate::Error::Gone)?,
178 parent,
179 fault: None,
180 };
181 let mut cache = crate::list::Cache::default();
182 let mut revision = 0u64;
183 scope.structural(move || {
184 let count = match host.source.refresh_with(|key| cache.row(key).is_some()) {
185 Ok(count) => count,
186 Err(error) => {
187 host.parent.report(error);
188 return;
189 }
190 };
191 revision = revision.checked_add(1).expect("list revision exhausted");
192 if let Err(error) = cache.reconcile(0..count, revision, &mut host) {
193 host.parent.report(crate::Error::List(error));
194 }
195 if let Some(error) = host.fault.take() {
196 if host.parent.clear_on_error() {
197 cache.clear(&mut host);
198 }
199 host.parent.report(error);
200 }
201 });
202 Ok(())
203 }
204}
205impl<S, F, W> Widget for Each<S, F>
206where
207 S: Source,
208 F: FnMut(S::Row) -> W + 'static,
209 W: Widget,
210{
211 fn build(&mut self, scope: &crate::Scope) -> Result {
212 self.validate(scope)?;
213 let anchor = scope.create(glory_render::Descriptor::new(glory_render::Props::Group))?;
214 let content = scope.child_in(anchor)?;
215 self.install_in(&content, UiMount::new(&content))?;
216 scope.retain(content)
217 }
218}