cranpose-ui 0.1.43

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
//! Full-pipeline render tests for `SwipeToDismiss`.
//!
//! Regression coverage for two device-confirmed bugs:
//!
//! 1. **Live drag follow** — the content must translate under the finger while
//!    dragging, not stay put and snap on release. The offset is applied as a
//!    graphics-layer translation resolved on every draw pass, so pumping
//!    pointer-move events must move the applied content offset 1:1 with the
//!    accumulated drag (without any recomposition/re-measure).
//!
//! 2. **Background reveal gating** — the `with_background` content must not draw
//!    at rest (offset 0), so it cannot flash through content that fades in with
//!    alpha < 1 during an entrance animation. It must draw once displaced.

use super::*;
use crate::modifier::ModifierNodeSlices;
use crate::modifier::{Color, PointerEvent, PointerEventKind};
use cranpose_core::NodeId;
use cranpose_ui_graphics::{DrawPrimitive, Point, Size as ViewportSize, Size as PrimSize};
use std::rc::Rc;

const BIN_RED: Color = Color(1.0, 0.0, 0.0, 1.0);

fn compose_row(with_background: bool) -> TestComposition {
    run_test_composition(move || {
        let mut spec = SwipeToDismissSpec::default();
        if with_background {
            spec = spec.with_background(|_side| {
                Box(
                    Modifier::empty().fill_max_size().background(BIN_RED),
                    BoxSpec::new(),
                    || {},
                );
            });
        }
        SwipeToDismiss(
            Modifier::empty().fill_max_width().height(48.0),
            spec,
            || {},
            || {
                Text(
                    "CONTENT".to_string(),
                    Modifier::empty(),
                    TextStyle::default(),
                );
            },
        );
    })
}

/// A `SwipeToDismiss` row (with a red bin background) inside a `LazyColumn`
/// item — the real app shape where the lingering-strip bug appears.
fn compose_lazy_row_with_background() -> TestComposition {
    use cranpose_foundation::lazy::{remember_lazy_list_state, LazyListScope};
    run_test_composition(move || {
        let list_state = remember_lazy_list_state();
        LazyColumn(
            Modifier::empty().fill_max_size(),
            list_state,
            LazyColumnSpec::default(),
            |scope| {
                scope.items(
                    1,
                    None::<fn(usize) -> u64>,
                    None::<fn(usize) -> u64>,
                    |_index| {
                        let spec = SwipeToDismissSpec::default().with_background(|_side| {
                            Box(
                                Modifier::empty().fill_max_size().background(BIN_RED),
                                BoxSpec::new(),
                                || {},
                            );
                        });
                        SwipeToDismiss(
                            Modifier::empty().fill_max_width().height(48.0),
                            spec,
                            || {},
                            || {
                                Text(
                                    "CONTENT".to_string(),
                                    Modifier::empty(),
                                    TextStyle::default(),
                                );
                            },
                        );
                    },
                );
            },
        );
    })
}

fn measure_row(composition: &mut TestComposition, root: NodeId) {
    let handle = composition.runtime_handle();
    let mut applier = composition.applier_mut();
    applier.set_runtime_handle(handle);
    crate::measure_layout(
        &mut applier,
        root,
        ViewportSize {
            width: 320.0,
            height: 480.0,
        },
    )
    .expect("layout measurement");
    applier.clear_runtime_handle();
}

fn layout_tree(composition: &mut TestComposition, root: NodeId) -> crate::LayoutTree {
    let handle = composition.runtime_handle();
    let mut applier = composition.applier_mut();
    applier.set_runtime_handle(handle);
    let tree = crate::build_layout_tree_from_applier(&mut applier, root)
        .expect("layout tree build")
        .expect("layout tree present");
    applier.clear_runtime_handle();
    tree
}

fn pointer_handler(tree: &crate::LayoutTree) -> Rc<dyn Fn(PointerEvent)> {
    fn walk(node: &crate::LayoutBox) -> Option<Rc<dyn Fn(PointerEvent)>> {
        if let Some(handler) = node.node_data.modifier_slices().pointer_inputs().first() {
            return Some(Rc::clone(handler));
        }
        node.children.iter().find_map(walk)
    }
    walk(tree.root()).expect("swipe pointer handler")
}

/// The single node carrying the content's graphics-layer resolver (the content
/// wrapper). Returned as an owned `Rc` so the resolver can be re-queried after
/// drags without rebuilding the tree.
fn content_layer_slices(tree: &crate::LayoutTree) -> Rc<ModifierNodeSlices> {
    fn walk(node: &crate::LayoutBox) -> Option<Rc<ModifierNodeSlices>> {
        if node.node_data.modifier_slices().graphics_layer().is_some() {
            return Some(Rc::clone(&node.node_data.modifier_slices));
        }
        node.children.iter().find_map(walk)
    }
    walk(tree.root()).expect("content graphics-layer node")
}

fn applied_translation_x(slices: &ModifierNodeSlices) -> f32 {
    slices
        .graphics_layer()
        .expect("content graphics layer")
        .translation_x
}

fn count_bin_primitives(tree: &crate::LayoutTree) -> usize {
    fn walk(node: &crate::LayoutBox, count: &mut usize) {
        let size = PrimSize {
            width: node.rect.width,
            height: node.rect.height,
        };
        for primitive in
            crate::execute_draw_commands(node.node_data.modifier_slices().draw_commands(), size)
        {
            if let DrawPrimitive::Rect { brush, .. } = primitive {
                if format!("{brush:?}").contains("1.0, 0.0, 0.0") {
                    *count += 1;
                }
            }
        }
        for child in &node.children {
            walk(child, count);
        }
    }
    let mut count = 0;
    walk(tree.root(), &mut count);
    count
}

fn down(x: f32) -> PointerEvent {
    let p = Point { x, y: 24.0 };
    PointerEvent::new(PointerEventKind::Down, p, p)
}

fn move_to(x: f32) -> PointerEvent {
    let p = Point { x, y: 24.0 };
    PointerEvent::new(PointerEventKind::Move, p, p)
}

fn up(x: f32) -> PointerEvent {
    let p = Point { x, y: 24.0 };
    PointerEvent::new(PointerEventKind::Up, p, p)
}

fn root_height(composition: &mut TestComposition, root: NodeId) -> f32 {
    layout_tree(composition, root).root().rect.height
}

/// Bug 1: pumping pointer-move events moves the applied content offset 1:1 with
/// the accumulated drag, in real time, before release — with no recomposition.
#[test]
fn dragging_translates_content_with_the_finger() {
    let mut composition = compose_row(false);
    let root = composition.root().expect("root");
    measure_row(&mut composition, root);
    let tree = layout_tree(&mut composition, root);

    let handler = pointer_handler(&tree);
    let content = content_layer_slices(&tree);

    // At rest, nothing is translated.
    assert_eq!(applied_translation_x(&content), 0.0);

    handler(down(10.0));

    // Within the drag slop: no capture yet, still no translation.
    handler(move_to(14.0));
    assert_eq!(applied_translation_x(&content), 0.0);

    // Crossing the slop captures the gesture; the content follows immediately.
    handler(move_to(30.0));
    assert!(
        (applied_translation_x(&content) - 20.0).abs() < 1e-3,
        "content should track the finger (dx=20), got {}",
        applied_translation_x(&content)
    );

    // Each subsequent move tracks the accumulated drag 1:1.
    handler(move_to(130.0));
    assert!(
        (applied_translation_x(&content) - 120.0).abs() < 1e-3,
        "content should track the finger (dx=120), got {}",
        applied_translation_x(&content)
    );

    handler(move_to(90.0));
    assert!(
        (applied_translation_x(&content) - 80.0).abs() < 1e-3,
        "content should track the finger back (dx=80), got {}",
        applied_translation_x(&content)
    );
}

/// Bug 2: the background produces no draw primitives at rest (offset 0) and
/// does once the row is displaced.
#[test]
fn background_only_draws_while_displaced() {
    let mut composition = compose_row(true);
    let root = composition.root().expect("root");
    measure_row(&mut composition, root);

    // At rest: the background is not composed, so it emits nothing — even
    // though the content behind/over it would (this is the entrance-flash bug).
    let tree = layout_tree(&mut composition, root);
    assert_eq!(
        count_bin_primitives(&tree),
        0,
        "background must not draw at offset 0"
    );

    // Displace the row past the slop.
    let handler = pointer_handler(&tree);
    handler(down(10.0));
    handler(move_to(80.0)); // dx = 70 -> revealed

    // Reading the reveal flag inside the subcompose subscribes the slot, so the
    // write recomposes it and the background is now composed and drawn.
    composition
        .process_invalid_scopes()
        .expect("recompose after reveal");
    measure_row(&mut composition, root);
    let displaced = layout_tree(&mut composition, root);
    assert!(
        count_bin_primitives(&displaced) > 0,
        "background must draw while the row is displaced"
    );
}

/// Bug 3: after a dismiss settles nothing may linger — no red "move to bin"
/// strip and no empty row — regardless of when the host removes the item. The
/// background stops drawing and the row collapses to zero height.
#[test]
fn dismissed_row_hides_background_and_collapses_to_zero_height() {
    let mut composition = compose_row(true);
    let root = composition.root().expect("root");
    measure_row(&mut composition, root);
    assert!(
        (root_height(&mut composition, root) - 48.0).abs() < 1e-3,
        "row starts at its natural 48px height"
    );

    let tree = layout_tree(&mut composition, root);
    let handler = pointer_handler(&tree);

    // Swipe past the dismiss threshold (0.5 * 320px width = 160px) and release.
    handler(down(10.0));
    handler(move_to(30.0)); // capture
    handler(move_to(210.0)); // dx = 200 >= threshold
    handler(up(210.0));

    // Settle the fling and run the collapse animation to completion.
    let handle = composition.runtime_handle();
    let mut frame_time = 0u64;
    for _ in 0..600 {
        frame_time += 16_666_667;
        composition.with_app_context(|| handle.drain_frame_callbacks(frame_time));
    }
    composition
        .process_invalid_scopes()
        .expect("recompose after the dismiss settles");
    measure_row(&mut composition, root);

    let settled = layout_tree(&mut composition, root);
    assert_eq!(
        count_bin_primitives(&settled),
        0,
        "no red background strip may linger after a dismiss"
    );
    assert!(
        settled.root().rect.height <= 0.5,
        "the dismissed row must collapse to zero height, got {}",
        settled.root().rect.height
    );
}

/// The same dismiss, but with the row inside a `LazyColumn` item — the real app
/// shape. The collapse re-measures via `schedule_measure_repass`, which bubbles
/// *measure* dirtiness so the list re-measures the shrinking item instead of
/// reusing its cached, full-height slot. Without that, the red background and
/// the pre-dismiss height linger until the whole list is rebuilt.
#[test]
fn dismissed_row_inside_lazy_column_leaves_no_lingering_strip() {
    let mut composition = compose_lazy_row_with_background();
    let root = composition.root().expect("root");
    measure_row(&mut composition, root);

    let tree = layout_tree(&mut composition, root);
    // The list's scroll node also carries a pointer handler; the swipe row's is
    // the deepest one, so dispatch there (the swipe consumes the horizontal
    // drag before it would reach the parent scroll).
    let handler = deepest_pointer_handler(&tree);

    // Swipe past the dismiss threshold (0.5 * 320px width = 160px) and release.
    handler(down(10.0));
    handler(move_to(30.0)); // capture
    handler(move_to(210.0)); // dx = 200 >= threshold

    // Sanity: the swipe captured and revealed the background (proves the events
    // reached the swipe handler, not the parent scroll).
    composition
        .process_invalid_scopes()
        .expect("recompose after capture");
    measure_row(&mut composition, root);
    assert!(
        count_bin_primitives(&layout_tree(&mut composition, root)) > 0,
        "the swipe must reveal the bin background mid-drag"
    );

    handler(up(210.0));

    // Settle + collapse. Interleave frame pumps with re-measures/recomposes, as
    // the shell does each frame: the collapse animation advances and schedules a
    // measure repass, which the next measure pass consumes to re-measure the row
    // through the LazyColumn (bubbling measure dirtiness defeats the item cache).
    let handle = composition.runtime_handle();
    let mut frame_time = 0u64;
    for _ in 0..120 {
        for _ in 0..8 {
            frame_time += 16_666_667;
            composition.with_app_context(|| handle.drain_frame_callbacks(frame_time));
        }
        composition
            .process_invalid_scopes()
            .expect("recompose during collapse");
        measure_row(&mut composition, root);
    }

    let settled = layout_tree(&mut composition, root);
    assert_eq!(
        count_bin_primitives(&settled),
        0,
        "no red background strip may linger after a dismiss inside a LazyColumn"
    );
    // The collapsing item node (the outer SubcomposeLayout that scales its height
    // by the collapse fraction) must have shrunk to zero inside the list.
    let item_height = outer_item_height(&settled).expect("collapsing item present in tree");
    assert!(
        item_height <= 0.5,
        "the dismissed row inside a LazyColumn must collapse to zero height, got {item_height}"
    );
}

/// A pointer handler tagged with the tree depth it was found at.
type DepthTaggedHandler = (usize, Rc<dyn Fn(PointerEvent)>);

/// The deepest pointer handler in the tree — the swipe row's, below the list's
/// scroll handler.
fn deepest_pointer_handler(tree: &crate::LayoutTree) -> Rc<dyn Fn(PointerEvent)> {
    fn walk(node: &crate::LayoutBox, best: &mut Option<DepthTaggedHandler>, depth: usize) {
        if let Some(handler) = node.node_data.modifier_slices().pointer_inputs().first() {
            if best.as_ref().is_none_or(|(d, _)| depth >= *d) {
                *best = Some((depth, Rc::clone(handler)));
            }
        }
        for child in &node.children {
            walk(child, best, depth + 1);
        }
    }
    let mut best = None;
    walk(tree.root(), &mut best, 0);
    best.expect("swipe pointer handler").1
}

/// Height of the collapsing item: the parent node of the swipe-handler node
/// (the outer SubcomposeLayout that scales height by the collapse fraction).
fn outer_item_height(tree: &crate::LayoutTree) -> Option<f32> {
    fn walk(node: &crate::LayoutBox) -> Option<f32> {
        for child in &node.children {
            if !child
                .node_data
                .modifier_slices()
                .pointer_inputs()
                .is_empty()
                && (node.rect.height - 480.0).abs() > 1.0
            {
                return Some(node.rect.height);
            }
            if let Some(h) = walk(child) {
                return Some(h);
            }
        }
        None
    }
    walk(tree.root())
}