1use std::hash::{DefaultHasher, Hash, Hasher};
12use std::sync::Arc;
13
14use repose_core::{BoxWithConstraintsScope, Modifier, SubcomposeScope, View, ViewKind};
15
16pub fn subcompose_hash_key<K: Hash>(key: &K) -> u64 {
22 let mut h = DefaultHasher::new();
23 key.hash(&mut h);
24 h.finish()
25}
26
27pub fn SubcomposeLayout<F>(modifier: Modifier, content: F) -> View
45where
46 F: Fn(SubcomposeScope) -> View + 'static,
47{
48 let wrapped: Arc<dyn Fn(&SubcomposeScope) -> Vec<(u64, View)>> =
49 Arc::new(move |scope| vec![(0, content(*scope))]);
50 View {
51 id: 0,
52 kind: ViewKind::SubcomposeLayout { content: wrapped },
53 modifier,
54 children: Vec::new(),
55 scope_key: None,
56 semantics: None,
57 }
58}
59
60pub fn subcompose_layout_with_slots<F>(modifier: Modifier, content: F) -> View
65where
66 F: Fn(SubcomposeScope) -> Vec<(u64, View)> + 'static,
67{
68 let wrapped: Arc<dyn Fn(&SubcomposeScope) -> Vec<(u64, View)>> =
69 Arc::new(move |scope| content(*scope));
70 View {
71 id: 0,
72 kind: ViewKind::SubcomposeLayout { content: wrapped },
73 modifier,
74 children: Vec::new(),
75 scope_key: None,
76 semantics: None,
77 }
78}
79
80pub fn BoxWithConstraints<F>(modifier: Modifier, content: F) -> View
87where
88 F: Fn(BoxWithConstraintsScope) -> View + 'static,
89{
90 SubcomposeLayout(modifier, move |scope| {
91 content(BoxWithConstraintsScope {
92 min_width: scope.min_width,
93 max_width: scope.max_width,
94 min_height: scope.min_height,
95 max_height: scope.max_height,
96 })
97 })
98}
99
100pub fn subcompose_with_key<K, F>(key: K, modifier: Modifier, content: F) -> View
115where
116 K: Hash,
117 F: Fn(SubcomposeScope) -> View + 'static,
118{
119 let hashed = subcompose_hash_key(&key);
120 SubcomposeLayout(modifier.key(hashed), content)
121}
122
123pub fn box_with_constraints_with_key<K, F>(key: K, modifier: Modifier, content: F) -> View
126where
127 K: Hash,
128 F: Fn(BoxWithConstraintsScope) -> View + 'static,
129{
130 subcompose_with_key(key, modifier, move |scope| {
131 content(BoxWithConstraintsScope {
132 min_width: scope.min_width,
133 max_width: scope.max_width,
134 min_height: scope.min_height,
135 max_height: scope.max_height,
136 })
137 })
138}
139
140pub fn subcompose_with_key_slots<K, F>(key: K, modifier: Modifier, content: F) -> View
144where
145 K: Hash,
146 F: Fn(SubcomposeScope) -> Vec<(u64, View)> + 'static,
147{
148 let hashed = subcompose_hash_key(&key);
149 subcompose_layout_with_slots(modifier.key(hashed), content)
150}
151
152#[cfg(test)]
153mod tests {
154 use super::*;
155 use crate::layout::LayoutEngine;
156 use crate::{Column, Interactions, ViewExt};
157 use std::collections::HashMap;
158 use std::sync::Arc;
159 use std::sync::atomic::{AtomicUsize, Ordering};
160
161 fn text_view(text: &str) -> View {
162 use repose_core::{
163 Color, FontStyle, FontWeight, TextAlign, TextDecoration, TextOverflow, ViewKind,
164 };
165 View {
166 id: 0,
167 kind: ViewKind::Text {
168 text: text.to_string(),
169 color: Color::WHITE,
170 font_size: 14.0,
171 soft_wrap: true,
172 max_lines: None,
173 overflow: TextOverflow::Visible,
174 font_family: None,
175 annotations: None,
176 text_align: TextAlign::Unspecified,
177 font_weight: FontWeight::NORMAL,
178 font_style: FontStyle::Normal,
179 text_decoration: TextDecoration::default(),
180 letter_spacing: 0.0,
181 line_height: 0.0,
182 url: None,
183 font_variation_settings: None,
184 },
185 modifier: Modifier::default(),
186 children: vec![],
187 scope_key: None,
188 semantics: None,
189 }
190 }
191
192 fn make_root(view: View) -> View {
193 Column(Modifier::new()).child(view)
194 }
195
196 #[test]
197 fn subcompose_hash_key_is_deterministic_and_distinguishes_values() {
198 assert_eq!(subcompose_hash_key(&"hello"), subcompose_hash_key(&"hello"));
199 assert_ne!(subcompose_hash_key(&"hello"), subcompose_hash_key(&"world"));
200 assert_eq!(
201 subcompose_hash_key(&(1u32, 2u32)),
202 subcompose_hash_key(&(1u32, 2u32))
203 );
204 assert_ne!(
205 subcompose_hash_key(&(1u32, 2u32)),
206 subcompose_hash_key(&(1u32, 3u32))
207 );
208 }
209
210 #[test]
211 fn subcompose_with_key_runs_closure_once_until_key_changes() {
212 let calls = Arc::new(AtomicUsize::new(0));
213 let calls_c = calls.clone();
214
215 let sub = subcompose_with_key(1u64, Modifier::new(), move |_scope| {
218 calls_c.fetch_add(1, Ordering::SeqCst);
219 text_view("k=1")
220 });
221 let root_v1 = make_root(sub);
222
223 let calls2 = calls.clone();
224 let sub = subcompose_with_key(2u64, Modifier::new(), move |_scope| {
225 calls2.fetch_add(1, Ordering::SeqCst);
226 text_view("k=2")
227 });
228 let root_v2 = make_root(sub);
229
230 let mut engine = LayoutEngine::new();
231
232 let _ = engine.layout_frame(
234 &root_v1,
235 (400, 400),
236 &HashMap::new(),
237 &Interactions::default(),
238 None,
239 );
240 assert_eq!(calls.load(Ordering::SeqCst), 1);
241
242 let _ = engine.layout_frame(
245 &root_v1,
246 (400, 400),
247 &HashMap::new(),
248 &Interactions::default(),
249 None,
250 );
251 assert_eq!(calls.load(Ordering::SeqCst), 2);
252
253 let _ = engine.layout_frame(
255 &root_v1,
256 (400, 400),
257 &HashMap::new(),
258 &Interactions::default(),
259 None,
260 );
261 assert_eq!(calls.load(Ordering::SeqCst), 2);
262
263 let _ = engine.layout_frame(
266 &root_v2,
267 (400, 400),
268 &HashMap::new(),
269 &Interactions::default(),
270 None,
271 );
272 assert_eq!(calls.load(Ordering::SeqCst), 3);
273 }
274
275 #[test]
276 fn box_with_constraints_with_key_forwards_scope() {
277 use crate::Box as RBox;
278 let sub = box_with_constraints_with_key(42u64, Modifier::new(), |scope| {
279 assert!(scope.max_width > 0.0);
280 RBox(Modifier::new())
281 });
282 match sub.kind {
284 ViewKind::SubcomposeLayout { .. } => {}
285 _ => panic!("expected SubcomposeLayout"),
286 }
287 }
288
289 #[test]
290 fn subcompose_with_key_slots_runs_closure_once_until_key_changes() {
291 let calls = Arc::new(AtomicUsize::new(0));
292 let calls_c = calls.clone();
293
294 let sub = subcompose_with_key_slots(1u64, Modifier::new(), move |_scope| {
295 calls_c.fetch_add(1, Ordering::SeqCst);
296 vec![(0, text_view("k=1")), (1, text_view("k=1b"))]
297 });
298 let root_v1 = make_root(sub);
299
300 let calls2 = calls.clone();
301 let sub2 = subcompose_with_key_slots(2u64, Modifier::new(), move |_scope| {
302 calls2.fetch_add(1, Ordering::SeqCst);
303 vec![(0, text_view("k=2"))]
304 });
305 let root_v2 = make_root(sub2);
306
307 let mut engine = LayoutEngine::new();
308
309 let _ = engine.layout_frame(
310 &root_v1,
311 (400, 400),
312 &HashMap::new(),
313 &Interactions::default(),
314 None,
315 );
316 assert_eq!(calls.load(Ordering::SeqCst), 1);
317
318 let _ = engine.layout_frame(
320 &root_v1,
321 (400, 400),
322 &HashMap::new(),
323 &Interactions::default(),
324 None,
325 );
326 assert_eq!(calls.load(Ordering::SeqCst), 2);
327
328 let _ = engine.layout_frame(
330 &root_v1,
331 (400, 400),
332 &HashMap::new(),
333 &Interactions::default(),
334 None,
335 );
336 assert_eq!(calls.load(Ordering::SeqCst), 2);
337
338 let _ = engine.layout_frame(
340 &root_v2,
341 (400, 400),
342 &HashMap::new(),
343 &Interactions::default(),
344 None,
345 );
346 assert_eq!(calls.load(Ordering::SeqCst), 3);
347 }
348}