hydrolysis 0.1.0

A modern UI framework for Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
use super::*;
use waterui_core::Computed;
use waterui_core::layout::LayoutPriority;
use waterui_core::metadata::MetadataKey;

pub(crate) fn gesture_group_identity(view: &AnyView) -> usize {
    gesture_group_identity_with_budget(view, 64)
}

pub(crate) fn flatten_environment_metadata_ref<'a>(
    mut view: &'a AnyView,
    env: &Environment,
) -> (&'a AnyView, Environment) {
    let mut scoped_env = env.clone();
    while let Some(metadata) = view.downcast_ref::<Metadata<Environment>>() {
        scoped_env = metadata.value.clone();
        view = &metadata.content;
    }
    (view, scoped_env)
}

pub(crate) fn flatten_environment_metadata_owned(
    mut view: AnyView,
    env: &Environment,
) -> (AnyView, Environment) {
    let mut scoped_env = env.clone();
    while view.is::<Metadata<Environment>>() {
        let Metadata { content, value } = *view
            .downcast::<Metadata<Environment>>()
            .expect("environment metadata flattening downcast must succeed");
        scoped_env = value.clone();
        view = content;
    }
    (view, scoped_env)
}

/// Extends `env` with an accessibility metadata value exactly like the retained
/// build's `Env`-scoping arm does. Layout normalization pre-resolves composite
/// bodies (`view.body(env)`), and a body may snapshot its environment into a
/// `Metadata<Environment>` override (the `With`-style env wrappers behind
/// `.foreground()` and friends), so normalization must resolve content under
/// the same scoped environment the build-time `Env` node installs — otherwise
/// the snapshot, which replaces the environment wholesale when flattened,
/// silently drops the accessibility scoping above it.
pub(crate) fn a11y_scoped_env<T: MetadataKey + Clone + 'static>(
    env: &Environment,
    value: &T,
) -> Environment {
    let mut scoped = env.clone();
    scoped.insert(value.clone());
    scoped
}

/// The `Env` scoping for naming metadata (`.a11y_label()` / `.a11y_role()`).
///
/// Alongside the value, the scope carries a [`ScopedAccessibilitySemantics`]
/// identity so the node that ends up representing the wrapped view can claim it.
/// A control claims it by registering its own node; a composed container finds it
/// unclaimed and synthesizes the node naming itself. Nesting two of these (the
/// usual `.a11y_label(..).a11y_role(..)` pair) is harmless: only the innermost is
/// visible below, and it is the one every consumer resolves against.
#[cfg(feature = "accessibility")]
pub(crate) fn a11y_naming_scoped_env<T: MetadataKey + Clone + 'static>(
    env: &Environment,
    value: &T,
) -> Environment {
    let mut scoped = a11y_scoped_env(env, value);
    scoped.insert(ScopedAccessibilitySemantics::new());
    scoped
}

/// Carries the accessibility naming scope across a `Metadata<Environment>`
/// override.
///
/// Naming metadata is *nearest-consumer*: layout normalization deliberately
/// leaves `.a11y_label()` / `.a11y_role()` / `.a11y_id()` out of the
/// environments composite bodies snapshot, because a group label baked into one
/// would relabel every descendant leaf. The build-time `Env` node above is what
/// the emitting leaf reads instead — which works right up until the body
/// *itself* produces a snapshot, because flattening one replaces the
/// environment wholesale and takes that `Env` scope with it.
///
/// A view hook does exactly that. [`Hook`](waterui_core::view::Hook) wraps
/// whatever it returns in a snapshot of the environment it was called with, and
/// hooks are how the self-drawn realizations install themselves, so with
/// `map-gpu` or `video-gpu` selected every `Map` and `Video` sits under one.
/// Without this, `.a11y_label("City map")` on such a map reached neither the
/// engine — `waterui-map-gpu` only generates its own description when it finds
/// no ambient label — nor the node hydrolysis emitted, and the caller's name was
/// silently replaced by a generated one.
///
/// Only what the snapshot does not already carry is restored, so a snapshot
/// taken *below* a naming scope still speaks for itself.
#[cfg(feature = "accessibility")]
pub(crate) fn restore_a11y_naming_scope(
    outer: &Environment,
    mut snapshot: Environment,
) -> Environment {
    macro_rules! restore {
        ($($ty:ty),+ $(,)?) => {
            $(
                if snapshot.get::<$ty>().is_none()
                    && let Some(value) = outer.get::<$ty>().cloned()
                {
                    snapshot.insert(value);
                }
            )+
        };
    }
    restore!(
        AccessibilityLabel,
        AccessibilityRole,
        AccessibilityChildren,
        ScopedAccessibilityIdentifier,
        // The claim identity travels with the name: carrying the label without
        // it would let the leaf render the name that a container above still
        // believes nobody spoke for, and both would emit it.
        ScopedAccessibilitySemantics,
    );
    snapshot
}

/// Without an accessibility tree there is no naming scope to carry.
#[cfg(not(feature = "accessibility"))]
pub(crate) const fn restore_a11y_naming_scope(
    _outer: &Environment,
    snapshot: Environment,
) -> Environment {
    snapshot
}

/// The `Env` scoping for a static [`AccessibilityState`]: a hidden state
/// suppresses the whole subtree's emission (a constant can never un-hide), and
/// the state is stored as a constant [`AccessibilityStateSignal`] so emission
/// resolves one code path for static and reactive state alike.
pub(crate) fn a11y_scoped_env_for_state(
    env: &Environment,
    value: &AccessibilityState,
) -> Environment {
    let mut scoped = env.clone();
    if value.is_hidden() {
        scoped.insert(AccessibilityHidden::new(true));
    }
    scoped.insert(AccessibilityStateSignal::new(Computed::constant(
        value.clone(),
    )));
    scoped
}

fn gesture_group_identity_with_budget(view: &AnyView, remaining: usize) -> usize {
    assert!(
        (remaining != 0),
        "hydrolysis gesture group identity extraction exceeded recursion budget for {}",
        view.name()
    );
    if let Some(metadata) = view.downcast_ref::<Metadata<Environment>>() {
        return gesture_group_identity_with_budget(&metadata.content, remaining - 1);
    }
    if let Some(content) = passthrough_content(view) {
        return gesture_group_identity_with_budget(content, remaining - 1);
    }
    view.stable_ptr() as usize
}

pub(crate) fn passthrough_content(view: &AnyView) -> Option<&AnyView> {
    macro_rules! passthrough_metadata_content {
        ($($ty:ty),+ $(,)?) => {
            $(
                if let Some(metadata) = view.downcast_ref::<Metadata<$ty>>() {
                    return Some(&metadata.content);
                }
            )+
        };
    }

    macro_rules! passthrough_ignorable_metadata_content {
        ($($ty:ty),+ $(,)?) => {
            $(
                if let Some(metadata) = view.downcast_ref::<IgnorableMetadata<$ty>>() {
                    return Some(&metadata.content);
                }
            )+
        };
    }

    passthrough_metadata_content!(
        Environment,
        Retain,
        Opacity,
        AppliedFilter,
        Scale,
        Rotation,
        Offset,
        ClipShape,
        Border,
        Shadow,
        Focused,
        Hittable,
        GestureObserver,
        LifeCycleHook,
        OnEvent,
        Secure,
        StandardDynamicRange,
        HighDynamicRange,
        Cursor,
        IgnoreSafeArea,
        ContextMenu,
        ResolvedContextMenu,
        Draggable,
        DropDestination,
        Background,
        NavigationTransitionSource,
        NavigationTransitionDestination
    );
    passthrough_ignorable_metadata_content!(
        MaterialBackground,
        AccessibilityIdentifier,
        AccessibilityLabel,
        AccessibilityRole,
        AccessibilityHidden,
        AccessibilityChildren,
        AccessibilityState,
        AccessibilityStateSignal
    );

    None
}

pub(crate) fn effective_stretch_axis(view: &AnyView) -> StretchAxis {
    if let Some(content) = passthrough_content(view) {
        return effective_stretch_axis(content);
    }
    if view.downcast_ref::<Divider>().is_some() {
        return StretchAxis::CrossAxis;
    }
    view.stretch_axis()
}

fn is_layout_terminal(view: &AnyView) -> bool {
    if view.downcast_ref::<Str>().is_some() || view.downcast_ref::<Divider>().is_some() {
        return true;
    }
    super::is_hydro_native_view(view)
}

pub(crate) fn normalize_view_for_render(view: AnyView, env: &Environment) -> AnyView {
    normalize_layout_view(view, env)
}

pub(crate) fn normalize_layout_view(view: AnyView, env: &Environment) -> AnyView {
    normalize_layout_view_with_budget(view, env, 64)
}

fn normalize_layout_view_with_budget(
    view: AnyView,
    env: &Environment,
    remaining: usize,
) -> AnyView {
    assert!(
        (remaining != 0),
        "hydrolysis layout normalization exceeded recursion budget for {}",
        view.name()
    );
    let next_remaining = remaining - 1;
    let mut view = view;

    if view.is::<Metadata<Environment>>() {
        let Metadata { content, value } = *view
            .downcast::<Metadata<Environment>>()
            .expect("layout normalization failed to downcast Metadata<Environment>");
        let normalized_content = normalize_layout_view_with_budget(content, &value, remaining);
        return AnyView::new(Metadata {
            content: normalized_content,
            value,
        });
    }
    macro_rules! normalize_passthrough_metadata {
        ($($ty:ty),+ $(,)?) => {
            $(
                if view.is::<Metadata<$ty>>() {
                    let Metadata { content, value } = *view
                        .downcast::<Metadata<$ty>>()
                        .expect("layout normalization failed to downcast metadata");
                    let normalized_content =
                        normalize_layout_view_with_budget(content, env, remaining);
                    return AnyView::new(Metadata {
                        content: normalized_content,
                        value,
                    });
                }
            )+
        };
    }
    macro_rules! normalize_passthrough_ignorable_metadata {
        ($($ty:ty),+ $(,)?) => {
            $(
                if view.is::<IgnorableMetadata<$ty>>() {
                    let IgnorableMetadata { content, value } = *view
                        .downcast::<IgnorableMetadata<$ty>>()
                        .expect("layout normalization failed to downcast ignorable metadata");
                    let normalized_content =
                        normalize_layout_view_with_budget(content, env, remaining);
                    return AnyView::new(IgnorableMetadata {
                        content: normalized_content,
                        value,
                    });
                }
            )+
        };
    }

    normalize_passthrough_metadata!(
        LayoutPriority,
        Retain,
        Opacity,
        AppliedFilter,
        Scale,
        Rotation,
        Offset,
        ClipShape,
        Border,
        Shadow,
        Focused,
        Hittable,
        GestureObserver,
        LifeCycleHook,
        OnEvent,
        Secure,
        StandardDynamicRange,
        HighDynamicRange,
        Cursor,
        IgnoreSafeArea,
        ContextMenu,
        ResolvedContextMenu,
        Draggable,
        DropDestination,
        Background,
        NavigationTransitionSource,
        NavigationTransitionDestination
    );
    // Label/role/children stay plain passthrough here: they are
    // nearest-consumer metadata — the leaf that emits the semantic node reads
    // them from the build-time `Env` scope, and they must not be baked into the
    // environments that composite bodies snapshot below (a group label would
    // then relabel every descendant leaf).
    normalize_passthrough_ignorable_metadata!(
        MaterialBackground,
        AccessibilityIdentifier,
        AccessibilityLabel,
        AccessibilityRole,
        AccessibilityChildren
    );

    // Hidden and (reactive) state have *subtree* semantics: every emission
    // below resolves them from its environment. Their content must therefore be
    // normalized under the same scoped environment the build arm installs — a
    // composite body resolved below may snapshot its environment into a
    // `Metadata<Environment>` override, and a snapshot taken without the
    // scoping would drop the hidden/state scope wholesale at flush (an
    // `a11y_state_signal(hidden)` overlay stayed visible exactly this way).
    macro_rules! normalize_env_scoping_ignorable_metadata {
        ($($ty:ty, $scope:expr);+ $(;)?) => {
            $(
                if view.is::<IgnorableMetadata<$ty>>() {
                    let IgnorableMetadata { content, value } = *view
                        .downcast::<IgnorableMetadata<$ty>>()
                        .expect("layout normalization failed to downcast ignorable metadata");
                    let scoped = $scope(env, &value);
                    let normalized_content =
                        normalize_layout_view_with_budget(content, &scoped, remaining);
                    return AnyView::new(IgnorableMetadata {
                        content: normalized_content,
                        value,
                    });
                }
            )+
        };
    }
    normalize_env_scoping_ignorable_metadata!(
        AccessibilityHidden, a11y_scoped_env;
        AccessibilityStateSignal, a11y_scoped_env;
        AccessibilityState, a11y_scoped_env_for_state
    );

    if view.is::<Native<FixedContainer>>() {
        let native = *view
            .downcast::<Native<FixedContainer>>()
            .expect("layout normalization failed to downcast Native<FixedContainer>");
        let (layout, children) = native.into_inner().into_inner();
        let mut normalized_children = Vec::with_capacity(children.len());
        for child in children {
            normalized_children.push(normalize_layout_view_with_budget(child, env, remaining));
        }
        return AnyView::new(Native::new(FixedContainer::from_parts(
            layout,
            normalized_children,
        )));
    }

    if view.is::<Native<LazyContainer>>() {
        return view;
    }

    if view.is::<Native<ScrollView>>() {
        let native = *view
            .downcast::<Native<ScrollView>>()
            .expect("layout normalization failed to downcast Native<ScrollView>");
        let (axis, content, controller) = native.into_inner().into_inner();
        let normalized_content = normalize_layout_view_with_budget(content, env, remaining);
        let scroll = ScrollView::new(axis, normalized_content);
        let scroll = match controller {
            Some(controller) => scroll.scroll_controller(&controller),
            None => scroll,
        };
        return AnyView::new(Native::new(scroll));
    }

    if view.is::<Native<NavigationView>>() {
        let native = *view
            .downcast::<Native<NavigationView>>()
            .expect("layout normalization failed to downcast Native<NavigationView>");
        let mut navigation = native.into_inner();
        navigation.bar.title =
            normalize_layout_view_with_budget(navigation.bar.title, env, remaining);
        navigation.bar.subtitle =
            normalize_layout_view_with_budget(navigation.bar.subtitle, env, remaining);
        for item in &mut navigation.bar.toolbar.items {
            item.content = normalize_layout_view_with_budget(
                core::mem::take(&mut item.content),
                env,
                remaining,
            );
        }
        navigation.content = normalize_layout_view_with_budget(navigation.content, env, remaining);
        return AnyView::new(Native::new(navigation));
    }

    if is_layout_terminal(&view) {
        return view;
    }

    view = AnyView::new(view.body(env));
    normalize_layout_view_with_budget(view, env, next_remaining)
}

pub(crate) fn estimate_layout_intrinsic<'a>(
    layout: &dyn Layout,
    children: impl IntoIterator<Item = &'a AnyView>,
    state: &mut HydroState,
    env: &Environment,
) -> LayoutSize {
    let state = RefCell::new(state);
    let children: Vec<&AnyView> = children.into_iter().collect();
    let mut subviews = Vec::new();
    for child in children {
        subviews.push(HydroSubview::from_view(child, &state, env));
    }
    let refs: Vec<&dyn SubView> = subviews.iter().map(|view| view as &dyn SubView).collect();
    layout.size_that_fits(ProposalSize::UNSPECIFIED, &refs)
}

pub(crate) fn resolved_color_to_peniko(color: ResolvedColor) -> vello::peniko::Color {
    let srgb = color.to_srgb_with_headroom();
    vello::peniko::Color::new([srgb.red, srgb.green, srgb.blue, color.opacity])
}

pub(crate) fn resolved_gradient_to_brush(
    gradient: &ResolvedGradient,
    bounds: vello::kurbo::Rect,
) -> vello::peniko::Brush {
    let mut stops: Vec<vello::peniko::ColorStop> =
        gradient.stops.iter().map(to_peniko_stop).collect();

    let brush = match gradient.gradient_type {
        GradientType::Linear => {
            let start = resolved_point_to_kurbo(gradient.start_point, bounds);
            let end = resolved_point_to_kurbo(gradient.end_point, bounds);
            vello::peniko::Gradient::new_linear(start, end).with_stops(&*stops)
        }
        GradientType::Radial => {
            let center = resolved_point_to_kurbo(gradient.start_point, bounds);
            let radius_scale = bounds.width().min(bounds.height()) as f32;
            let start_radius = gradient.start_value * radius_scale;
            let end_radius = gradient.end_value * radius_scale;
            vello::peniko::Gradient::new_two_point_radial(center, start_radius, center, end_radius)
                .with_stops(&*stops)
        }
        GradientType::Angular => {
            let sweep = gradient.end_value - gradient.start_value;
            let sweep_fraction = f64::from(sweep) / TAU;
            if sweep_fraction < 1.0 {
                let last_color = stops
                    .last()
                    .expect("resolved gradient must contain at least one stop")
                    .color;
                for stop in &mut stops {
                    stop.offset = (f64::from(stop.offset) * sweep_fraction) as f32;
                }
                stops.push(vello::peniko::ColorStop {
                    offset: sweep_fraction as f32,
                    color: last_color,
                });
                stops.push(vello::peniko::ColorStop {
                    offset: 1.0,
                    color: last_color,
                });
            }
            let center = resolved_point_to_kurbo(gradient.start_point, bounds);
            vello::peniko::Gradient::new_sweep(center, gradient.start_value, 0.0)
                .with_stops(&*stops)
        }
        GradientType::Mesh => {
            panic!("resolved mesh gradient must not be dispatched through ResolvedGradient")
        }
    };

    vello::peniko::Brush::Gradient(brush)
}

fn resolved_point_to_kurbo(point: [f32; 2], bounds: vello::kurbo::Rect) -> vello::kurbo::Point {
    vello::kurbo::Point::new(
        f64::from(point[0]) * bounds.width(),
        f64::from(point[1]) * bounds.height(),
    )
}

fn to_peniko_stop(stop: &ResolvedGradientStop) -> vello::peniko::ColorStop {
    vello::peniko::ColorStop {
        offset: stop.position,
        color: resolved_color_to_peniko(stop.color).into(),
    }
}

/// Resolves a shape into a concrete path for `bounds`.
///
/// Structured shape kinds resolve their normalized corner radii against the
/// shorter side, producing circular corners — the same interpretation the
/// morph path and the Apple backend use. Scaling the unit-space path
/// non-uniformly instead (the `CustomPath` fallback) stretches corner arcs
/// into ellipse segments on wide containers, which violates the Material
/// corner shape (e.g. a 4dp snackbar radius smeared across a 1500px bar).
pub(crate) fn resolved_shape_to_path(
    shape: &ResolvedShape,
    bounds: vello::kurbo::Rect,
) -> vello::kurbo::BezPath {
    shape_kind_path(shape.kind, bounds)
        .unwrap_or_else(|| path_commands_to_path(&shape.commands, bounds))
}

/// Bounds-aware path for the structured shape kinds; `None` for custom paths,
/// which only exist as unit-space commands.
pub(crate) fn shape_kind_path(
    kind: ShapeKind,
    bounds: vello::kurbo::Rect,
) -> Option<vello::kurbo::BezPath> {
    use vello::kurbo::Shape as _;
    const PATH_TOLERANCE: f64 = 0.05;
    match kind {
        ShapeKind::Rect
        | ShapeKind::RoundedRect { .. }
        | ShapeKind::UnevenRoundedRect { .. }
        | ShapeKind::Capsule => Some(rounded_rect_path(bounds, shape_kind_radii(kind))),
        ShapeKind::Circle => {
            let radius = bounds.width().min(bounds.height()).max(0.0) / 2.0;
            Some(vello::kurbo::Circle::new(bounds.center(), radius).into_path(PATH_TOLERANCE))
        }
        ShapeKind::Ellipse => {
            Some(vello::kurbo::Ellipse::from_rect(bounds).into_path(PATH_TOLERANCE))
        }
        ShapeKind::CustomPath => None,
    }
}

pub(crate) fn resolved_morph_shape_to_path(
    shape: &ResolvedMorphShape,
    progress: f32,
    bounds: vello::kurbo::Rect,
) -> vello::kurbo::BezPath {
    let from = shape_kind_radii(shape.from);
    let to = shape_kind_radii(shape.to);
    let progress = progress.clamp(0.0, 1.0);
    let radii = [
        lerp(from[0], to[0], progress),
        lerp(from[1], to[1], progress),
        lerp(from[2], to[2], progress),
        lerp(from[3], to[3], progress),
    ];
    rounded_rect_path(bounds, radii)
}

fn shape_kind_radii(kind: ShapeKind) -> [f32; 4] {
    match kind {
        ShapeKind::Rect => [0.0; 4],
        ShapeKind::Circle | ShapeKind::Ellipse | ShapeKind::Capsule => [0.5; 4],
        ShapeKind::RoundedRect { corner_radius } => [corner_radius.clamp(0.0, 0.5); 4],
        ShapeKind::UnevenRoundedRect {
            top_left,
            top_right,
            bottom_left,
            bottom_right,
        } => [
            top_left.clamp(0.0, 0.5),
            top_right.clamp(0.0, 0.5),
            bottom_right.clamp(0.0, 0.5),
            bottom_left.clamp(0.0, 0.5),
        ],
        ShapeKind::CustomPath => {
            panic!("hydrolysis morph shape rendering requires built-in shape kinds")
        }
    }
}

fn rounded_rect_path(bounds: vello::kurbo::Rect, radii: [f32; 4]) -> vello::kurbo::BezPath {
    const KAPPA: f64 = 0.552_284_749_830_793_6;
    let min_side = bounds.width().min(bounds.height()).max(0.0);
    let [tl, tr, br, bl] = radii.map(|radius| f64::from(radius.clamp(0.0, 0.5)) * min_side);
    let mut path = vello::kurbo::BezPath::new();

    path.move_to((bounds.x0 + tl, bounds.y0));
    path.line_to((bounds.x1 - tr, bounds.y0));
    append_corner(
        &mut path,
        vello::kurbo::Point::new(bounds.x1 - tr, bounds.y0 + tr),
        tr,
        -core::f64::consts::FRAC_PI_2,
        0.0,
        KAPPA,
    );
    path.line_to((bounds.x1, bounds.y1 - br));
    append_corner(
        &mut path,
        vello::kurbo::Point::new(bounds.x1 - br, bounds.y1 - br),
        br,
        0.0,
        core::f64::consts::FRAC_PI_2,
        KAPPA,
    );
    path.line_to((bounds.x0 + bl, bounds.y1));
    append_corner(
        &mut path,
        vello::kurbo::Point::new(bounds.x0 + bl, bounds.y1 - bl),
        bl,
        core::f64::consts::FRAC_PI_2,
        core::f64::consts::PI,
        KAPPA,
    );
    path.line_to((bounds.x0, bounds.y0 + tl));
    append_corner(
        &mut path,
        vello::kurbo::Point::new(bounds.x0 + tl, bounds.y0 + tl),
        tl,
        core::f64::consts::PI,
        core::f64::consts::PI + core::f64::consts::FRAC_PI_2,
        KAPPA,
    );
    path.close_path();
    path
}

fn append_corner(
    path: &mut vello::kurbo::BezPath,
    center: vello::kurbo::Point,
    radius: f64,
    start: f64,
    end: f64,
    kappa: f64,
) {
    if radius <= 0.0 {
        return;
    }
    let start_point = vello::kurbo::Point::new(
        center.x + radius * start.cos(),
        center.y + radius * start.sin(),
    );
    let end_point =
        vello::kurbo::Point::new(center.x + radius * end.cos(), center.y + radius * end.sin());
    let c1 = vello::kurbo::Point::new(
        start_point.x - radius * kappa * start.sin(),
        start_point.y + radius * kappa * start.cos(),
    );
    let c2 = vello::kurbo::Point::new(
        end_point.x + radius * kappa * end.sin(),
        end_point.y - radius * kappa * end.cos(),
    );
    path.curve_to(c1, c2, end_point);
}

fn lerp(from: f32, to: f32, progress: f32) -> f32 {
    from + (to - from) * progress
}

pub(crate) fn path_commands_to_path(
    commands: &[PathCommand],
    bounds: vello::kurbo::Rect,
) -> vello::kurbo::BezPath {
    let width = bounds.width();
    let height = bounds.height();
    let mut path = vello::kurbo::BezPath::new();
    let mut has_current = false;

    for command in commands {
        match command {
            PathCommand::MoveTo { x, y } => {
                path.move_to(vello::kurbo::Point::new(
                    f64::from(*x) * width,
                    f64::from(*y) * height,
                ));
                has_current = true;
            }
            PathCommand::LineTo { x, y } => {
                assert!(
                    has_current,
                    "PathCommand::LineTo requires an active current point"
                );
                path.line_to(vello::kurbo::Point::new(
                    f64::from(*x) * width,
                    f64::from(*y) * height,
                ));
            }
            PathCommand::QuadTo { cx, cy, x, y } => {
                assert!(
                    has_current,
                    "PathCommand::QuadTo requires an active current point"
                );
                path.quad_to(
                    vello::kurbo::Point::new(f64::from(*cx) * width, f64::from(*cy) * height),
                    vello::kurbo::Point::new(f64::from(*x) * width, f64::from(*y) * height),
                );
            }
            PathCommand::CubicTo {
                c1x,
                c1y,
                c2x,
                c2y,
                x,
                y,
            } => {
                assert!(
                    has_current,
                    "PathCommand::CubicTo requires an active current point"
                );
                path.curve_to(
                    vello::kurbo::Point::new(f64::from(*c1x) * width, f64::from(*c1y) * height),
                    vello::kurbo::Point::new(f64::from(*c2x) * width, f64::from(*c2y) * height),
                    vello::kurbo::Point::new(f64::from(*x) * width, f64::from(*y) * height),
                );
            }
            PathCommand::Arc {
                cx,
                cy,
                rx,
                ry,
                start,
                sweep,
            } => {
                let center_x = f64::from(*cx) * width;
                let center_y = f64::from(*cy) * height;
                let radius_x = f64::from(*rx) * width;
                let radius_y = f64::from(*ry) * height;
                let start = f64::from(*start);
                let step = f64::from(*sweep) / 32.0;

                let start_point = vello::kurbo::Point::new(
                    center_x + radius_x * start.cos(),
                    center_y + radius_y * start.sin(),
                );
                if has_current {
                    path.line_to(start_point);
                } else {
                    path.move_to(start_point);
                    has_current = true;
                }

                let mut angle = start;
                for _ in 0..32 {
                    angle += step;
                    path.line_to(vello::kurbo::Point::new(
                        center_x + radius_x * angle.cos(),
                        center_y + radius_y * angle.sin(),
                    ));
                }
            }
            PathCommand::Close => {
                path.close_path();
                has_current = false;
            }
        }
    }

    path
}

pub(crate) fn anchor_point(
    bounds: vello::kurbo::Rect,
    anchor: waterui::style::Anchor,
) -> vello::kurbo::Point {
    vello::kurbo::Point::new(
        bounds.x0 + bounds.width() * f64::from(anchor.x),
        bounds.y0 + bounds.height() * f64::from(anchor.y),
    )
}

pub(crate) fn resolved_color_to_rgba8(color: ResolvedColor) -> [u8; 4] {
    let srgb = color.to_srgb_with_headroom();
    [
        (srgb.red.clamp(0.0, 1.0) * 255.0).round() as u8,
        (srgb.green.clamp(0.0, 1.0) * 255.0).round() as u8,
        (srgb.blue.clamp(0.0, 1.0) * 255.0).round() as u8,
        (color.opacity.clamp(0.0, 1.0) * 255.0).round() as u8,
    ]
}

pub(crate) fn rgba8_to_peniko(color: [u8; 4]) -> vello::peniko::Color {
    vello::peniko::Color::new([
        f32::from(color[0]) / 255.0,
        f32::from(color[1]) / 255.0,
        f32::from(color[2]) / 255.0,
        f32::from(color[3]) / 255.0,
    ])
}

pub(crate) fn parley_font_weight(weight: TextFontWeight) -> parley::FontWeight {
    let value = match weight {
        TextFontWeight::Thin => 100.0,
        TextFontWeight::UltraLight => 200.0,
        TextFontWeight::Light => 300.0,
        TextFontWeight::Normal => 400.0,
        TextFontWeight::Medium => 500.0,
        TextFontWeight::SemiBold => 600.0,
        TextFontWeight::Bold => 700.0,
        TextFontWeight::UltraBold => 800.0,
        TextFontWeight::Black => 900.0,
    };
    parley::FontWeight::new(value)
}

pub(crate) fn parley_alignment(
    alignment: HorizontalAlignment,
    right_to_left: bool,
) -> parley::Alignment {
    if alignment == HorizontalAlignment::Leading && right_to_left
        || alignment == HorizontalAlignment::Trailing && !right_to_left
    {
        parley::Alignment::Right
    } else if alignment == HorizontalAlignment::Leading
        || alignment == HorizontalAlignment::Trailing
    {
        parley::Alignment::Left
    } else {
        parley::Alignment::Center
    }
}

pub(crate) fn transformed_rect(
    transform: vello::kurbo::Affine,
    rect: vello::kurbo::Rect,
) -> vello::kurbo::Rect {
    let points = [
        transform * vello::kurbo::Point::new(rect.x0, rect.y0),
        transform * vello::kurbo::Point::new(rect.x1, rect.y0),
        transform * vello::kurbo::Point::new(rect.x0, rect.y1),
        transform * vello::kurbo::Point::new(rect.x1, rect.y1),
    ];
    let min_x = points
        .iter()
        .fold(f64::INFINITY, |acc, point| acc.min(point.x));
    let min_y = points
        .iter()
        .fold(f64::INFINITY, |acc, point| acc.min(point.y));
    let max_x = points
        .iter()
        .fold(f64::NEG_INFINITY, |acc, point| acc.max(point.x));
    let max_y = points
        .iter()
        .fold(f64::NEG_INFINITY, |acc, point| acc.max(point.y));
    vello::kurbo::Rect::new(min_x, min_y, max_x, max_y)
}

pub(crate) fn circle_arc_path(
    center: vello::kurbo::Point,
    radius: f64,
    start_angle: f64,
    sweep: f64,
) -> vello::kurbo::BezPath {
    let mut path = vello::kurbo::BezPath::new();
    if sweep == 0.0 {
        return path;
    }
    let segments = 64usize;
    let step = sweep / segments as f64;
    let mut angle = start_angle;
    path.move_to(vello::kurbo::Point::new(
        center.x + radius * angle.cos(),
        center.y + radius * angle.sin(),
    ));
    for _ in 0..segments {
        angle += step;
        path.line_to(vello::kurbo::Point::new(
            center.x + radius * angle.cos(),
            center.y + radius * angle.sin(),
        ));
    }
    path
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn logical_text_alignment_follows_environment_direction() {
        assert_eq!(
            parley_alignment(HorizontalAlignment::Leading, false),
            parley::Alignment::Left
        );
        assert_eq!(
            parley_alignment(HorizontalAlignment::Leading, true),
            parley::Alignment::Right
        );
        assert_eq!(
            parley_alignment(HorizontalAlignment::Trailing, true),
            parley::Alignment::Left
        );
    }
}