fret-ui 0.1.0

Mechanism-layer UI engine for Fret with tree, layout, focus, routing, and interaction contracts.
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
use super::ElementHostWidget;
use crate::declarative::frame::element_record_for_node;
use crate::declarative::prelude::*;
use fret_core::Modifiers;

const SCROLL_CONSUMED_EPS: f32 = 0.001;
const TOUCH_PAN_SCROLL_THRESHOLD_PX: f32 = 6.0;
static DEBUG_VLIST_WHEEL_PRINTED: std::sync::atomic::AtomicBool =
    std::sync::atomic::AtomicBool::new(false);

#[derive(Debug, Default, Clone, Copy)]
struct TouchPanScrollTracking {
    pointer_id: Option<fret_core::PointerId>,
    start: Option<Point>,
    last: Option<Point>,
    panning: bool,
    captured_for_pan: bool,
}

fn touch_pan_delta_for_move<H: UiHost>(
    cx: &mut EventCx<'_, H>,
    window: AppWindowId,
    element: crate::GlobalElementId,
    pointer_id: fret_core::PointerId,
    position: Point,
) -> Option<Point> {
    let mut out: Option<Point> = None;
    crate::elements::with_element_state(
        &mut *cx.app,
        window,
        element,
        TouchPanScrollTracking::default,
        |st| {
            if st.pointer_id != Some(pointer_id) {
                return;
            }
            let Some(prev) = st.last else {
                st.last = Some(position);
                return;
            };
            let Some(start) = st.start else {
                st.start = Some(position);
                st.last = Some(position);
                return;
            };

            let total_dx = position.x.0 - start.x.0;
            let total_dy = position.y.0 - start.y.0;
            if !st.panning {
                let dist = (total_dx * total_dx + total_dy * total_dy).sqrt();
                if dist > TOUCH_PAN_SCROLL_THRESHOLD_PX {
                    st.panning = true;
                }
            }

            st.last = Some(position);
            if st.panning {
                out = Some(Point::new(
                    Px(position.x.0 - prev.x.0),
                    Px(position.y.0 - prev.y.0),
                ));
            }
        },
    );
    out
}

fn clear_touch_pan_tracking<H: UiHost>(
    cx: &mut EventCx<'_, H>,
    window: AppWindowId,
    element: crate::GlobalElementId,
    pointer_id: fret_core::PointerId,
) -> bool {
    let mut captured_for_pan = false;
    crate::elements::with_element_state(
        &mut *cx.app,
        window,
        element,
        TouchPanScrollTracking::default,
        |st| {
            if st.pointer_id == Some(pointer_id) {
                captured_for_pan = st.captured_for_pan;
                *st = TouchPanScrollTracking::default();
            }
        },
    );
    captured_for_pan
}

fn mark_touch_pan_captured<H: UiHost>(
    cx: &mut EventCx<'_, H>,
    window: AppWindowId,
    element: crate::GlobalElementId,
    pointer_id: fret_core::PointerId,
) {
    crate::elements::with_element_state(
        &mut *cx.app,
        window,
        element,
        TouchPanScrollTracking::default,
        |st| {
            if st.pointer_id == Some(pointer_id) {
                st.captured_for_pan = true;
            }
        },
    );
}

fn clear_pressed_pressable_if_any<H: UiHost>(cx: &mut EventCx<'_, H>, window: AppWindowId) {
    if let Some(prev_node) = crate::elements::set_pressed_pressable(&mut *cx.app, window, None) {
        cx.invalidate(prev_node, Invalidation::Paint);
    }
}

fn foreign_touch_capture_is_pressable<H: UiHost>(
    cx: &mut EventCx<'_, H>,
    window: AppWindowId,
) -> bool {
    let Some(captured) = cx.captured else {
        return false;
    };
    if captured == cx.node {
        return false;
    }
    element_record_for_node(&mut *cx.app, window, captured).is_some_and(|record| {
        matches!(
            record.instance,
            crate::declarative::frame::ElementInstance::Pressable(_)
        )
    })
}

fn apply_virtual_list_scroll_delta<H: UiHost>(
    cx: &mut EventCx<'_, H>,
    window: AppWindowId,
    element: crate::GlobalElementId,
    props: &crate::element::VirtualListProps,
    delta: Point,
    modifiers: Modifiers,
) -> (bool, bool) {
    crate::elements::with_element_state(
        &mut *cx.app,
        window,
        element,
        crate::element::VirtualListState::default,
        |state| {
            let axis = props.axis;
            state.metrics.ensure_with_mode(
                props.measure_mode,
                props.len,
                props.estimate_row_height,
                props.gap,
                props.scroll_margin,
            );
            let viewport = match axis {
                fret_core::Axis::Vertical => Px(state.viewport_h.0.max(0.0)),
                fret_core::Axis::Horizontal => Px(state.viewport_w.0.max(0.0)),
            };
            if viewport.0 <= 0.0 || props.len == 0 {
                return (false, false);
            }

            let prev = props.scroll_handle.offset();
            let prev_offset = match axis {
                fret_core::Axis::Vertical => prev.y,
                fret_core::Axis::Horizontal => prev.x,
            };
            let offset = state.metrics.clamp_offset(prev_offset, viewport);

            let delta = match axis {
                fret_core::Axis::Vertical => delta.y,
                fret_core::Axis::Horizontal => {
                    if modifiers.shift {
                        delta.y
                    } else {
                        delta.x
                    }
                }
            };
            let next = state.metrics.clamp_offset(Px(offset.0 - delta.0), viewport);
            if crate::runtime_config::ui_runtime_config().debug_scroll_wheel_vlist
                && !DEBUG_VLIST_WHEEL_PRINTED.swap(true, std::sync::atomic::Ordering::Relaxed)
            {
                let max = props.scroll_handle.max_offset();
                let viewport_size = props.scroll_handle.viewport_size();
                let content_size = props.scroll_handle.content_size();
                eprintln!(
                    "scroll wheel vlist element={:?} handle_key={} axis={:?} delta={:.3} prev={:.3} next={:.3} viewport=({:.3},{:.3}) content=({:.3},{:.3}) max=({:.3},{:.3})",
                    element,
                    props.scroll_handle.base_handle().binding_key(),
                    axis,
                    delta.0,
                    prev_offset.0,
                    next.0,
                    viewport_size.width.0,
                    viewport_size.height.0,
                    content_size.width.0,
                    content_size.height.0,
                    max.x.0,
                    max.y.0,
                );
            }

            if (prev_offset.0 - next.0).abs() > SCROLL_CONSUMED_EPS {
                let visible_range = state.metrics.visible_range(next, viewport, 0);
                let needs_visible_range_rerender = visible_range.is_some_and(|visible| match state
                    .render_window_range
                {
                    None => visible.count > 0,
                    Some(rendered) => {
                        if rendered.count == 0 {
                            visible.count > 0
                        } else {
                            let rendered_start =
                                rendered.start_index.saturating_sub(rendered.overscan);
                            let rendered_end = (rendered.end_index + rendered.overscan)
                                .min(rendered.count.saturating_sub(1));
                            visible.start_index < rendered_start || visible.end_index > rendered_end
                        }
                    }
                });

                match axis {
                    fret_core::Axis::Vertical => {
                        props
                            .scroll_handle
                            .set_offset(fret_core::Point::new(prev.x, next));
                    }
                    fret_core::Axis::Horizontal => {
                        props
                            .scroll_handle
                            .set_offset(fret_core::Point::new(next, prev.y));
                    }
                }
                (true, needs_visible_range_rerender)
            } else {
                (false, false)
            }
        },
    )
}

pub(super) fn handle_virtual_list<H: UiHost>(
    this: &mut ElementHostWidget,
    cx: &mut EventCx<'_, H>,
    window: AppWindowId,
    props: crate::element::VirtualListProps,
    event: &Event,
) -> bool {
    // Wheel events should be handled by the *innermost* scrollable under the pointer. If we scroll
    // ancestors during the capture phase, nested scrollables (code blocks, tables, editors) never
    // see the wheel delta.
    if cx.input_ctx.dispatch_phase == fret_runtime::InputDispatchPhase::Capture
        && matches!(event, Event::Pointer(fret_core::PointerEvent::Wheel { .. }))
    {
        return true;
    }

    if cx.input_ctx.dispatch_phase == fret_runtime::InputDispatchPhase::Bubble
        && matches!(
            event,
            Event::Pointer(fret_core::PointerEvent::Down { .. })
                | Event::Pointer(fret_core::PointerEvent::Up { .. })
                | Event::PointerCancel(_)
        )
    {
        return true;
    }

    let mut consumed = false;
    let mut needs_visible_range_rerender = false;

    match event {
        Event::Pointer(fret_core::PointerEvent::Wheel {
            delta, modifiers, ..
        }) => {
            (consumed, needs_visible_range_rerender) = apply_virtual_list_scroll_delta(
                cx,
                window,
                this.element,
                &props,
                *delta,
                *modifiers,
            );
        }
        Event::Pointer(fret_core::PointerEvent::Down {
            pointer_type,
            button,
            pointer_id,
            position,
            ..
        }) => {
            if *button == MouseButton::Left {
                cx.request_focus(cx.node);
            }
            if *pointer_type == fret_core::PointerType::Touch && *button == MouseButton::Left {
                crate::elements::with_element_state(
                    &mut *cx.app,
                    window,
                    this.element,
                    TouchPanScrollTracking::default,
                    |st| {
                        st.pointer_id = Some(*pointer_id);
                        st.start = Some(*position);
                        st.last = Some(*position);
                        st.panning = false;
                        st.captured_for_pan = false;
                    },
                );
            }
        }
        Event::Pointer(fret_core::PointerEvent::Move {
            pointer_id,
            position,
            pointer_type,
            modifiers,
            ..
        }) => {
            if *pointer_type == fret_core::PointerType::Touch {
                let foreign_capture = cx.captured.is_some_and(|n| n != cx.node);
                let foreign_capture_is_pressable =
                    foreign_capture && foreign_touch_capture_is_pressable(cx, window);
                match cx.input_ctx.dispatch_phase {
                    fret_runtime::InputDispatchPhase::Capture
                        if !foreign_capture || foreign_capture_is_pressable =>
                    {
                        return true;
                    }
                    fret_runtime::InputDispatchPhase::Bubble
                        if foreign_capture && !foreign_capture_is_pressable =>
                    {
                        return true;
                    }
                    _ => {}
                }
            }

            if *pointer_type == fret_core::PointerType::Touch
                && let Some(delta) =
                    touch_pan_delta_for_move(cx, window, this.element, *pointer_id, *position)
            {
                (consumed, needs_visible_range_rerender) = apply_virtual_list_scroll_delta(
                    cx,
                    window,
                    this.element,
                    &props,
                    delta,
                    *modifiers,
                );
                if consumed && cx.captured != Some(cx.node) {
                    cx.capture_pointer(cx.node);
                    mark_touch_pan_captured(cx, window, this.element, *pointer_id);
                    clear_pressed_pressable_if_any(cx, window);
                }
            }
        }
        Event::Pointer(fret_core::PointerEvent::Up {
            pointer_id,
            pointer_type,
            ..
        }) => {
            if *pointer_type == fret_core::PointerType::Touch {
                let captured_for_pan =
                    clear_touch_pan_tracking(cx, window, this.element, *pointer_id);
                if captured_for_pan && cx.captured == Some(cx.node) {
                    cx.release_pointer_capture();
                }
            }
        }
        Event::PointerCancel(e) => {
            if e.pointer_type == fret_core::PointerType::Touch {
                let captured_for_pan =
                    clear_touch_pan_tracking(cx, window, this.element, e.pointer_id);
                if captured_for_pan && cx.captured == Some(cx.node) {
                    cx.release_pointer_capture();
                }
            }
        }
        _ => {}
    }

    if consumed {
        let inv = Invalidation::HitTestOnly;
        super::invalidate_scroll_handle_bindings(
            cx,
            window,
            props.scroll_handle.base_handle().binding_key(),
            inv,
        );
        // VirtualList scrolling is applied via a children-only render transform, so hit-testing
        // must be invalidated to refresh coordinate mapping under the updated offset. This does
        // not force a layout pass.
        cx.invalidate_self(inv);
        if needs_visible_range_rerender {
            let retained_host =
                crate::elements::with_window_state(&mut *cx.app, window, |window_state| {
                    let retained = window_state
                        .has_state::<crate::windowed_surface_host::RetainedVirtualListHostMarker>(
                        this.element,
                    );
                    if retained {
                        window_state.mark_retained_virtual_list_needs_reconcile(
                            this.element,
                            crate::tree::UiDebugRetainedVirtualListReconcileKind::Escape,
                        );
                    }
                    retained
                });

            if !retained_host {
                cx.notify();
            }
        }
        cx.request_redraw();
        cx.stop_propagation();
    }

    true
}

pub(super) fn handle_scroll<H: UiHost>(
    this: &mut ElementHostWidget,
    cx: &mut EventCx<'_, H>,
    window: AppWindowId,
    props: crate::element::ScrollProps,
    event: &Event,
) -> bool {
    // Same rationale as `handle_virtual_list`: let the deepest scrollable consume wheel deltas
    // first, then fall back to ancestors in bubble.
    if cx.input_ctx.dispatch_phase == fret_runtime::InputDispatchPhase::Capture
        && matches!(event, Event::Pointer(fret_core::PointerEvent::Wheel { .. }))
    {
        return true;
    }

    if cx.input_ctx.dispatch_phase == fret_runtime::InputDispatchPhase::Bubble
        && matches!(
            event,
            Event::Pointer(fret_core::PointerEvent::Down { .. })
                | Event::Pointer(fret_core::PointerEvent::Up { .. })
                | Event::PointerCancel(_)
        )
    {
        return true;
    }

    if let Event::Pointer(fret_core::PointerEvent::Wheel {
        delta, modifiers, ..
    }) = event
    {
        let (delta_x, delta_y) = match props.axis {
            crate::element::ScrollAxis::X => {
                // Trackpads often report diagonal deltas. If the gesture is primarily vertical and
                // Shift is not held, let the parent (typically a Y-scroll) handle it.
                if !modifiers.shift && delta.y.0.abs() > delta.x.0.abs() {
                    (Px(0.0), Px(0.0))
                } else if modifiers.shift && delta.x.0.abs() < 0.01 {
                    (delta.y, Px(0.0))
                } else {
                    (delta.x, Px(0.0))
                }
            }
            crate::element::ScrollAxis::Y => (Px(0.0), delta.y),
            crate::element::ScrollAxis::Both => (delta.x, delta.y),
        };

        let (consumed, clamped_at_edge) = if let Some(handle) = props.scroll_handle.as_ref() {
            let prev = handle.offset();
            let max = handle.max_offset();
            let desired = Point::new(Px(prev.x.0 - delta_x.0), Px(prev.y.0 - delta_y.0));
            handle.set_offset(desired);
            let next = handle.offset();
            if crate::runtime_config::ui_runtime_config().debug_scroll_wheel {
                eprintln!(
                    "scroll wheel element={:?} handle_key={} axis={:?} delta=({:.3},{:.3}) prev=({:.3},{:.3}) next=({:.3},{:.3}) max=({:.3},{:.3})",
                    this.element,
                    handle.binding_key(),
                    props.axis,
                    delta_x.0,
                    delta_y.0,
                    prev.x.0,
                    prev.y.0,
                    next.x.0,
                    next.y.0,
                    max.x.0,
                    max.y.0,
                );
            }
            let has_delta =
                delta_x.0.abs() > SCROLL_CONSUMED_EPS || delta_y.0.abs() > SCROLL_CONSUMED_EPS;
            let moved = (prev.x.0 - next.x.0).abs() > SCROLL_CONSUMED_EPS
                || (prev.y.0 - next.y.0).abs() > SCROLL_CONSUMED_EPS;
            let consumed = has_delta && moved;
            // If a wheel delta is clamped at the scroll extent edge, the scroll container may be
            // relying on cached content extents even though descendants have grown (e.g. expanding
            // a tabs panel near the bottom of a docs page). Schedule an extent probe so the next
            // layout pass remeasures the subtree and updates max offsets.
            let clamp_x_max = props.axis.scroll_x()
                && delta_x.0 < -SCROLL_CONSUMED_EPS
                && desired.x.0 > max.x.0 + 0.5
                && next.x.0 + 0.5 >= max.x.0;
            let clamp_x_min = props.axis.scroll_x()
                && delta_x.0 > SCROLL_CONSUMED_EPS
                && desired.x.0 < -0.5
                && next.x.0 <= 0.5;
            let clamp_y_max = props.axis.scroll_y()
                && delta_y.0 < -SCROLL_CONSUMED_EPS
                && desired.y.0 > max.y.0 + 0.5
                && next.y.0 + 0.5 >= max.y.0;
            let clamp_y_min = props.axis.scroll_y()
                && delta_y.0 > SCROLL_CONSUMED_EPS
                && desired.y.0 < -0.5
                && next.y.0 <= 0.5;
            let clamped_at_edge =
                has_delta && (clamp_x_max || clamp_x_min || clamp_y_max || clamp_y_min);
            (consumed, clamped_at_edge)
        } else {
            crate::elements::with_element_state(
                &mut *cx.app,
                window,
                this.element,
                crate::element::ScrollState::default,
                |state| {
                    let prev = state.scroll_handle.offset();
                    let max = state.scroll_handle.max_offset();
                    let desired = Point::new(Px(prev.x.0 - delta_x.0), Px(prev.y.0 - delta_y.0));
                    state.scroll_handle.set_offset(desired);
                    let next = state.scroll_handle.offset();
                    let has_delta = delta_x.0.abs() > SCROLL_CONSUMED_EPS
                        || delta_y.0.abs() > SCROLL_CONSUMED_EPS;
                    let moved = (prev.x.0 - next.x.0).abs() > SCROLL_CONSUMED_EPS
                        || (prev.y.0 - next.y.0).abs() > SCROLL_CONSUMED_EPS;
                    let consumed = has_delta && moved;
                    let clamp_x_max = props.axis.scroll_x()
                        && delta_x.0 < -SCROLL_CONSUMED_EPS
                        && desired.x.0 > max.x.0 + 0.5
                        && next.x.0 + 0.5 >= max.x.0;
                    let clamp_x_min = props.axis.scroll_x()
                        && delta_x.0 > SCROLL_CONSUMED_EPS
                        && desired.x.0 < -0.5
                        && next.x.0 <= 0.5;
                    let clamp_y_max = props.axis.scroll_y()
                        && delta_y.0 < -SCROLL_CONSUMED_EPS
                        && desired.y.0 > max.y.0 + 0.5
                        && next.y.0 + 0.5 >= max.y.0;
                    let clamp_y_min = props.axis.scroll_y()
                        && delta_y.0 > SCROLL_CONSUMED_EPS
                        && desired.y.0 < -0.5
                        && next.y.0 <= 0.5;
                    let clamped_at_edge =
                        has_delta && (clamp_x_max || clamp_x_min || clamp_y_max || clamp_y_min);
                    (consumed, clamped_at_edge)
                },
            )
        };

        if consumed {
            if let Some(handle) = props.scroll_handle.as_ref() {
                super::invalidate_scroll_handle_bindings(
                    cx,
                    window,
                    handle.binding_key(),
                    Invalidation::HitTestOnly,
                );
            }
            cx.invalidate_self(Invalidation::HitTestOnly);
            cx.request_redraw();
            cx.stop_propagation();
        } else if clamped_at_edge {
            // A wheel delta clamped at the current edge warrants a follow-up layout pass, but it
            // should not force an unbounded extent probe by itself. Real growth cases are already
            // surfaced via descendant layout dirtiness / subtree-dirty aggregation during layout,
            // while budget-hit recovery uses `pending_extent_probe` from the layout path.
            cx.invalidate_self(Invalidation::Layout);
            cx.request_redraw();
        }
        return true;
    }

    match event {
        Event::Pointer(fret_core::PointerEvent::Down {
            pointer_type,
            button,
            pointer_id,
            position,
            ..
        }) => {
            if *pointer_type == fret_core::PointerType::Touch && *button == MouseButton::Left {
                crate::elements::with_element_state(
                    &mut *cx.app,
                    window,
                    this.element,
                    TouchPanScrollTracking::default,
                    |st| {
                        st.pointer_id = Some(*pointer_id);
                        st.start = Some(*position);
                        st.last = Some(*position);
                        st.panning = false;
                        st.captured_for_pan = false;
                    },
                );
            }
        }
        Event::Pointer(fret_core::PointerEvent::Move {
            pointer_id,
            position,
            pointer_type,
            ..
        }) => {
            if *pointer_type != fret_core::PointerType::Touch {
                return true;
            }
            let foreign_capture = cx.captured.is_some_and(|n| n != cx.node);
            let foreign_capture_is_pressable =
                foreign_capture && foreign_touch_capture_is_pressable(cx, window);
            match cx.input_ctx.dispatch_phase {
                fret_runtime::InputDispatchPhase::Capture
                    if !foreign_capture || foreign_capture_is_pressable =>
                {
                    return true;
                }
                fret_runtime::InputDispatchPhase::Bubble
                    if foreign_capture && !foreign_capture_is_pressable =>
                {
                    return true;
                }
                _ => {}
            }
            let Some(delta) =
                touch_pan_delta_for_move(cx, window, this.element, *pointer_id, *position)
            else {
                return true;
            };

            let delta_x = if props.axis.scroll_x() {
                delta.x
            } else {
                Px(0.0)
            };
            let delta_y = if props.axis.scroll_y() {
                delta.y
            } else {
                Px(0.0)
            };

            let consumed = if let Some(handle) = props.scroll_handle.as_ref() {
                let prev = handle.offset();
                let desired = Point::new(Px(prev.x.0 - delta_x.0), Px(prev.y.0 - delta_y.0));
                handle.set_offset(desired);
                let next = handle.offset();
                (prev.x.0 - next.x.0).abs() > SCROLL_CONSUMED_EPS
                    || (prev.y.0 - next.y.0).abs() > SCROLL_CONSUMED_EPS
            } else {
                crate::elements::with_element_state(
                    &mut *cx.app,
                    window,
                    this.element,
                    crate::element::ScrollState::default,
                    |state| {
                        let prev = state.scroll_handle.offset();
                        let desired =
                            Point::new(Px(prev.x.0 - delta_x.0), Px(prev.y.0 - delta_y.0));
                        state.scroll_handle.set_offset(desired);
                        let next = state.scroll_handle.offset();
                        (prev.x.0 - next.x.0).abs() > SCROLL_CONSUMED_EPS
                            || (prev.y.0 - next.y.0).abs() > SCROLL_CONSUMED_EPS
                    },
                )
            };

            if consumed {
                if cx.captured != Some(cx.node) {
                    cx.capture_pointer(cx.node);
                    mark_touch_pan_captured(cx, window, this.element, *pointer_id);
                    clear_pressed_pressable_if_any(cx, window);
                }
                if let Some(handle) = props.scroll_handle.as_ref() {
                    super::invalidate_scroll_handle_bindings(
                        cx,
                        window,
                        handle.binding_key(),
                        Invalidation::HitTestOnly,
                    );
                }
                cx.invalidate_self(Invalidation::HitTestOnly);
                cx.request_redraw();
                cx.stop_propagation();
            }
        }
        Event::Pointer(fret_core::PointerEvent::Up {
            pointer_id,
            pointer_type,
            ..
        }) => {
            if *pointer_type == fret_core::PointerType::Touch {
                let captured_for_pan =
                    clear_touch_pan_tracking(cx, window, this.element, *pointer_id);
                if captured_for_pan && cx.captured == Some(cx.node) {
                    cx.release_pointer_capture();
                }
            }
        }
        Event::PointerCancel(e) => {
            if e.pointer_type == fret_core::PointerType::Touch {
                let captured_for_pan =
                    clear_touch_pan_tracking(cx, window, this.element, e.pointer_id);
                if captured_for_pan && cx.captured == Some(cx.node) {
                    cx.release_pointer_capture();
                }
            }
        }
        _ => {}
    }

    true
}