cranpose-ui 0.1.46

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
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
//! Headless tests for the SwipeToDismiss gesture math and dismiss/restore
//! behavior. The controller state machine is driven with synthetic pointer
//! events; spring animations advance through manual frame-clock pumps.

use super::*;
use crate::collect_modifier_slices;
use crate::scroll::ScrollState;
use cranpose_core::{DefaultScheduler, Runtime};
use cranpose_foundation::{
    BasicModifierNodeContext, ModifierNodeChain, PointerButton, PointerButtons,
};
use cranpose_ui_graphics::Point;

const FRAME_NANOS: u64 = 16_666_667; // ~60 FPS

fn point(x: f32, y: f32) -> Point {
    Point { x, y }
}

fn primary_buttons() -> PointerButtons {
    PointerButtons::new().with(PointerButton::Primary)
}

fn down(x: f32, y: f32) -> PointerEvent {
    PointerEvent::new(PointerEventKind::Down, point(x, y), point(x, y))
        .with_buttons(primary_buttons())
}

fn move_to(x: f32, y: f32) -> PointerEvent {
    PointerEvent::new(PointerEventKind::Move, point(x, y), point(x, y))
        .with_buttons(primary_buttons())
}

fn up(x: f32, y: f32) -> PointerEvent {
    PointerEvent::new(PointerEventKind::Up, point(x, y), point(x, y))
        .with_buttons(primary_buttons())
}

fn cancel() -> PointerEvent {
    PointerEvent::new(PointerEventKind::Cancel, point(0.0, 0.0), point(0.0, 0.0))
}

struct Harness {
    // The collapse watcher requests render/layout invalidation, which needs an
    // active app context; keep one alive for the whole harness.
    _app_context: crate::render_state::TestAppContextScope,
    _runtime: Runtime,
    controller: Rc<SwipeToDismissController>,
    dismiss_count: Rc<Cell<usize>>,
    frame_time: u64,
}

impl Harness {
    fn new(width: f32, threshold_fraction: f32) -> Self {
        let app_context = crate::render_state::app_context_test_scope();
        let runtime = Runtime::new(std::sync::Arc::new(DefaultScheduler));
        let controller = SwipeToDismissController::new(runtime.handle());
        controller.width_px.set(width);
        controller.threshold_fraction.set(threshold_fraction);

        let dismiss_count = Rc::new(Cell::new(0usize));
        let count = Rc::clone(&dismiss_count);
        *controller.on_dismiss.borrow_mut() = Some(Rc::new(move || {
            count.set(count.get() + 1);
        }));

        Self {
            _app_context: app_context,
            _runtime: runtime,
            controller,
            dismiss_count,
            frame_time: 0,
        }
    }

    fn send(&self, event: &PointerEvent) {
        handle_swipe_event(&self.controller, event);
    }

    fn offset(&self) -> f32 {
        self.controller.current_offset()
    }

    /// Non-reactive read of the background-reveal flag driven by the gesture.
    fn revealed(&self) -> bool {
        self.controller.revealed.get_non_reactive()
    }

    /// Current row height scale (`1.0` at rest, `0.0` fully collapsed).
    fn collapse(&self) -> f32 {
        self.controller.collapse_fraction()
    }

    /// The edge the background is revealed on for the current displacement.
    fn side(&self) -> SwipeDismissSide {
        self.controller.revealed_side()
    }

    /// Advances the frame clock, driving springs and the settle watcher.
    fn pump_frames(&mut self, frames: usize) {
        let handle = self._runtime.handle();
        for _ in 0..frames {
            self.frame_time += FRAME_NANOS;
            handle.drain_frame_callbacks(self.frame_time);
        }
    }
}

// ---------------------------------------------------------------------------
// Pure gesture math
// ---------------------------------------------------------------------------

#[test]
fn axis_stays_undecided_within_slop() {
    assert_eq!(decide_axis(0.0, 0.0, 8.0), SwipeAxisDecision::Undecided);
    assert_eq!(decide_axis(8.0, 0.0, 8.0), SwipeAxisDecision::Undecided);
    assert_eq!(decide_axis(-7.9, 5.0, 8.0), SwipeAxisDecision::Undecided);
    assert_eq!(decide_axis(0.0, -8.0, 8.0), SwipeAxisDecision::Undecided);
}

#[test]
fn axis_capture_requires_horizontal_dominance() {
    assert_eq!(decide_axis(9.0, 0.0, 8.0), SwipeAxisDecision::Horizontal);
    assert_eq!(decide_axis(-9.0, 4.0, 8.0), SwipeAxisDecision::Horizontal);
    // Ties go to the swipe (main axis wins on >=, like the scroll modifier).
    assert_eq!(decide_axis(9.0, 9.0, 8.0), SwipeAxisDecision::Horizontal);
}

#[test]
fn axis_locks_out_on_decisive_vertical_travel() {
    assert_eq!(decide_axis(0.0, 9.0, 8.0), SwipeAxisDecision::Vertical);
    assert_eq!(decide_axis(4.0, -12.0, 8.0), SwipeAxisDecision::Vertical);
}

#[test]
fn dismissal_target_uses_threshold_fraction_and_sign() {
    assert_eq!(dismissal_target(99.0, 200.0, 0.5), None);
    assert_eq!(dismissal_target(100.0, 200.0, 0.5), Some(200.0));
    assert_eq!(dismissal_target(-150.0, 200.0, 0.5), Some(-200.0));
    assert_eq!(dismissal_target(30.0, 200.0, 0.1), Some(200.0));
    // Unknown or degenerate widths never dismiss.
    assert_eq!(dismissal_target(500.0, f32::NAN, 0.5), None);
    assert_eq!(dismissal_target(500.0, 0.0, 0.5), None);
}

#[test]
fn clamp_offset_limits_travel_to_one_width() {
    assert_eq!(clamp_offset(250.0, 200.0), 200.0);
    assert_eq!(clamp_offset(-250.0, 200.0), -200.0);
    assert_eq!(clamp_offset(50.0, 200.0), 50.0);
    // Unknown width leaves the offset untouched.
    assert_eq!(clamp_offset(250.0, f32::NAN), 250.0);
}

// ---------------------------------------------------------------------------
// Gesture state machine
// ---------------------------------------------------------------------------

#[test]
fn horizontal_drag_captures_and_follows_the_finger() {
    let harness = Harness::new(200.0, 0.5);

    harness.send(&down(10.0, 10.0));

    // Within the slop: no capture, no consumption.
    let small = move_to(14.0, 10.0);
    harness.send(&small);
    assert!(!small.is_consumed());
    assert_eq!(harness.offset(), 0.0);

    // Crossing the slop horizontally captures and consumes.
    let capture = move_to(30.0, 12.0);
    harness.send(&capture);
    assert!(capture.is_consumed());
    assert_eq!(harness.offset(), 20.0);

    // Subsequent moves track the finger relative to the down position.
    let drag = move_to(140.0, 12.0);
    harness.send(&drag);
    assert!(drag.is_consumed());
    assert_eq!(harness.offset(), 130.0);

    // The offset clamps at one content width.
    let far = move_to(500.0, 12.0);
    harness.send(&far);
    assert_eq!(harness.offset(), 200.0);
}

#[test]
fn vertical_drag_locks_out_and_leaves_events_for_the_parent() {
    let harness = Harness::new(200.0, 0.5);

    harness.send(&down(10.0, 10.0));

    // Decisively vertical: the parent (LazyColumn) must win.
    let vertical = move_to(12.0, 40.0);
    harness.send(&vertical);
    assert!(!vertical.is_consumed());

    // Even large horizontal travel later in the gesture stays unconsumed.
    let late_horizontal = move_to(80.0, 40.0);
    harness.send(&late_horizontal);
    assert!(!late_horizontal.is_consumed());
    assert_eq!(harness.offset(), 0.0);

    let release = up(80.0, 40.0);
    harness.send(&release);
    assert!(!release.is_consumed());
}

#[test]
fn tap_consumes_nothing() {
    let harness = Harness::new(200.0, 0.5);

    let press = down(10.0, 10.0);
    harness.send(&press);
    assert!(!press.is_consumed());

    let release = up(11.0, 10.0);
    harness.send(&release);
    assert!(!release.is_consumed());
    assert_eq!(harness.offset(), 0.0);
}

#[test]
fn release_past_threshold_animates_out_and_fires_on_dismiss_once() {
    let mut harness = Harness::new(200.0, 0.5);

    harness.send(&down(10.0, 10.0));
    harness.send(&move_to(30.0, 10.0));
    harness.send(&move_to(140.0, 10.0)); // offset 130 >= threshold 100

    let release = up(140.0, 10.0);
    harness.send(&release);
    assert!(release.is_consumed());
    assert_eq!(harness.dismiss_count.get(), 0, "fires only after settling");

    harness.pump_frames(300);
    assert_eq!(harness.dismiss_count.get(), 1);
    assert!(
        (harness.offset() - 200.0).abs() <= 0.5,
        "content settles off-screen, got offset {}",
        harness.offset()
    );

    // Further frames never re-fire the callback.
    harness.pump_frames(60);
    assert_eq!(harness.dismiss_count.get(), 1);
}

#[test]
fn release_below_threshold_springs_back_without_dismissing() {
    let mut harness = Harness::new(200.0, 0.5);

    harness.send(&down(10.0, 10.0));
    harness.send(&move_to(30.0, 10.0));
    harness.send(&move_to(70.0, 10.0)); // offset 60 < threshold 100
    harness.send(&up(70.0, 10.0));

    harness.pump_frames(300);
    assert_eq!(harness.dismiss_count.get(), 0);
    assert!(
        harness.offset().abs() <= 0.5,
        "content springs back to rest, got offset {}",
        harness.offset()
    );
}

#[test]
fn leftward_swipe_dismisses_to_the_left() {
    let mut harness = Harness::new(200.0, 0.5);

    harness.send(&down(150.0, 10.0));
    harness.send(&move_to(30.0, 10.0)); // offset -120

    harness.send(&up(30.0, 10.0));
    harness.pump_frames(300);
    assert_eq!(harness.dismiss_count.get(), 1);
    assert!((harness.offset() + 200.0).abs() <= 0.5);
}

#[test]
fn revealed_side_follows_the_swipe_direction() {
    let mut harness = Harness::new(200.0, 0.5);

    // Swipe right: the row moves right, revealing the leading (start) edge.
    harness.send(&down(20.0, 10.0));
    harness.send(&move_to(120.0, 10.0)); // offset +100
    assert!(harness.offset() > 0.0);
    assert_eq!(harness.side(), SwipeDismissSide::Start);
    harness.send(&cancel());
    harness.pump_frames(300);

    // Swipe left: the row moves left, revealing the trailing (end) edge.
    harness.send(&down(180.0, 10.0));
    harness.send(&move_to(60.0, 10.0)); // offset -120
    assert!(harness.offset() < 0.0);
    assert_eq!(harness.side(), SwipeDismissSide::End);
}

#[test]
fn consumed_move_abandons_the_gesture_and_restores() {
    let mut harness = Harness::new(200.0, 0.5);

    harness.send(&down(10.0, 10.0));
    harness.send(&move_to(60.0, 10.0)); // captured, offset 50

    // A foreign handler consumed this sequence; the swipe must let go.
    let foreign = move_to(80.0, 10.0);
    foreign.consume();
    harness.send(&foreign);

    harness.pump_frames(300);
    assert_eq!(harness.dismiss_count.get(), 0);
    assert!(harness.offset().abs() <= 0.5);
}

#[test]
fn background_reveal_tracks_displacement() {
    let mut harness = Harness::new(200.0, 0.5);
    assert!(!harness.revealed(), "background hidden at rest");

    harness.send(&down(10.0, 10.0));
    assert!(
        !harness.revealed(),
        "still hidden before the swipe captures"
    );

    harness.send(&move_to(30.0, 10.0)); // captured, offset 20
    assert!(harness.revealed(), "revealed once the row is displaced");

    // Below threshold: springs back and the reveal turns off at rest so the
    // background cannot flash while the row sits still.
    harness.send(&up(30.0, 10.0));
    harness.pump_frames(300);
    assert!(harness.offset().abs() <= 0.5);
    assert!(
        !harness.revealed(),
        "background hidden again after springing back to rest"
    );
}

#[test]
fn dismiss_hides_background_and_collapses_row() {
    let mut harness = Harness::new(200.0, 0.5);
    assert_eq!(harness.collapse(), 1.0, "row starts at full height");

    harness.send(&down(10.0, 10.0));
    harness.send(&move_to(30.0, 10.0));
    harness.send(&move_to(140.0, 10.0)); // offset 130 >= threshold 100
    assert!(harness.revealed(), "background revealed while displaced");
    harness.send(&up(140.0, 10.0));
    harness.pump_frames(300);

    // The dismiss fired exactly once and the row has fully settled: the
    // background is no longer drawn (nothing to leave a red strip) and the row
    // has collapsed to zero height (no lingering gap) regardless of when the
    // host removes the item.
    assert_eq!(harness.dismiss_count.get(), 1);
    assert!(
        !harness.revealed(),
        "background must be hidden after a dismiss settles, not left as a strip"
    );
    assert!(
        harness.collapse() <= 0.01,
        "row must collapse to zero height after a dismiss, got scale {}",
        harness.collapse()
    );
}

#[test]
fn cancel_springs_back() {
    let mut harness = Harness::new(200.0, 0.5);

    harness.send(&down(10.0, 10.0));
    harness.send(&move_to(90.0, 10.0)); // captured, offset 80
    harness.send(&cancel());

    harness.pump_frames(300);
    assert_eq!(harness.dismiss_count.get(), 0);
    assert!(harness.offset().abs() <= 0.5);
}

// ---------------------------------------------------------------------------
// Nested with a vertical scroll parent (modifier-chain dispatch, mirroring
// the shell's leaf-first hit-path order like the nested scroll tests)
// ---------------------------------------------------------------------------

/// Builds the production pointer handler for a modifier by driving it
/// through a real modifier-node chain (same approach as zoom/scroll tests).
fn pointer_handler_for(modifier: crate::Modifier) -> (Rc<dyn Fn(PointerEvent)>, ModifierNodeChain) {
    let elements = modifier.elements();
    let mut chain = ModifierNodeChain::new();
    let mut context = BasicModifierNodeContext::new();
    chain.update_from_slice(&elements, &mut context);
    let slices = collect_modifier_slices(&chain);
    let handler = slices
        .pointer_inputs()
        .first()
        .cloned()
        .expect("swipe modifier should provide a pointer input handler");
    (handler, chain)
}

/// Dispatches an event leaf-first (swipe child, then scroll parent),
/// mirroring the shell's hit-path dispatch order.
fn dispatch_nested(
    child: &Rc<dyn Fn(PointerEvent)>,
    parent: &Rc<dyn Fn(PointerEvent)>,
    event: PointerEvent,
) -> PointerEvent {
    child(event.clone());
    parent(event.clone());
    event
}

struct NestedHarness {
    harness: Harness,
    swipe: Rc<dyn Fn(PointerEvent)>,
    _swipe_chain: ModifierNodeChain,
    scroll_state: ScrollState,
    scroll: Rc<dyn Fn(PointerEvent)>,
    _scroll_chain: ModifierNodeChain,
    _app_context: crate::render_state::TestAppContextScope,
}

impl NestedHarness {
    /// A swipeable row nested in a vertical scroll container.
    fn new() -> Self {
        let app_context = crate::render_state::app_context_test_scope();
        let harness = Harness::new(200.0, 0.5);

        let (swipe, swipe_chain) = pointer_handler_for(swipe_gesture_modifier(
            crate::Modifier::empty(),
            Rc::clone(&harness.controller),
        ));

        let scroll_state = ScrollState::new(100.0);
        scroll_state.set_max_value(400.0);
        let (scroll, scroll_chain) = pointer_handler_for(
            crate::Modifier::empty().vertical_scroll(scroll_state.clone(), false),
        );

        Self {
            harness,
            swipe,
            _swipe_chain: swipe_chain,
            scroll_state,
            scroll,
            _scroll_chain: scroll_chain,
            _app_context: app_context,
        }
    }

    fn send(&self, event: PointerEvent) -> PointerEvent {
        dispatch_nested(&self.swipe, &self.scroll, event)
    }
}

#[test]
fn vertical_drag_on_row_scrolls_parent_list() {
    let nested = NestedHarness::new();

    nested.send(down(100.0, 100.0));
    // Decisively vertical: the swipe locks itself out, the parent scrolls.
    nested.send(move_to(102.0, 60.0));
    nested.send(move_to(104.0, 20.0));
    nested.send(up(104.0, 20.0));

    assert_eq!(nested.harness.offset(), 0.0, "row must not move sideways");
    assert_eq!(nested.harness.dismiss_count.get(), 0);
    assert!(
        (nested.scroll_state.value_non_reactive() - 180.0).abs() < 1.0,
        "vertical drag must scroll the parent list (finger up 80 => offset +80), got {}",
        nested.scroll_state.value_non_reactive()
    );
}

#[test]
fn horizontal_swipe_on_row_does_not_scroll_parent_list() {
    let mut nested = NestedHarness::new();

    nested.send(down(20.0, 100.0));
    // Mostly horizontal with vertical jitter: the swipe captures, so the
    // consumed moves never scroll the parent.
    let capture = nested.send(move_to(40.0, 104.0));
    assert!(capture.is_consumed(), "swipe must capture horizontal drags");
    nested.send(move_to(160.0, 98.0));
    let release = nested.send(up(160.0, 98.0));
    assert!(release.is_consumed());

    assert_eq!(
        nested.scroll_state.value_non_reactive(),
        100.0,
        "parent list must not scroll during a captured swipe"
    );

    nested.harness.pump_frames(300);
    assert_eq!(
        nested.harness.dismiss_count.get(),
        1,
        "the released swipe crossed the threshold and must dismiss"
    );
}

#[test]
fn pressing_mid_flight_pins_the_content() {
    let mut harness = Harness::new(200.0, 0.5);

    // Swipe below threshold and release: spring-back starts.
    harness.send(&down(10.0, 10.0));
    harness.send(&move_to(90.0, 10.0));
    harness.send(&up(90.0, 10.0));
    harness.pump_frames(3);
    let mid_flight = harness.offset();
    assert!(mid_flight > 0.0 && mid_flight < 80.0);

    // Pressing again stops the animation where it is.
    harness.send(&down(50.0, 10.0));
    harness.pump_frames(5);
    assert_eq!(harness.offset(), mid_flight);

    // And the new drag continues from the pinned offset.
    let drag = move_to(70.0, 10.0);
    harness.send(&drag);
    assert!(drag.is_consumed());
    assert_eq!(harness.offset(), mid_flight + 20.0);
}