frust-core 0.6.3

Frust's declarative View API, retained Widget tree, box-constraint layout and rebuild/layout/paint pass.
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
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
//! Focus, IME surface and focus-session identity tests for RenderRoot.

use super::*;
use crate::event::EditingState;

// --- Focus / IME surface fixtures: a root editable that focuses + publishes
//     an IME surface on a `Down` in its left half, and blurs (no focus) on a
//     `Down` in its right half. ---

struct ImeWidget;

impl crate::widget::Widget for ImeWidget {
    fn layout(&mut self, _ctx: &mut LayoutCtx, bc: &BoxConstraints) -> Size {
        bc.constrain(Size::new(100.0, 100.0))
    }
    fn paint(&mut self, _ctx: &mut PaintCtx, _scene: &mut dyn PaintScene) {}
    fn event(&mut self, ctx: &mut crate::event::EventCtx, event: &InputEvent) -> EventResult {
        if let InputEvent::Pointer(p) = event {
            if p.phase == PointerPhase::Down && p.position.x < 50.0 {
                ctx.request_focus();
                ctx.publish_ime_state(ImeState {
                    active: true,
                    editing: EditingState {
                        text: "abc".to_string(),
                        selection_base: 3,
                        selection_extent: 3,
                        composing_base: -1,
                        composing_extent: -1,
                    },
                    caret: Some(kurbo::Rect::new(0.0, 0.0, 1.0, 12.0)),
                    content_type: Default::default(),
                    suppress_soft_keyboard: false,
                });
                return EventResult::Handled;
            }
            if p.phase == PointerPhase::Down {
                // Right-half tap: a blur (no focus request).
                return EventResult::Handled;
            }
        }
        EventResult::Ignored
    }
}

struct ImeView;

impl View<ClickState> for ImeView {
    type Element = ImeWidget;
    fn build(&self, _ctx: &mut BuildCtx<'_>) -> ImeWidget {
        ImeWidget
    }
    fn rebuild(
        &self,
        _prev: &Self,
        _element: &mut ImeWidget,
        _ctx: &mut BuildCtx<'_>,
    ) -> ChangeFlags {
        ChangeFlags::NONE
    }
}

fn ime_logic(_state: &mut ClickState) -> ImeView {
    ImeView
}

#[test]
fn focus_and_ime_state_surface_and_clear_on_blur() {
    let mut root: RenderRoot<ClickState, ImeView> = RenderRoot::new();
    let mut state = ClickState::default();
    root.rebuild(&mut ime_logic, &mut state);
    root.layout(Size::new(100.0, 100.0));

    // No focus / no IME surface initially.
    assert!(!root.is_focus_active());
    assert!(root.ime_state().is_none());

    // A left-half Down focuses the widget and publishes an IME surface.
    root.event(&mut state, &pointer(PointerPhase::Down, 10.0, 10.0));
    assert!(root.is_focus_active());
    let ime = root
        .ime_state()
        .expect("focused widget published an IME surface");
    assert!(ime.active);
    assert_eq!(ime.editing.text, "abc");

    // The published surface survives a rebuild (shell can query it between
    // frames).
    root.rebuild(&mut ime_logic, &mut state);
    assert!(root.ime_state().is_some());

    // A right-half Down is a blur: focus and the IME surface both clear.
    root.event(&mut state, &pointer(PointerPhase::Down, 80.0, 10.0));
    assert!(!root.is_focus_active());
    assert!(root.ime_state().is_none());
}

// --- Session release: the focus session must die with its owner ---
//
// A session can end without input reaching the root: a widget publishes an INACTIVE
// IME surface, or a reconciler unmounts the focused pod (raising `mark_focus_orphaned`).
// Both must perform the same full release a blur does.

/// The navigator's cleared surface, spelled out here so the fixture below
/// publishes exactly the shape `nav::navigator::cleared_ime_state` does
/// (`frust-core` cannot name it — `frust-widgets` sits above this crate).
fn cleared_surface() -> ImeState {
    ImeState {
        active: false,
        editing: EditingState {
            text: String::new(),
            selection_base: -1,
            selection_extent: -1,
            composing_base: -1,
            composing_extent: -1,
        },
        caret: None,
        content_type: Default::default(),
        suppress_soft_keyboard: false,
    }
}

/// A focused editable that publishes its active surface from paint (like a
/// real field), and — once `clear` is raised — publishes the *inactive*
/// surface from paint instead: the container-after-a-pop shape.
struct PopImeWidget {
    clear: Rc<Cell<bool>>,
}

impl crate::widget::Widget for PopImeWidget {
    fn layout(&mut self, _ctx: &mut LayoutCtx, bc: &BoxConstraints) -> Size {
        bc.constrain(Size::new(100.0, 100.0))
    }
    fn paint(&mut self, ctx: &mut PaintCtx, _scene: &mut dyn PaintScene) {
        if self.clear.get() {
            ctx.publish_ime_state(cleared_surface());
        } else if ctx.has_focus() {
            ctx.publish_ime_state(PaintImeWidget::surface("abc"));
        }
    }
    fn event(&mut self, ctx: &mut crate::event::EventCtx, event: &InputEvent) -> EventResult {
        match event {
            InputEvent::Pointer(p) if p.phase == PointerPhase::Down => {
                ctx.request_focus();
                ctx.publish_ime_state(PaintImeWidget::surface("abc"));
                EventResult::Handled
            }
            _ => EventResult::Ignored,
        }
    }
}

struct PopImeView {
    clear: Rc<Cell<bool>>,
}

impl View<ClickState> for PopImeView {
    type Element = PopImeWidget;
    fn build(&self, _ctx: &mut BuildCtx<'_>) -> PopImeWidget {
        PopImeWidget {
            clear: self.clear.clone(),
        }
    }
    fn rebuild(
        &self,
        _prev: &Self,
        _element: &mut PopImeWidget,
        _ctx: &mut BuildCtx<'_>,
    ) -> ChangeFlags {
        ChangeFlags::NONE
    }
}

#[test]
fn inactive_paint_publish_releases_the_whole_session() {
    // (a) The pop shape. Before this, the paint take stored `Some(inactive)`
    // and never touched `focus_active`, so the session outlived the page.
    let clear = Rc::new(Cell::new(false));
    let mut build = {
        let clear = clear.clone();
        move |_state: &mut ClickState| PopImeView {
            clear: clear.clone(),
        }
    };
    let mut root: RenderRoot<ClickState, PopImeView> = RenderRoot::new();
    let mut state = ClickState::default();
    root.rebuild(&mut build, &mut state);
    root.layout(Size::new(100.0, 100.0));

    let mut scene = RecordingScene::default();
    root.event(&mut state, &pointer(PointerPhase::Down, 10.0, 10.0));
    root.paint(&mut scene, FrameTime::ZERO);
    assert!(root.is_focus_active());
    assert!(root.ime_state().is_some_and(|s| s.active));
    let focused = root.focus_ime_generation();

    // The "pop": the next paint publishes the cleared surface.
    clear.set(true);
    root.paint(&mut scene, FrameTime::ZERO);
    assert!(
        !root.is_focus_active(),
        "an inactive publish ends the session, not just the surface"
    );
    assert_eq!(
        root.ime_state(),
        None,
        "the surface is dropped, never parked at Some(inactive)"
    );
    assert_eq!(
        root.focus_ime_generation(),
        focused.wrapping_add(1),
        "one release is exactly one edge"
    );

    // The widget keeps publishing the cleared surface every frame; the paint take's
    // `focus_active` guard makes each a no-op, so the session neither resurrects nor spins.
    let released = root.focus_ime_generation();
    for _ in 0..30 {
        root.paint(&mut scene, FrameTime::ZERO);
    }
    assert!(!root.is_focus_active());
    assert!(root.ime_state().is_none());
    assert_eq!(
        root.focus_ime_generation(),
        released,
        "an inactive publish against an already-released root is inert"
    );
}

/// A view whose rebuild raises the generic-unmount orphan mark on demand —
/// standing in for `frust-widgets`' reconcilers, which clear a focused
/// `ChildPod` mid-diff and raise exactly this flag (this crate has no
/// multi-child container of its own to diff).
struct UnmountView {
    orphan: Rc<Cell<bool>>,
}

impl View<ClickState> for UnmountView {
    type Element = ImeWidget;
    fn build(&self, _ctx: &mut BuildCtx<'_>) -> ImeWidget {
        ImeWidget
    }
    fn rebuild(
        &self,
        _prev: &Self,
        _element: &mut ImeWidget,
        _ctx: &mut BuildCtx<'_>,
    ) -> ChangeFlags {
        if self.orphan.get() {
            crate::event::mark_focus_orphaned();
            return ChangeFlags::LAYOUT | ChangeFlags::PAINT;
        }
        ChangeFlags::NONE
    }
}

#[test]
fn generic_unmount_orphan_releases_the_whole_session() {
    // (b) The child-list-diff shape: no publish, no event; the focused widget
    // just stops existing, so only the reconciler's mark can release it.
    let _ = crate::event::take_focus_orphaned();
    let orphan = Rc::new(Cell::new(false));
    let mut build = {
        let orphan = orphan.clone();
        move |_state: &mut ClickState| UnmountView {
            orphan: orphan.clone(),
        }
    };
    let mut root: RenderRoot<ClickState, UnmountView> = RenderRoot::new();
    let mut state = ClickState::default();
    root.rebuild(&mut build, &mut state);
    root.layout(Size::new(100.0, 100.0));

    root.event(&mut state, &pointer(PointerPhase::Down, 10.0, 10.0));
    assert!(root.is_focus_active());
    assert!(root.ime_state().is_some());
    let focused = root.focus_ime_generation();

    // The unmount rebuild.
    orphan.set(true);
    root.rebuild(&mut build, &mut state);
    assert!(
        !root.is_focus_active(),
        "the root's focus mirror does not outlive the widget it mirrors"
    );
    assert!(root.ime_state().is_none());
    assert_eq!(
        root.focus_ime_generation(),
        focused.wrapping_add(1),
        "one orphaned focus path is exactly one edge"
    );

    // The mark was drained, so an ordinary rebuild afterwards is inert...
    let released = root.focus_ime_generation();
    orphan.set(false);
    root.rebuild(&mut build, &mut state);
    assert_eq!(root.focus_ime_generation(), released);

    // ...and re-marking against an already-released root fires no edge
    // either (a stale `focused` flag torn down later must not spin it).
    orphan.set(true);
    root.rebuild(&mut build, &mut state);
    assert_eq!(root.focus_ime_generation(), released);
    assert!(!root.is_focus_active());
}

// --- The focus/IME EDGE generation ---
//
// `focus_ime_generation` must move on EVERY real transition and on NO same-value write,
// or a focused screen forces a frame every vsync.

/// A widget that focuses on `Down`, releases focus on any `Key`, and — the
/// point of the fixture — re-publishes an IME surface from its **paint**
/// pass on every frame, reading the text from a shared cell so a test can
/// make a republish genuinely change (or genuinely not).
struct PaintImeWidget {
    published: Rc<RefCell<String>>,
}

impl PaintImeWidget {
    fn surface(text: &str) -> ImeState {
        ImeState {
            active: true,
            editing: EditingState {
                text: text.to_string(),
                selection_base: 0,
                selection_extent: 0,
                composing_base: -1,
                composing_extent: -1,
            },
            caret: Some(kurbo::Rect::new(0.0, 0.0, 1.0, 12.0)),
            content_type: Default::default(),
            suppress_soft_keyboard: false,
        }
    }
}

impl crate::widget::Widget for PaintImeWidget {
    fn layout(&mut self, _ctx: &mut LayoutCtx, bc: &BoxConstraints) -> Size {
        bc.constrain(Size::new(100.0, 100.0))
    }
    fn paint(&mut self, ctx: &mut PaintCtx, _scene: &mut dyn PaintScene) {
        ctx.publish_ime_state(Self::surface(&self.published.borrow()));
    }
    fn event(&mut self, ctx: &mut crate::event::EventCtx, event: &InputEvent) -> EventResult {
        match event {
            InputEvent::Pointer(p) if p.phase == PointerPhase::Down => {
                ctx.request_focus();
                EventResult::Handled
            }
            InputEvent::Key(_) => {
                ctx.release_focus();
                EventResult::Handled
            }
            _ => EventResult::Ignored,
        }
    }
}

struct PaintImeView {
    published: Rc<RefCell<String>>,
}

impl View<ClickState> for PaintImeView {
    type Element = PaintImeWidget;
    fn build(&self, _ctx: &mut BuildCtx<'_>) -> PaintImeWidget {
        PaintImeWidget {
            published: self.published.clone(),
        }
    }
    fn rebuild(
        &self,
        _prev: &Self,
        _element: &mut PaintImeWidget,
        _ctx: &mut BuildCtx<'_>,
    ) -> ChangeFlags {
        ChangeFlags::NONE
    }
}

fn key_event() -> InputEvent {
    InputEvent::Key(crate::event::KeyEvent {
        key: crate::event::Key::Named(crate::event::NamedKey::Enter),
        modifiers: crate::event::Modifiers::default(),
        repeat: false,
    })
}

#[test]
fn focus_ime_generation_moves_on_every_pointer_transition_only() {
    let mut root: RenderRoot<ClickState, ImeView> = RenderRoot::new();
    let mut state = ClickState::default();
    root.rebuild(&mut ime_logic, &mut state);
    root.layout(Size::new(100.0, 100.0));

    // Idle: a rebuild/layout touches neither focus nor the IME surface.
    let idle = root.focus_ime_generation();
    root.rebuild(&mut ime_logic, &mut state);
    assert_eq!(
        root.focus_ime_generation(),
        idle,
        "a rebuild is not an edge"
    );

    // Focus gained + IME surface published (one transition for a shell,
    // however many field writes it took).
    root.event(&mut state, &pointer(PointerPhase::Down, 10.0, 10.0));
    let focused = root.focus_ime_generation();
    assert_ne!(
        focused, idle,
        "focus + IME publish must move the generation"
    );

    // The same tap on the already-focused widget publishing an identical surface:
    // no state moved, so no edge.
    root.event(&mut state, &pointer(PointerPhase::Down, 10.0, 10.0));
    assert_eq!(
        root.focus_ime_generation(),
        focused,
        "a same-value focus/IME write must not spin the edge"
    );

    // Blur: focus cleared and the surface dropped — a real transition.
    root.event(&mut state, &pointer(PointerPhase::Down, 80.0, 10.0));
    let blurred = root.focus_ime_generation();
    assert_ne!(blurred, focused, "a blur must move the generation");

    // Blur while already blurred (a tap on inert chrome — the commonest
    // event there is) writes `false`/`None` back over `false`/`None`.
    root.event(&mut state, &pointer(PointerPhase::Down, 80.0, 20.0));
    assert_eq!(
        root.focus_ime_generation(),
        blurred,
        "blurring an already-blurred root must not move the generation"
    );
}

#[test]
fn focus_ime_generation_ignores_an_unchanged_paint_republish() {
    // Paint re-publishes the IME surface every frame; if that write moved the
    // generation, the edge would fire per frame for the whole focus session.
    let published = Rc::new(RefCell::new("abc".to_string()));
    let mut build = {
        let published = published.clone();
        move |_state: &mut ClickState| PaintImeView {
            published: published.clone(),
        }
    };
    let mut root: RenderRoot<ClickState, PaintImeView> = RenderRoot::new();
    let mut state = ClickState::default();
    root.rebuild(&mut build, &mut state);
    root.layout(Size::new(100.0, 100.0));

    // Focus the field, then let it paint: the first paint publishes.
    root.event(&mut state, &pointer(PointerPhase::Down, 10.0, 10.0));
    let mut scene = RecordingScene::default();
    root.paint(&mut scene, FrameTime::ZERO);
    let steady = root.focus_ime_generation();
    assert_eq!(
        root.ime_state(),
        Some(PaintImeWidget::surface("abc")),
        "the paint pass published the focused widget's surface"
    );

    // 120 further frames of the same focused, unchanged field: the caret
    // blinks, nothing else moves. Not one edge.
    for _ in 0..120 {
        root.paint(&mut scene, FrameTime::ZERO);
    }
    assert_eq!(
        root.focus_ime_generation(),
        steady,
        "an unchanged paint republish must never move the generation"
    );

    // A real change (the app applied a controlled edit) publishes a
    // different surface: exactly one edge, then quiet again.
    *published.borrow_mut() = "abcd".to_string();
    root.paint(&mut scene, FrameTime::ZERO);
    let edited = root.focus_ime_generation();
    assert_ne!(edited, steady, "a changed republish IS an edge");
    for _ in 0..10 {
        root.paint(&mut scene, FrameTime::ZERO);
    }
    assert_eq!(
        root.focus_ime_generation(),
        edited,
        "the session goes quiet again at the new value"
    );
}

#[test]
fn focus_ime_generation_moves_on_a_focus_release_only_once() {
    // The Key/Ime/Scroll arm of the root focus path: a dispatch that
    // RELEASES focus clears both the flag and the published surface.
    let published = Rc::new(RefCell::new("abc".to_string()));
    let mut build = {
        let published = published.clone();
        move |_state: &mut ClickState| PaintImeView {
            published: published.clone(),
        }
    };
    let mut root: RenderRoot<ClickState, PaintImeView> = RenderRoot::new();
    let mut state = ClickState::default();
    root.rebuild(&mut build, &mut state);
    root.layout(Size::new(100.0, 100.0));
    root.event(&mut state, &pointer(PointerPhase::Down, 10.0, 10.0));
    let mut scene = RecordingScene::default();
    root.paint(&mut scene, FrameTime::ZERO);
    let focused = root.focus_ime_generation();
    assert!(root.is_focus_active());

    // A key that releases focus: one edge.
    root.event(&mut state, &key_event());
    let released = root.focus_ime_generation();
    assert!(!root.is_focus_active());
    assert!(root.ime_state().is_none());
    assert_ne!(
        released, focused,
        "a focus release must move the generation"
    );

    // A second release against an already-released root: no edge (paint only
    // accepts a publish while focus is active).
    root.event(&mut state, &key_event());
    root.paint(&mut scene, FrameTime::ZERO);
    assert_eq!(
        root.focus_ime_generation(),
        released,
        "releasing an already-released focus must not move the generation"
    );
}

// --- The focus session's IDENTITY, beside the edge generation ---
//
// `focus_epoch` answers "is this still the session that asked?", which a counter over
// the published surface's value cannot: two fields publish equal surfaces, and a field
// may take focus and publish nothing. Each test asserts what both counters did.

/// The text both fields publish from a press, so neither can be told from
/// the other by the published value alone.
const SHARED_FIELD_TEXT: &str = "shared";

/// One field of the two-field fixture.
///
/// A press takes the focus session and, only if `publishes`, publishes the shared
/// surface (a text input pressed inside already-selected text claims focus and
/// publishes nothing). An `Ime` edit republishes the field's own `name`, which
/// proves which field a focus-routed event reached.
struct SessionField {
    name: &'static str,
    publishes: bool,
}

impl SessionField {
    /// The surface a field publishes; it carries nothing that tells fields apart, so
    /// two fields with the same text and caret publish equal values.
    fn surface(text: &str) -> ImeState {
        ImeState {
            active: true,
            editing: EditingState {
                text: text.to_string(),
                selection_base: 0,
                selection_extent: 0,
                composing_base: -1,
                composing_extent: -1,
            },
            caret: Some(kurbo::Rect::new(0.0, 0.0, 1.0, 12.0)),
            content_type: Default::default(),
            suppress_soft_keyboard: false,
        }
    }
}

impl crate::widget::Widget for SessionField {
    fn layout(&mut self, _ctx: &mut LayoutCtx, bc: &BoxConstraints) -> Size {
        bc.constrain(Size::new(100.0, 20.0))
    }
    fn paint(&mut self, _ctx: &mut PaintCtx, _scene: &mut dyn PaintScene) {}
    fn event(&mut self, ctx: &mut EventCtx, event: &InputEvent) -> EventResult {
        match event {
            InputEvent::Pointer(p) if p.phase == PointerPhase::Down => {
                ctx.request_focus();
                if self.publishes {
                    ctx.publish_ime_state(Self::surface(SHARED_FIELD_TEXT));
                }
                EventResult::Handled
            }
            InputEvent::Ime(_) => {
                ctx.publish_ime_state(Self::surface(self.name));
                EventResult::Handled
            }
            _ => EventResult::Ignored,
        }
    }
}

/// The fixture's root: two stacked fields, with a pointer event hit-tested
/// to the one under it and a focus-routed event forwarded down the recorded
/// focus path without a hit test — the routing every real container does.
struct TwoFields {
    top: crate::widget::ChildPod,
    bottom: crate::widget::ChildPod,
}

impl crate::widget::Widget for TwoFields {
    fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints) -> Size {
        self.top.layout_child(ctx, bc);
        self.top.set_origin(Point::new(0.0, 0.0));
        self.bottom.layout_child(ctx, bc);
        self.bottom.set_origin(Point::new(0.0, 50.0));
        bc.max()
    }
    fn paint(&mut self, ctx: &mut PaintCtx, scene: &mut dyn PaintScene) {
        self.top.paint_child(ctx, scene);
        self.bottom.paint_child(ctx, scene);
    }
    fn event(&mut self, ctx: &mut EventCtx, event: &InputEvent) -> EventResult {
        if matches!(event, InputEvent::Pointer(_)) {
            let pos = event.position();
            if self.top.contains(pos) {
                return self.top.event_child(ctx, event);
            }
            if self.bottom.contains(pos) {
                return self.bottom.event_child(ctx, event);
            }
            return EventResult::Ignored;
        }
        if self.top.holds_live_focus() {
            return self.top.event_child(ctx, event);
        }
        if self.bottom.holds_live_focus() {
            return self.bottom.event_child(ctx, event);
        }
        EventResult::Ignored
    }
    fn semantics(&self, ctx: &mut SemanticsCtx) {
        self.top.semantics_child(ctx);
        self.bottom.semantics_child(ctx);
    }
}

struct TwoFieldsView {
    bottom_publishes: bool,
}

impl View<ClickState> for TwoFieldsView {
    type Element = TwoFields;
    fn build(&self, _ctx: &mut BuildCtx<'_>) -> TwoFields {
        TwoFields {
            top: crate::widget::ChildPod::new(Box::new(SessionField {
                name: "top",
                publishes: true,
            })),
            bottom: crate::widget::ChildPod::new(Box::new(SessionField {
                name: "bottom",
                publishes: self.bottom_publishes,
            })),
        }
    }
    fn rebuild(
        &self,
        _prev: &Self,
        _element: &mut TwoFields,
        _ctx: &mut BuildCtx<'_>,
    ) -> ChangeFlags {
        ChangeFlags::NONE
    }
}

/// An edit pushed down the focus path by the platform IME: it claims no
/// focus, so only the field already holding the session sees it — which is
/// what makes the surface it republishes name that field.
fn edit_event() -> InputEvent {
    InputEvent::Ime(crate::event::ImeEvent::ApplyEditingState(
        SessionField::surface(SHARED_FIELD_TEXT).editing,
    ))
}

/// Mount the two-field fixture and press the top field, returning the root
/// with a live session on it.
fn two_fields_focused(
    bottom_publishes: bool,
) -> (RenderRoot<ClickState, TwoFieldsView>, ClickState) {
    let mut root: RenderRoot<ClickState, TwoFieldsView> = RenderRoot::new();
    let mut state = ClickState::default();
    root.rebuild(&mut |_| TwoFieldsView { bottom_publishes }, &mut state);
    root.layout(Size::new(200.0, 200.0));
    root.event(&mut state, &pointer(PointerPhase::Down, 10.0, 10.0));
    assert!(root.is_focus_active(), "the top field opened a session");
    (root, state)
}

#[test]
fn focus_epoch_moves_when_focus_crosses_two_fields_publishing_alike() {
    let (mut root, mut state) = two_fields_focused(true);
    let first_session = root.focus_epoch();
    let steady_edge = root.focus_ime_generation();
    assert_eq!(
        root.ime_state(),
        Some(SessionField::surface(SHARED_FIELD_TEXT))
    );

    // Press the bottom field: focus moves, but nothing observable about the
    // published surface does (it compares equal to the first field's).
    root.event(&mut state, &pointer(PointerPhase::Down, 10.0, 60.0));
    assert!(root.is_focus_active());
    assert_eq!(
        root.focus_ime_generation(),
        steady_edge,
        "the edge generation is blind to this move, which is why a caller \
         asking 'is this the same session?' must not be built on it"
    );
    assert_ne!(
        root.focus_epoch(),
        first_session,
        "the session identity must move when focus crosses to another field"
    );

    // Not merely "some counter moved": the focus PATH is the bottom
    // field's now, which a focus-routed event proves by reaching it.
    root.event(&mut state, &edit_event());
    assert_eq!(
        root.ime_state(),
        Some(SessionField::surface("bottom")),
        "the second field is the one holding the session"
    );
}

#[test]
fn focus_epoch_moves_when_the_field_taking_focus_publishes_nothing() {
    let (mut root, mut state) = two_fields_focused(false);
    let first_session = root.focus_epoch();
    let steady_edge = root.focus_ime_generation();

    // The bottom field takes the session and publishes nothing: the surface of the
    // field the user left is dropped, and the edge generation moves with it.
    root.event(&mut state, &pointer(PointerPhase::Down, 10.0, 60.0));
    assert!(root.is_focus_active());
    assert_eq!(root.ime_state(), None, "the left field's surface is gone");
    assert_ne!(root.focus_ime_generation(), steady_edge);
    assert_ne!(
        root.focus_epoch(),
        first_session,
        "an honoured claim moves the session identity whether or not the \
         claimant publishes anything"
    );

    // The move is a real one: the focus path now ends at the field that
    // published nothing.
    root.event(&mut state, &edit_event());
    assert_eq!(
        root.ime_state(),
        Some(SessionField::surface("bottom")),
        "the second field is the one holding the session"
    );
}

#[test]
fn focus_epoch_ignores_an_edit_inside_one_session() {
    // The converse, and why the counters stay apart: an identity holder can let a
    // harmless edit ride, while a value comparer must give up on every keystroke.
    let (mut root, mut state) = two_fields_focused(true);
    let session = root.focus_epoch();
    let before_edit = root.focus_ime_generation();

    root.event(&mut state, &edit_event());
    assert_eq!(
        root.ime_state(),
        Some(SessionField::surface("top")),
        "the focused field applied the edit and republished"
    );
    assert_ne!(
        root.focus_ime_generation(),
        before_edit,
        "a changed surface IS an edge"
    );
    assert_eq!(
        root.focus_epoch(),
        session,
        "an edit does not end or restart the session it lands in"
    );

    // A release ends the identity too, so a stale one can never come back
    // round to matching by standing still.
    root.event(&mut state, &pointer(PointerPhase::Down, 10.0, 90.0));
    assert!(!root.is_focus_active());
    assert_ne!(
        root.focus_epoch(),
        session,
        "a release retires the session's identity"
    );
}

#[test]
fn pointer_focus_move_to_a_non_publishing_pod_clears_the_surface_at_paint() {
    let (mut root, mut state) = two_fields_focused(false);
    let first_session = root.focus_epoch();
    let mut scene = RecordingScene::default();
    root.paint(&mut scene, FrameTime::ZERO);
    assert!(root.ime_state().is_some(), "the top field's surface stands");

    root.event(&mut state, &pointer(PointerPhase::Down, 10.0, 60.0));
    let moved = root.focus_epoch();
    assert_ne!(moved, first_session);
    root.paint(&mut scene, FrameTime::ZERO);
    assert!(root.is_focus_active(), "the bottom pod holds the session");
    assert!(root.ime_state().is_none(), "no stale surface after paint");
    assert_eq!(root.focus_epoch(), moved, "the epoch moved exactly once");
    root.paint(&mut scene, FrameTime::ZERO);
    assert!(root.ime_state().is_none());
}

#[test]
fn pointer_focus_move_between_publishing_pods_keeps_the_new_surface() {
    let (mut root, mut state) = two_fields_focused(true);
    let mut scene = RecordingScene::default();
    root.paint(&mut scene, FrameTime::ZERO);
    root.event(&mut state, &pointer(PointerPhase::Down, 10.0, 60.0));
    root.paint(&mut scene, FrameTime::ZERO);
    assert!(root.is_focus_active());
    assert_eq!(
        root.ime_state(),
        Some(SessionField::surface(SHARED_FIELD_TEXT))
    );
}

#[test]
fn outside_tap_blur_still_clears_the_surface() {
    let (mut root, mut state) = two_fields_focused(true);
    let mut scene = RecordingScene::default();
    root.paint(&mut scene, FrameTime::ZERO);
    root.event(&mut state, &pointer(PointerPhase::Down, 10.0, 90.0));
    root.paint(&mut scene, FrameTime::ZERO);
    assert!(!root.is_focus_active());
    assert!(root.ime_state().is_none());
}

/// A field whose first `Down` publishes the surface and later ones claim focus and publish nothing.
struct ReclaimField {
    downs: u32,
}

impl crate::widget::Widget for ReclaimField {
    fn layout(&mut self, _ctx: &mut LayoutCtx, bc: &BoxConstraints) -> Size {
        bc.constrain(Size::new(100.0, 20.0))
    }
    fn paint(&mut self, _ctx: &mut PaintCtx, _scene: &mut dyn PaintScene) {}
    fn event(&mut self, ctx: &mut EventCtx, event: &InputEvent) -> EventResult {
        match event {
            InputEvent::Pointer(p) if p.phase == PointerPhase::Down => {
                ctx.request_focus();
                if self.downs == 0 {
                    ctx.publish_ime_state(SessionField::surface(SHARED_FIELD_TEXT));
                }
                self.downs += 1;
                EventResult::Handled
            }
            _ => EventResult::Ignored,
        }
    }
}

/// A root holding one [`ReclaimField`] pod.
struct ReclaimRoot {
    pod: crate::widget::ChildPod,
}

impl crate::widget::Widget for ReclaimRoot {
    fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints) -> Size {
        self.pod.layout_child(ctx, bc);
        self.pod.set_origin(Point::new(0.0, 0.0));
        bc.max()
    }
    fn paint(&mut self, ctx: &mut PaintCtx, scene: &mut dyn PaintScene) {
        self.pod.paint_child(ctx, scene);
    }
    fn event(&mut self, ctx: &mut EventCtx, event: &InputEvent) -> EventResult {
        self.pod.event_child(ctx, event)
    }
    fn semantics(&self, ctx: &mut SemanticsCtx) {
        self.pod.semantics_child(ctx);
    }
}

struct ReclaimView;

impl View<ClickState> for ReclaimView {
    type Element = ReclaimRoot;
    fn build(&self, _ctx: &mut BuildCtx<'_>) -> ReclaimRoot {
        ReclaimRoot {
            pod: crate::widget::ChildPod::new(Box::new(ReclaimField { downs: 0 })),
        }
    }
    fn rebuild(
        &self,
        _prev: &Self,
        _element: &mut ReclaimRoot,
        _ctx: &mut BuildCtx<'_>,
    ) -> ChangeFlags {
        ChangeFlags::NONE
    }
}

#[test]
fn a_reclaim_from_the_live_chain_keeps_the_surface() {
    let mut root: RenderRoot<ClickState, ReclaimView> = RenderRoot::new();
    let mut state = ClickState::default();
    root.rebuild(&mut |_| ReclaimView, &mut state);
    root.layout(Size::new(200.0, 200.0));
    root.event(&mut state, &pointer(PointerPhase::Down, 10.0, 10.0));
    let surface = root.ime_state();
    assert!(surface.is_some(), "the first press opened the surface");
    let edge = root.focus_ime_generation();

    // The pod already holds the live link: the publish-nothing re-claim keeps everything.
    root.event(&mut state, &pointer(PointerPhase::Down, 10.0, 10.0));
    assert!(root.is_focus_active());
    assert_eq!(
        root.ime_state(),
        surface,
        "a live re-claim keeps the surface"
    );
    assert_eq!(root.focus_ime_generation(), edge, "and fires no IME edge");
}