hydrolysis 0.3.0

GPU-required self-drawn backend for WaterUI
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
#[cfg(feature = "accessibility")]
use crate::renderer::AccessibilityActionTarget;
use crate::renderer::{
    HydroNativeView, HydroState, InteractionKey, RenderContext, WidgetRenderContext,
    measure_label_intrinsic, transformed_rect,
};
#[cfg(feature = "accessibility")]
use accesskit::{
    Action as AccessibilityAction, Node as AccessibilityNode, Role as AccessibilityNodeRole,
    Toggled as AccessibilityToggled,
};
use std::cell::RefCell;
use std::rc::Rc;
use waterui_controls::toggle::{ToggleConfig, ToggleStyle};
use waterui_core::layout::Size as LayoutSize;
use waterui_core::layout::{ProposalSize, ViewDimensions};
use waterui_core::{AnyView, Environment, Native};

use crate::renderer::RetainedSubview;
use crate::renderer::local_interaction_state;
use crate::widgets::util::widget_disabled;

/// The retained render state of a toggle: the cloneable [`ToggleConfig`] drives the
/// control + accessibility, and its main label is held as a [`RetainedSubview`]
/// built once and re-flushed each frame so reactive label content stays live.
pub(crate) struct ToggleRenderState {
    config: ToggleConfig,
    label_view: RetainedSubview,
}

impl ToggleRenderState {
    pub(crate) fn from_config(config: ToggleConfig) -> Self {
        Self {
            label_view: RetainedSubview::new(AnyView::new(config.label.clone())),
            config,
        }
    }

    /// Eagerly build the label sub-view (the measure path has only
    /// `&mut HydroState`, no renderer, so it must be built before then).
    pub(crate) fn prebuild(
        &mut self,
        renderer: &mut crate::renderer::SemanticCore,
        env: &Environment,
    ) {
        self.label_view.ensure_built(renderer, env);
    }
}

impl HydroNativeView for Native<ToggleConfig> {
    fn intrinsic(
        state: &mut crate::renderer::HydroState,
        view: &Self,
        env: &Environment,
        theme: &Rc<dyn crate::engine::WidgetTheme>,
    ) -> LayoutSize {
        measure_toggle_intrinsic(view.as_inner(), state, env, theme)
    }
}

/// Emits a toggle's accessibility node from its config. Shared by the rendered
/// `Widget`-node flush (which passes its [`RenderContext`]) and the semantic
/// emission walk (which passes `None` — a semantic node carries no bounds).
pub(crate) fn toggle_accessibility(
    renderer: &mut crate::renderer::SemanticCore,
    ctx: Option<RenderContext>,
    toggle: &ToggleConfig,
    env: &Environment,
    focus_keys: &[crate::renderer::InteractionKey],
) {
    #[cfg(feature = "accessibility")]
    {
        let mut node = AccessibilityNode::new(renderer.resolve_accessibility_role(
            env,
            match toggle.style {
                ToggleStyle::Automatic | ToggleStyle::Switch => AccessibilityNodeRole::Switch,
                ToggleStyle::Checkbox => AccessibilityNodeRole::CheckBox,
                _ => panic!("hydrolysis ToggleStyle variant is not implemented"),
            },
        ));
        let default_label = renderer.accessibility_label_from_label(&toggle.label, env);
        let label = renderer.resolve_accessibility_label(env, default_label);
        if let Some(label) = label {
            node.set_label(label);
        }
        let checked = renderer.read_signal(&toggle.toggle);
        node.set_toggled(AccessibilityToggled::from(checked));
        node.add_action(AccessibilityAction::Focus);
        // A disabled toggle stays in the tree (focusable, announced as
        // disabled) but exposes no click action and no action target.
        let disabled = renderer.read_signal(&widget_disabled(env));
        let action_target = if disabled {
            node.set_disabled();
            None
        } else {
            node.add_action(AccessibilityAction::Click);
            Some(AccessibilityActionTarget::Toggle {
                binding: toggle.toggle.clone(),
            })
        };
        if let Some(node_id) = renderer.register_accessibility_leaf(ctx, node, env, action_target) {
            for key in focus_keys {
                renderer.register_accessibility_focus_link(key, node_id);
            }
        }
    }
    #[cfg(not(feature = "accessibility"))]
    {
        let _ = (renderer, ctx, toggle, env, focus_keys);
    }
}

/// Measures a retained toggle leaf from its [`ToggleRenderState`], reading the
/// label size from its already-built [`RetainedSubview`] so layout and render agree.
pub(crate) fn measure_toggle_node(
    render_state: &ToggleRenderState,
    _proposal: ProposalSize,
    state: &mut HydroState,
    env: &Environment,
    theme: &Rc<dyn crate::engine::WidgetTheme>,
) -> ViewDimensions {
    let metrics = theme.toggle_metrics(render_state.config.style);
    let label_size = render_state.label_view.measure_built(state, env, theme);
    let label_width = f64::from(label_size.width);
    let width = if label_width > 0.0 {
        label_width + metrics.label_spacing + metrics.width
    } else {
        metrics.width
    };
    let height = f64::from(label_size.height).max(metrics.height);
    ViewDimensions::new(LayoutSize::new(width as f32, height as f32))
}

/// Renders a retained toggle leaf every flush: emits a11y (unless hidden) then the
/// control + label + tap target, reading the config's live signals each frame.
pub(crate) fn render_toggle_node(
    ctx: &mut WidgetRenderContext<'_>,
    state: &Rc<RefCell<ToggleRenderState>>,
    env: &Environment,
) {
    let hidden = env
        .get::<waterui::accessibility::AccessibilityHidden>()
        .is_some_and(waterui::accessibility::AccessibilityHidden::is_hidden);
    if !hidden {
        let render_ctx = ctx.render_context();
        toggle_accessibility(
            ctx.renderer_mut(),
            Some(render_ctx),
            &state.borrow().config,
            env,
            &[crate::renderer::InteractionKey::for_rc(state, 0)],
        );
    }
    render_toggle_parts(ctx, state, env);
}

pub(crate) fn render_toggle_parts(
    ctx: &mut WidgetRenderContext<'_>,
    state: &Rc<RefCell<ToggleRenderState>>,
    env: &Environment,
) {
    let visual_interaction_key = InteractionKey::for_rc(state, 0);
    let activation_interaction_key = InteractionKey::for_rc(state, 1);
    let theme = ctx.theme();
    let mut state = state.borrow_mut();
    let style = state.config.style;
    let metrics = theme.toggle_metrics(style);
    // Reading the disabled signal watches it, so a change schedules a frame
    // and this persistent node re-renders (and re-registers input) with the
    // new state.
    let disabled = {
        let signal = widget_disabled(env);
        ctx.renderer_mut().read_signal(&signal)
    };
    // The label is a retained node sub-view re-flushed at its rect; reactive
    // content stays live through the node's own per-frame re-flush, with no dispatch.
    let label_size = state.label_view.measure_intrinsic(ctx.renderer_mut(), env);
    let (control_bounds, label_bounds) =
        toggle_control_and_label_bounds(ctx.bounds, style, metrics, label_size);
    if label_bounds.width() > 0.0 {
        // A disabled control dims its label to the theme's disabled-content
        // alpha (Material: on-surface at 38% for default-colored labels).
        if disabled {
            ctx.push_layer_rect(theme.disabled_content_alpha(), label_bounds);
        }
        // The label's semantics are merged into the toggle's own node by
        // `toggle_accessibility`, so the sub-view flushes visual-only.
        let render_ctx = ctx.render_context();
        let label_view = &mut state.label_view;
        ctx.renderer_mut()
            .with_suppressed_accessibility(|renderer| {
                label_view.flush_in_rect(
                    renderer,
                    render_ctx,
                    env,
                    ProposalSize::UNSPECIFIED,
                    label_bounds,
                );
            });
        if disabled {
            ctx.pop_layer();
        }
    }

    // Reading the toggle value through `resolve_toggle_progress` watches the
    // signal (it registers a retained-refresh watcher), so a value change
    // schedules a frame and this persistent node re-renders the new state.
    let (thumb_progress, selected) = {
        let binding = state.config.toggle.clone();
        ctx.renderer_mut()
            .resolve_toggle_progress(&binding, theme.toggle_value_animation())
    };
    let visual_hit_bounds = transformed_rect(ctx.hit_transform, control_bounds);
    let activation_hit_bounds = transformed_rect(ctx.hit_transform, ctx.bounds);
    let (interaction, press_slot, _) = ctx.renderer_mut().bind_control_interaction_target(
        visual_interaction_key.clone(),
        visual_hit_bounds,
        env,
        disabled,
    );
    let interaction = local_interaction_state(interaction, ctx.hit_transform);
    {
        let mut draw = ctx.draw_context();
        match style {
            ToggleStyle::Automatic | ToggleStyle::Switch => {
                theme.draw_toggle_switch(
                    &mut draw,
                    control_bounds,
                    thumb_progress,
                    selected,
                    interaction,
                );
                theme.draw_toggle_switch_state_layer(
                    &mut draw,
                    control_bounds,
                    thumb_progress,
                    selected,
                    interaction,
                );
            }
            ToggleStyle::Checkbox => {
                theme.draw_toggle_checkbox(&mut draw, control_bounds, thumb_progress, interaction);
                theme.draw_toggle_checkbox_state_layer(
                    &mut draw,
                    control_bounds,
                    thumb_progress,
                    interaction,
                );
            }
            _ => panic!("hydrolysis ToggleStyle variant is not implemented"),
        }
    }
    // A disabled toggle registers no tap target: the pointer neither presses
    // nor toggles it. Targets are re-registered every flush, so re-enabling
    // restores interactivity on the next frame.
    if disabled {
        return;
    }
    // Keep the label row as an activation target without attaching it to the
    // switch's visual interaction. An associated the Material label toggles the switch
    // but does not originate a switch ripple or pressed thumb from the label's
    // distant pointer coordinate.
    let binding = state.config.toggle.clone();
    if label_bounds.width() > 0.0 {
        let mut activation_press_slot = press_slot.clone();
        activation_press_slot.key = activation_interaction_key;
        ctx.renderer_mut()
            .register_interactive_pointer_target_with_keyboard(
                activation_hit_bounds,
                activation_press_slot,
                false,
                toggle_binding_action(binding.clone(), visual_interaction_key.clone()),
            );
    }
    // Register the visual control last so it wins hit testing where the full-row
    // activation target overlaps it.
    ctx.renderer_mut().register_interactive_pointer_target(
        visual_hit_bounds,
        press_slot,
        toggle_binding_action(binding, visual_interaction_key),
    );
}

fn toggle_binding_action(
    binding: nami::Binding<bool>,
    visual_interaction_key: InteractionKey,
) -> impl FnMut(&mut crate::renderer::SemanticCore, vello::kurbo::Point, &Environment) -> bool {
    move |renderer, _point, _env| {
        // A pointer press on the non-focusable label target temporarily owns an
        // interaction-only key. Restore semantic keyboard focus to the switch
        // itself when the label activates it.
        renderer.set_keyboard_focus(Some(visual_interaction_key.clone()), false);
        binding.set(!binding.get());
        true
    }
}

pub(crate) fn measure_toggle_intrinsic(
    toggle: &ToggleConfig,
    state: &mut HydroState,
    env: &Environment,
    theme: &Rc<dyn crate::engine::WidgetTheme>,
) -> LayoutSize {
    let metrics = theme.toggle_metrics(toggle.style);
    let label_size = measure_label_intrinsic(&toggle.label, state, env, theme);
    let label_width = f64::from(label_size.width);
    let width = if label_width > 0.0 {
        label_width + metrics.label_spacing + metrics.width
    } else {
        metrics.width
    };
    let height = f64::from(label_size.height).max(metrics.height);
    LayoutSize::new(width as f32, height as f32)
}

fn toggle_control_and_label_bounds(
    bounds: vello::kurbo::Rect,
    style: ToggleStyle,
    metrics: waterui_backend_core::widget::ToggleMetrics,
    label_size: LayoutSize,
) -> (vello::kurbo::Rect, vello::kurbo::Rect) {
    let control_y0 = bounds.y0 + ((bounds.height() - metrics.height) / 2.0).max(0.0);
    let control_y1 = control_y0 + metrics.height;
    let has_label = label_size.width > 0.0 || label_size.height > 0.0;
    match style {
        ToggleStyle::Checkbox => {
            let control_x0 = bounds.x0;
            let control_x1 = control_x0 + metrics.width;
            let label_x0 = if has_label {
                (control_x1 + metrics.label_spacing).min(bounds.x1)
            } else {
                control_x1
            };
            let max_label_width = (bounds.x1 - label_x0).max(0.0);
            let label_width = f64::from(label_size.width).min(max_label_width);
            let label_height = f64::from(label_size.height).min(bounds.height());
            let label_y0 = bounds.y0 + (bounds.height() - label_height) * 0.5;
            (
                vello::kurbo::Rect::new(control_x0, control_y0, control_x1, control_y1),
                vello::kurbo::Rect::new(
                    label_x0,
                    label_y0,
                    label_x0 + label_width,
                    label_y0 + label_height,
                ),
            )
        }
        ToggleStyle::Automatic | ToggleStyle::Switch => {
            let control_x0 = (bounds.x1 - metrics.width).max(bounds.x0);
            let label_x1 = if has_label {
                (control_x0 - metrics.label_spacing).max(bounds.x0)
            } else {
                bounds.x0
            };
            (
                vello::kurbo::Rect::new(
                    control_x0,
                    control_y0,
                    control_x0 + metrics.width,
                    control_y1,
                ),
                vello::kurbo::Rect::new(bounds.x0, bounds.y0, label_x1, bounds.y1),
            )
        }
        _ => panic!("hydrolysis ToggleStyle variant is not implemented"),
    }
}

/// Emits a retained toggle's accessibility node for the semantic walk — the
/// same node `toggle_accessibility` registers, with no bounds. The label
/// sub-view flushes visual-only, so there is nothing else to emit.
#[cfg(feature = "accessibility")]
pub(crate) fn emit_toggle_accessibility(
    renderer: &mut crate::renderer::SemanticCore,
    state: &Rc<RefCell<ToggleRenderState>>,
    env: &Environment,
) {
    toggle_accessibility(
        renderer,
        None,
        &state.borrow().config,
        env,
        &[crate::renderer::InteractionKey::for_rc(state, 0)],
    );
}

#[cfg(test)]
mod tests {
    use super::toggle_control_and_label_bounds;
    use vello::kurbo::Rect;
    use waterui_backend_core::widget::ToggleMetrics;
    use waterui_controls::toggle::ToggleStyle;
    use waterui_core::layout::Size;

    #[test]
    fn checkbox_layout_places_control_before_label() {
        let metrics = ToggleMetrics::new(18.0, 18.0, 8.0);
        let (control, label) = toggle_control_and_label_bounds(
            Rect::new(16.0, 20.0, 320.0, 60.0),
            ToggleStyle::Checkbox,
            metrics,
            Size::new(64.0, 16.0),
        );

        assert_eq!(control, Rect::new(16.0, 31.0, 34.0, 49.0));
        assert_eq!(label, Rect::new(42.0, 32.0, 106.0, 48.0));
    }

    #[test]
    fn switch_layout_keeps_control_trailing() {
        let metrics = ToggleMetrics::new(52.0, 32.0, 8.0);
        let (control, label) = toggle_control_and_label_bounds(
            Rect::new(16.0, 20.0, 320.0, 60.0),
            ToggleStyle::Switch,
            metrics,
            Size::new(64.0, 16.0),
        );

        assert_eq!(control, Rect::new(268.0, 24.0, 320.0, 56.0));
        assert_eq!(label, Rect::new(16.0, 20.0, 260.0, 60.0));
    }
}