gpui-cn 0.7.2

shadcn-style components for GPUI, built on gpui-base.
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
//! UI integration tests for `Button`: real input through a headless window.

use std::{cell::Cell, rc::Rc};

use gpui_cn::{Button, ButtonSize, ButtonVariant, Icon, Theme, ThemeMode};
use gpui_kit::base::Root;
use gpui_kit::{
    AppContext as _, Context, IntoElement, ParentElement as _, Render, Styled as _, TestAppContext,
    Window,
    base::{Disableable as _, Selectable as _},
    div,
    prelude::FluentBuilder as _,
    px, size,
    test::TestWindowExt as _,
};

const DOT: &[u8] = br#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><circle cx="12" cy="12" r="8"/></svg>"#;

/// A view with one configurable button and a counter it drives.
struct Harness {
    focus: gpui_kit::FocusHandle,
    clicks: Rc<Cell<usize>>,
    keyboard_clicks: Rc<Cell<usize>>,
    disabled: bool,
    loading: bool,
    icon_only: bool,
    variant: ButtonVariant,
}

impl Render for Harness {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        let clicks = self.clicks.clone();
        let keyboard_clicks = self.keyboard_clicks.clone();
        let mut button = Button::new("save")
            .track_focus(&self.focus)
            .variant(self.variant)
            .disabled(self.disabled)
            .loading(self.loading)
            .on_click(move |event, _, _| {
                clicks.set(clicks.get() + 1);
                if matches!(event, gpui_kit::ClickEvent::Keyboard(_)) {
                    keyboard_clicks.set(keyboard_clicks.get() + 1);
                }
            });
        button = if self.icon_only {
            button
                .icon(Icon::from_bytes(DOT))
                .accessibility_label("Save")
                .tooltip("Save")
        } else {
            button.label("Save")
        };
        div()
            .size_full()
            .p_4()
            .child(button)
            .child(Button::new("other").outline().label("Other"))
    }
}

struct Setup {
    view: gpui_kit::Entity<Harness>,
    handle: gpui_kit::WindowHandle<Root>,
    clicks: Rc<Cell<usize>>,
    keyboard_clicks: Rc<Cell<usize>>,
}

/// Presses and releases a key on the focused element. `press` sends only
/// the key down; keyboard activation needs the matching key up.
fn tap(window: &mut Window, key: &str, cx: &mut gpui_kit::App) {
    let keystroke = gpui_kit::Keystroke::parse(key).unwrap();
    window.press(key, cx);
    window.dispatch_event(
        gpui_kit::PlatformInput::KeyUp(gpui_kit::KeyUpEvent { keystroke }),
        cx,
    );
    window.render_frame(cx);
}

fn focus_save(setup: &Setup, cx: &mut TestAppContext) {
    let focus = setup.view.read_with(cx, |view, _| view.focus.clone());
    cx.update_window(setup.handle.into(), |_, window, cx| {
        window.focus(&focus, cx);
        window.render_frame(cx);
    })
    .unwrap();
}

fn setup(cx: &mut TestAppContext, configure: impl FnOnce(&mut Harness)) -> Setup {
    cx.update(|cx| {
        gpui_kit::init(cx);
        gpui_cn::init(cx);
    });
    let clicks = Rc::new(Cell::new(0));
    let keyboard_clicks = Rc::new(Cell::new(0));
    let mut view = None;
    let handle = cx.open_window(size(px(400.), px(300.)), |window, cx| {
        let harness = cx.new(|cx| {
            let mut harness = Harness {
                focus: cx.focus_handle(),
                clicks: clicks.clone(),
                keyboard_clicks: keyboard_clicks.clone(),
                disabled: false,
                loading: false,
                icon_only: false,
                variant: ButtonVariant::Default,
            };
            configure(&mut harness);
            harness
        });
        view = Some(harness.clone());
        Root::new(harness, window, cx)
    });
    Setup {
        view: view.unwrap(),
        handle,
        clicks,
        keyboard_clicks,
    }
}

#[gpui_kit::test]
fn click_fires_once_and_label_is_the_accessible_name(cx: &mut TestAppContext) {
    let setup = setup(cx, |_| {});
    cx.update_window(setup.handle.into(), |_, window, cx| {
        window.render_frame(cx);
        let save = window.find("save");
        assert_eq!(save.label(), Some("Save"));
        assert!(save.bounds().size.height > px(0.));
        window.click("save", cx);
    })
    .unwrap();
    assert_eq!(setup.clicks.get(), 1);
    assert_eq!(setup.keyboard_clicks.get(), 0);
}

#[gpui_kit::test]
fn enter_and_space_activate_the_focused_button(cx: &mut TestAppContext) {
    let setup = setup(cx, |_| {});
    cx.update_window(setup.handle.into(), |_, window, cx| {
        window.render_frame(cx);
        // A pointer press does not move focus onto a button, as on macOS.
        window.click("save", cx);
        assert_eq!(window.find("save").focused(), Some(false));
    })
    .unwrap();
    focus_save(&setup, cx);
    cx.update_window(setup.handle.into(), |_, window, cx| {
        assert_eq!(window.find("save").focused(), Some(true));
        tap(window, "enter", cx);
        tap(window, "space", cx);
    })
    .unwrap();
    assert_eq!(setup.clicks.get(), 3);
    assert_eq!(setup.keyboard_clicks.get(), 2);
}

#[gpui_kit::test]
fn tab_moves_focus_between_buttons(cx: &mut TestAppContext) {
    let setup = setup(cx, |_| {});
    focus_save(&setup, cx);
    cx.update_window(setup.handle.into(), |_, window, cx| {
        assert_eq!(window.find("save").focused(), Some(true));
        window.press("tab", cx);
        assert_eq!(window.find("other").focused(), Some(true));
        assert_eq!(window.find("save").focused(), Some(false));
        window.press("shift-tab", cx);
        assert_eq!(window.find("save").focused(), Some(true));
    })
    .unwrap();
}

#[gpui_kit::test]
fn disabled_button_ignores_pointer_and_keyboard(cx: &mut TestAppContext) {
    let setup = setup(cx, |harness| harness.disabled = true);
    cx.update_window(setup.handle.into(), |_, window, cx| {
        window.render_frame(cx);
        window.click("save", cx);
    })
    .unwrap();
    focus_save(&setup, cx);
    cx.update_window(setup.handle.into(), |_, window, cx| {
        // A disabled button does not take focus, so Enter goes nowhere.
        assert_ne!(window.find("save").focused(), Some(true));
        tap(window, "enter", cx);
        tap(window, "space", cx);
    })
    .unwrap();
    assert_eq!(setup.clicks.get(), 0);
}

#[gpui_kit::test]
fn a_loading_button_keeps_focus_and_ignores_activation(cx: &mut TestAppContext) {
    let setup = setup(cx, |harness| harness.loading = true);
    cx.update_window(setup.handle.into(), |_, window, cx| {
        window.render_frame(cx);
        assert_eq!(
            window
                .find(gpui_kit::ElementId::NamedChild(
                    gpui_kit::ElementId::from("save").into(),
                    "spinner".into()
                ))
                .role(),
            Some(gpui_kit::Role::Status)
        );
        window.click("save", cx);
    })
    .unwrap();
    focus_save(&setup, cx);
    cx.update_window(setup.handle.into(), |_, window, cx| {
        assert_eq!(window.find("save").focused(), Some(true));
        tap(window, "enter", cx);
        tap(window, "space", cx);
    })
    .unwrap();
    assert_eq!(setup.clicks.get(), 0);

    setup.view.update(cx, |view, cx| {
        view.loading = false;
        cx.notify();
    });
    cx.update_window(setup.handle.into(), |_, window, cx| {
        window.render_frame(cx);
        window.click("save", cx);
        tap(window, "enter", cx);
    })
    .unwrap();
    assert_eq!(setup.clicks.get(), 2);
}

#[gpui_kit::test]
fn a_loading_icon_only_button_stays_square(cx: &mut TestAppContext) {
    let setup = setup(cx, |harness| {
        harness.icon_only = true;
        harness.loading = true;
    });
    cx.update_window(setup.handle.into(), |_, window, cx| {
        window.render_frame(cx);
        let bounds = window.find("save").bounds();
        assert_eq!(bounds.size.width, px(28.));
        assert_eq!(bounds.size.height, px(28.));
    })
    .unwrap();
}

#[gpui_kit::test]
fn icon_only_button_exposes_its_accessibility_label(cx: &mut TestAppContext) {
    let setup = setup(cx, |harness| harness.icon_only = true);
    cx.update_window(setup.handle.into(), |_, window, cx| {
        window.render_frame(cx);
        let save = window.find("save");
        assert_eq!(save.label(), Some("Save"));
        let bounds = save.bounds();
        assert_eq!(
            bounds.size.width, bounds.size.height,
            "icon-only buttons are square"
        );
        assert_eq!(bounds.size.width, px(28.));
        window.click("save", cx);
    })
    .unwrap();
    assert_eq!(setup.clicks.get(), 1);
}

#[gpui_kit::test]
fn sizes_follow_the_reference_heights_and_scale_with_the_font_size(cx: &mut TestAppContext) {
    let setup = setup(cx, |_| {});
    cx.update_window(setup.handle.into(), |_, window, cx| {
        window.render_frame(cx);
        assert_eq!(window.find("save").bounds().size.height, px(28.));
        assert_eq!(window.find("other").bounds().size.height, px(28.));
    })
    .unwrap();
    cx.update(|cx| Theme::set_ui_font_size(cx, px(20.)));
    cx.update_window(setup.handle.into(), |_, window, cx| {
        window.render_frame(cx);
        assert_eq!(window.find("save").bounds().size.height, px(35.));
    })
    .unwrap();
}

/// One button per size tier, plus one with a style override.
struct Sizes;

impl Render for Sizes {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        div()
            .size_full()
            .p_4()
            .flex()
            .gap_2()
            .child(Button::new("xs").size(ButtonSize::Xs).label("Save"))
            .child(Button::new("sm").size(ButtonSize::Sm).label("Save"))
            .child(Button::new("default").label("Save"))
            .child(Button::new("lg").size(ButtonSize::Lg).label("Save"))
            .child(Button::new("tall").h(px(40.)).w(px(200.)).label("Save"))
    }
}

#[gpui_kit::test]
fn size_tiers_match_the_reference_and_styled_overrides_win(cx: &mut TestAppContext) {
    cx.update(|cx| {
        gpui_kit::init(cx);
        gpui_cn::init(cx);
    });
    let handle = cx.open_window(size(px(600.), px(200.)), |window, cx| {
        let view = cx.new(|_| Sizes);
        Root::new(view, window, cx)
    });
    cx.update_window(handle.into(), |_, window, cx| {
        window.render_frame(cx);
        for (id, height) in [("xs", 20.), ("sm", 24.), ("default", 28.), ("lg", 32.)] {
            assert_eq!(window.find(id).bounds().size.height, px(height), "{id}");
        }
        let tall = window.find("tall").bounds().size;
        assert_eq!(tall.height, px(40.), "h() overrides the tier height");
        assert_eq!(tall.width, px(200.), "w() overrides the content width");
    })
    .unwrap();
}

#[gpui_kit::test]
fn tab_reaches_the_first_button_when_nothing_is_focused(cx: &mut TestAppContext) {
    let setup = setup(cx, |_| {});
    cx.update_window(setup.handle.into(), |_, window, cx| {
        window.render_frame(cx);
        assert_eq!(window.find("save").focused(), Some(false));
        window.press("tab", cx);
        assert_eq!(window.find("save").focused(), Some(true));
        window.press("tab", cx);
        assert_eq!(window.find("other").focused(), Some(true));
        window.press("tab", cx);
        assert_eq!(window.find("save").focused(), Some(true), "wraps around");
    })
    .unwrap();
}

/// Two buttons with id-keyed focus handles; the first can be removed.
struct Removable {
    show_first: bool,
}

impl Render for Removable {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        div()
            .size_full()
            .p_4()
            .flex()
            .gap_2()
            .when(self.show_first, |this| {
                this.child(Button::new("first").label("First"))
            })
            .child(Button::new("second").label("Second"))
    }
}

#[gpui_kit::test]
fn tab_recovers_after_the_focused_button_disappears(cx: &mut TestAppContext) {
    cx.update(|cx| {
        gpui_kit::init(cx);
        gpui_cn::init(cx);
    });
    let mut view = None;
    let handle = cx.open_window(size(px(400.), px(200.)), |window, cx| {
        let removable = cx.new(|_| Removable { show_first: true });
        view = Some(removable.clone());
        Root::new(removable, window, cx)
    });
    let view = view.unwrap();
    cx.update_window(handle.into(), |_, window, cx| {
        window.render_frame(cx);
        window.press("tab", cx);
        assert_eq!(window.find("first").focused(), Some(true));
    })
    .unwrap();
    view.update(cx, |view, cx| {
        view.show_first = false;
        cx.notify();
    });
    cx.update_window(handle.into(), |_, window, cx| {
        // The removed button's focus handle goes with its element state, so
        // the window has no focus and Root takes it back on the next frame.
        window.render_frame(cx);
        window.render_frame(cx);
        window.press("tab", cx);
        assert_eq!(window.find("second").focused(), Some(true));
    })
    .unwrap();
}

/// One button per variant, all at the default size.
struct Variants;

impl Render for Variants {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        div()
            .size_full()
            .p_4()
            .flex()
            .gap_2()
            .child(Button::new("default").label("Save"))
            .child(Button::new("primary").primary().label("Save"))
            .child(Button::new("destructive").destructive().label("Save"))
            .child(Button::new("outline").outline().label("Save"))
            .child(Button::new("ghost").ghost().label("Save"))
            .child(Button::new("link").link().label("Save"))
    }
}

#[gpui_kit::test]
fn every_variant_shares_one_height_and_one_width(cx: &mut TestAppContext) {
    cx.update(|cx| {
        gpui_kit::init(cx);
        gpui_cn::init(cx);
    });
    let handle = cx.open_window(size(px(600.), px(200.)), |window, cx| {
        let view = cx.new(|_| Variants);
        Root::new(view, window, cx)
    });
    cx.update_window(handle.into(), |_, window, cx| {
        window.render_frame(cx);
        let reference = window.find("default").bounds().size;
        for id in ["primary", "destructive", "outline", "ghost", "link"] {
            let bounds = window.find(id).bounds().size;
            assert_eq!(bounds.height, reference.height, "{id} height");
            assert_eq!(bounds.width, reference.width, "{id} width");
        }
        assert_eq!(reference.height, px(28.));
    })
    .unwrap();
}

#[gpui_kit::test]
fn link_variant_has_the_link_role(cx: &mut TestAppContext) {
    let setup = setup(cx, |harness| harness.variant = ButtonVariant::Link);
    cx.update_window(setup.handle.into(), |_, window, cx| {
        window.render_frame(cx);
        assert_eq!(window.find("save").role(), Some(gpui_kit::Role::Link));
        assert_eq!(window.find("other").role(), Some(gpui_kit::Role::Button));
    })
    .unwrap();
}

#[gpui_kit::test]
fn theme_changes_redraw_the_button(cx: &mut TestAppContext) {
    let setup = setup(cx, |_| {});
    cx.update_window(setup.handle.into(), |_, window, cx| {
        window.render_frame(cx);
        assert!(window.find("save").visible());
    })
    .unwrap();
    cx.update(|cx| Theme::change(ThemeMode::Dark, cx));
    cx.update_window(setup.handle.into(), |_, window, cx| {
        window.render_frame(cx);
        assert!(window.find("save").visible());
        window.click("save", cx);
    })
    .unwrap();
    assert_eq!(setup.clicks.get(), 1);
    drop(setup.view);
}

/// A wide and a narrow button with the same long label.
struct Truncated;

impl Render for Truncated {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        div()
            .size_full()
            .p_4()
            .flex()
            .flex_col()
            .gap_2()
            .child(
                Button::new("disabled-with-reason")
                    .disabled(true)
                    .tooltip("Select a file first")
                    .label("Upload"),
            )
            .child(Button::new("wide").w(px(360.)).label("A long label"))
            .child(Button::new("narrow").w(px(60.)).label("A long label"))
            .child(
                Button::new("opted-out")
                    .w(px(60.))
                    .truncation_tooltip(false)
                    .label("A long label"),
            )
    }
}

#[gpui_kit::test]
fn a_truncated_label_shows_itself_as_a_tooltip(cx: &mut TestAppContext) {
    cx.update(|cx| {
        gpui_kit::init(cx);
        gpui_cn::init(cx);
    });
    let handle = cx.open_window(size(px(400.), px(200.)), |window, cx| {
        let view = cx.new(|_| Truncated);
        Root::new(view, window, cx)
    });
    let hover_and_settle = |cx: &mut TestAppContext, id: &'static str| {
        cx.update_window(handle.into(), |_, window, cx| {
            window.render_frame(cx);
            window.hover(id, cx);
        })
        .unwrap();
        // The overlay's show delay is a timer created when its task first
        // polls, so park once before advancing the clock past it.
        cx.run_until_parked();
        cx.executor()
            .advance_clock(std::time::Duration::from_millis(700));
        cx.run_until_parked();
        cx.update_window(handle.into(), |_, window, cx| {
            window.render_frame(cx);
            window
                .try_find("gpui-cn-tooltip")
                .is_some_and(|tooltip| tooltip.visible())
        })
        .unwrap()
    };
    assert!(
        !hover_and_settle(cx, "wide"),
        "a label that fits has no tooltip"
    );
    assert!(
        hover_and_settle(cx, "narrow"),
        "a truncated label shows one"
    );
    assert!(
        !hover_and_settle(cx, "opted-out"),
        "unless the button opted out"
    );
}

/// Buttons whose accessibility state is set by the caller.
struct States;

impl Render for States {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        div()
            .size_full()
            .p_4()
            .flex()
            .gap_2()
            .child(Button::new("plain").label("Plain"))
            .child(Button::new("selected").selected(true).label("Selected"))
            .child(Button::new("open").open(true).label("Open"))
            .child(Button::new("toggled").toggled(true).label("Bold"))
            .child(Button::new("untoggled").toggled(false).label("Bold"))
            .child(Button::new("off").disabled(true).label("Off"))
    }
}

#[gpui_kit::test]
fn toggled_reaches_assistive_technology_and_other_states_render(cx: &mut TestAppContext) {
    cx.update(|cx| {
        gpui_kit::init(cx);
        gpui_cn::init(cx);
    });
    let handle = cx.open_window(size(px(600.), px(200.)), |window, cx| {
        let view = cx.new(|_| States);
        Root::new(view, window, cx)
    });
    cx.update_window(handle.into(), |_, window, cx| {
        window.render_frame(cx);
        // `selected` and `open` are visual in base and set no accessibility
        // flag; `toggled` is the channel assistive technology reads.
        assert!(window.find("selected").visible());
        assert!(window.find("open").visible());
        assert_eq!(window.find("plain").checked(), None, "not a toggle");
        assert_eq!(window.find("toggled").checked(), Some(true));
        assert_eq!(window.find("untoggled").checked(), Some(false));
        // Base does not expose a disabled flag in snapshots; disabled is
        // proven by behavior in `disabled_button_ignores_pointer_and_keyboard`.
        assert!(window.find("off").visible());
    })
    .unwrap();
}

/// Two buttons that report hover to the test.
struct Hovers {
    entered: Rc<Cell<usize>>,
    left: Rc<Cell<usize>>,
}

impl Render for Hovers {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        let entered = self.entered.clone();
        let left = self.left.clone();
        div()
            .size_full()
            .p_4()
            .flex()
            .gap_2()
            .child(
                Button::new("first")
                    .label("First")
                    .on_hover(move |hovered, _, _| {
                        if *hovered {
                            entered.set(entered.get() + 1);
                        } else {
                            left.set(left.get() + 1);
                        }
                    }),
            )
            .child(Button::new("second").label("Second"))
    }
}

#[gpui_kit::test]
fn on_hover_reports_enter_and_leave(cx: &mut TestAppContext) {
    cx.update(|cx| {
        gpui_kit::init(cx);
        gpui_cn::init(cx);
    });
    let entered = Rc::new(Cell::new(0));
    let left = Rc::new(Cell::new(0));
    let (entered_for_view, left_for_view) = (entered.clone(), left.clone());
    let handle = cx.open_window(size(px(400.), px(200.)), |window, cx| {
        let view = cx.new(|_| Hovers {
            entered: entered_for_view,
            left: left_for_view,
        });
        Root::new(view, window, cx)
    });
    cx.update_window(handle.into(), |_, window, cx| {
        window.render_frame(cx);
        window.hover("first", cx);
        assert_eq!((entered.get(), left.get()), (1, 0));
        window.hover("second", cx);
        assert_eq!((entered.get(), left.get()), (1, 1));
        window.hover("first", cx);
        assert_eq!((entered.get(), left.get()), (2, 1));
    })
    .unwrap();
}

#[gpui_kit::test]
fn a_press_dragged_off_the_button_does_not_click(cx: &mut TestAppContext) {
    let setup = setup(cx, |_| {});
    cx.update_window(setup.handle.into(), |_, window, cx| {
        window.render_frame(cx);
        window.drag_to("save", "other", cx);
        window.render_frame(cx);
        // The button is still interactive afterwards.
        window.click("save", cx);
    })
    .unwrap();
    assert_eq!(setup.clicks.get(), 1, "only the real click counts");
}

#[gpui_kit::test]
fn a_button_disabled_while_hovered_stays_inert_and_recovers(cx: &mut TestAppContext) {
    let setup = setup(cx, |_| {});
    cx.update_window(setup.handle.into(), |_, window, cx| {
        window.render_frame(cx);
        window.hover("save", cx);
    })
    .unwrap();
    setup.view.update(cx, |view, cx| {
        view.disabled = true;
        cx.notify();
    });
    cx.update_window(setup.handle.into(), |_, window, cx| {
        window.render_frame(cx);
        window.click("save", cx);
    })
    .unwrap();
    assert_eq!(setup.clicks.get(), 0);
    setup.view.update(cx, |view, cx| {
        view.disabled = false;
        cx.notify();
    });
    cx.update_window(setup.handle.into(), |_, window, cx| {
        window.render_frame(cx);
        window.click("save", cx);
    })
    .unwrap();
    assert_eq!(setup.clicks.get(), 1);
}

#[gpui_kit::test]
fn an_explicit_tooltip_shows_and_hides_with_the_pointer(cx: &mut TestAppContext) {
    let setup = setup(cx, |harness| harness.icon_only = true);
    let handle = setup.handle;
    let tooltip_visible = |cx: &mut TestAppContext| {
        cx.run_until_parked();
        cx.executor()
            .advance_clock(std::time::Duration::from_millis(700));
        cx.run_until_parked();
        cx.update_window(handle.into(), |_, window, cx| {
            window.render_frame(cx);
            window
                .try_find("gpui-cn-tooltip")
                .is_some_and(|tooltip| tooltip.visible())
        })
        .unwrap()
    };
    cx.update_window(handle.into(), |_, window, cx| {
        window.render_frame(cx);
        window.hover("save", cx);
    })
    .unwrap();
    assert!(tooltip_visible(cx), "shown after the delay");
    cx.update_window(handle.into(), |_, window, cx| {
        // "other" has no tooltip, so moving there hides it.
        window.hover("other", cx);
    })
    .unwrap();
    assert!(!tooltip_visible(cx), "hidden after leaving");
    cx.update_window(handle.into(), |_, window, cx| {
        window.hover("save", cx);
        window.click("save", cx);
    })
    .unwrap();
    assert!(
        !tooltip_visible(cx),
        "a press hides it and does not reschedule"
    );
}

/// A button in a plain window with no `gpui_kit::base::Root`.
struct Bare {
    clicks: Rc<Cell<usize>>,
}

impl Render for Bare {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        let clicks = self.clicks.clone();
        div().size_full().p_4().child(
            Button::new("save")
                .label("Save")
                .tooltip("Save the file")
                .on_click(move |_, _, _| clicks.set(clicks.get() + 1)),
        )
    }
}

#[gpui_kit::test]
fn works_without_root(cx: &mut TestAppContext) {
    cx.update(|cx| {
        gpui_kit::init(cx);
        gpui_cn::init(cx);
    });
    let clicks = Rc::new(Cell::new(0));
    let clicks_for_view = clicks.clone();
    let handle = cx.open_window(size(px(400.), px(200.)), |_, _| Bare {
        clicks: clicks_for_view,
    });
    cx.update_window(handle.into(), |_, window, cx| {
        window.render_frame(cx);
        assert!(
            gpui_cn::TooltipHost::overlay(window, cx).is_none(),
            "no root, no overlay"
        );
        let save = window.find("save");
        assert_eq!(save.label(), Some("Save"));
        assert_eq!(save.bounds().size.height, px(28.));
        window.click("save", cx);
        // Without an overlay the tooltip is GPUI's native one; hovering
        // must not panic and the button stays interactive.
        window.hover("save", cx);
        window.click("save", cx);
    })
    .unwrap();
    assert_eq!(clicks.get(), 2);
}

/// The Link variant with its own on_click, next to a plain button.
struct Links;

impl Render for Links {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        div()
            .size_full()
            .p_4()
            .flex()
            .gap_2()
            .child(Button::new("docs").link().label("Read the docs"))
            .child(
                Button::new("icon-link")
                    .link()
                    .icon(Icon::from_bytes(DOT))
                    .accessibility_label("Open"),
            )
    }
}

#[gpui_kit::test]
fn link_variant_exposes_its_name_and_keeps_the_tier_height(cx: &mut TestAppContext) {
    cx.update(|cx| {
        gpui_kit::init(cx);
        gpui_cn::init(cx);
    });
    let handle = cx.open_window(size(px(400.), px(200.)), |window, cx| {
        let view = cx.new(|_| Links);
        Root::new(view, window, cx)
    });
    cx.update_window(handle.into(), |_, window, cx| {
        window.render_frame(cx);
        let docs = window.find("docs");
        assert_eq!(docs.role(), Some(gpui_kit::Role::Link));
        assert_eq!(docs.label(), Some("Read the docs"));
        assert_eq!(docs.bounds().size.height, px(28.));
        let icon = window.find("icon-link");
        assert_eq!(icon.role(), Some(gpui_kit::Role::Link));
        assert_eq!(icon.label(), Some("Open"));
        assert_eq!(
            icon.bounds().size.width,
            px(28.),
            "icon-only links are square too"
        );
    })
    .unwrap();
}