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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
use super::ElementHostWidget;
use crate::declarative::prelude::*;
use fret_runtime::DragHost;
use std::sync::Arc;

fn position_local(bounds: Rect, mapped: Point) -> Point {
    Point::new(
        fret_core::Px(mapped.x.0 - bounds.origin.x.0),
        fret_core::Px(mapped.y.0 - bounds.origin.y.0),
    )
}

#[derive(Debug, Default, Clone, Copy)]
struct PressablePressTracking {
    pointer_id: Option<fret_core::PointerId>,
    down_position: Option<Point>,
    down_hit_is_text_input: bool,
    down_hit_pressable_target: Option<crate::GlobalElementId>,
    down_hit_pressable_target_in_descendant_subtree: bool,
}

pub(super) fn handle_pressable<H: UiHost>(
    this: &mut ElementHostWidget,
    cx: &mut EventCx<'_, H>,
    window: AppWindowId,
    props: PressableProps,
    event: &Event,
) {
    if !props.enabled {
        return;
    }

    let pixels_per_point = cx
        .app
        .global::<fret_core::WindowMetricsService>()
        .and_then(|svc| svc.scale_factor(window))
        .unwrap_or(1.0);

    struct PressablePointerHookHost<'a, H: UiHost> {
        app: &'a mut H,
        window: AppWindowId,
        element: crate::GlobalElementId,
        node: NodeId,
        bounds: Rect,
        source_test_id: Option<Arc<str>>,
        input_ctx: &'a fret_runtime::InputContext,
        prevented_default_actions: &'a mut fret_runtime::DefaultActionSet,
        requested_focus_target: &'a mut Option<crate::GlobalElementId>,
        requested_capture: &'a mut Option<Option<NodeId>>,
        requested_cursor: &'a mut Option<fret_core::CursorIcon>,
        notify_requested: &'a mut bool,
        notify_requested_location: &'a mut Option<crate::widget::UiSourceLocation>,
        invalidations: &'a mut Vec<(NodeId, Invalidation)>,
    }

    impl<H: UiHost> action::UiActionHost for PressablePointerHookHost<'_, H> {
        fn models_mut(&mut self) -> &mut fret_runtime::ModelStore {
            self.app.models_mut()
        }

        fn push_effect(&mut self, effect: Effect) {
            match effect {
                Effect::SetTimer {
                    window: Some(window),
                    token,
                    ..
                } if window == self.window => {
                    crate::elements::record_timer_target(
                        &mut *self.app,
                        window,
                        token,
                        self.element,
                    );
                }
                Effect::CancelTimer { token } => {
                    crate::elements::clear_timer_target(&mut *self.app, self.window, token);
                }
                _ => {}
            }
            self.app.push_effect(effect);
        }

        fn request_redraw(&mut self, window: AppWindowId) {
            self.app.request_redraw(window);
        }

        fn next_timer_token(&mut self) -> fret_runtime::TimerToken {
            self.app.next_timer_token()
        }

        fn next_clipboard_token(&mut self) -> fret_runtime::ClipboardToken {
            self.app.next_clipboard_token()
        }

        fn next_share_sheet_token(&mut self) -> fret_runtime::ShareSheetToken {
            self.app.next_share_sheet_token()
        }

        fn record_transient_event(&mut self, cx: action::ActionCx, key: u64) {
            crate::elements::record_transient_event(&mut *self.app, cx.window, cx.target, key);
        }

        fn record_pending_command_dispatch_source(
            &mut self,
            cx: action::ActionCx,
            command: &fret_runtime::CommandId,
            reason: ActivateReason,
        ) {
            let kind = match reason {
                ActivateReason::Pointer => fret_runtime::CommandDispatchSourceKindV1::Pointer,
                ActivateReason::Keyboard => fret_runtime::CommandDispatchSourceKindV1::Keyboard,
            };
            let source = fret_runtime::CommandDispatchSourceV1 {
                kind,
                element: Some(cx.target.0),
                test_id: self.source_test_id.clone(),
            };
            self.app.with_global_mut(
                fret_runtime::WindowPendingCommandDispatchSourceService::default,
                |svc, app| {
                    svc.record(cx.window, app.tick_id(), command.clone(), source);
                },
            );
        }

        #[track_caller]
        fn notify(&mut self, _cx: action::ActionCx) {
            *self.notify_requested = true;
            if self.notify_requested_location.is_none() {
                let caller = std::panic::Location::caller();
                *self.notify_requested_location = Some(crate::widget::UiSourceLocation {
                    file: caller.file(),
                    line: caller.line(),
                    column: caller.column(),
                });
            }
        }
    }

    impl<H: UiHost> action::UiFocusActionHost for PressablePointerHookHost<'_, H> {
        fn request_focus(&mut self, target: crate::GlobalElementId) {
            *self.requested_focus_target = Some(target);
        }
    }

    impl<H: UiHost> action::UiPointerActionHost for PressablePointerHookHost<'_, H> {
        fn bounds(&self) -> Rect {
            self.bounds
        }

        fn capture_pointer(&mut self) {
            *self.requested_capture = Some(Some(self.node));
        }

        fn release_pointer_capture(&mut self) {
            *self.requested_capture = Some(None);
        }

        fn set_cursor_icon(&mut self, icon: fret_core::CursorIcon) {
            if !self.input_ctx.caps.ui.cursor_icons {
                return;
            }
            *self.requested_cursor = Some(icon);
        }

        fn prevent_default(&mut self, action: fret_runtime::DefaultAction) {
            self.prevented_default_actions.insert(action);
        }

        fn invalidate(&mut self, invalidation: Invalidation) {
            self.invalidations.push((self.node, invalidation));
        }
    }

    impl<H: UiHost> action::UiDragActionHost for PressablePointerHookHost<'_, H> {
        fn begin_drag_with_kind(
            &mut self,
            pointer_id: fret_core::PointerId,
            kind: fret_runtime::DragKindId,
            source_window: AppWindowId,
            start: Point,
        ) {
            DragHost::begin_drag_with_kind(
                &mut *self.app,
                pointer_id,
                kind,
                source_window,
                start,
                (),
            );
        }

        fn begin_cross_window_drag_with_kind(
            &mut self,
            pointer_id: fret_core::PointerId,
            kind: fret_runtime::DragKindId,
            source_window: AppWindowId,
            start: Point,
        ) {
            DragHost::begin_cross_window_drag_with_kind(
                &mut *self.app,
                pointer_id,
                kind,
                source_window,
                start,
                (),
            );
        }

        fn drag(&self, pointer_id: fret_core::PointerId) -> Option<&fret_runtime::DragSession> {
            DragHost::drag(&*self.app, pointer_id)
        }

        fn drag_mut(
            &mut self,
            pointer_id: fret_core::PointerId,
        ) -> Option<&mut fret_runtime::DragSession> {
            DragHost::drag_mut(&mut *self.app, pointer_id)
        }

        fn cancel_drag(&mut self, pointer_id: fret_core::PointerId) {
            DragHost::cancel_drag(&mut *self.app, pointer_id);
        }
    }

    struct PressableActionHookHost<'a, H: UiHost> {
        app: &'a mut H,
        window: AppWindowId,
        element: crate::GlobalElementId,
        source_test_id: Option<Arc<str>>,
        notify_requested: &'a mut bool,
        notify_requested_location: &'a mut Option<crate::widget::UiSourceLocation>,
    }

    impl<H: UiHost> action::UiActionHost for PressableActionHookHost<'_, H> {
        fn models_mut(&mut self) -> &mut fret_runtime::ModelStore {
            self.app.models_mut()
        }

        fn push_effect(&mut self, effect: Effect) {
            match effect {
                Effect::SetTimer {
                    window: Some(window),
                    token,
                    ..
                } if window == self.window => {
                    crate::elements::record_timer_target(
                        &mut *self.app,
                        window,
                        token,
                        self.element,
                    );
                }
                Effect::CancelTimer { token } => {
                    crate::elements::clear_timer_target(&mut *self.app, self.window, token);
                }
                _ => {}
            }
            self.app.push_effect(effect);
        }

        fn request_redraw(&mut self, window: AppWindowId) {
            self.app.request_redraw(window);
        }

        fn next_timer_token(&mut self) -> fret_runtime::TimerToken {
            self.app.next_timer_token()
        }

        fn next_clipboard_token(&mut self) -> fret_runtime::ClipboardToken {
            self.app.next_clipboard_token()
        }

        fn next_share_sheet_token(&mut self) -> fret_runtime::ShareSheetToken {
            self.app.next_share_sheet_token()
        }

        fn record_transient_event(&mut self, cx: action::ActionCx, key: u64) {
            crate::elements::record_transient_event(&mut *self.app, cx.window, cx.target, key);
        }

        fn record_pending_command_dispatch_source(
            &mut self,
            cx: action::ActionCx,
            command: &fret_runtime::CommandId,
            reason: ActivateReason,
        ) {
            let kind = match reason {
                ActivateReason::Pointer => fret_runtime::CommandDispatchSourceKindV1::Pointer,
                ActivateReason::Keyboard => fret_runtime::CommandDispatchSourceKindV1::Keyboard,
            };
            let source = fret_runtime::CommandDispatchSourceV1 {
                kind,
                element: Some(cx.target.0),
                test_id: self.source_test_id.clone(),
            };
            self.app.with_global_mut(
                fret_runtime::WindowPendingCommandDispatchSourceService::default,
                |svc, app| {
                    svc.record(cx.window, app.tick_id(), command.clone(), source);
                },
            );
        }

        fn record_pending_action_payload(
            &mut self,
            cx: action::ActionCx,
            action: &fret_runtime::ActionId,
            payload: Box<dyn std::any::Any + Send + Sync>,
        ) {
            self.app.with_global_mut(
                fret_runtime::WindowPendingActionPayloadService::default,
                |svc, app| {
                    svc.record(cx.window, app.tick_id(), action.clone(), payload);
                },
            );
        }

        #[track_caller]
        fn notify(&mut self, _cx: action::ActionCx) {
            *self.notify_requested = true;
            if self.notify_requested_location.is_none() {
                let caller = std::panic::Location::caller();
                *self.notify_requested_location = Some(crate::widget::UiSourceLocation {
                    file: caller.file(),
                    line: caller.line(),
                    column: caller.column(),
                });
            }
        }
    }

    match event {
        Event::Pointer(pe) => match pe {
            fret_core::PointerEvent::Move {
                pointer_id,
                position,
                buttons,
                modifiers,
                pointer_type,
            } => {
                cx.set_cursor_icon(CursorIcon::Pointer);

                // If we captured the pointer on down but never receive a matching pointer-up,
                // a subsequent move can report no pressed buttons while this pressable remains
                // globally pressed. Clear pressed + capture to avoid stuck Active visuals.
                if cx.captured == Some(cx.node)
                    && !buttons.left
                    && crate::elements::is_pressed_pressable(&mut *cx.app, window, this.element)
                {
                    cx.release_pointer_capture();
                    if let Some(prev_node) =
                        crate::elements::set_pressed_pressable(&mut *cx.app, window, None)
                    {
                        cx.invalidate(prev_node, Invalidation::Paint);
                    }
                    cx.invalidate_self(Invalidation::Paint);
                    cx.request_redraw();
                }

                let hook = crate::elements::with_element_state(
                    &mut *cx.app,
                    window,
                    this.element,
                    crate::action::PressableActionHooks::default,
                    |hooks| hooks.on_pointer_move.clone(),
                );

                if let Some(h) = hook {
                    let velocity_window = cx
                        .app
                        .global::<crate::pointer_motion::WindowPointerMotionService>()
                        .and_then(|svc| svc.velocity_window(window, *pointer_id));
                    let mv = action::PointerMoveCx {
                        pointer_id: *pointer_id,
                        position: *position,
                        position_local: position_local(cx.bounds, *position),
                        position_window: cx.event_window_position,
                        tick_id: cx.app.tick_id(),
                        pixels_per_point,
                        velocity_window,
                        buttons: *buttons,
                        modifiers: *modifiers,
                        pointer_type: *pointer_type,
                    };

                    let mut host = PressablePointerHookHost {
                        app: &mut *cx.app,
                        window,
                        element: this.element,
                        node: cx.node,
                        bounds: cx.bounds,
                        source_test_id: props.a11y.test_id.clone(),
                        input_ctx: &cx.input_ctx,
                        prevented_default_actions: cx.prevented_default_actions,
                        requested_focus_target: &mut cx.requested_focus_target,
                        requested_capture: &mut cx.requested_capture,
                        requested_cursor: &mut cx.requested_cursor,
                        notify_requested: &mut cx.notify_requested,
                        notify_requested_location: &mut cx.notify_requested_location,
                        invalidations: &mut cx.invalidations,
                    };

                    if h(
                        &mut host,
                        action::ActionCx {
                            window,
                            target: this.element,
                        },
                        mv,
                    ) {
                        cx.stop_propagation();
                    }
                }
            }
            fret_core::PointerEvent::Down {
                position,
                button,
                modifiers,
                pointer_type,
                click_count,
                pointer_id,
                ..
            } => {
                let hook = crate::elements::with_element_state(
                    &mut *cx.app,
                    window,
                    this.element,
                    crate::action::PressableActionHooks::default,
                    |hooks| hooks.on_pointer_down.clone(),
                );

                if let Some(h) = hook {
                    let down = action::PointerDownCx {
                        pointer_id: *pointer_id,
                        position: *position,
                        position_local: position_local(cx.bounds, *position),
                        position_window: cx.event_window_position,
                        tick_id: cx.app.tick_id(),
                        pixels_per_point,
                        button: *button,
                        modifiers: *modifiers,
                        click_count: *click_count,
                        pointer_type: *pointer_type,
                        hit_is_text_input: cx.pointer_hit_is_text_input,
                        hit_is_pressable: cx.pointer_hit_is_pressable,
                        hit_pressable_target: cx.pointer_hit_pressable_target,
                        hit_pressable_target_in_descendant_subtree: cx
                            .pointer_hit_pressable_target_in_descendant_subtree,
                    };

                    let mut host = PressablePointerHookHost {
                        app: &mut *cx.app,
                        window,
                        element: this.element,
                        node: cx.node,
                        bounds: cx.bounds,
                        source_test_id: props.a11y.test_id.clone(),
                        input_ctx: &cx.input_ctx,
                        prevented_default_actions: cx.prevented_default_actions,
                        requested_focus_target: &mut cx.requested_focus_target,
                        requested_capture: &mut cx.requested_capture,
                        requested_cursor: &mut cx.requested_cursor,
                        notify_requested: &mut cx.notify_requested,
                        notify_requested_location: &mut cx.notify_requested_location,
                        invalidations: &mut cx.invalidations,
                    };

                    match h(
                        &mut host,
                        action::ActionCx {
                            window,
                            target: this.element,
                        },
                        down,
                    ) {
                        action::PressablePointerDownResult::Continue => {}
                        action::PressablePointerDownResult::SkipDefault => {
                            return;
                        }
                        action::PressablePointerDownResult::SkipDefaultAndStopPropagation => {
                            cx.stop_propagation();
                            return;
                        }
                    }
                }

                if *button != MouseButton::Left {
                    return;
                }
                // Defer focus until pointer-up activation:
                // focusing on pointer-down can trigger scroll-to-focus policies that move content
                // under a stationary cursor, causing the subsequent pointer-up to miss and cancel
                // activation.
                cx.prevent_default(fret_runtime::DefaultAction::FocusOnPointerDown);
                cx.capture_pointer(cx.node);
                if let Some(prev_node) = crate::elements::set_pressed_pressable_with_node(
                    &mut *cx.app,
                    window,
                    Some((this.element, cx.node)),
                ) {
                    cx.invalidate(prev_node, Invalidation::Paint);
                }
                crate::elements::with_element_state(
                    &mut *cx.app,
                    window,
                    this.element,
                    PressablePressTracking::default,
                    |st| {
                        st.pointer_id = Some(*pointer_id);
                        st.down_position = Some(*position);
                        st.down_hit_is_text_input = cx.pointer_hit_is_text_input;
                        st.down_hit_pressable_target = cx.pointer_hit_pressable_target;
                        st.down_hit_pressable_target_in_descendant_subtree =
                            cx.pointer_hit_pressable_target_in_descendant_subtree;
                    },
                );
                cx.invalidate_self(Invalidation::Paint);
                cx.request_redraw();
                cx.stop_propagation();
            }
            fret_core::PointerEvent::Up {
                pointer_id,
                position,
                button,
                modifiers,
                is_click,
                click_count,
                pointer_type,
            } => {
                let hook = crate::elements::with_element_state(
                    &mut *cx.app,
                    window,
                    this.element,
                    crate::action::PressableActionHooks::default,
                    |hooks| hooks.on_pointer_up.clone(),
                );

                let mut skip_activate = false;
                if let Some(h) = hook.clone() {
                    let up = action::PointerUpCx {
                        pointer_id: *pointer_id,
                        position: *position,
                        position_local: position_local(cx.bounds, *position),
                        position_window: cx.event_window_position,
                        tick_id: cx.app.tick_id(),
                        pixels_per_point,
                        velocity_window: cx
                            .app
                            .global::<crate::pointer_motion::WindowPointerMotionService>()
                            .and_then(|svc| svc.velocity_window(window, *pointer_id)),
                        button: *button,
                        modifiers: *modifiers,
                        is_click: *is_click,
                        click_count: *click_count,
                        pointer_type: *pointer_type,
                        down_hit_pressable_target: crate::elements::with_element_state(
                            &mut *cx.app,
                            window,
                            this.element,
                            PressablePressTracking::default,
                            |st| st.down_hit_pressable_target,
                        ),
                        down_hit_pressable_target_in_descendant_subtree:
                            crate::elements::with_element_state(
                                &mut *cx.app,
                                window,
                                this.element,
                                PressablePressTracking::default,
                                |st| st.down_hit_pressable_target_in_descendant_subtree,
                            ),
                    };

                    let mut host = PressablePointerHookHost {
                        app: &mut *cx.app,
                        window,
                        element: this.element,
                        node: cx.node,
                        bounds: cx.bounds,
                        source_test_id: props.a11y.test_id.clone(),
                        input_ctx: &cx.input_ctx,
                        prevented_default_actions: cx.prevented_default_actions,
                        requested_focus_target: &mut cx.requested_focus_target,
                        requested_capture: &mut cx.requested_capture,
                        requested_cursor: &mut cx.requested_cursor,
                        notify_requested: &mut cx.notify_requested,
                        notify_requested_location: &mut cx.notify_requested_location,
                        invalidations: &mut cx.invalidations,
                    };

                    skip_activate = matches!(
                        h(
                            &mut host,
                            action::ActionCx {
                                window,
                                target: this.element,
                            },
                            up,
                        ),
                        action::PressablePointerUpResult::SkipActivate
                    );
                }

                if *button != MouseButton::Left {
                    if hook.is_some() {
                        cx.request_redraw();
                    }
                    return;
                }

                let pressed =
                    crate::elements::is_pressed_pressable(&mut *cx.app, window, this.element);

                let (down_pointer_id, down_position, down_hit_is_text_input) =
                    crate::elements::with_element_state(
                        &mut *cx.app,
                        window,
                        this.element,
                        PressablePressTracking::default,
                        |st| {
                            let out = (st.pointer_id, st.down_position, st.down_hit_is_text_input);
                            st.pointer_id = None;
                            st.down_position = None;
                            st.down_hit_is_text_input = false;
                            out
                        },
                    );

                cx.release_pointer_capture();
                if let Some(prev_node) =
                    crate::elements::set_pressed_pressable(&mut *cx.app, window, None)
                {
                    cx.invalidate(prev_node, Invalidation::Paint);
                }
                cx.invalidate_self(Invalidation::Paint);
                cx.request_redraw();

                // Activate based on the pointer-up position, not the cached hovered state. This
                // keeps click-through outside-press dismissal semantics (ADR 0069) robust even
                // when overlay policies update hover state in an observer pass.
                //
                // If the pointer did not move between down/up, treat the interaction as "hovered"
                // even if the node moved (e.g. scroll-to-focus policies).
                let moved = if down_pointer_id == Some(*pointer_id)
                    && let Some(prev) = down_position
                {
                    let dx = position.x.0 - prev.x.0;
                    let dy = position.y.0 - prev.y.0;
                    (dx * dx + dy * dy).sqrt()
                } else {
                    f32::INFINITY
                };
                let hovered = cx.bounds.contains(*position) || moved <= 2.0;

                let is_touch = *pointer_type == fret_core::PointerType::Touch;
                if pressed && hovered && (!is_touch || *is_click) && !down_hit_is_text_input {
                    // Pointer interactions should move focus to the pressable even when
                    // activation is handled by component-owned pointer hooks. When the press
                    // started inside a text-input descendant, keep focus on that descendant so
                    // text input / IME routing is not stolen by an ancestor pressable.
                    //
                    // Note: `PressableProps.focusable` models the Tab-order "focus traversal stop"
                    // (Radix roving-focus `tabIndex=0` vs `-1`), not whether pointer clicks can
                    // focus the node.
                    cx.request_focus(cx.node);
                }
                if pressed && hovered && (!is_touch || *is_click) && !skip_activate {
                    let hook = crate::elements::with_element_state(
                        &mut *cx.app,
                        window,
                        this.element,
                        crate::action::PressableActionHooks::default,
                        |hooks| hooks.on_activate.clone(),
                    );

                    if let Some(h) = hook {
                        let mut host = PressableActionHookHost {
                            app: &mut *cx.app,
                            window,
                            element: this.element,
                            source_test_id: props.a11y.test_id.clone(),
                            notify_requested: &mut cx.notify_requested,
                            notify_requested_location: &mut cx.notify_requested_location,
                        };
                        h(
                            &mut host,
                            action::ActionCx {
                                window,
                                target: this.element,
                            },
                            ActivateReason::Pointer,
                        );
                    }
                }
                cx.release_pointer_capture();
                cx.invalidate_self(Invalidation::Paint);
                cx.request_redraw();
                cx.stop_propagation();
            }
            _ => {}
        },
        Event::PointerCancel(_) => {
            if !crate::elements::is_pressed_pressable(&mut *cx.app, window, this.element) {
                return;
            }
            crate::elements::with_element_state(
                &mut *cx.app,
                window,
                this.element,
                PressablePressTracking::default,
                |st| {
                    st.pointer_id = None;
                    st.down_position = None;
                },
            );
            if cx.captured == Some(cx.node) {
                cx.release_pointer_capture();
            }
            if let Some(prev_node) =
                crate::elements::set_pressed_pressable(&mut *cx.app, window, None)
            {
                cx.invalidate(prev_node, Invalidation::Paint);
            }
            cx.invalidate_self(Invalidation::Paint);
            cx.request_redraw();
        }
        Event::KeyDown { key, repeat, .. } => {
            if *repeat {
                return;
            }
            if cx.focus != Some(cx.node) {
                return;
            }
            if !props.key_activation.allows(*key) {
                return;
            }
            if let Some(prev_node) = crate::elements::set_pressed_pressable_with_node(
                &mut *cx.app,
                window,
                Some((this.element, cx.node)),
            ) {
                cx.invalidate(prev_node, Invalidation::Paint);
            }
            cx.invalidate_self(Invalidation::Paint);
            cx.request_redraw();
            cx.stop_propagation();
        }
        Event::KeyUp { key, .. } => {
            if cx.focus != Some(cx.node) {
                return;
            }
            if !props.key_activation.allows(*key) {
                return;
            }
            let pressed = crate::elements::is_pressed_pressable(&mut *cx.app, window, this.element);
            if !pressed {
                return;
            }
            if let Some(prev_node) =
                crate::elements::set_pressed_pressable(&mut *cx.app, window, None)
            {
                cx.invalidate(prev_node, Invalidation::Paint);
            }
            cx.invalidate_self(Invalidation::Paint);
            cx.request_redraw();
            let hook = crate::elements::with_element_state(
                &mut *cx.app,
                window,
                this.element,
                crate::action::PressableActionHooks::default,
                |hooks| hooks.on_activate.clone(),
            );

            if let Some(h) = hook {
                let mut host = PressableActionHookHost {
                    app: &mut *cx.app,
                    window,
                    element: this.element,
                    source_test_id: props.a11y.test_id.clone(),
                    notify_requested: &mut cx.notify_requested,
                    notify_requested_location: &mut cx.notify_requested_location,
                };
                h(
                    &mut host,
                    action::ActionCx {
                        window,
                        target: this.element,
                    },
                    ActivateReason::Keyboard,
                );
            }
            cx.invalidate_self(Invalidation::Paint);
            cx.request_redraw();
            cx.stop_propagation();
        }
        Event::ClipboardWriteCompleted { token, outcome } => {
            let hook = crate::elements::with_element_state(
                &mut *cx.app,
                window,
                this.element,
                crate::action::PressableActionHooks::default,
                |hooks| hooks.on_clipboard_write_completed.clone(),
            );

            let Some(hook) = hook else {
                return;
            };

            let mut host = PressableActionHookHost {
                app: &mut *cx.app,
                window,
                element: this.element,
                source_test_id: props.a11y.test_id.clone(),
                notify_requested: &mut cx.notify_requested,
                notify_requested_location: &mut cx.notify_requested_location,
            };
            if hook(
                &mut host,
                action::ActionCx {
                    window,
                    target: this.element,
                },
                *token,
                outcome,
            ) {
                cx.stop_propagation();
            }
        }
        _ => {}
    };
}