cranpose-ui 0.1.113

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
use std::{rc::Rc, sync::Arc};

use cranpose_core::{DefaultScheduler, Runtime};
use cranpose_foundation::{
    BasicModifierNodeContext, ModifierNodeChain, PointerButton, PointerButtons, PointerEvent,
    PointerEventKind,
};
use cranpose_ui_graphics::Point;

use crate::{Modifier, collect_modifier_slices, zoom::ZoomState};

fn with_test_runtime<T>(f: impl FnOnce() -> T) -> T {
    let _runtime = Runtime::new(Arc::new(DefaultScheduler));
    f()
}

fn pointer_handler_for(modifier: 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("zoomable modifier should provide pointer input handler");
    (handler, chain)
}

fn touch(kind: PointerEventKind, id: u64, x: f32, y: f32) -> PointerEvent {
    PointerEvent::new(kind, Point { x, y }, Point { x, y })
        .with_buttons(PointerButtons::new().with(PointerButton::Primary))
        .with_id(id)
}

#[test]
fn apply_transform_zooms_about_the_centroid() {
    let _app_context = crate::render_state::app_context_test_scope();
    with_test_runtime(|| {
        let state = ZoomState::with_scale_range(0.5, 8.0);

        state.apply_transform(Point { x: 100.0, y: 50.0 }, Point { x: 0.0, y: 0.0 }, 2.0);

        assert_eq!(state.scale_non_reactive(), 2.0);
        let offset = state.offset_non_reactive();
        assert!(
            (100.0 * 2.0 + offset.x - 100.0).abs() < 1e-4
                && (50.0 * 2.0 + offset.y - 50.0).abs() < 1e-4,
            "focal point must stay fixed, offset={offset:?}"
        );
    });
}

#[test]
fn apply_transform_focal_point_stays_fixed_across_steps() {
    let _app_context = crate::render_state::app_context_test_scope();
    with_test_runtime(|| {
        let state = ZoomState::with_scale_range(0.5, 8.0);
        let focal = Point { x: 40.0, y: 200.0 };

        let content = focal;
        for zoom_step in [1.3f32, 1.3, 0.9, 1.1] {
            state.apply_transform(focal, Point { x: 0.0, y: 0.0 }, zoom_step);
            let scale = state.scale_non_reactive();
            let offset = state.offset_non_reactive();
            let display = Point {
                x: content.x * scale + offset.x,
                y: content.y * scale + offset.y,
            };
            assert!(
                (display.x - focal.x).abs() < 1e-3 && (display.y - focal.y).abs() < 1e-3,
                "focal content point drifted to {display:?} at scale {scale}"
            );
        }
    });
}

#[test]
fn apply_transform_clamps_scale_to_range() {
    let _app_context = crate::render_state::app_context_test_scope();
    with_test_runtime(|| {
        let state = ZoomState::with_scale_range(1.0, 4.0);

        state.apply_transform(Point { x: 0.0, y: 0.0 }, Point { x: 0.0, y: 0.0 }, 100.0);
        assert_eq!(state.scale_non_reactive(), 4.0);

        state.apply_transform(Point { x: 0.0, y: 0.0 }, Point { x: 0.0, y: 0.0 }, 0.0001);
        assert_eq!(state.scale_non_reactive(), 1.0);
    });
}

#[test]
fn apply_transform_pans_by_the_gesture_delta() {
    let _app_context = crate::render_state::app_context_test_scope();
    with_test_runtime(|| {
        let state = ZoomState::with_scale_range(0.5, 8.0);
        state.set_scale(2.0);

        state.apply_transform(Point { x: 0.0, y: 0.0 }, Point { x: 15.0, y: -7.0 }, 1.0);

        assert_eq!(state.offset_non_reactive(), Point { x: 15.0, y: -7.0 });
        assert_eq!(state.scale_non_reactive(), 2.0);
    });
}

#[test]
fn reset_restores_identity() {
    let _app_context = crate::render_state::app_context_test_scope();
    with_test_runtime(|| {
        let state = ZoomState::new();
        state.apply_transform(Point { x: 10.0, y: 10.0 }, Point { x: 5.0, y: 5.0 }, 3.0);
        assert!(state.is_transformed());

        state.reset();
        assert!(!state.is_transformed());
        assert_eq!(state.scale_non_reactive(), 1.0);
        assert_eq!(state.offset_non_reactive(), Point { x: 0.0, y: 0.0 });
    });
}

#[test]
fn layer_reflects_scale_offset_with_top_left_origin() {
    let _app_context = crate::render_state::app_context_test_scope();
    with_test_runtime(|| {
        let state = ZoomState::new();
        state.set_scale(2.5);
        state.set_offset(Point { x: -30.0, y: 12.0 });

        let layer = state.layer();
        assert_eq!(layer.scale_x, 2.5);
        assert_eq!(layer.scale_y, 2.5);
        assert_eq!(layer.translation_x, -30.0);
        assert_eq!(layer.translation_y, 12.0);
        assert_eq!(layer.transform_origin.pivot_fraction_x, 0.0);
        assert_eq!(layer.transform_origin.pivot_fraction_y, 0.0);
    });
}

#[test]
fn two_finger_pinch_out_scales_up_and_consumes_moves() {
    let _app_context = crate::render_state::app_context_test_scope();
    with_test_runtime(|| {
        let state = ZoomState::new();
        let (handler, _chain) = pointer_handler_for(Modifier::empty().zoomable(state));

        handler(touch(PointerEventKind::Down, 0, 100.0, 100.0));
        handler(touch(PointerEventKind::Down, 1, 200.0, 100.0));

        let pinch_move = touch(PointerEventKind::Move, 1, 300.0, 100.0);
        handler(pinch_move.clone());

        assert!(
            (state.scale_non_reactive() - 2.0).abs() < 1e-4,
            "doubling the spread must double the scale, got {}",
            state.scale_non_reactive()
        );
        assert!(
            pinch_move.is_consumed(),
            "active pinch must consume move events"
        );

        handler(touch(PointerEventKind::Up, 1, 300.0, 100.0));
        handler(touch(PointerEventKind::Up, 0, 100.0, 100.0));
    });
}

#[test]
fn pinch_keeps_content_under_the_fingers() {
    let _app_context = crate::render_state::app_context_test_scope();
    with_test_runtime(|| {
        let state = ZoomState::with_scale_range(1.0, 8.0);
        let (handler, _chain) = pointer_handler_for(Modifier::empty().zoomable(state));

        handler(touch(PointerEventKind::Down, 0, 100.0, 100.0));
        handler(touch(PointerEventKind::Down, 1, 200.0, 100.0));
        handler(touch(PointerEventKind::Move, 0, 50.0, 100.0));
        handler(touch(PointerEventKind::Move, 1, 250.0, 100.0));

        let scale = state.scale_non_reactive();
        let offset = state.offset_non_reactive();
        let display_x = 150.0 * scale + offset.x;
        let display_y = 100.0 * scale + offset.y;
        assert!(
            (display_x - 150.0).abs() < 1.0 && (display_y - 100.0).abs() < 1.0,
            "pinch center drifted: ({display_x}, {display_y}) at scale {scale}"
        );
        assert!(scale > 1.5, "symmetric spread must zoom in, got {scale}");
    });
}

#[test]
fn secondary_pointer_down_and_up_are_consumed() {
    let _app_context = crate::render_state::app_context_test_scope();
    with_test_runtime(|| {
        let state = ZoomState::new();
        let (handler, _chain) = pointer_handler_for(Modifier::empty().zoomable(state));

        handler(touch(PointerEventKind::Down, 0, 100.0, 100.0));
        let secondary_down = touch(PointerEventKind::Down, 1, 200.0, 100.0);
        handler(secondary_down.clone());
        assert!(
            secondary_down.is_consumed(),
            "secondary finger down must not leak to single-pointer handlers"
        );

        let secondary_up = touch(PointerEventKind::Up, 1, 200.0, 100.0);
        handler(secondary_up.clone());
        assert!(
            secondary_up.is_consumed(),
            "secondary finger up must not leak to single-pointer handlers"
        );
    });
}

#[test]
fn single_finger_drag_pans_only_when_transformed() {
    let _app_context = crate::render_state::app_context_test_scope();
    with_test_runtime(|| {
        let state = ZoomState::new();
        let (handler, _chain) = pointer_handler_for(Modifier::empty().zoomable(state));

        handler(touch(PointerEventKind::Down, 0, 100.0, 100.0));
        let plain_drag = touch(PointerEventKind::Move, 0, 100.0, 160.0);
        handler(plain_drag.clone());
        handler(touch(PointerEventKind::Up, 0, 100.0, 160.0));
        assert_eq!(state.offset_non_reactive(), Point { x: 0.0, y: 0.0 });
        assert!(
            !plain_drag.is_consumed(),
            "identity zoomable must not steal plain drags"
        );

        state.set_scale(2.0);
        handler(touch(PointerEventKind::Down, 0, 100.0, 100.0));
        handler(touch(PointerEventKind::Move, 0, 100.0, 120.0));
        handler(touch(PointerEventKind::Move, 0, 100.0, 160.0));
        handler(touch(PointerEventKind::Up, 0, 100.0, 160.0));

        let offset = state.offset_non_reactive();
        assert!(
            offset.y > 30.0,
            "zoomed content must pan with the finger, got {offset:?}"
        );
    });
}

#[test]
fn two_finger_centroid_pan_at_identity_scale_is_inert() {
    let _app_context = crate::render_state::app_context_test_scope();
    with_test_runtime(|| {
        let state = ZoomState::with_scale_range(0.5, 8.0);
        let (handler, _chain) = pointer_handler_for(Modifier::empty().zoomable(state));

        handler(touch(PointerEventKind::Down, 0, 100.0, 100.0));
        handler(touch(PointerEventKind::Down, 1, 200.0, 100.0));
        handler(touch(PointerEventKind::Move, 0, 140.0, 125.0));
        handler(touch(PointerEventKind::Move, 1, 240.0, 125.0));
        handler(touch(PointerEventKind::Up, 1, 240.0, 125.0));
        handler(touch(PointerEventKind::Up, 0, 140.0, 125.0));

        assert!(
            (state.scale_non_reactive() - 1.0).abs() < 1e-4,
            "a parallel two-finger pan must not change the scale, got {}",
            state.scale_non_reactive()
        );
        assert_eq!(
            state.offset_non_reactive(),
            Point { x: 0.0, y: 0.0 },
            "centroid pan at identity scale must be inert"
        );
    });
}

#[test]
fn released_pinch_that_zoomed_back_out_leaves_no_offset() {
    let _app_context = crate::render_state::app_context_test_scope();
    with_test_runtime(|| {
        let state = ZoomState::new();
        let (handler, _chain) = pointer_handler_for(Modifier::empty().zoomable(state));

        handler(touch(PointerEventKind::Down, 0, 100.0, 100.0));
        handler(touch(PointerEventKind::Down, 1, 200.0, 100.0));
        handler(touch(PointerEventKind::Move, 1, 300.0, 100.0));
        assert!(state.is_zoomed_in(), "pinch out must zoom in");
        handler(touch(PointerEventKind::Move, 0, 130.0, 140.0));
        handler(touch(PointerEventKind::Move, 1, 330.0, 140.0));
        handler(touch(PointerEventKind::Move, 1, 210.0, 140.0));
        handler(touch(PointerEventKind::Up, 1, 210.0, 140.0));
        handler(touch(PointerEventKind::Up, 0, 130.0, 140.0));

        assert_eq!(state.scale_non_reactive(), 1.0);
        assert_eq!(
            state.offset_non_reactive(),
            Point { x: 0.0, y: 0.0 },
            "a released pinch that zoomed back out must not leave a stray offset"
        );
        assert!(!state.is_transformed());
    });
}

#[test]
fn one_finger_drag_does_not_pan_offset_only_state() {
    let _app_context = crate::render_state::app_context_test_scope();
    with_test_runtime(|| {
        let state = ZoomState::new();
        let (handler, _chain) = pointer_handler_for(Modifier::empty().zoomable(state));
        state.set_offset(Point { x: 5.0, y: 5.0 });

        handler(touch(PointerEventKind::Down, 0, 100.0, 100.0));
        let drag = touch(PointerEventKind::Move, 0, 100.0, 160.0);
        handler(drag.clone());
        handler(touch(PointerEventKind::Up, 0, 100.0, 160.0));

        assert_eq!(state.offset_non_reactive(), Point { x: 5.0, y: 5.0 });
        assert!(
            !drag.is_consumed(),
            "an unzoomed zoomable must not steal drags even with a programmatic offset"
        );
    });
}

fn timed_touch(kind: PointerEventKind, x: f32, y: f32, time_ms: i64) -> PointerEvent {
    touch(kind, 0, x, y).with_time_ms(Some(time_ms))
}

#[test]
fn double_tap_resets_transformed_state() {
    let _app_context = crate::render_state::app_context_test_scope();
    with_test_runtime(|| {
        let state = ZoomState::new();
        let (handler, _chain) = pointer_handler_for(Modifier::empty().zoomable(state));
        state.set_scale(2.5);
        state.set_offset(Point { x: -30.0, y: 12.0 });

        handler(timed_touch(PointerEventKind::Down, 100.0, 100.0, 0));
        handler(timed_touch(PointerEventKind::Up, 100.0, 100.0, 50));
        handler(timed_touch(PointerEventKind::Down, 103.0, 98.0, 200));
        let second_up = timed_touch(PointerEventKind::Up, 103.0, 98.0, 250);
        handler(second_up.clone());

        assert_eq!(state.scale_non_reactive(), 1.0);
        assert_eq!(state.offset_non_reactive(), Point { x: 0.0, y: 0.0 });
        assert!(
            !state.is_transformed(),
            "double tap must reset the transform"
        );
        assert!(
            second_up.is_consumed(),
            "the resetting tap must not leak to click handlers"
        );
    });
}

#[test]
fn slow_second_tap_does_not_reset() {
    let _app_context = crate::render_state::app_context_test_scope();
    with_test_runtime(|| {
        let state = ZoomState::new();
        let (handler, _chain) = pointer_handler_for(Modifier::empty().zoomable(state));
        state.set_scale(2.0);

        handler(timed_touch(PointerEventKind::Down, 100.0, 100.0, 0));
        handler(timed_touch(PointerEventKind::Up, 100.0, 100.0, 50));
        handler(timed_touch(PointerEventKind::Down, 100.0, 100.0, 500));
        handler(timed_touch(PointerEventKind::Up, 100.0, 100.0, 550));

        assert_eq!(
            state.scale_non_reactive(),
            2.0,
            "taps slower than the double-tap timeout must not reset"
        );
    });
}

#[test]
fn double_tap_at_identity_is_not_consumed() {
    let _app_context = crate::render_state::app_context_test_scope();
    with_test_runtime(|| {
        let state = ZoomState::new();
        let (handler, _chain) = pointer_handler_for(Modifier::empty().zoomable(state));

        handler(timed_touch(PointerEventKind::Down, 100.0, 100.0, 0));
        handler(timed_touch(PointerEventKind::Up, 100.0, 100.0, 50));
        handler(timed_touch(PointerEventKind::Down, 100.0, 100.0, 200));
        let second_up = timed_touch(PointerEventKind::Up, 100.0, 100.0, 250);
        handler(second_up.clone());

        assert!(
            !second_up.is_consumed(),
            "a double tap with nothing to reset must stay visible to click handlers"
        );
    });
}

#[test]
fn zoom_events_scale_about_the_cursor() {
    let _app_context = crate::render_state::app_context_test_scope();
    with_test_runtime(|| {
        let state = ZoomState::with_scale_range(1.0, 8.0);
        let (handler, _chain) = pointer_handler_for(Modifier::empty().zoomable(state));

        let cursor = Point { x: 80.0, y: 60.0 };
        let zoom_event =
            PointerEvent::new(PointerEventKind::Zoom, cursor, cursor).with_zoom_delta(1.5);
        handler(zoom_event.clone());

        assert!(
            (state.scale_non_reactive() - 1.5).abs() < 1e-4,
            "ctrl+wheel step must multiply the scale, got {}",
            state.scale_non_reactive()
        );
        assert!(zoom_event.is_consumed(), "zoom steps must be consumed");

        let offset = state.offset_non_reactive();
        let display_x = cursor.x * 1.5 + offset.x;
        let display_y = cursor.y * 1.5 + offset.y;
        assert!(
            (display_x - cursor.x).abs() < 1e-3 && (display_y - cursor.y).abs() < 1e-3,
            "wheel zoom must anchor at the cursor, got ({display_x}, {display_y})"
        );
    });
}

#[test]
fn scroll_ignores_secondary_pointer_events() {
    let _app_context = crate::render_state::app_context_test_scope();
    with_test_runtime(|| {
        let scroll_state = crate::ScrollState::new(100.0);
        scroll_state.set_max_value(400.0);
        let (handler, _chain) =
            pointer_handler_for(Modifier::empty().vertical_scroll(scroll_state, false));

        handler(touch(PointerEventKind::Down, 0, 0.0, 100.0));
        handler(touch(PointerEventKind::Move, 0, 0.0, 80.0));
        let before = scroll_state.value_non_reactive();

        handler(touch(PointerEventKind::Down, 1, 0.0, 300.0));
        handler(touch(PointerEventKind::Move, 1, 0.0, 500.0));
        handler(touch(PointerEventKind::Up, 1, 0.0, 500.0));

        assert_eq!(
            scroll_state.value_non_reactive(),
            before,
            "secondary pointer events must not scroll"
        );

        handler(touch(PointerEventKind::Move, 0, 0.0, 60.0));
        assert!(
            (scroll_state.value_non_reactive() - (before + 20.0)).abs() < 1e-3,
            "primary drag must continue from its own last position"
        );
        handler(touch(PointerEventKind::Up, 0, 0.0, 60.0));
    });
}