Skip to main content

repose_tree/
hash.rs

1//! Content hashing for change detection.
2
3use rapidhash::fast::RapidHasher;
4use repose_core::{
5    Brush, Color, Dp, GradientStops, Modifier, Px, Sp, TextOverflow, View, ViewKind,
6    animation::{AnimationSpec, Easing},
7    scroll::ScrollBinding,
8    text::{Shadow, SpanStyle, TextStyle},
9};
10use std::hash::{Hash, Hasher};
11
12#[allow(dead_code)]
13fn hash_view_content_inner(view: &View, hasher: &mut impl Hasher) {
14    view.id.hash(hasher);
15    hash_view_kind(&view.kind, hasher);
16    hash_modifier(&view.modifier, hasher);
17    view.scope_key.hash(hasher);
18    if let Some(semantics) = &view.semantics {
19        1u8.hash(hasher);
20        std::mem::discriminant(&semantics.role).hash(hasher);
21        semantics.label.hash(hasher);
22        semantics.focused.hash(hasher);
23        semantics.enabled.hash(hasher);
24        semantics.selectable_group.hash(hasher);
25        semantics.checked.hash(hasher);
26        semantics.selected.hash(hasher);
27        semantics.value.hash(hasher);
28    } else {
29        0u8.hash(hasher);
30    }
31    match view.modifier.key {
32        Some(key) => {
33            1u8.hash(hasher);
34            key.hash(hasher);
35        }
36        None => 0u8.hash(hasher),
37    }
38}
39
40pub fn hash_view_content(view: &View) -> u64 {
41    hash_view_facets(view).combined()
42}
43
44#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
45pub struct ViewHashes {
46    pub layout: u64,
47    pub measurement: u64,
48    pub paint: u64,
49    pub semantics: u64,
50}
51
52impl ViewHashes {
53    pub fn combined(self) -> u64 {
54        let mut hasher = RapidHasher::default();
55        0x5245_504f_5345_2d48u64.hash(&mut hasher);
56        self.layout.hash(&mut hasher);
57        self.measurement.hash(&mut hasher);
58        self.paint.hash(&mut hasher);
59        self.semantics.hash(&mut hasher);
60        hasher.finish()
61    }
62}
63
64#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
65pub struct SubtreeHashes {
66    pub layout: u64,
67    pub measurement: u64,
68    pub paint: u64,
69    pub semantics: u64,
70}
71
72impl SubtreeHashes {
73    pub fn combined(self) -> u64 {
74        let mut hasher = RapidHasher::default();
75        0x5245_504f_5345_2d53u64.hash(&mut hasher);
76        self.layout.hash(&mut hasher);
77        self.measurement.hash(&mut hasher);
78        self.paint.hash(&mut hasher);
79        self.semantics.hash(&mut hasher);
80        hasher.finish()
81    }
82}
83
84pub(crate) struct SubtreeHashBuilder {
85    layout: RapidHasher<'static>,
86    measurement: RapidHasher<'static>,
87    paint: RapidHasher<'static>,
88    semantics: RapidHasher<'static>,
89}
90
91impl SubtreeHashBuilder {
92    pub fn new(child_count: usize) -> Self {
93        let mut builder = Self {
94            layout: RapidHasher::default(),
95            measurement: RapidHasher::default(),
96            paint: RapidHasher::default(),
97            semantics: RapidHasher::default(),
98        };
99        builder.layout.write_u64(0x5355_4254_5245_45);
100        builder.measurement.write_u64(0x534d_4541_5355_5245);
101        builder.paint.write_u64(0x5041_494e_545f_5355);
102        builder.semantics.write_u64(0x5345_4d41_4e54_4943);
103        builder.layout.write_usize(child_count);
104        builder.measurement.write_usize(child_count);
105        builder.paint.write_usize(child_count);
106        builder.semantics.write_usize(child_count);
107        builder
108    }
109
110    pub fn push(&mut self, child: SubtreeHashes) {
111        self.layout.write_u64(child.layout);
112        self.measurement.write_u64(child.measurement);
113        self.paint.write_u64(child.paint);
114        self.semantics.write_u64(child.semantics);
115    }
116
117    pub fn finish(mut self, content: ViewHashes) -> SubtreeHashes {
118        self.layout.write_u64(content.layout);
119        self.measurement.write_u64(content.measurement);
120        self.paint.write_u64(content.paint);
121        self.semantics.write_u64(content.semantics);
122        SubtreeHashes {
123            layout: self.layout.finish(),
124            measurement: self.measurement.finish(),
125            paint: self.paint.finish(),
126            semantics: self.semantics.finish(),
127        }
128    }
129}
130
131const FACET_LAYOUT: u8 = 1;
132const FACET_MEASUREMENT: u8 = 2;
133const FACET_PAINT: u8 = 4;
134const FACET_SEMANTICS: u8 = 8;
135const FACET_LAYOUT_MEASUREMENT: u8 = FACET_LAYOUT | FACET_MEASUREMENT;
136const FACET_LAYOUT_PAINT: u8 = FACET_LAYOUT | FACET_PAINT;
137const FACET_LAYOUT_MEASUREMENT_PAINT: u8 = FACET_LAYOUT | FACET_MEASUREMENT | FACET_PAINT;
138const FACET_ALL: u8 = FACET_LAYOUT | FACET_MEASUREMENT | FACET_PAINT | FACET_SEMANTICS;
139
140type FacetHasher = RapidHasher<'static>;
141
142#[derive(Default)]
143struct FacetHashers {
144    hashers: [FacetHasher; 4],
145}
146
147impl FacetHashers {
148    fn hash(&mut self, mask: u8, write: impl Fn(&mut FacetHasher)) {
149        for (index, hasher) in self.hashers.iter_mut().enumerate() {
150            if mask & (1 << index) != 0 {
151                write(hasher);
152            }
153        }
154    }
155
156    fn finish(self) -> ViewHashes {
157        ViewHashes {
158            layout: self.hashers[0].finish(),
159            measurement: self.hashers[1].finish(),
160            paint: self.hashers[2].finish(),
161            semantics: self.hashers[3].finish(),
162        }
163    }
164}
165
166pub fn hash_view_facets(view: &View) -> ViewHashes {
167    let mut hashers = FacetHashers::default();
168    hashers.hash(FACET_ALL, |hasher| {
169        0x5245_504f_5345_2d56u64.hash(hasher);
170    });
171    facet_hash_view_kind(&view.kind, &mut hashers);
172    facet_hash_modifier(&view.modifier, &mut hashers);
173    hashers.hash(FACET_LAYOUT_PAINT, |hasher| view.id.hash(hasher));
174    hashers.hash(FACET_ALL ^ FACET_SEMANTICS, |hasher| {
175        view.scope_key.hash(hasher);
176    });
177    facet_hash_semantics(view.semantics.as_ref(), &mut hashers, FACET_SEMANTICS);
178    hashers.finish()
179}
180
181pub fn hash_view_layout(view: &View) -> u64 {
182    hash_view_facets(view).layout
183}
184
185pub fn hash_view_measurement(view: &View) -> u64 {
186    hash_view_facets(view).measurement
187}
188
189pub fn hash_view_paint(view: &View) -> u64 {
190    hash_view_facets(view).paint
191}
192
193pub fn hash_view_semantics(view: &View) -> u64 {
194    hash_view_facets(view).semantics
195}
196
197pub fn hash_subtree_facets(content: ViewHashes, children: &[SubtreeHashes]) -> SubtreeHashes {
198    let mut builder = SubtreeHashBuilder::new(children.len());
199    for child in children {
200        builder.push(*child);
201    }
202    builder.finish(content)
203}
204
205fn facet_hash_view_kind(kind: &ViewKind, hashers: &mut FacetHashers) {
206    hashers.hash(FACET_ALL, |hasher| {
207        std::mem::discriminant(kind).hash(hasher);
208    });
209    match kind {
210        ViewKind::Text {
211            text,
212            color,
213            font_size,
214            soft_wrap,
215            max_lines,
216            overflow,
217            font_family,
218            annotations,
219            text_align,
220            font_weight,
221            font_style,
222            text_decoration,
223            letter_spacing,
224            line_height,
225            url,
226            font_variation_settings,
227            draw_style,
228        } => {
229            hashers.hash(FACET_LAYOUT_MEASUREMENT, |h| text.hash(h));
230            hashers.hash(FACET_PAINT, |h| hash_color(color, h));
231            hashers.hash(FACET_LAYOUT_MEASUREMENT, |h| hash_sp(*font_size, h));
232            hashers.hash(FACET_LAYOUT_MEASUREMENT, |h| soft_wrap.hash(h));
233            hashers.hash(FACET_LAYOUT_MEASUREMENT, |h| max_lines.hash(h));
234            hashers.hash(FACET_LAYOUT_MEASUREMENT, |h| {
235                hash_text_overflow(overflow, h)
236            });
237            hashers.hash(FACET_LAYOUT_MEASUREMENT, |h| font_family.hash(h));
238            hashers.hash(FACET_LAYOUT_MEASUREMENT, |h| text_align.hash(h));
239            hashers.hash(FACET_LAYOUT_MEASUREMENT, |h| font_weight.0.hash(h));
240            hashers.hash(FACET_LAYOUT_MEASUREMENT, |h| font_style.hash(h));
241            hashers.hash(FACET_PAINT, |h| hash_text_decoration(text_decoration, h));
242            hashers.hash(FACET_LAYOUT_MEASUREMENT, |h| hash_sp(*letter_spacing, h));
243            hashers.hash(FACET_LAYOUT_MEASUREMENT, |h| hash_sp(*line_height, h));
244            hashers.hash(FACET_PAINT, |h| url.hash(h));
245            hashers.hash(FACET_LAYOUT_MEASUREMENT, |h| {
246                font_variation_settings.hash(h)
247            });
248            hashers.hash(FACET_LAYOUT_MEASUREMENT_PAINT, |h| {
249                hash_draw_style(draw_style, h)
250            });
251            if let Some(annotations) = annotations {
252                hashers.hash(FACET_LAYOUT_MEASUREMENT_PAINT, |h| {
253                    1u8.hash(h);
254                    annotations.len().hash(h);
255                    for span in annotations.iter() {
256                        span.start.hash(h);
257                        span.end.hash(h);
258                    }
259                });
260                for span in annotations.iter() {
261                    facet_hash_span_style(&span.style, hashers, FACET_ALL);
262                    if let Some(url) = &span.url {
263                        hashers.hash(FACET_PAINT, |h| url.hash(h));
264                    } else {
265                        hashers.hash(FACET_PAINT, |h| 0u8.hash(h));
266                    }
267                }
268            } else {
269                hashers.hash(FACET_LAYOUT_MEASUREMENT_PAINT, |h| 0u8.hash(h));
270            }
271        }
272        ViewKind::Image {
273            handle,
274            tint,
275            fit,
276            filter,
277            source_rect,
278            alignment,
279        } => {
280            hashers.hash(FACET_LAYOUT_MEASUREMENT_PAINT, |h| handle.hash(h));
281            // Natural size drives the view's intrinsic measurement, and it can
282            // arrive after the view was first composed (encoded uploads are
283            // decoded by the renderer), so it must invalidate measurement.
284            hashers.hash(FACET_MEASUREMENT, |h| {
285                repose_core::image_intrinsic_size(*handle).hash(h)
286            });
287            hashers.hash(FACET_PAINT, |h| hash_color(tint, h));
288            hashers.hash(FACET_PAINT, |h| std::mem::discriminant(fit).hash(h));
289            hashers.hash(FACET_PAINT, |h| std::mem::discriminant(filter).hash(h));
290            hashers.hash(FACET_PAINT, |h| source_rect.hash(h));
291            hashers.hash(FACET_PAINT, |h| std::mem::discriminant(alignment).hash(h));
292        }
293        ViewKind::Box
294        | ViewKind::Row
295        | ViewKind::Column
296        | ViewKind::ZStack
297        | ViewKind::OverlayHost
298        | ViewKind::SubcomposeLayout { .. } => {}
299        _ => {}
300    }
301}
302
303fn facet_hash_modifier(modifier: &Modifier, hashers: &mut FacetHashers) {
304    macro_rules! hash_value {
305        ($mask:expr, $value:expr) => {
306            hashers.hash($mask, |hasher| $value.hash(hasher));
307        };
308    }
309    macro_rules! hash_f32_value {
310        ($mask:expr, $value:expr) => {
311            hashers.hash($mask, |hasher| hash_f32($value, hasher));
312        };
313    }
314    macro_rules! hash_opt_f32_value {
315        ($mask:expr, $value:expr) => {
316            hashers.hash($mask, |hasher| hash_opt_f32($value, hasher));
317        };
318    }
319    macro_rules! hash_opt_dp_value {
320        ($mask:expr, $value:expr) => {
321            hashers.hash($mask, |hasher| hash_opt_dp($value, hasher));
322        };
323    }
324
325    hash_value!(FACET_LAYOUT_PAINT, modifier.key);
326    match modifier.size {
327        Some(size) => {
328            hashers.hash(FACET_LAYOUT, |h| {
329                hash_dp(size.width, h);
330                hash_dp(size.height, h);
331            });
332        }
333        None => hashers.hash(FACET_LAYOUT, |h| 0u8.hash(h)),
334    }
335    hash_opt_dp_value!(FACET_LAYOUT, modifier.width);
336    hash_opt_dp_value!(FACET_LAYOUT, modifier.height);
337    hashers.hash(FACET_LAYOUT, |h| match modifier.wrap_content {
338        Some((align, unbounded)) => {
339            1u8.hash(h);
340            std::mem::discriminant(&align).hash(h);
341            unbounded.hash(h);
342        }
343        None => 0u8.hash(h),
344    });
345    match modifier.required_size {
346        Some(size) => hashers.hash(FACET_LAYOUT, |h| {
347            hash_dp(size.width, h);
348            hash_dp(size.height, h);
349        }),
350        None => hashers.hash(FACET_LAYOUT, |h| 0u8.hash(h)),
351    }
352    hash_opt_dp_value!(FACET_LAYOUT, modifier.required_min_width);
353    hash_opt_dp_value!(FACET_LAYOUT, modifier.required_max_width);
354    hash_opt_dp_value!(FACET_LAYOUT, modifier.required_min_height);
355    hash_opt_dp_value!(FACET_LAYOUT, modifier.required_max_height);
356    hash_opt_dp_value!(FACET_LAYOUT, modifier.default_min_width);
357    hash_opt_dp_value!(FACET_LAYOUT, modifier.default_min_height);
358    hash_opt_f32_value!(FACET_LAYOUT, modifier.fill_max);
359    hash_opt_f32_value!(FACET_LAYOUT, modifier.fill_max_w);
360    hash_opt_f32_value!(FACET_LAYOUT, modifier.fill_max_h);
361    hashers.hash(FACET_PAINT, |h| modifier.repaint_boundary.hash(h));
362    hash_opt_dp_value!(FACET_LAYOUT, modifier.padding);
363    match modifier.padding_values {
364        Some(padding) => hashers.hash(FACET_LAYOUT, |h| {
365            hash_dp(padding.left, h);
366            hash_dp(padding.right, h);
367            hash_dp(padding.top, h);
368            hash_dp(padding.bottom, h);
369        }),
370        None => hashers.hash(FACET_LAYOUT, |h| 0u8.hash(h)),
371    }
372    hash_opt_dp_value!(FACET_LAYOUT, modifier.min_width);
373    hash_opt_dp_value!(FACET_LAYOUT, modifier.min_height);
374    hash_opt_dp_value!(FACET_LAYOUT, modifier.max_width);
375    hash_opt_dp_value!(FACET_LAYOUT, modifier.max_height);
376    if let Some(background) = &modifier.background {
377        hashers.hash(FACET_PAINT, |h| hash_brush(background, h));
378    } else {
379        hashers.hash(FACET_PAINT, |h| 0u8.hash(h));
380    }
381    hash_opt_f32_value!(FACET_PAINT, modifier.background_alpha);
382    hashers.hash(FACET_PAINT, |h| {
383        hash_opt_shape(modifier.background_shape.as_ref(), h)
384    });
385    if let Some(border) = &modifier.border {
386        hashers.hash(FACET_PAINT, |h| {
387            hash_dp(border.width, h);
388            hash_brush(&border.brush, h);
389            for radius in border.radius {
390                hash_dp(radius, h);
391            }
392        });
393    } else {
394        hashers.hash(FACET_PAINT, |h| 0u8.hash(h));
395    }
396    hashers.hash(FACET_PAINT, |h| {
397        hash_opt_shape(modifier.border_shape.as_ref(), h)
398    });
399    hash_opt_f32_value!(FACET_LAYOUT, modifier.flex_grow);
400    hash_opt_f32_value!(FACET_LAYOUT, modifier.flex_shrink);
401    hash_opt_dp_value!(FACET_LAYOUT, modifier.flex_basis);
402    hashers.hash(FACET_LAYOUT, |h| {
403        modifier
404            .flex_wrap
405            .map(|value| std::mem::discriminant(&value))
406            .hash(h);
407        modifier.flex_basis_content.hash(h);
408        modifier.flex_line_count.hash(h);
409        modifier
410            .flex_dir
411            .map(|value| std::mem::discriminant(&value))
412            .hash(h);
413    });
414    hashers.hash(FACET_LAYOUT, |h| {
415        modifier
416            .align_self
417            .map(|value| (value.keyword as u8, value.safety as u8))
418            .hash(h);
419        modifier
420            .justify_content
421            .map(|value| (value.keyword as u8, value.safety as u8))
422            .hash(h);
423        modifier
424            .align_items_container
425            .map(|value| (value.keyword as u8, value.safety as u8))
426            .hash(h);
427        modifier
428            .align_content
429            .map(|value| (value.keyword as u8, value.safety as u8))
430            .hash(h);
431        modifier.baseline_align.hash(h);
432    });
433    if let Some(radius) = modifier.clip_rounded {
434        hashers.hash(FACET_PAINT, |h| {
435            for value in radius {
436                hash_dp(value, h);
437            }
438        });
439    } else {
440        hashers.hash(FACET_PAINT, |h| 0u8.hash(h));
441    }
442    hashers.hash(FACET_PAINT, |h| {
443        hash_opt_shape(modifier.clip_shape.as_ref(), h)
444    });
445    if let Some(transform) = &modifier.transform {
446        hashers.hash(FACET_PAINT, |h| {
447            hash_f32(transform.translate_x, h);
448            hash_f32(transform.translate_y, h);
449            hash_f32(transform.scale_x, h);
450            hash_f32(transform.scale_y, h);
451            hash_f32(transform.rotate, h);
452            hash_f32(transform.shear_x, h);
453            hash_f32(transform.shear_y, h);
454            hash_f32(transform.rotation_x, h);
455            hash_f32(transform.rotation_y, h);
456            hash_f32(transform.camera_distance, h);
457            hash_f32(transform.origin_x, h);
458            hash_f32(transform.origin_y, h);
459            for value in transform.perspective {
460                hash_f32(value, h);
461            }
462        });
463    } else {
464        hashers.hash(FACET_PAINT, |h| 0u8.hash(h));
465    }
466    hash_opt_f32_value!(FACET_PAINT, modifier.alpha);
467    hashers.hash(FACET_LAYOUT, |h| {
468        modifier
469            .position_type
470            .map(|value| std::mem::discriminant(&value))
471            .hash(h);
472    });
473    hash_opt_dp_value!(FACET_LAYOUT, modifier.offset_left);
474    hash_opt_dp_value!(FACET_LAYOUT, modifier.offset_right);
475    hash_opt_dp_value!(FACET_LAYOUT, modifier.offset_top);
476    hash_opt_dp_value!(FACET_LAYOUT, modifier.offset_bottom);
477    hashers.hash(FACET_LAYOUT, |h| {
478        hash_opt_dp_pair(modifier.layout_offset, h)
479    });
480    hashers.hash(FACET_LAYOUT, |h| {
481        hash_opt_dp_pair(modifier.absolute_layout_offset, h)
482    });
483    if let Some(grid) = &modifier.grid {
484        hashers.hash(FACET_LAYOUT, |h| {
485            grid.columns.hash(h);
486            hash_dp(grid.row_gap, h);
487            hash_dp(grid.column_gap, h);
488        });
489    } else {
490        hashers.hash(FACET_LAYOUT, |h| 0u8.hash(h));
491    }
492    hashers.hash(FACET_LAYOUT, |h| {
493        modifier.grid_col_span.hash(h);
494        modifier.grid_row_span.hash(h);
495    });
496    hash_opt_f32_value!(FACET_LAYOUT, modifier.aspect_ratio);
497    hashers.hash(FACET_LAYOUT, |h| modifier.intrinsic_width.hash(h));
498    hashers.hash(FACET_LAYOUT, |h| modifier.intrinsic_height.hash(h));
499    hash_opt_dp_value!(FACET_LAYOUT, modifier.fit_content_width);
500    hash_opt_dp_value!(FACET_LAYOUT, modifier.fit_content_height);
501    hashers.hash(FACET_LAYOUT, |h| match modifier.contain {
502        None => 0u8.hash(h),
503        Some(value) if value == taffy::Contain::CONTENT => 3u8.hash(h),
504        Some(value) if value == taffy::Contain::LAYOUT => 1u8.hash(h),
505        Some(value) if value == taffy::Contain::PAINT => 2u8.hash(h),
506        Some(_) => 4u8.hash(h),
507    });
508    hash_f32_value!(FACET_PAINT, modifier.z_index);
509    hash_opt_f32_value!(FACET_PAINT, modifier.render_z_index);
510    hashers.hash(FACET_PAINT, |h| {
511        modifier.input_blocker.hash(h);
512        modifier.click.hash(h);
513        modifier.disabled.hash(h);
514        modifier.focusable.hash(h);
515        modifier.hit_passthrough.hash(h);
516    });
517    hashers.hash(FACET_LAYOUT_MEASUREMENT_PAINT, |h| modifier.visible.hash(h));
518    hashers.hash(FACET_LAYOUT, |h| modifier.propagate_min.hash(h));
519    hashers.hash(FACET_PAINT, |h| modifier.focus_group.hash(h));
520    if let Some(blur) = &modifier.blur {
521        hashers.hash(FACET_PAINT, |h| {
522            hash_dp(blur.radius_x, h);
523            hash_dp(blur.radius_y, h);
524            std::mem::discriminant(&blur.edge_treatment).hash(h);
525        });
526    } else {
527        hashers.hash(FACET_PAINT, |h| 0u8.hash(h));
528    }
529    facet_hash_scroll_binding(modifier.scroll.as_ref(), hashers);
530    facet_hash_nested_scroll_connection(modifier.nested_scroll_connection.as_ref(), hashers);
531    facet_hash_scrollbar_style(modifier.scrollbar_style.as_ref(), hashers);
532    hashers.hash(FACET_PAINT, |h| {
533        modifier
534            .overflow
535            .map(|value| std::mem::discriminant(&value))
536            .hash(h);
537    });
538    if let Some(rect) = modifier.clip_rect {
539        hashers.hash(FACET_PAINT, |h| {
540            hash_dp(rect.left, h);
541            hash_dp(rect.top, h);
542            hash_dp(rect.right, h);
543            hash_dp(rect.bottom, h);
544            std::mem::discriminant(&rect.op).hash(h);
545        });
546    } else {
547        hashers.hash(FACET_PAINT, |h| 0u8.hash(h));
548    }
549    hash_opt_dp_value!(FACET_LAYOUT, modifier.gap);
550    hash_opt_dp_value!(FACET_LAYOUT, modifier.row_gap);
551    hash_opt_dp_value!(FACET_LAYOUT, modifier.column_gap);
552    hash_opt_dp_value!(FACET_LAYOUT, modifier.margin_top);
553    hash_opt_dp_value!(FACET_LAYOUT, modifier.margin_left);
554    hash_opt_dp_value!(FACET_LAYOUT, modifier.margin_right);
555    hash_opt_dp_value!(FACET_LAYOUT, modifier.margin_bottom);
556    hash_opt_f32_value!(FACET_PAINT, modifier.graphics_layer);
557    hashers.hash(FACET_PAINT, |h| {
558        if let Some(scope) = &modifier.graphics_layer_scope {
559            1u8.hash(h);
560            hash_rc_ptr_identity(scope, h);
561        } else {
562            0u8.hash(h);
563        }
564    });
565    hashers.hash(FACET_LAYOUT, |h| {
566        modifier.match_parent_size.hash(h);
567        if let Some((axis, enabled)) = modifier.fill_parent_max_size {
568            std::mem::discriminant(&axis).hash(h);
569            enabled.hash(h);
570        } else {
571            0u8.hash(h);
572        }
573        hash_opt_dp(modifier.padding_from_baseline, h);
574    });
575    hashers.hash(FACET_PAINT, |h| modifier.paint_order.hash(h));
576    if let Some(shadow) = &modifier.shadow {
577        hashers.hash(FACET_PAINT, |h| {
578            hash_dp(shadow.blur_radius, h);
579            hash_dp(shadow.offset_x, h);
580            hash_dp(shadow.offset_y, h);
581            hash_dp(shadow.spread, h);
582            hash_color(&shadow.color, h);
583            shadow.inset.hash(h);
584        });
585    } else {
586        hashers.hash(FACET_PAINT, |h| 0u8.hash(h));
587    }
588    if let Some(radius) = modifier.shadow_shape {
589        hashers.hash(FACET_PAINT, |h| {
590            for value in radius {
591                hash_dp(value, h);
592            }
593        });
594    } else {
595        hashers.hash(FACET_PAINT, |h| 0u8.hash(h));
596    }
597    facet_hash_semantics(modifier.semantics.as_ref(), hashers, FACET_SEMANTICS);
598    hashers.hash(FACET_PAINT, |h| {
599        hash_cursor(&modifier.cursor, h);
600        modifier.painter.is_some().hash(h);
601        modifier.text_selection.is_some().hash(h);
602        modifier.paint_callback.is_some().hash(h);
603        modifier.indication.is_some().hash(h);
604        modifier.drag_preview.is_some().hash(h);
605        modifier.on_scroll.is_some().hash(h);
606        modifier.on_pointer_down.is_some().hash(h);
607        modifier.on_pointer_move.is_some().hash(h);
608        modifier.on_pointer_up.is_some().hash(h);
609        modifier.on_pointer_cancel.is_some().hash(h);
610        modifier.on_pointer_enter.is_some().hash(h);
611        modifier.on_pointer_leave.is_some().hash(h);
612        modifier.on_click.is_some().hash(h);
613        modifier.on_double_click.is_some().hash(h);
614        modifier.on_long_click.is_some().hash(h);
615        modifier.on_globally_positioned.is_some().hash(h);
616        modifier.on_size_changed.is_some().hash(h);
617        modifier.on_key_event.is_some().hash(h);
618        modifier.on_preview_key_event.is_some().hash(h);
619        modifier.on_ime.is_some().hash(h);
620        modifier.on_drag_start.is_some().hash(h);
621        modifier.on_drag_end.is_some().hash(h);
622        modifier.on_drag_enter.is_some().hash(h);
623        modifier.on_drag_over.is_some().hash(h);
624        modifier.on_drag_leave.is_some().hash(h);
625        modifier.on_drop.is_some().hash(h);
626        modifier.on_action.is_some().hash(h);
627        modifier.focus_requester.is_some().hash(h);
628        modifier.bring_into_view.is_some().hash(h);
629        modifier.on_focused_bounds_changed.is_some().hash(h);
630        modifier.on_focus_changed.is_some().hash(h);
631        modifier.interaction_source.is_some().hash(h);
632        modifier.shared_element.is_some().hash(h);
633        modifier.nodes.len().hash(h);
634    });
635    hashers.hash(FACET_LAYOUT, |h| {
636        if let Some(layout) = &modifier.layout {
637            1u8.hash(h);
638            hash_rc_ptr_identity(layout, h);
639        } else {
640            0u8.hash(h);
641        }
642    });
643    if let Some(input) = &modifier.text_input {
644        facet_hash_text_input(input, hashers);
645    } else {
646        hashers.hash(FACET_ALL, |h| 0u8.hash(h));
647    }
648    if let Some(colors) = &modifier.state_colors {
649        hashers.hash(FACET_PAINT, |h| {
650            hash_color(&colors.default, h);
651            hash_color(&colors.hovered, h);
652            hash_color(&colors.focused, h);
653            hash_color(&colors.pressed, h);
654            hash_color(&colors.disabled, h);
655            hash_color(&colors.dragged, h);
656        });
657    } else {
658        hashers.hash(FACET_PAINT, |h| 0u8.hash(h));
659    }
660    if let Some(elevation) = &modifier.state_elevation {
661        hashers.hash(FACET_PAINT, |h| {
662            hash_dp(elevation.default, h);
663            hash_dp(elevation.hovered, h);
664            hash_dp(elevation.focused, h);
665            hash_dp(elevation.pressed, h);
666            hash_dp(elevation.disabled, h);
667            hash_dp(elevation.dragged, h);
668        });
669    } else {
670        hashers.hash(FACET_PAINT, |h| 0u8.hash(h));
671    }
672    if let Some(spec) = &modifier.animate_content_size {
673        facet_hash_animation_spec(spec, hashers, FACET_LAYOUT);
674    } else {
675        hashers.hash(FACET_LAYOUT, |h| 0u8.hash(h));
676    }
677}
678
679fn facet_hash_semantics(
680    semantics: Option<&repose_core::Semantics>,
681    hashers: &mut FacetHashers,
682    mask: u8,
683) {
684    hashers.hash(mask, |hasher| {
685        if let Some(semantics) = semantics {
686            1u8.hash(hasher);
687            std::mem::discriminant(&semantics.role).hash(hasher);
688            semantics.label.hash(hasher);
689            semantics.focused.hash(hasher);
690            semantics.enabled.hash(hasher);
691            semantics.selectable_group.hash(hasher);
692            semantics.checked.hash(hasher);
693            semantics.selected.hash(hasher);
694            semantics.value.hash(hasher);
695        } else {
696            0u8.hash(hasher);
697        }
698    });
699}
700
701fn facet_hash_span_style(style: &SpanStyle, hashers: &mut FacetHashers, mask: u8) {
702    hashers.hash(mask & FACET_PAINT, |h| {
703        hash_opt_color(style.color.as_ref(), h)
704    });
705    hashers.hash(mask & FACET_LAYOUT_MEASUREMENT, |h| {
706        if let Some(size) = style.font_size {
707            1u8.hash(h);
708            hash_sp(size, h);
709        } else {
710            0u8.hash(h);
711        }
712    });
713    hashers.hash(mask & FACET_LAYOUT_MEASUREMENT, |h| {
714        style.font_weight.hash(h);
715        style.font_family.hash(h);
716        style.font_style.hash(h);
717        style.text_align.hash(h);
718        if let Some(value) = style.letter_spacing {
719            1u8.hash(h);
720            hash_sp(value, h);
721        } else {
722            0u8.hash(h);
723        }
724        if let Some(value) = style.line_height {
725            1u8.hash(h);
726            hash_sp(value, h);
727        } else {
728            0u8.hash(h);
729        }
730    });
731    hashers.hash(mask & FACET_PAINT, |h| {
732        hash_opt_color(style.background.as_ref(), h);
733        if let Some(decoration) = &style.text_decoration {
734            1u8.hash(h);
735            hash_text_decoration(decoration, h);
736        } else {
737            0u8.hash(h);
738        }
739        hash_f32(style.alpha, h);
740    });
741    hashers.hash(mask & FACET_LAYOUT_MEASUREMENT, |h| {
742        hash_discriminant_option(&style.text_direction, h);
743        hash_discriminant_option(&style.font_synthesis, h);
744        if let Some(value) = style.baseline_shift {
745            1u8.hash(h);
746            hash_f32(value.0, h);
747        } else {
748            0u8.hash(h);
749        }
750        hash_discriminant_option(&style.hyphens, h);
751        hash_discriminant_option(&style.line_break, h);
752        if let Some(indent) = &style.text_indent {
753            1u8.hash(h);
754            hash_dp(indent.first_line, h);
755            hash_dp(indent.rest_lines, h);
756        } else {
757            0u8.hash(h);
758        }
759    });
760    hashers.hash(mask & FACET_LAYOUT_MEASUREMENT_PAINT, |h| {
761        if let Some(style) = &style.draw_style {
762            1u8.hash(h);
763            hash_draw_style(style, h);
764        } else {
765            0u8.hash(h);
766        }
767    });
768    hashers.hash(mask & FACET_LAYOUT_MEASUREMENT, |h| {
769        style.font_variation_settings.hash(h);
770    });
771}
772
773fn facet_hash_text_style(style: &TextStyle, hashers: &mut FacetHashers) {
774    hashers.hash(FACET_LAYOUT_MEASUREMENT, |h| {
775        hash_sp(style.font_size, h);
776        style.font_weight.hash(h);
777        style.font_family.hash(h);
778        style.font_style.hash(h);
779        style.text_align.hash(h);
780        hash_sp(style.letter_spacing, h);
781        hash_sp(style.line_height, h);
782        hash_discriminant_option(&style.text_direction, h);
783        std::mem::discriminant(&style.font_synthesis).hash(h);
784        hash_f32(style.baseline_shift.0, h);
785        std::mem::discriminant(&style.hyphens).hash(h);
786        std::mem::discriminant(&style.line_break).hash(h);
787        if let Some(indent) = &style.text_indent {
788            1u8.hash(h);
789            hash_dp(indent.first_line, h);
790            hash_dp(indent.rest_lines, h);
791        } else {
792            0u8.hash(h);
793        }
794        hash_draw_style(&style.draw_style, h);
795        style.locale_list.hash(h);
796        style.font_feature_settings.hash(h);
797        style.font_variation_settings.hash(h);
798    });
799    hashers.hash(FACET_PAINT, |h| {
800        hash_opt_color(style.color.as_ref(), h);
801        hash_opt_color(style.background.as_ref(), h);
802        if let Some(decoration) = &style.text_decoration {
803            1u8.hash(h);
804            hash_text_decoration(decoration, h);
805        } else {
806            0u8.hash(h);
807        }
808        if let Some(shadow) = &style.shadow {
809            1u8.hash(h);
810            hash_shadow(shadow, h);
811        } else {
812            0u8.hash(h);
813        }
814        hash_f32(style.alpha, h);
815        hash_draw_style(&style.draw_style, h);
816    });
817}
818
819fn facet_hash_text_input(input: &repose_core::TextInputConfig, hashers: &mut FacetHashers) {
820    hashers.hash(FACET_LAYOUT_MEASUREMENT, |h| {
821        input.hint.hash(h);
822        input.multiline.hash(h);
823        input.value.hash(h);
824        input.max_lines.hash(h);
825        input.min_lines.hash(h);
826        match &input.line_limits {
827            Some(repose_core::text::TextFieldLineLimits::SingleLine) => 1u8.hash(h),
828            Some(repose_core::text::TextFieldLineLimits::MultiLine {
829                min_height_in_lines,
830                max_height_in_lines,
831            }) => {
832                2u8.hash(h);
833                min_height_in_lines.hash(h);
834                max_height_in_lines.hash(h);
835            }
836            None => 0u8.hash(h),
837        }
838    });
839    hashers.hash(FACET_PAINT, |h| {
840        input.on_change.is_some().hash(h);
841        input.on_submit.is_some().hash(h);
842        input.focus_tracker.is_some().hash(h);
843        input.enabled.hash(h);
844        input.read_only.hash(h);
845        input.sensitive.hash(h);
846        std::mem::discriminant(&input.keyboard_type).hash(h);
847        std::mem::discriminant(&input.capitalization).hash(h);
848        std::mem::discriminant(&input.ime_action).hash(h);
849        input.auto_correct_enabled.hash(h);
850        if let Some(color) = input.cursor_color {
851            1u8.hash(h);
852            hash_color(&color, h);
853        } else {
854            0u8.hash(h);
855        }
856        input.on_text_layout.is_some().hash(h);
857        input.interaction_source.is_some().hash(h);
858        input.visual_transformation.is_some().hash(h);
859        if let Some(actions) = &input.keyboard_actions {
860            1u8.hash(h);
861            actions.on_done.is_some().hash(h);
862            actions.on_go.is_some().hash(h);
863            actions.on_next.is_some().hash(h);
864            actions.on_previous.is_some().hash(h);
865            actions.on_search.is_some().hash(h);
866            actions.on_send.is_some().hash(h);
867        } else {
868            0u8.hash(h);
869        }
870    });
871    if let Some(style) = &input.text_style {
872        hashers.hash(FACET_LAYOUT_MEASUREMENT_PAINT, |h| 1u8.hash(h));
873        facet_hash_text_style(style, hashers);
874    } else {
875        hashers.hash(FACET_LAYOUT_MEASUREMENT_PAINT, |h| 0u8.hash(h));
876    }
877}
878
879fn facet_hash_nested_scroll_connection(
880    connection: Option<&repose_core::nested_scroll::NestedScrollConnection>,
881    hashers: &mut FacetHashers,
882) {
883    hashers.hash(FACET_PAINT, |h| match connection {
884        Some(connection) => {
885            1u8.hash(h);
886            if let Some(callback) = &connection.on_pre_scroll {
887                1u8.hash(h);
888                hash_rc_ptr_identity(callback, h);
889            } else {
890                0u8.hash(h);
891            }
892            if let Some(callback) = &connection.on_post_scroll {
893                1u8.hash(h);
894                hash_rc_ptr_identity(callback, h);
895            } else {
896                0u8.hash(h);
897            }
898            if let Some(callback) = &connection.on_pre_fling {
899                1u8.hash(h);
900                hash_rc_ptr_identity(callback, h);
901            } else {
902                0u8.hash(h);
903            }
904            if let Some(callback) = &connection.on_post_fling {
905                1u8.hash(h);
906                hash_rc_ptr_identity(callback, h);
907            } else {
908                0u8.hash(h);
909            }
910        }
911        None => 0u8.hash(h),
912    });
913}
914
915fn facet_hash_scrollbar_style(
916    style: Option<&repose_core::ScrollbarStyle>,
917    hashers: &mut FacetHashers,
918) {
919    let Some(style) = style else {
920        hashers.hash(FACET_PAINT, |h| 0u8.hash(h));
921        return;
922    };
923    hashers.hash(FACET_PAINT, |h| {
924        1u8.hash(h);
925        style.thickness.0.to_bits().hash(h);
926        style.track_inset.0.to_bits().hash(h);
927        style.min_thumb_length.0.to_bits().hash(h);
928        style
929            .fixed_thumb_length
930            .map(|value| value.0.to_bits())
931            .hash(h);
932        style.radius.map(|value| value.0.to_bits()).hash(h);
933    });
934    facet_hash_control_visual_set(&style.track_visuals, hashers);
935    facet_hash_control_visual_set(&style.thumb_visuals, hashers);
936}
937
938fn facet_hash_control_visual_set(set: &repose_core::ControlVisualSet, hashers: &mut FacetHashers) {
939    for visual in [
940        &set.normal,
941        &set.hovered,
942        &set.pressed,
943        &set.dragged,
944        &set.focused,
945        &set.disabled,
946    ] {
947        match visual {
948            Some(visual) => {
949                hashers.hash(FACET_PAINT, |h| 1u8.hash(h));
950                facet_hash_control_visual(visual, hashers);
951            }
952            None => hashers.hash(FACET_PAINT, |h| 0u8.hash(h)),
953        }
954    }
955}
956
957fn facet_hash_control_visual(visual: &repose_core::ControlVisual, hashers: &mut FacetHashers) {
958    hashers.hash(FACET_PAINT, |h| std::mem::discriminant(visual).hash(h));
959    match visual {
960        repose_core::ControlVisual::Rect { brush, radius } => {
961            hashers.hash(FACET_PAINT, |h| {
962                hash_brush(brush, h);
963                for value in radius {
964                    value.0.to_bits().hash(h);
965                }
966            });
967        }
968        repose_core::ControlVisual::Image {
969            handle,
970            source_rect,
971            tint,
972            fit,
973            filter,
974            alignment,
975        } => {
976            hashers.hash(FACET_PAINT, |h| {
977                handle.hash(h);
978                source_rect.hash(h);
979                hash_color(tint, h);
980                std::mem::discriminant(fit).hash(h);
981                std::mem::discriminant(filter).hash(h);
982                std::mem::discriminant(alignment).hash(h);
983            });
984        }
985        repose_core::ControlVisual::Custom(_) => {
986            hashers.hash(FACET_PAINT, |h| {
987                visual.custom_identity().unwrap_or_default().hash(h)
988            });
989        }
990        _ => {}
991    }
992}
993
994fn facet_hash_scroll_binding(binding: Option<&ScrollBinding>, hashers: &mut FacetHashers) {
995    match binding {
996        Some(ScrollBinding::Vertical(binding)) => {
997            hashers.hash(FACET_LAYOUT_PAINT, |h| 1u8.hash(h));
998            facet_hash_scroll_axis(binding, hashers);
999        }
1000        Some(ScrollBinding::Horizontal(binding)) => {
1001            hashers.hash(FACET_LAYOUT_PAINT, |h| 2u8.hash(h));
1002            facet_hash_scroll_axis(binding, hashers);
1003        }
1004        Some(ScrollBinding::Both(binding)) => {
1005            hashers.hash(FACET_LAYOUT_PAINT, |h| 3u8.hash(h));
1006            hashers.hash(FACET_LAYOUT, |h| binding.show_scrollbar.hash(h));
1007            hashers.hash(FACET_PAINT, |h| {
1008                binding.show_scrollbar.hash(h);
1009                hash_rc_identity(&binding.on_scroll, h);
1010                hash_rc_identity(&binding.on_scroll_with_source, h);
1011                hash_rc_identity(&binding.set_viewport_width, h);
1012                hash_rc_identity(&binding.set_viewport_height, h);
1013                hash_rc_identity(&binding.set_content_width, h);
1014                hash_rc_identity(&binding.set_content_height, h);
1015                hash_rc_identity(&binding.get_offset_xy, h);
1016                hash_rc_identity(&binding.set_offset_xy, h);
1017                hash_rc_identity(&binding.tick, h);
1018                hash_rc_identity(&binding.set_nested_scroll_parent, h);
1019            });
1020        }
1021        None => hashers.hash(FACET_LAYOUT_PAINT, |h| 0u8.hash(h)),
1022    }
1023}
1024
1025fn facet_hash_scroll_axis(
1026    binding: &repose_core::scroll::ScrollAxisBinding,
1027    hashers: &mut FacetHashers,
1028) {
1029    hashers.hash(FACET_LAYOUT, |h| binding.show_scrollbar.hash(h));
1030    hashers.hash(FACET_PAINT, |h| {
1031        binding.show_scrollbar.hash(h);
1032        hash_rc_identity(&binding.on_scroll, h);
1033        hash_rc_identity(&binding.on_scroll_with_source, h);
1034        hash_rc_identity(&binding.set_viewport_main, h);
1035        hash_rc_identity(&binding.set_content_main, h);
1036        hash_rc_identity(&binding.get_offset_main, h);
1037        hash_rc_identity(&binding.set_offset_main, h);
1038        hash_rc_identity(&binding.tick, h);
1039        hash_rc_identity(&binding.set_nested_scroll_parent, h);
1040    });
1041}
1042
1043fn facet_hash_animation_spec(spec: &AnimationSpec, hashers: &mut FacetHashers, mask: u8) {
1044    hashers.hash(mask, |h| {
1045        spec.duration.as_nanos().hash(h);
1046        hash_easing(&spec.easing, h);
1047        spec.delay.as_nanos().hash(h);
1048        if let Some(spring) = &spec.spring {
1049            1u8.hash(h);
1050            hash_f32(spring.damping_ratio, h);
1051            hash_f32(spring.stiffness, h);
1052            hash_f32(spring.settle_progress, h);
1053            hash_f32(spring.settle_velocity, h);
1054        } else {
1055            0u8.hash(h);
1056        }
1057        if let Some(repeat) = &spec.repeat {
1058            1u8.hash(h);
1059            repeat.iterations.hash(h);
1060            repeat.reverse.hash(h);
1061            repeat.delay_between.as_nanos().hash(h);
1062        } else {
1063            0u8.hash(h);
1064        }
1065    });
1066}
1067
1068pub fn hash_subtree(content_hash: u64, children_hashes: &[u64]) -> u64 {
1069    let mut hasher = RapidHasher::default();
1070    content_hash.hash(&mut hasher);
1071    children_hashes.len().hash(&mut hasher);
1072    for hash in children_hashes {
1073        hash.hash(&mut hasher);
1074    }
1075    hasher.finish()
1076}
1077
1078fn hash_opt_color(color: Option<&Color>, hasher: &mut impl Hasher) {
1079    match color {
1080        Some(color) => {
1081            1u8.hash(hasher);
1082            hash_color(color, hasher);
1083        }
1084        None => 0u8.hash(hasher),
1085    }
1086}
1087
1088fn hash_path_effect(path_effect: &Option<repose_core::PathEffect>, hasher: &mut impl Hasher) {
1089    match path_effect {
1090        Some(repose_core::PathEffect::Corner { radius }) => {
1091            0u8.hash(hasher);
1092            hash_f32(*radius, hasher);
1093        }
1094        Some(repose_core::PathEffect::Dash { intervals, phase }) => {
1095            1u8.hash(hasher);
1096            intervals.len().hash(hasher);
1097            for interval in intervals {
1098                hash_f32(*interval, hasher);
1099            }
1100            hash_f32(*phase, hasher);
1101        }
1102        None => 2u8.hash(hasher),
1103    }
1104}
1105
1106fn hash_draw_style(style: &repose_core::DrawStyle, hasher: &mut impl Hasher) {
1107    match style {
1108        repose_core::DrawStyle::Fill => 0u8.hash(hasher),
1109        repose_core::DrawStyle::Stroke {
1110            width,
1111            cap,
1112            join,
1113            miter,
1114            path_effect,
1115        }
1116        | repose_core::DrawStyle::FillAndStroke {
1117            width,
1118            cap,
1119            join,
1120            miter,
1121            path_effect,
1122            ..
1123        } => {
1124            std::mem::discriminant(style).hash(hasher);
1125            if let repose_core::DrawStyle::FillAndStroke { outline_color, .. } = style {
1126                hash_opt_color(outline_color.as_ref(), hasher);
1127            }
1128            hash_f32(*width, hasher);
1129            (*cap as u8).hash(hasher);
1130            (*join as u8).hash(hasher);
1131            hash_f32(*miter, hasher);
1132            hash_path_effect(path_effect, hasher);
1133        }
1134    }
1135}
1136
1137fn hash_text_decoration(decoration: &repose_core::TextDecoration, hasher: &mut impl Hasher) {
1138    decoration.underline.hash(hasher);
1139    decoration.strikethrough.hash(hasher);
1140    hash_opt_color(decoration.color.as_ref(), hasher);
1141}
1142
1143fn hash_shadow(shadow: &Shadow, hasher: &mut impl Hasher) {
1144    hash_color(&shadow.color, hasher);
1145    hash_dp(shadow.offset_x, hasher);
1146    hash_dp(shadow.offset_y, hasher);
1147    hash_dp(shadow.blur_radius, hasher);
1148}
1149
1150#[allow(dead_code)]
1151fn hash_text_style(style: &TextStyle, hasher: &mut impl Hasher) {
1152    hash_sp(style.font_size, hasher);
1153    hash_opt_color(style.color.as_ref(), hasher);
1154    style.font_weight.hash(hasher);
1155    style.font_family.hash(hasher);
1156    style.font_style.hash(hasher);
1157    style.text_align.hash(hasher);
1158    hash_sp(style.letter_spacing, hasher);
1159    hash_sp(style.line_height, hasher);
1160    hash_opt_color(style.background.as_ref(), hasher);
1161    match &style.text_decoration {
1162        Some(decoration) => {
1163            1u8.hash(hasher);
1164            hash_text_decoration(decoration, hasher);
1165        }
1166        None => 0u8.hash(hasher),
1167    }
1168    match &style.shadow {
1169        Some(shadow) => {
1170            1u8.hash(hasher);
1171            hash_shadow(shadow, hasher);
1172        }
1173        None => 0u8.hash(hasher),
1174    }
1175    hash_discriminant_option(&style.text_direction, hasher);
1176    std::mem::discriminant(&style.font_synthesis).hash(hasher);
1177    hash_f32(style.baseline_shift.0, hasher);
1178    std::mem::discriminant(&style.hyphens).hash(hasher);
1179    std::mem::discriminant(&style.line_break).hash(hasher);
1180    match &style.text_indent {
1181        Some(indent) => {
1182            1u8.hash(hasher);
1183            hash_dp(indent.first_line, hasher);
1184            hash_dp(indent.rest_lines, hasher);
1185        }
1186        None => 0u8.hash(hasher),
1187    }
1188    hash_draw_style(&style.draw_style, hasher);
1189    hash_f32(style.alpha, hasher);
1190    style.locale_list.hash(hasher);
1191    style.font_feature_settings.hash(hasher);
1192    style.font_variation_settings.hash(hasher);
1193}
1194
1195#[allow(dead_code)]
1196fn hash_span_style(style: &SpanStyle, hasher: &mut impl Hasher) {
1197    hash_opt_color(style.color.as_ref(), hasher);
1198    match style.font_size {
1199        Some(size) => {
1200            1u8.hash(hasher);
1201            hash_sp(size, hasher);
1202        }
1203        None => 0u8.hash(hasher),
1204    }
1205    style.font_weight.hash(hasher);
1206    style.font_family.hash(hasher);
1207    style.font_style.hash(hasher);
1208    style.text_align.hash(hasher);
1209    match style.letter_spacing {
1210        Some(value) => {
1211            1u8.hash(hasher);
1212            hash_sp(value, hasher);
1213        }
1214        None => 0u8.hash(hasher),
1215    }
1216    match style.line_height {
1217        Some(value) => {
1218            1u8.hash(hasher);
1219            hash_sp(value, hasher);
1220        }
1221        None => 0u8.hash(hasher),
1222    }
1223    hash_opt_color(style.background.as_ref(), hasher);
1224    match &style.text_decoration {
1225        Some(decoration) => {
1226            1u8.hash(hasher);
1227            hash_text_decoration(decoration, hasher);
1228        }
1229        None => 0u8.hash(hasher),
1230    }
1231    hash_discriminant_option(&style.text_direction, hasher);
1232    hash_discriminant_option(&style.font_synthesis, hasher);
1233    match style.baseline_shift {
1234        Some(value) => {
1235            1u8.hash(hasher);
1236            hash_f32(value.0, hasher);
1237        }
1238        None => 0u8.hash(hasher),
1239    }
1240    hash_discriminant_option(&style.hyphens, hasher);
1241    hash_discriminant_option(&style.line_break, hasher);
1242    match &style.text_indent {
1243        Some(indent) => {
1244            1u8.hash(hasher);
1245            hash_dp(indent.first_line, hasher);
1246            hash_dp(indent.rest_lines, hasher);
1247        }
1248        None => 0u8.hash(hasher),
1249    }
1250    match &style.draw_style {
1251        Some(style) => {
1252            1u8.hash(hasher);
1253            hash_draw_style(style, hasher);
1254        }
1255        None => 0u8.hash(hasher),
1256    }
1257    hash_f32(style.alpha, hasher);
1258    style.font_variation_settings.hash(hasher);
1259}
1260
1261#[allow(dead_code)]
1262fn hash_view_kind(kind: &ViewKind, hasher: &mut impl Hasher) {
1263    std::mem::discriminant(kind).hash(hasher);
1264    match kind {
1265        ViewKind::Text {
1266            text,
1267            color,
1268            font_size,
1269            soft_wrap,
1270            max_lines,
1271            overflow,
1272            font_family,
1273            annotations,
1274            text_align,
1275            font_weight,
1276            font_style,
1277            text_decoration,
1278            letter_spacing,
1279            line_height,
1280            url,
1281            font_variation_settings,
1282            draw_style,
1283        } => {
1284            text.hash(hasher);
1285            hash_color(color, hasher);
1286            hash_sp(*font_size, hasher);
1287            soft_wrap.hash(hasher);
1288            max_lines.hash(hasher);
1289            hash_text_overflow(overflow, hasher);
1290            font_family.hash(hasher);
1291            text_align.hash(hasher);
1292            font_weight.0.hash(hasher);
1293            font_style.hash(hasher);
1294            hash_text_decoration(text_decoration, hasher);
1295            hash_sp(*letter_spacing, hasher);
1296            hash_sp(*line_height, hasher);
1297            url.hash(hasher);
1298            font_variation_settings.hash(hasher);
1299            hash_draw_style(draw_style, hasher);
1300            match annotations {
1301                Some(annotations) => {
1302                    1u8.hash(hasher);
1303                    annotations.len().hash(hasher);
1304                    for span in annotations.iter() {
1305                        span.start.hash(hasher);
1306                        span.end.hash(hasher);
1307                        hash_span_style(&span.style, hasher);
1308                        match &span.url {
1309                            Some(url) => {
1310                                1u8.hash(hasher);
1311                                url.hash(hasher);
1312                            }
1313                            None => 0u8.hash(hasher),
1314                        }
1315                    }
1316                }
1317                None => 0u8.hash(hasher),
1318            }
1319        }
1320        ViewKind::Image {
1321            handle,
1322            tint,
1323            fit,
1324            filter,
1325            source_rect,
1326            alignment,
1327        } => {
1328            handle.hash(hasher);
1329            repose_core::image_intrinsic_size(*handle).hash(hasher);
1330            hash_color(tint, hasher);
1331            std::mem::discriminant(fit).hash(hasher);
1332            std::mem::discriminant(filter).hash(hasher);
1333            source_rect.hash(hasher);
1334            std::mem::discriminant(alignment).hash(hasher);
1335        }
1336        ViewKind::OverlayHost
1337        | ViewKind::Box
1338        | ViewKind::Row
1339        | ViewKind::Column
1340        | ViewKind::ZStack
1341        | ViewKind::SubcomposeLayout { .. } => {}
1342        _ => {}
1343    }
1344}
1345
1346fn hash_discriminant_option<T>(value: &Option<T>, hasher: &mut impl Hasher) {
1347    match value {
1348        Some(value) => {
1349            1u8.hash(hasher);
1350            std::mem::discriminant(value).hash(hasher);
1351        }
1352        None => 0u8.hash(hasher),
1353    }
1354}
1355
1356fn hash_f32(v: f32, hasher: &mut impl Hasher) {
1357    v.to_bits().hash(hasher);
1358}
1359
1360fn hash_rc_identity<T: ?Sized>(value: &Option<std::rc::Rc<T>>, hasher: &mut impl Hasher) {
1361    value.is_some().hash(hasher);
1362}
1363
1364fn hash_rc_ptr_identity<T: ?Sized>(value: &std::rc::Rc<T>, hasher: &mut impl Hasher) {
1365    (std::rc::Rc::as_ptr(value) as *const () as usize).hash(hasher);
1366}
1367
1368#[allow(dead_code)]
1369fn hash_control_visual(visual: &repose_core::ControlVisual, hasher: &mut impl Hasher) {
1370    std::mem::discriminant(visual).hash(hasher);
1371    match visual {
1372        repose_core::ControlVisual::Rect { brush, radius } => {
1373            hash_brush(brush, hasher);
1374            for value in radius {
1375                value.0.to_bits().hash(hasher);
1376            }
1377        }
1378        repose_core::ControlVisual::Image {
1379            handle,
1380            source_rect,
1381            tint,
1382            fit,
1383            filter,
1384            alignment,
1385        } => {
1386            handle.hash(hasher);
1387            source_rect.hash(hasher);
1388            hash_color(tint, hasher);
1389            std::mem::discriminant(fit).hash(hasher);
1390            std::mem::discriminant(filter).hash(hasher);
1391            std::mem::discriminant(alignment).hash(hasher);
1392        }
1393        repose_core::ControlVisual::Custom(_) => {
1394            visual.custom_identity().unwrap_or_default().hash(hasher)
1395        }
1396        _ => {}
1397    }
1398}
1399
1400#[allow(dead_code)]
1401fn hash_control_visual_set(set: &repose_core::ControlVisualSet, hasher: &mut impl Hasher) {
1402    for visual in [
1403        &set.normal,
1404        &set.hovered,
1405        &set.pressed,
1406        &set.dragged,
1407        &set.focused,
1408        &set.disabled,
1409    ] {
1410        match visual {
1411            Some(visual) => {
1412                true.hash(hasher);
1413                hash_control_visual(visual, hasher);
1414            }
1415            None => false.hash(hasher),
1416        }
1417    }
1418}
1419
1420#[allow(dead_code)]
1421fn hash_scrollbar_style(style: Option<&repose_core::ScrollbarStyle>, hasher: &mut impl Hasher) {
1422    let Some(style) = style else {
1423        0u8.hash(hasher);
1424        return;
1425    };
1426    1u8.hash(hasher);
1427    style.thickness.0.to_bits().hash(hasher);
1428    style.track_inset.0.to_bits().hash(hasher);
1429    style.min_thumb_length.0.to_bits().hash(hasher);
1430    style
1431        .fixed_thumb_length
1432        .map(|value| value.0.to_bits())
1433        .hash(hasher);
1434    style.radius.map(|value| value.0.to_bits()).hash(hasher);
1435    hash_control_visual_set(&style.track_visuals, hasher);
1436    hash_control_visual_set(&style.thumb_visuals, hasher);
1437}
1438
1439#[allow(dead_code)]
1440fn hash_scroll_axis_binding(
1441    binding: &repose_core::scroll::ScrollAxisBinding,
1442    hasher: &mut impl Hasher,
1443) {
1444    binding.show_scrollbar.hash(hasher);
1445    hash_rc_identity(&binding.on_scroll, hasher);
1446    hash_rc_identity(&binding.on_scroll_with_source, hasher);
1447    hash_rc_identity(&binding.set_viewport_main, hasher);
1448    hash_rc_identity(&binding.set_content_main, hasher);
1449    hash_rc_identity(&binding.get_offset_main, hasher);
1450    hash_rc_identity(&binding.set_offset_main, hasher);
1451    hash_rc_identity(&binding.tick, hasher);
1452    hash_rc_identity(&binding.set_nested_scroll_parent, hasher);
1453}
1454
1455#[allow(dead_code)]
1456fn hash_scroll_binding(binding: &ScrollBinding, hasher: &mut impl Hasher) {
1457    match binding {
1458        ScrollBinding::Vertical(binding) => {
1459            1u8.hash(hasher);
1460            hash_scroll_axis_binding(binding, hasher);
1461        }
1462        ScrollBinding::Horizontal(binding) => {
1463            2u8.hash(hasher);
1464            hash_scroll_axis_binding(binding, hasher);
1465        }
1466        ScrollBinding::Both(binding) => {
1467            3u8.hash(hasher);
1468            binding.show_scrollbar.hash(hasher);
1469            hash_rc_identity(&binding.on_scroll, hasher);
1470            hash_rc_identity(&binding.on_scroll_with_source, hasher);
1471            hash_rc_identity(&binding.set_viewport_width, hasher);
1472            hash_rc_identity(&binding.set_viewport_height, hasher);
1473            hash_rc_identity(&binding.set_content_width, hasher);
1474            hash_rc_identity(&binding.set_content_height, hasher);
1475            hash_rc_identity(&binding.get_offset_xy, hasher);
1476            hash_rc_identity(&binding.set_offset_xy, hasher);
1477            hash_rc_identity(&binding.tick, hasher);
1478            hash_rc_identity(&binding.set_nested_scroll_parent, hasher);
1479        }
1480    }
1481}
1482
1483#[allow(dead_code)]
1484fn hash_cursor(cursor: &Option<repose_core::CursorIcon>, hasher: &mut impl Hasher) {
1485    match cursor {
1486        Some(repose_core::CursorIcon::Custom(image)) => {
1487            10u8.hash(hasher);
1488            image.size.hash(hasher);
1489            image.hotspot.hash(hasher);
1490            image.rgba.hash(hasher);
1491        }
1492        Some(cursor) => {
1493            1u8.hash(hasher);
1494            std::mem::discriminant(cursor).hash(hasher);
1495        }
1496        None => 0u8.hash(hasher),
1497    }
1498}
1499
1500fn hash_opt_f32(v: Option<f32>, hasher: &mut impl Hasher) {
1501    match v {
1502        Some(value) => {
1503            1u8.hash(hasher);
1504            hash_f32(value, hasher);
1505        }
1506        None => 0u8.hash(hasher),
1507    }
1508}
1509
1510fn hash_shape(shape: &repose_core::Shape, hasher: &mut impl Hasher) {
1511    match shape {
1512        repose_core::Shape::Rectangle => 0u8.hash(hasher),
1513        repose_core::Shape::Rounded { radii } => {
1514            1u8.hash(hasher);
1515            for radius in radii {
1516                hash_dp(*radius, hasher);
1517            }
1518        }
1519        repose_core::Shape::Percent(percent) => {
1520            2u8.hash(hasher);
1521            hash_f32(*percent, hasher);
1522        }
1523        repose_core::Shape::Circle => 3u8.hash(hasher),
1524        repose_core::Shape::Path(path) => {
1525            4u8.hash(hasher);
1526            path.commands.len().hash(hasher);
1527            for command in &path.commands {
1528                match command {
1529                    repose_core::PathCommand::MoveTo(x, y) => {
1530                        0u8.hash(hasher);
1531                        hash_dp(*x, hasher);
1532                        hash_dp(*y, hasher);
1533                    }
1534                    repose_core::PathCommand::LineTo(x, y) => {
1535                        1u8.hash(hasher);
1536                        hash_dp(*x, hasher);
1537                        hash_dp(*y, hasher);
1538                    }
1539                    repose_core::PathCommand::CubicTo { c1, c2, end } => {
1540                        2u8.hash(hasher);
1541                        hash_dp(c1.0, hasher);
1542                        hash_dp(c1.1, hasher);
1543                        hash_dp(c2.0, hasher);
1544                        hash_dp(c2.1, hasher);
1545                        hash_dp(end.0, hasher);
1546                        hash_dp(end.1, hasher);
1547                    }
1548                    repose_core::PathCommand::Close => 3u8.hash(hasher),
1549                }
1550            }
1551        }
1552    }
1553}
1554
1555fn hash_opt_shape(shape: Option<&repose_core::Shape>, hasher: &mut impl Hasher) {
1556    match shape {
1557        Some(shape) => {
1558            1u8.hash(hasher);
1559            hash_shape(shape, hasher);
1560        }
1561        None => 0u8.hash(hasher),
1562    }
1563}
1564
1565fn hash_dp(v: Dp, hasher: &mut impl Hasher) {
1566    hash_f32(v.0, hasher);
1567}
1568
1569fn hash_opt_dp(v: Option<Dp>, hasher: &mut impl Hasher) {
1570    match v {
1571        Some(value) => {
1572            1u8.hash(hasher);
1573            hash_dp(value, hasher);
1574        }
1575        None => 0u8.hash(hasher),
1576    }
1577}
1578
1579fn hash_opt_dp_pair(v: Option<(Dp, Dp)>, hasher: &mut impl Hasher) {
1580    match v {
1581        Some((x, y)) => {
1582            1u8.hash(hasher);
1583            hash_dp(x, hasher);
1584            hash_dp(y, hasher);
1585        }
1586        None => 0u8.hash(hasher),
1587    }
1588}
1589
1590fn hash_sp(v: Sp, hasher: &mut impl Hasher) {
1591    hash_f32(v.0, hasher);
1592}
1593
1594#[allow(dead_code)]
1595fn hash_px(v: Px, hasher: &mut impl Hasher) {
1596    hash_f32(v.0, hasher);
1597}
1598
1599#[allow(dead_code)]
1600fn hash_modifier(m: &Modifier, hasher: &mut impl Hasher) {
1601    match m.size {
1602        Some(size) => {
1603            1u8.hash(hasher);
1604            hash_dp(size.width, hasher);
1605            hash_dp(size.height, hasher);
1606        }
1607        None => 0u8.hash(hasher),
1608    }
1609    hash_opt_dp(m.width, hasher);
1610    hash_opt_dp(m.height, hasher);
1611    match m.wrap_content {
1612        Some((align, unbounded)) => {
1613            1u8.hash(hasher);
1614            std::mem::discriminant(&align).hash(hasher);
1615            unbounded.hash(hasher);
1616        }
1617        None => 0u8.hash(hasher),
1618    }
1619    match m.required_size {
1620        Some(size) => {
1621            1u8.hash(hasher);
1622            hash_dp(size.width, hasher);
1623            hash_dp(size.height, hasher);
1624        }
1625        None => 0u8.hash(hasher),
1626    }
1627    hash_opt_dp(m.required_min_width, hasher);
1628    hash_opt_dp(m.required_max_width, hasher);
1629    hash_opt_dp(m.required_min_height, hasher);
1630    hash_opt_dp(m.required_max_height, hasher);
1631    hash_opt_dp(m.default_min_width, hasher);
1632    hash_opt_dp(m.default_min_height, hasher);
1633    hash_opt_f32(m.fill_max, hasher);
1634    hash_opt_f32(m.fill_max_w, hasher);
1635    hash_opt_f32(m.fill_max_h, hasher);
1636    m.repaint_boundary.hash(hasher);
1637    hash_opt_dp(m.padding, hasher);
1638    match m.padding_values {
1639        Some(padding) => {
1640            1u8.hash(hasher);
1641            hash_dp(padding.left, hasher);
1642            hash_dp(padding.right, hasher);
1643            hash_dp(padding.top, hasher);
1644            hash_dp(padding.bottom, hasher);
1645        }
1646        None => 0u8.hash(hasher),
1647    }
1648    hash_opt_dp(m.min_width, hasher);
1649    hash_opt_dp(m.min_height, hasher);
1650    hash_opt_dp(m.max_width, hasher);
1651    hash_opt_dp(m.max_height, hasher);
1652    match &m.background {
1653        Some(background) => {
1654            1u8.hash(hasher);
1655            hash_brush(background, hasher);
1656        }
1657        None => 0u8.hash(hasher),
1658    }
1659    hash_opt_f32(m.background_alpha, hasher);
1660    hash_opt_shape(m.background_shape.as_ref(), hasher);
1661    match &m.border {
1662        Some(border) => {
1663            1u8.hash(hasher);
1664            hash_dp(border.width, hasher);
1665            hash_brush(&border.brush, hasher);
1666            for &radius in &border.radius {
1667                hash_dp(radius, hasher);
1668            }
1669        }
1670        None => 0u8.hash(hasher),
1671    }
1672    hash_opt_shape(m.border_shape.as_ref(), hasher);
1673    hash_opt_f32(m.flex_grow, hasher);
1674    hash_opt_f32(m.flex_shrink, hasher);
1675    hash_opt_dp(m.flex_basis, hasher);
1676    m.flex_wrap
1677        .map(|value| std::mem::discriminant(&value))
1678        .hash(hasher);
1679    m.flex_basis_content.hash(hasher);
1680    m.flex_line_count.hash(hasher);
1681    m.flex_dir
1682        .map(|value| std::mem::discriminant(&value))
1683        .hash(hasher);
1684    m.align_self
1685        .map(|value| (value.keyword as u8, value.safety as u8))
1686        .hash(hasher);
1687    m.justify_content
1688        .map(|value| (value.keyword as u8, value.safety as u8))
1689        .hash(hasher);
1690    m.align_items_container
1691        .map(|value| (value.keyword as u8, value.safety as u8))
1692        .hash(hasher);
1693    m.align_content
1694        .map(|value| (value.keyword as u8, value.safety as u8))
1695        .hash(hasher);
1696    m.baseline_align.hash(hasher);
1697    match m.clip_rounded {
1698        Some(radius) => {
1699            1u8.hash(hasher);
1700            for value in radius {
1701                hash_dp(value, hasher);
1702            }
1703        }
1704        None => 0u8.hash(hasher),
1705    }
1706    hash_opt_shape(m.clip_shape.as_ref(), hasher);
1707    match m.transform {
1708        Some(transform) => {
1709            1u8.hash(hasher);
1710            hash_f32(transform.translate_x, hasher);
1711            hash_f32(transform.translate_y, hasher);
1712            hash_f32(transform.scale_x, hasher);
1713            hash_f32(transform.scale_y, hasher);
1714            hash_f32(transform.rotate, hasher);
1715            hash_f32(transform.shear_x, hasher);
1716            hash_f32(transform.shear_y, hasher);
1717            hash_f32(transform.rotation_x, hasher);
1718            hash_f32(transform.rotation_y, hasher);
1719            hash_f32(transform.camera_distance, hasher);
1720            hash_f32(transform.origin_x, hasher);
1721            hash_f32(transform.origin_y, hasher);
1722            for value in transform.perspective {
1723                hash_f32(value, hasher);
1724            }
1725        }
1726        None => 0u8.hash(hasher),
1727    }
1728    hash_opt_f32(m.alpha, hasher);
1729    m.position_type
1730        .map(|value| std::mem::discriminant(&value))
1731        .hash(hasher);
1732    hash_opt_dp(m.offset_left, hasher);
1733    hash_opt_dp(m.offset_right, hasher);
1734    hash_opt_dp(m.offset_top, hasher);
1735    hash_opt_dp(m.offset_bottom, hasher);
1736    hash_opt_dp_pair(m.layout_offset, hasher);
1737    hash_opt_dp_pair(m.absolute_layout_offset, hasher);
1738    match &m.grid {
1739        Some(grid) => {
1740            1u8.hash(hasher);
1741            grid.columns.hash(hasher);
1742            hash_dp(grid.row_gap, hasher);
1743            hash_dp(grid.column_gap, hasher);
1744        }
1745        None => 0u8.hash(hasher),
1746    }
1747    m.grid_col_span.hash(hasher);
1748    m.grid_row_span.hash(hasher);
1749    hash_opt_f32(m.aspect_ratio, hasher);
1750    m.intrinsic_width.hash(hasher);
1751    m.intrinsic_height.hash(hasher);
1752    hash_opt_dp(m.fit_content_width, hasher);
1753    hash_opt_dp(m.fit_content_height, hasher);
1754    match m.contain {
1755        None => 0u8.hash(hasher),
1756        Some(value) if value == taffy::Contain::CONTENT => 3u8.hash(hasher),
1757        Some(value) if value == taffy::Contain::LAYOUT => 1u8.hash(hasher),
1758        Some(value) if value == taffy::Contain::PAINT => 2u8.hash(hasher),
1759        Some(_) => 4u8.hash(hasher),
1760    }
1761    hash_f32(m.z_index, hasher);
1762    hash_opt_f32(m.render_z_index, hasher);
1763    m.input_blocker.hash(hasher);
1764    m.click.hash(hasher);
1765    m.disabled.hash(hasher);
1766    m.focusable.hash(hasher);
1767    m.hit_passthrough.hash(hasher);
1768    m.propagate_min.hash(hasher);
1769    m.focus_group.hash(hasher);
1770    m.visible.hash(hasher);
1771    match &m.blur {
1772        Some(blur) => {
1773            1u8.hash(hasher);
1774            hash_dp(blur.radius_x, hasher);
1775            hash_dp(blur.radius_y, hasher);
1776            std::mem::discriminant(&blur.edge_treatment).hash(hasher);
1777        }
1778        None => 0u8.hash(hasher),
1779    }
1780    match &m.scroll {
1781        Some(binding) => hash_scroll_binding(binding, hasher),
1782        None => 0u8.hash(hasher),
1783    }
1784    hash_scrollbar_style(m.scrollbar_style.as_ref(), hasher);
1785    m.overflow
1786        .map(|value| std::mem::discriminant(&value))
1787        .hash(hasher);
1788    match m.clip_rect {
1789        Some(rect) => {
1790            1u8.hash(hasher);
1791            hash_dp(rect.left, hasher);
1792            hash_dp(rect.top, hasher);
1793            hash_dp(rect.right, hasher);
1794            hash_dp(rect.bottom, hasher);
1795            std::mem::discriminant(&rect.op).hash(hasher);
1796        }
1797        None => 0u8.hash(hasher),
1798    }
1799    hash_opt_dp(m.gap, hasher);
1800    hash_opt_dp(m.row_gap, hasher);
1801    hash_opt_dp(m.column_gap, hasher);
1802    hash_opt_dp(m.margin_top, hasher);
1803    hash_opt_dp(m.margin_left, hasher);
1804    hash_opt_dp(m.margin_right, hasher);
1805    hash_opt_dp(m.margin_bottom, hasher);
1806    hash_opt_f32(m.graphics_layer, hasher);
1807    if let Some(scope) = &m.graphics_layer_scope {
1808        1u8.hash(hasher);
1809        hash_rc_ptr_identity(scope, hasher);
1810    } else {
1811        0u8.hash(hasher);
1812    }
1813    m.match_parent_size.hash(hasher);
1814    if let Some((axis, enabled)) = m.fill_parent_max_size {
1815        std::mem::discriminant(&axis).hash(hasher);
1816        enabled.hash(hasher);
1817    } else {
1818        0u8.hash(hasher);
1819    }
1820    hash_opt_dp(m.padding_from_baseline, hasher);
1821    m.paint_order.hash(hasher);
1822    match &m.shadow {
1823        Some(shadow) => {
1824            1u8.hash(hasher);
1825            hash_dp(shadow.blur_radius, hasher);
1826            hash_dp(shadow.offset_x, hasher);
1827            hash_dp(shadow.offset_y, hasher);
1828            hash_dp(shadow.spread, hasher);
1829            hash_color(&shadow.color, hasher);
1830            shadow.inset.hash(hasher);
1831        }
1832        None => 0u8.hash(hasher),
1833    }
1834    match m.shadow_shape {
1835        Some(radius) => {
1836            1u8.hash(hasher);
1837            for value in radius {
1838                hash_dp(value, hasher);
1839            }
1840        }
1841        None => 0u8.hash(hasher),
1842    }
1843    match &m.semantics {
1844        Some(semantics) => {
1845            1u8.hash(hasher);
1846            std::mem::discriminant(&semantics.role).hash(hasher);
1847            semantics.label.hash(hasher);
1848            semantics.focused.hash(hasher);
1849            semantics.enabled.hash(hasher);
1850            semantics.selectable_group.hash(hasher);
1851            semantics.checked.hash(hasher);
1852            semantics.selected.hash(hasher);
1853            semantics.value.hash(hasher);
1854        }
1855        None => 0u8.hash(hasher),
1856    }
1857    hash_cursor(&m.cursor, hasher);
1858    match &m.text_input {
1859        Some(input) => {
1860            1u8.hash(hasher);
1861            input.hint.hash(hasher);
1862            input.multiline.hash(hasher);
1863            input.value.hash(hasher);
1864            input.enabled.hash(hasher);
1865            input.read_only.hash(hasher);
1866            input.sensitive.hash(hasher);
1867            input.max_lines.hash(hasher);
1868            input.min_lines.hash(hasher);
1869            std::mem::discriminant(&input.keyboard_type).hash(hasher);
1870            std::mem::discriminant(&input.capitalization).hash(hasher);
1871            std::mem::discriminant(&input.ime_action).hash(hasher);
1872            input.auto_correct_enabled.hash(hasher);
1873            match input.cursor_color {
1874                Some(color) => {
1875                    1u8.hash(hasher);
1876                    hash_color(&color, hasher);
1877                }
1878                None => 0u8.hash(hasher),
1879            }
1880            match &input.text_style {
1881                Some(style) => {
1882                    1u8.hash(hasher);
1883                    hash_text_style(style, hasher);
1884                }
1885                None => 0u8.hash(hasher),
1886            }
1887            match &input.line_limits {
1888                Some(repose_core::text::TextFieldLineLimits::SingleLine) => 1u8.hash(hasher),
1889                Some(repose_core::text::TextFieldLineLimits::MultiLine {
1890                    min_height_in_lines,
1891                    max_height_in_lines,
1892                }) => {
1893                    2u8.hash(hasher);
1894                    min_height_in_lines.hash(hasher);
1895                    max_height_in_lines.hash(hasher);
1896                }
1897                None => 0u8.hash(hasher),
1898            }
1899        }
1900        None => 0u8.hash(hasher),
1901    }
1902    m.text_selection.is_some().hash(hasher);
1903    match &m.state_colors {
1904        Some(colors) => {
1905            1u8.hash(hasher);
1906            hash_color(&colors.default, hasher);
1907            hash_color(&colors.hovered, hasher);
1908            hash_color(&colors.focused, hasher);
1909            hash_color(&colors.pressed, hasher);
1910            hash_color(&colors.disabled, hasher);
1911            hash_color(&colors.dragged, hasher);
1912        }
1913        None => 0u8.hash(hasher),
1914    }
1915    match &m.state_elevation {
1916        Some(elevation) => {
1917            1u8.hash(hasher);
1918            hash_dp(elevation.default, hasher);
1919            hash_dp(elevation.hovered, hasher);
1920            hash_dp(elevation.focused, hasher);
1921            hash_dp(elevation.pressed, hasher);
1922            hash_dp(elevation.disabled, hasher);
1923            hash_dp(elevation.dragged, hasher);
1924        }
1925        None => 0u8.hash(hasher),
1926    }
1927    match &m.animate_content_size {
1928        Some(spec) => {
1929            1u8.hash(hasher);
1930            hash_animation_spec(spec, hasher);
1931        }
1932        None => 0u8.hash(hasher),
1933    }
1934}
1935
1936#[allow(dead_code)]
1937fn hash_animation_spec(spec: &AnimationSpec, hasher: &mut impl Hasher) {
1938    spec.duration.as_nanos().hash(hasher);
1939    hash_easing(&spec.easing, hasher);
1940    spec.delay.as_nanos().hash(hasher);
1941    match &spec.spring {
1942        Some(spring) => {
1943            1u8.hash(hasher);
1944            hash_f32(spring.damping_ratio, hasher);
1945            hash_f32(spring.stiffness, hasher);
1946            hash_f32(spring.settle_progress, hasher);
1947            hash_f32(spring.settle_velocity, hasher);
1948        }
1949        None => 0u8.hash(hasher),
1950    }
1951    match &spec.repeat {
1952        Some(repeat) => {
1953            1u8.hash(hasher);
1954            repeat.iterations.hash(hasher);
1955            repeat.reverse.hash(hasher);
1956            repeat.delay_between.as_nanos().hash(hasher);
1957        }
1958        None => 0u8.hash(hasher),
1959    }
1960}
1961
1962fn hash_easing(easing: &Easing, hasher: &mut impl Hasher) {
1963    std::mem::discriminant(easing).hash(hasher);
1964    match easing {
1965        Easing::SpringCrit { omega } => hash_f32(*omega, hasher),
1966        Easing::Custom(cb) => {
1967            hash_f32(cb.p1x, hasher);
1968            hash_f32(cb.p1y, hasher);
1969            hash_f32(cb.p2x, hasher);
1970            hash_f32(cb.p2y, hasher);
1971        }
1972        _ => {}
1973    }
1974}
1975
1976fn hash_color(c: &Color, hasher: &mut impl Hasher) {
1977    c.0.hash(hasher);
1978    c.1.hash(hasher);
1979    c.2.hash(hasher);
1980    c.3.hash(hasher);
1981}
1982
1983fn hash_brush(b: &Brush, hasher: &mut impl Hasher) {
1984    std::mem::discriminant(b).hash(hasher);
1985    match b {
1986        Brush::Solid(c) => hash_color(c, hasher),
1987        Brush::Linear {
1988            start,
1989            end,
1990            start_color,
1991            end_color,
1992        } => {
1993            hash_f32(start.x, hasher);
1994            hash_f32(start.y, hasher);
1995            hash_f32(end.x, hasher);
1996            hash_f32(end.y, hasher);
1997            hash_color(start_color, hasher);
1998            hash_color(end_color, hasher);
1999        }
2000        Brush::LinearNormalized {
2001            start,
2002            end,
2003            start_color,
2004            end_color,
2005        } => {
2006            hash_f32(start.x, hasher);
2007            hash_f32(start.y, hasher);
2008            hash_f32(end.x, hasher);
2009            hash_f32(end.y, hasher);
2010            hash_color(start_color, hasher);
2011            hash_color(end_color, hasher);
2012        }
2013        Brush::Radial {
2014            center,
2015            radius,
2016            start_color,
2017            end_color,
2018        } => {
2019            hash_f32(center.x, hasher);
2020            hash_f32(center.y, hasher);
2021            hash_f32(*radius, hasher);
2022            hash_color(start_color, hasher);
2023            hash_color(end_color, hasher);
2024        }
2025        Brush::Sweep {
2026            center,
2027            start_color,
2028            end_color,
2029        } => {
2030            hash_f32(center.x, hasher);
2031            hash_f32(center.y, hasher);
2032            hash_color(start_color, hasher);
2033            hash_color(end_color, hasher);
2034        }
2035        Brush::LinearStops {
2036            start,
2037            end,
2038            stops,
2039            tile_mode,
2040        } => {
2041            hash_f32(start.x, hasher);
2042            hash_f32(start.y, hasher);
2043            hash_f32(end.x, hasher);
2044            hash_f32(end.y, hasher);
2045            hash_gradient_stops(stops, hasher);
2046            std::mem::discriminant(tile_mode).hash(hasher);
2047        }
2048        Brush::LinearNormalizedStops {
2049            start,
2050            end,
2051            stops,
2052            tile_mode,
2053        } => {
2054            hash_f32(start.x, hasher);
2055            hash_f32(start.y, hasher);
2056            hash_f32(end.x, hasher);
2057            hash_f32(end.y, hasher);
2058            hash_gradient_stops(stops, hasher);
2059            std::mem::discriminant(tile_mode).hash(hasher);
2060        }
2061        Brush::RadialStops {
2062            center,
2063            radius,
2064            stops,
2065            tile_mode,
2066        } => {
2067            hash_f32(center.x, hasher);
2068            hash_f32(center.y, hasher);
2069            hash_f32(*radius, hasher);
2070            hash_gradient_stops(stops, hasher);
2071            std::mem::discriminant(tile_mode).hash(hasher);
2072        }
2073        Brush::SweepStops {
2074            center,
2075            stops,
2076            tile_mode,
2077        } => {
2078            hash_f32(center.x, hasher);
2079            hash_f32(center.y, hasher);
2080            hash_gradient_stops(stops, hasher);
2081            std::mem::discriminant(tile_mode).hash(hasher);
2082        }
2083        _ => {}
2084    }
2085}
2086
2087fn hash_gradient_stops(stops: &GradientStops, hasher: &mut impl Hasher) {
2088    let slice = stops.as_slice();
2089    slice.len().hash(hasher);
2090    for stop in slice {
2091        hash_f32(stop.offset, hasher);
2092        hash_color(&stop.color, hasher);
2093    }
2094}
2095
2096fn hash_text_overflow(o: &TextOverflow, hasher: &mut impl Hasher) {
2097    std::mem::discriminant(o).hash(hasher);
2098}
2099
2100#[cfg(test)]
2101mod tests {
2102    use super::*;
2103    use repose_core::{
2104        DrawStyle, FontStyle, FontWeight, ImageAlignment, ImageFilter, ImageFit, ImageSourceRect,
2105        Modifier, TextAlign, TextDecoration, UnitExt, View, ViewKind,
2106    };
2107
2108    #[test]
2109    fn test_same_view_same_hash() {
2110        let v1 = View::new(0, ViewKind::Box).modifier(Modifier::new().width(100.0.dp()));
2111        let v2 = View::new(0, ViewKind::Box).modifier(Modifier::new().width(100.0.dp()));
2112
2113        assert_eq!(hash_view_content(&v1), hash_view_content(&v2));
2114    }
2115
2116    #[test]
2117    fn image_source_rect_changes_hash() {
2118        let image = |source_rect| {
2119            View::new(
2120                0,
2121                ViewKind::Image {
2122                    handle: 1,
2123                    tint: Color::WHITE,
2124                    fit: ImageFit::FillBounds,
2125                    filter: ImageFilter::Nearest,
2126                    source_rect,
2127                    alignment: ImageAlignment::Center,
2128                },
2129            )
2130        };
2131        let full = image(None);
2132        let first = image(Some(ImageSourceRect::new(0, 0, 8, 8)));
2133        let second = image(Some(ImageSourceRect::new(8, 0, 8, 8)));
2134        assert_ne!(hash_view_content(&full), hash_view_content(&first));
2135        assert_ne!(hash_view_content(&first), hash_view_content(&second));
2136    }
2137
2138    #[test]
2139    fn test_scroll_presence_changes_hash() {
2140        use repose_core::scroll::ScrollAxisBinding;
2141
2142        let v1 = View::new(0, ViewKind::Box).modifier(Modifier::new());
2143        let v2 = View::new(0, ViewKind::Box).modifier(Modifier::new().vertical_scroll(
2144            ScrollAxisBinding {
2145                show_scrollbar: true,
2146                ..Default::default()
2147            },
2148        ));
2149
2150        assert_ne!(hash_view_content(&v1), hash_view_content(&v2));
2151    }
2152
2153    #[test]
2154    fn test_different_view_different_hash() {
2155        let v1 = View::new(0, ViewKind::Box).modifier(Modifier::new().width(100.0.dp()));
2156        let v2 = View::new(0, ViewKind::Box).modifier(Modifier::new().width(200.0.dp()));
2157
2158        assert_ne!(hash_view_content(&v1), hash_view_content(&v2));
2159    }
2160
2161    #[test]
2162    fn test_text_content_hash() {
2163        let v1 = View::new(
2164            0,
2165            ViewKind::Text {
2166                text: "Hello".to_string(),
2167                color: Color::WHITE,
2168                font_size: 16.0.sp(),
2169                soft_wrap: true,
2170                max_lines: None,
2171                overflow: TextOverflow::Visible,
2172                font_family: None,
2173                annotations: None,
2174                text_align: TextAlign::Unspecified,
2175                font_weight: FontWeight::NORMAL,
2176                font_style: FontStyle::Normal,
2177                text_decoration: TextDecoration::default(),
2178                letter_spacing: Sp::ZERO,
2179                line_height: Sp::ZERO,
2180                url: None,
2181                font_variation_settings: None,
2182                draw_style: DrawStyle::Fill,
2183            },
2184        );
2185        let v2 = View::new(
2186            0,
2187            ViewKind::Text {
2188                text: "Hello".to_string(),
2189                color: Color::WHITE,
2190                font_size: 16.0.sp(),
2191                soft_wrap: true,
2192                max_lines: None,
2193                overflow: TextOverflow::Visible,
2194                font_family: None,
2195                annotations: None,
2196                text_align: TextAlign::Unspecified,
2197                font_weight: FontWeight::NORMAL,
2198                font_style: FontStyle::Normal,
2199                text_decoration: TextDecoration::default(),
2200                letter_spacing: Sp::ZERO,
2201                line_height: Sp::ZERO,
2202                url: None,
2203                font_variation_settings: None,
2204                draw_style: DrawStyle::Fill,
2205            },
2206        );
2207        let v3 = View::new(
2208            0,
2209            ViewKind::Text {
2210                text: "World".to_string(),
2211                color: Color::WHITE,
2212                font_size: 16.0.sp(),
2213                soft_wrap: true,
2214                max_lines: None,
2215                overflow: TextOverflow::Visible,
2216                font_family: None,
2217                annotations: None,
2218                text_align: TextAlign::Unspecified,
2219                font_weight: FontWeight::NORMAL,
2220                font_style: FontStyle::Normal,
2221                text_decoration: TextDecoration::default(),
2222                letter_spacing: Sp::ZERO,
2223                line_height: Sp::ZERO,
2224                url: None,
2225                font_variation_settings: None,
2226                draw_style: DrawStyle::Fill,
2227            },
2228        );
2229
2230        assert_eq!(hash_view_content(&v1), hash_view_content(&v2));
2231        assert_ne!(hash_view_content(&v1), hash_view_content(&v3));
2232    }
2233
2234    fn text_view_with_url(url: Option<&str>) -> View {
2235        View::new(
2236            0,
2237            ViewKind::Text {
2238                text: "link".to_string(),
2239                color: Color::WHITE,
2240                font_size: 16.0.sp(),
2241                soft_wrap: true,
2242                max_lines: None,
2243                overflow: TextOverflow::Visible,
2244                font_family: None,
2245                annotations: None,
2246                text_align: TextAlign::Unspecified,
2247                font_weight: FontWeight::NORMAL,
2248                font_style: FontStyle::Normal,
2249                text_decoration: TextDecoration::default(),
2250                letter_spacing: Sp::ZERO,
2251                line_height: Sp::ZERO,
2252                url: url.map(std::sync::Arc::from),
2253                font_variation_settings: None,
2254                draw_style: DrawStyle::Fill,
2255            },
2256        )
2257    }
2258
2259    #[test]
2260    fn view_id_and_span_baseline_change_hash() {
2261        let first = View::new(1, ViewKind::Box);
2262        let second = View::new(2, ViewKind::Box);
2263        assert_ne!(hash_view_content(&first), hash_view_content(&second));
2264
2265        let mut a = text_view_with_url(None);
2266        let mut b = a.clone();
2267        let span = repose_core::TextSpan {
2268            start: 0,
2269            end: 4,
2270            style: repose_core::SpanStyle::default()
2271                .baseline_shift(repose_core::BaselineShift::Superscript),
2272            url: None,
2273        };
2274        if let ViewKind::Text { annotations, .. } = &mut a.kind {
2275            *annotations = Some(std::sync::Arc::from([span.clone()]));
2276        }
2277        if let ViewKind::Text { annotations, .. } = &mut b.kind {
2278            *annotations = Some(std::sync::Arc::from([repose_core::TextSpan {
2279                style: repose_core::SpanStyle::default()
2280                    .baseline_shift(repose_core::BaselineShift::Subscript),
2281                ..span
2282            }]));
2283        }
2284        assert_ne!(hash_view_content(&a), hash_view_content(&b));
2285    }
2286
2287    #[test]
2288    fn test_text_url_change_invalidates() {
2289        let a = text_view_with_url(None);
2290        let b = text_view_with_url(Some("https://a.example"));
2291        let c = text_view_with_url(Some("https://b.example"));
2292        assert_ne!(hash_view_content(&a), hash_view_content(&b));
2293        assert_ne!(hash_view_content(&b), hash_view_content(&c));
2294    }
2295
2296    #[test]
2297    fn test_semantics_state_invalidates() {
2298        let mk = |checked: Option<bool>| {
2299            View::new(0, ViewKind::Box).semantics(
2300                repose_core::Semantics::new(repose_core::Role::Checkbox)
2301                    .with_checked(checked.unwrap_or(false)),
2302            )
2303        };
2304        assert_ne!(
2305            hash_view_content(&mk(Some(true))),
2306            hash_view_content(&mk(Some(false)))
2307        );
2308        let m1 = View::new(0, ViewKind::Box).modifier(
2309            Modifier::new().semantics(repose_core::Semantics::new(repose_core::Role::Button)),
2310        );
2311        let m2 = View::new(0, ViewKind::Box).modifier(
2312            Modifier::new()
2313                .semantics(repose_core::Semantics::new(repose_core::Role::Button).with_value("v")),
2314        );
2315        assert_ne!(hash_view_content(&m1), hash_view_content(&m2));
2316    }
2317
2318    #[test]
2319    fn test_then_preserves_layer_fields() {
2320        let base = Modifier::new().width(10.0.dp());
2321        let overlay = Modifier::new()
2322            .blur(4.0.dp())
2323            .shadow_with_color(8.0.dp(), 2.0.dp(), Color::BLACK)
2324            .graphics_layer(0.5);
2325        let merged = base.then(overlay);
2326        assert!(merged.blur.is_some());
2327        assert!(merged.shadow.is_some());
2328        assert!(merged.graphics_layer.is_some());
2329        assert_eq!(
2330            Modifier::new()
2331                .z_index(5.0)
2332                .then(Modifier::new().z_index(0.0))
2333                .z_index,
2334            0.0
2335        );
2336    }
2337}