cranpose-ui 0.1.105

UI primitives for Cranpose
Documentation
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
//! Rotary input modifiers (Wear OS crown / rotating bezel).
//!
//! Mirrors Jetpack Compose for Wear OS's `Modifier.onRotaryScrollEvent` and
//! `Modifier.onPreRotaryScrollEvent`. Returning `true` from a handler consumes
//! the event and stops propagation, exactly as in Compose.
//!
//! ## Dispatch order
//!
//! Rotary events run two passes over the target node's modifier chain, matching
//! `RotaryInputModifierNode`'s documented contract:
//!
//! 1. **Capture (pre)** — root to focused node, invoking
//!    [`Modifier::on_pre_rotary_scroll_event`] handlers. An ancestor can
//!    intercept the event before the focused node sees it.
//! 2. **Bubble** — focused node to root, invoking
//!    [`Modifier::on_rotary_scroll_event`] handlers.
//!
//! The first handler that returns `true` ends both passes.

use std::{
    cell::Cell,
    fmt,
    hash::{Hash, Hasher},
    rc::Rc,
};

use cranpose_foundation::{
    DelegatableNode, ModifierNode, ModifierNodeElement, NodeCapabilities, NodeState, PointerEvent,
    PointerEventKind, PointerInputNode, RotaryScrollEvent, impl_pointer_input_node,
};

use super::{Modifier, inspector_metadata};

/// Which dispatch pass a rotary handler listens on.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
enum RotaryPass {
    /// Capture pass, root to focused node (`onPreRotaryScrollEvent`).
    Pre,
    /// Bubble pass, focused node to root (`onRotaryScrollEvent`).
    Bubble,
}

impl RotaryPass {
    fn matches(self, kind: PointerEventKind) -> bool {
        matches!(
            (self, kind),
            (RotaryPass::Pre, PointerEventKind::RotaryScrollPre)
                | (RotaryPass::Bubble, PointerEventKind::RotaryScroll)
        )
    }
}

type RotaryHandler = Rc<dyn Fn(RotaryScrollEvent) -> bool>;

impl Modifier {
    /// Handles rotary scroll events (Pixel Watch crown, Galaxy Watch rotating
    /// bezel) during the **bubble** pass.
    ///
    /// The handler receives a [`RotaryScrollEvent`] whose scroll amounts are
    /// already in pixels. Return `true` to consume the event and stop it
    /// propagating to ancestors; return `false` to let it keep bubbling.
    ///
    /// Equivalent to Compose's `Modifier.onRotaryScrollEvent`.
    ///
    /// ```ignore
    /// Modifier::new().on_rotary_scroll_event(move |event| {
    ///     offset.set(offset.get() + event.vertical_scroll_pixels);
    ///     true
    /// })
    /// ```
    pub fn on_rotary_scroll_event<F>(self, handler: F) -> Self
    where
        F: Fn(RotaryScrollEvent) -> bool + 'static,
    {
        self.rotary_element(RotaryPass::Bubble, Rc::new(handler), "onRotaryScrollEvent")
    }

    /// Handles rotary scroll events during the **capture** pass, before the
    /// focused node sees them.
    ///
    /// Return `true` to consume the event and stop it reaching descendants.
    ///
    /// Equivalent to Compose's `Modifier.onPreRotaryScrollEvent`.
    pub fn on_pre_rotary_scroll_event<F>(self, handler: F) -> Self
    where
        F: Fn(RotaryScrollEvent) -> bool + 'static,
    {
        self.rotary_element(RotaryPass::Pre, Rc::new(handler), "onPreRotaryScrollEvent")
    }

    fn rotary_element(
        self,
        pass: RotaryPass,
        handler: RotaryHandler,
        inspector_name: &'static str,
    ) -> Self {
        let element = RotaryInputElement::new(pass, handler);
        let handler_id = element.handler_id;
        self.then(
            Self::with_element(element).with_inspector_metadata(inspector_metadata(
                inspector_name,
                move |info| {
                    info.add_property("handlerId", handler_id.to_string());
                },
            )),
        )
    }
}

#[derive(Clone)]
struct RotaryInputElement {
    pass: RotaryPass,
    handler: RotaryHandler,
    handler_id: u64,
}

impl RotaryInputElement {
    fn new(pass: RotaryPass, handler: RotaryHandler) -> Self {
        let handler_id = Rc::as_ptr(&handler) as *const () as usize as u64;
        Self {
            pass,
            handler,
            handler_id,
        }
    }
}

impl fmt::Debug for RotaryInputElement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RotaryInputElement")
            .field("pass", &self.pass)
            .field("handler_id", &self.handler_id)
            .finish()
    }
}

impl PartialEq for RotaryInputElement {
    fn eq(&self, other: &Self) -> bool {
        // Compare only the pass, never the closure identity: handlers are
        // recreated on every recomposition, and comparing them would drop and
        // rebuild the node each frame. Matches `PointerInputElement`.
        self.pass == other.pass
    }
}

impl Eq for RotaryInputElement {}

impl Hash for RotaryInputElement {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.pass.hash(state);
    }
}

impl ModifierNodeElement for RotaryInputElement {
    type Node = RotaryInputModifierNode;

    fn create(&self) -> Self::Node {
        RotaryInputModifierNode::new(self.pass, self.handler.clone())
    }

    fn update(&self, node: &mut Self::Node) {
        // Always refresh the closure so the node calls the latest captured
        // state, without restarting anything.
        node.handler.set(Some(self.handler.clone()));
    }

    fn always_update(&self) -> bool {
        true
    }

    fn capabilities(&self) -> NodeCapabilities {
        NodeCapabilities::POINTER_INPUT
    }
}

/// Modifier node that forwards rotary scroll events to an app handler.
///
/// Named after Compose's `RotaryInputModifierNode`. It rides the existing
/// pointer dispatch path: the shell sends rotary passes as
/// [`PointerEventKind::RotaryScrollPre`]/[`PointerEventKind::RotaryScroll`]
/// pointer events, and this node filters for its own pass.
pub struct RotaryInputModifierNode {
    handler: Rc<Cell<Option<RotaryHandler>>>,
    dispatch: Rc<dyn Fn(PointerEvent)>,
    state: NodeState,
}

impl RotaryInputModifierNode {
    fn new(pass: RotaryPass, handler: RotaryHandler) -> Self {
        let handler_cell: Rc<Cell<Option<RotaryHandler>>> = Rc::new(Cell::new(Some(handler)));
        let handler_for_dispatch = Rc::clone(&handler_cell);
        // One closure allocated per node at construction time, not per event.
        let dispatch = Rc::new(move |event: PointerEvent| {
            if !pass.matches(event.kind) || event.is_consumed() {
                return;
            }
            let Some(rotary) = event.rotary_scroll_event() else {
                return;
            };
            // Take-and-restore keeps the handler behind a `Cell` (no RefCell
            // borrow can be held across the call, so a handler is free to
            // rebuild the composition).
            let Some(handler) = handler_for_dispatch.take() else {
                return;
            };
            let consumed = handler(rotary);
            if handler_for_dispatch.take().is_none() {
                // Nothing replaced it while we were running: put ours back.
                handler_for_dispatch.set(Some(handler));
            }
            if consumed {
                event.consume();
            }
        });

        Self {
            handler: handler_cell,
            dispatch,
            state: NodeState::new(),
        }
    }
}

impl ModifierNode for RotaryInputModifierNode {
    impl_pointer_input_node!();
}

impl DelegatableNode for RotaryInputModifierNode {
    fn node_state(&self) -> &NodeState {
        &self.state
    }
}

impl PointerInputNode for RotaryInputModifierNode {
    fn pointer_input_handler(&self) -> Option<Rc<dyn Fn(PointerEvent)>> {
        Some(Rc::clone(&self.dispatch))
    }
}

#[cfg(test)]
mod tests {
    use std::cell::RefCell;

    use cranpose_ui_graphics::Point;

    use super::*;

    fn rotary_event(kind: PointerEventKind, vertical: f32) -> PointerEvent {
        PointerEvent::rotary(
            kind,
            RotaryScrollEvent::new(vertical, 0.0, 42),
            Point { x: 0.0, y: 0.0 },
        )
    }

    fn node(pass: RotaryPass, handler: RotaryHandler) -> RotaryInputModifierNode {
        RotaryInputModifierNode::new(pass, handler)
    }

    #[test]
    fn bubble_node_receives_bubble_events_only() {
        let seen = Rc::new(RefCell::new(Vec::new()));
        let sink = Rc::clone(&seen);
        let node = node(
            RotaryPass::Bubble,
            Rc::new(move |event: RotaryScrollEvent| {
                sink.borrow_mut().push(event.vertical_scroll_pixels);
                false
            }),
        );
        let dispatch = node.pointer_input_handler().expect("handler");

        dispatch(rotary_event(PointerEventKind::RotaryScrollPre, -1.0));
        dispatch(rotary_event(PointerEventKind::RotaryScroll, -2.0));
        dispatch(PointerEvent::new(
            PointerEventKind::Scroll,
            Point { x: 0.0, y: 0.0 },
            Point { x: 0.0, y: 0.0 },
        ));

        assert_eq!(*seen.borrow(), vec![-2.0]);
    }

    #[test]
    fn pre_node_receives_capture_events_only() {
        let seen = Rc::new(RefCell::new(Vec::new()));
        let sink = Rc::clone(&seen);
        let node = node(
            RotaryPass::Pre,
            Rc::new(move |event: RotaryScrollEvent| {
                sink.borrow_mut().push(event.vertical_scroll_pixels);
                false
            }),
        );
        let dispatch = node.pointer_input_handler().expect("handler");

        dispatch(rotary_event(PointerEventKind::RotaryScrollPre, -1.0));
        dispatch(rotary_event(PointerEventKind::RotaryScroll, -2.0));

        assert_eq!(*seen.borrow(), vec![-1.0]);
    }

    #[test]
    fn returning_true_consumes_the_event() {
        let node = node(RotaryPass::Bubble, Rc::new(|_| true));
        let dispatch = node.pointer_input_handler().expect("handler");

        let event = rotary_event(PointerEventKind::RotaryScroll, -8.0);
        dispatch(event.clone());

        assert!(event.is_consumed());
    }

    #[test]
    fn returning_false_leaves_the_event_unconsumed() {
        let node = node(RotaryPass::Bubble, Rc::new(|_| false));
        let dispatch = node.pointer_input_handler().expect("handler");

        let event = rotary_event(PointerEventKind::RotaryScroll, -8.0);
        dispatch(event.clone());

        assert!(!event.is_consumed());
    }

    #[test]
    fn an_already_consumed_event_never_reaches_the_handler() {
        // This is what stops propagation: once an inner node consumed the
        // event, outer nodes in the same pass must not run.
        let calls = Rc::new(Cell::new(0));
        let counter = Rc::clone(&calls);
        let node = node(
            RotaryPass::Bubble,
            Rc::new(move |_| {
                counter.set(counter.get() + 1);
                false
            }),
        );
        let dispatch = node.pointer_input_handler().expect("handler");

        let event = rotary_event(PointerEventKind::RotaryScroll, -8.0);
        event.consume();
        dispatch(event);

        assert_eq!(calls.get(), 0);
    }

    #[test]
    fn handler_sees_the_full_rotary_payload() {
        let captured = Rc::new(Cell::new(None));
        let sink = Rc::clone(&captured);
        let node = node(
            RotaryPass::Bubble,
            Rc::new(move |event: RotaryScrollEvent| {
                sink.set(Some(event));
                true
            }),
        );
        let dispatch = node.pointer_input_handler().expect("handler");

        dispatch(PointerEvent::rotary(
            PointerEventKind::RotaryScroll,
            RotaryScrollEvent::new(-64.0, 32.0, 777),
            Point { x: 0.0, y: 0.0 },
        ));

        assert_eq!(
            captured.get(),
            Some(RotaryScrollEvent::new(-64.0, 32.0, 777))
        );
    }

    #[test]
    fn element_reuses_the_node_across_recomposition() {
        // Equal elements (same pass) must not churn the node, but the closure
        // must still be refreshed so it observes the latest state.
        let first = RotaryInputElement::new(RotaryPass::Bubble, Rc::new(|_| false));
        let second = RotaryInputElement::new(RotaryPass::Bubble, Rc::new(|_| true));
        assert_eq!(first, second);
        assert!(first.always_update());

        let mut node = first.create();
        second.update(&mut node);

        let event = rotary_event(PointerEventKind::RotaryScroll, -1.0);
        node.pointer_input_handler().expect("handler")(event.clone());

        assert!(event.is_consumed(), "updated handler should be in effect");
    }

    #[test]
    fn pre_and_bubble_elements_are_distinct() {
        let pre = RotaryInputElement::new(RotaryPass::Pre, Rc::new(|_| false));
        let bubble = RotaryInputElement::new(RotaryPass::Bubble, Rc::new(|_| false));

        assert_ne!(pre, bubble);
    }

    #[test]
    fn modifier_builders_add_pointer_input_elements() {
        let modifier = Modifier::empty()
            .on_pre_rotary_scroll_event(|_| false)
            .on_rotary_scroll_event(|_| true);

        assert_eq!(modifier.elements().len(), 2);
    }
}