1use std::hash::{DefaultHasher, Hash, Hasher};
8use std::sync::Arc;
9
10use repose_core::{BoxWithConstraintsScope, Modifier, SubcomposeScope, View, ViewKind};
11
12pub fn subcompose_hash_key<K: Hash>(key: &K) -> u64 {
18 let mut h = DefaultHasher::new();
19 key.hash(&mut h);
20 h.finish()
21}
22
23pub fn SubcomposeLayout<F>(modifier: Modifier, content: F) -> View
41where
42 F: Fn(SubcomposeScope) -> View + 'static,
43{
44 let wrapped: Arc<dyn Fn(&SubcomposeScope) -> Vec<(u64, View)>> =
45 Arc::new(move |scope| vec![(0, content(*scope))]);
46 View {
47 id: 0,
48 kind: ViewKind::SubcomposeLayout { content: wrapped },
49 modifier,
50 children: Vec::new(),
51 scope_key: None,
52 semantics: None,
53 }
54}
55
56pub fn subcompose_layout_with_slots<F>(modifier: Modifier, content: F) -> View
61where
62 F: Fn(SubcomposeScope) -> Vec<(u64, View)> + 'static,
63{
64 let wrapped: Arc<dyn Fn(&SubcomposeScope) -> Vec<(u64, View)>> =
65 Arc::new(move |scope| content(*scope));
66 View {
67 id: 0,
68 kind: ViewKind::SubcomposeLayout { content: wrapped },
69 modifier,
70 children: Vec::new(),
71 scope_key: None,
72 semantics: None,
73 }
74}
75
76pub fn BoxWithConstraints<F>(modifier: Modifier, content: F) -> View
83where
84 F: Fn(BoxWithConstraintsScope) -> View + 'static,
85{
86 SubcomposeLayout(modifier, move |scope| {
87 content(BoxWithConstraintsScope {
88 min_width: scope.min_width,
89 max_width: scope.max_width,
90 min_height: scope.min_height,
91 max_height: scope.max_height,
92 })
93 })
94}
95
96pub fn subcompose_with_key<K, F>(key: K, modifier: Modifier, content: F) -> View
111where
112 K: Hash,
113 F: Fn(SubcomposeScope) -> View + 'static,
114{
115 let hashed = subcompose_hash_key(&key);
116 SubcomposeLayout(modifier.key(hashed), content)
117}
118
119pub fn box_with_constraints_with_key<K, F>(key: K, modifier: Modifier, content: F) -> View
122where
123 K: Hash,
124 F: Fn(BoxWithConstraintsScope) -> View + 'static,
125{
126 subcompose_with_key(key, modifier, move |scope| {
127 content(BoxWithConstraintsScope {
128 min_width: scope.min_width,
129 max_width: scope.max_width,
130 min_height: scope.min_height,
131 max_height: scope.max_height,
132 })
133 })
134}
135
136pub fn subcompose_with_key_slots<K, F>(key: K, modifier: Modifier, content: F) -> View
140where
141 K: Hash,
142 F: Fn(SubcomposeScope) -> Vec<(u64, View)> + 'static,
143{
144 let hashed = subcompose_hash_key(&key);
145 subcompose_layout_with_slots(modifier.key(hashed), content)
146}
147
148#[cfg(test)]
149mod tests {
150 use super::*;
151 use crate::layout::LayoutEngine;
152 use crate::{Column, Interactions, ViewExt};
153 use std::collections::HashMap;
154 use std::sync::Arc;
155 use std::sync::atomic::{AtomicUsize, Ordering};
156
157 fn text_view(text: &str) -> View {
158 use repose_core::{
159 Color, FontStyle, FontWeight, TextAlign, TextDecoration, TextOverflow, ViewKind,
160 };
161 View {
162 id: 0,
163 kind: ViewKind::Text {
164 text: text.to_string(),
165 color: Color::WHITE,
166 font_size: 14.0,
167 soft_wrap: true,
168 max_lines: None,
169 overflow: TextOverflow::Visible,
170 font_family: None,
171 annotations: None,
172 text_align: TextAlign::Unspecified,
173 font_weight: FontWeight::NORMAL,
174 font_style: FontStyle::Normal,
175 text_decoration: TextDecoration::default(),
176 letter_spacing: 0.0,
177 line_height: 0.0,
178 },
179 modifier: Modifier::default(),
180 children: vec![],
181 scope_key: None,
182 semantics: None,
183 }
184 }
185
186 fn make_root(view: View) -> View {
187 Column(Modifier::new()).child(view)
188 }
189
190 #[test]
191 fn subcompose_hash_key_is_deterministic_and_distinguishes_values() {
192 assert_eq!(subcompose_hash_key(&"hello"), subcompose_hash_key(&"hello"));
193 assert_ne!(subcompose_hash_key(&"hello"), subcompose_hash_key(&"world"));
194 assert_eq!(
195 subcompose_hash_key(&(1u32, 2u32)),
196 subcompose_hash_key(&(1u32, 2u32))
197 );
198 assert_ne!(
199 subcompose_hash_key(&(1u32, 2u32)),
200 subcompose_hash_key(&(1u32, 3u32))
201 );
202 }
203
204 #[test]
205 fn subcompose_with_key_runs_closure_once_until_key_changes() {
206 let calls = Arc::new(AtomicUsize::new(0));
207 let calls_c = calls.clone();
208
209 let sub = subcompose_with_key(1u64, Modifier::new(), move |_scope| {
212 calls_c.fetch_add(1, Ordering::SeqCst);
213 text_view("k=1")
214 });
215 let root_v1 = make_root(sub);
216
217 let calls2 = calls.clone();
218 let sub = subcompose_with_key(2u64, Modifier::new(), move |_scope| {
219 calls2.fetch_add(1, Ordering::SeqCst);
220 text_view("k=2")
221 });
222 let root_v2 = make_root(sub);
223
224 let mut engine = LayoutEngine::new();
225
226 let _ = engine.layout_frame(
228 &root_v1,
229 (400, 400),
230 &HashMap::new(),
231 &Interactions::default(),
232 None,
233 );
234 assert_eq!(calls.load(Ordering::SeqCst), 1);
235
236 let _ = engine.layout_frame(
238 &root_v1,
239 (400, 400),
240 &HashMap::new(),
241 &Interactions::default(),
242 None,
243 );
244 assert_eq!(calls.load(Ordering::SeqCst), 1);
245
246 let _ = engine.layout_frame(
249 &root_v2,
250 (400, 400),
251 &HashMap::new(),
252 &Interactions::default(),
253 None,
254 );
255 assert_eq!(calls.load(Ordering::SeqCst), 2);
256 }
257
258 #[test]
259 fn box_with_constraints_with_key_forwards_scope() {
260 use crate::Box as RBox;
261 let sub = box_with_constraints_with_key(42u64, Modifier::new(), |scope| {
262 assert!(scope.max_width > 0.0);
263 RBox(Modifier::new())
264 });
265 match sub.kind {
267 ViewKind::SubcomposeLayout { .. } => {}
268 _ => panic!("expected SubcomposeLayout"),
269 }
270 }
271
272 #[test]
273 fn subcompose_with_key_slots_runs_closure_once_until_key_changes() {
274 let calls = Arc::new(AtomicUsize::new(0));
275 let calls_c = calls.clone();
276
277 let sub = subcompose_with_key_slots(1u64, Modifier::new(), move |_scope| {
278 calls_c.fetch_add(1, Ordering::SeqCst);
279 vec![(0, text_view("k=1")), (1, text_view("k=1b"))]
280 });
281 let root_v1 = make_root(sub);
282
283 let calls2 = calls.clone();
284 let sub2 = subcompose_with_key_slots(2u64, Modifier::new(), move |_scope| {
285 calls2.fetch_add(1, Ordering::SeqCst);
286 vec![(0, text_view("k=2"))]
287 });
288 let root_v2 = make_root(sub2);
289
290 let mut engine = LayoutEngine::new();
291
292 let _ = engine.layout_frame(
293 &root_v1,
294 (400, 400),
295 &HashMap::new(),
296 &Interactions::default(),
297 None,
298 );
299 assert_eq!(calls.load(Ordering::SeqCst), 1);
300
301 let _ = engine.layout_frame(
303 &root_v1,
304 (400, 400),
305 &HashMap::new(),
306 &Interactions::default(),
307 None,
308 );
309 assert_eq!(calls.load(Ordering::SeqCst), 1);
310
311 let _ = engine.layout_frame(
313 &root_v2,
314 (400, 400),
315 &HashMap::new(),
316 &Interactions::default(),
317 None,
318 );
319 assert_eq!(calls.load(Ordering::SeqCst), 2);
320 }
321}