gpui-box-kit 0.1.1

GPUI Box Kit design-system components and interaction primitives
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
//! The anchored surface every menu-shaped component is built from.
//!
//! [`Popover`] is the component: a surface that hangs off a trigger and owns
//! nothing but whether it is open. The free functions around it are the parts
//! `Select`, `Menu`, `ContextMenu` and `CommandPalette` share — row geometry,
//! key classification, cursor movement, and the match ranking a filterable
//! list orders itself by.

use std::rc::Rc;

use gpui::{
    Anchor, AnyElement, App, Bounds, Context, ElementId, EventEmitter, FocusHandle, Focusable,
    InteractiveElement, IntoElement, KeyDownEvent, ParentElement, Pixels, Point, Render,
    SharedString, Styled, Window, div, prelude::*, px,
};
use gpui_kit_assets::Icon;
use gpui_kit_semantics::{NodeSpec, Role, Semantic};
use gpui_kit_theme::{ActiveTheme, Elevation, Space, TextTone, Theme, TypeScale};

use crate::controls::button::Button;
use crate::foundation::{Ident, StyledExt, text};
use crate::overlay::focus::FocusTrap;
use crate::overlay::layer::{Overlay, Placement, surface};

use crate::motion;

/// What a keystroke means to a menu-like surface, once the platform's
/// modifier conventions have been applied.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MenuKey {
    Up,
    Down,
    /// Enters a submenu, which is the leading edge on a left-to-right layout.
    Right,
    /// Leaves a submenu.
    Left,
    Enter,
    ModifiedEnter,
    Escape,
    Backspace,
    Other,
}

/// Reads one keystroke as a menu intent, so every menu-like surface answers
/// the same keys the same way.
pub fn classify_key(key: &str, command: bool, control: bool) -> MenuKey {
    match key {
        "up" => MenuKey::Up,
        "down" => MenuKey::Down,
        "right" => MenuKey::Right,
        "left" => MenuKey::Left,
        "enter" if command || control => MenuKey::ModifiedEnter,
        "enter" => MenuKey::Enter,
        "escape" => MenuKey::Escape,
        "backspace" => MenuKey::Backspace,
        _ => MenuKey::Other,
    }
}

/// The letter a keystroke types, for a menu that jumps on it.
///
/// Only a bare letter counts: a modified keystroke is a shortcut, and treating
/// it as type-ahead would move the cursor while the typist meant to act.
pub fn typed_letter(key: &str, modifiers: gpui::Modifiers) -> Option<char> {
    if modifiers.platform || modifiers.control || modifiers.alt || modifiers.function {
        return None;
    }
    let mut characters = key.chars();
    let letter = characters.next()?;
    if characters.next().is_some() || !letter.is_alphanumeric() {
        return None;
    }
    Some(letter.to_ascii_lowercase())
}

/// The next entry whose label starts with `letter`, searching forward from the
/// cursor and wrapping. `None` in `labels` marks an entry that cannot be
/// jumped to, such as a separator or a refused row.
pub fn jump_to<S: AsRef<str>>(
    labels: &[Option<S>],
    from: Option<usize>,
    letter: char,
) -> Option<usize> {
    let count = labels.len();
    if count == 0 {
        return None;
    }
    let start = from.map_or(0, |index| index + 1);
    let letter = letter.to_lowercase().next()?;
    (0..count)
        .map(|offset| (start + offset) % count)
        .find(|index| {
            labels[*index].as_ref().is_some_and(|label| {
                label
                    .as_ref()
                    .chars()
                    .next()
                    .and_then(|first| first.to_lowercase().next())
                    == Some(letter)
            })
        })
}

/// The index `delta` steps from `active`, wrapping at the ends.
///
/// A menu wraps where a strip stops: the list is short, the whole of it is on
/// screen, and arrowing off the bottom onto the top is how a menu has always
/// behaved. A strip stops instead, which `foundation::stepping` handles.
pub fn step(active: Option<usize>, count: usize, delta: isize) -> Option<usize> {
    if count == 0 {
        return None;
    }
    let count = count as isize;
    Some(match active {
        None if delta >= 0 => 0,
        None => count - 1,
        Some(index) => (index as isize + delta).rem_euclid(count),
    } as usize)
}

/// How well `label` answers `query`, lower being better, or `None` when it
/// does not answer it at all.
///
/// A typist who knows what they are looking for types its beginning, so a
/// prefix outranks the start of a later word, which outranks a match in the
/// middle of one, which outranks letters merely occurring in order.
pub fn match_rank(query: &str, label: &str) -> Option<usize> {
    let query = query.trim().to_lowercase();
    if query.is_empty() {
        return Some(1);
    }
    let label = label.to_lowercase();
    if label.starts_with(&query) {
        Some(0)
    } else if word_starts(&label).any(|start| label[start..].starts_with(&query)) {
        Some(1)
    } else if label.contains(&query) {
        Some(2)
    } else if is_subsequence(&query, &label) {
        Some(3)
    } else {
        None
    }
}

/// Byte offsets of every character that begins a word.
fn word_starts(label: &str) -> impl Iterator<Item = usize> + '_ {
    label.char_indices().filter_map(move |(index, character)| {
        if index == 0 || !character.is_alphanumeric() {
            return None;
        }
        let previous = label[..index].chars().next_back()?;
        (!previous.is_alphanumeric()).then_some(index)
    })
}

fn is_subsequence(query: &str, label: &str) -> bool {
    let mut characters = label.chars();
    query
        .chars()
        .all(|wanted| characters.any(|character| character == wanted))
}

/// The indices of the labels `query` answers, best answer first.
pub fn filter_indices<S: AsRef<str>>(query: &str, labels: &[S]) -> Vec<usize> {
    let mut ranked: Vec<_> = labels
        .iter()
        .enumerate()
        .filter_map(|(index, label)| match_rank(query, label.as_ref()).map(|rank| (rank, index)))
        .collect();
    ranked.sort_by_key(|&(rank, index)| (rank, index));
    ranked.into_iter().map(|(_, index)| index).collect()
}

/// The elevated surface every anchored overlay draws.
pub fn card(theme: &Theme) -> gpui::Div {
    div()
        .rounded(px(theme.radii.card))
        .elevation(theme, Elevation::Overlay)
        .p(px(theme.spacing.xs))
        .overflow_hidden()
        .bg(theme.colors.overlay)
        .text_color(theme.colors.text)
}

/// [`card`] without the inner padding, for a surface that draws its own rows
/// edge to edge.
pub fn card_flush(theme: &Theme) -> gpui::Div {
    card(theme).p_0()
}

/// The viewport policy for a select-like popup whose trigger was measured on
/// the preceding frame.
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct MenuGeometry {
    pub placement: Placement,
    pub max_height: f32,
    pub width: f32,
}

/// Resolves one menu against the actual window and trigger bounds.
///
/// The fallback is deliberately bounded by the whole usable viewport. It is
/// used only before the trigger's first prepaint; that prepaint requests the
/// corrective frame with side-specific space.
pub(crate) fn menu_geometry(
    window: &Window,
    trigger: Bounds<Pixels>,
    theme: &Theme,
    desired_height: f32,
    min_width: f32,
) -> MenuGeometry {
    let viewport = window.viewport_size();
    let viewport_height = f32::from(viewport.height);
    let viewport_width = f32::from(viewport.width);
    let margin = theme.spacing.sm;
    let gap = (theme.spacing.sm - 2.0).max(0.0);
    let usable_width = (viewport_width - margin * 2.0).max(0.0);
    let measured_width = f32::from(trigger.size.width);
    let width = measured_width.max(min_width).min(usable_width);
    let measured = measured_width > 0.0 && f32::from(trigger.size.height) > 0.0;

    if !measured {
        return MenuGeometry {
            placement: Placement::Below,
            max_height: desired_height.min((viewport_height - margin * 2.0 - gap).max(0.0)),
            width,
        };
    }

    let below = (viewport_height - margin - f32::from(trigger.bottom()) - gap).max(0.0);
    let above = (f32::from(trigger.top()) - margin - gap).max(0.0);
    let placement = if below >= desired_height || below >= above {
        Placement::Below
    } else {
        Placement::Above
    };
    let available = match placement {
        Placement::Above => above,
        _ => below,
    };

    MenuGeometry {
        placement,
        max_height: desired_height.min(available),
        width,
    }
}

/// Paints a side-resolved menu through the canonical popover layer.
pub(crate) fn menu_overlay(
    ident: &Ident,
    theme: &Theme,
    placement: Placement,
    content: AnyElement,
) -> AnyElement {
    let gap = px((theme.spacing.sm - 2.0).max(0.0));
    let frame = div()
        .occlude()
        .when(placement == Placement::Below, |element| element.pt(gap))
        .when(placement == Placement::Above, |element| element.pb(gap))
        .child(content);

    Overlay::new(ident.child("overlay"))
        .placement(placement)
        .window_snap_margin(px(theme.spacing.sm))
        .child(motion::menu_in(ident.element_id(), theme, frame))
        .into_any_element()
}

fn pinned(layer: AnyElement) -> AnyElement {
    div()
        .absolute()
        .top_0()
        .left_0()
        .size_0()
        .child(layer)
        .into_any_element()
}

/// Places `content` under `anchor`, flipping above when there is no room.
pub fn anchored_below(id: impl Into<ElementId>, theme: &Theme, content: AnyElement) -> AnyElement {
    pinned(
        gpui::deferred(
            gpui::anchored()
                .anchor(Anchor::TopLeft)
                .snap_to_window_with_margin(px(theme.spacing.sm))
                .child(motion::menu_in(
                    id,
                    theme,
                    div()
                        .occlude()
                        .pt(px(theme.spacing.sm - 2.0))
                        .child(content),
                )),
        )
        .priority(1)
        .into_any_element(),
    )
}

/// Places `content` over `anchor`, flipping below when there is no room.
pub fn anchored_above(id: impl Into<ElementId>, theme: &Theme, content: AnyElement) -> AnyElement {
    pinned(
        gpui::deferred(
            gpui::anchored()
                .anchor(Anchor::BottomLeft)
                .snap_to_window_with_margin(px(theme.spacing.sm))
                .child(motion::menu_in(
                    id,
                    theme,
                    div()
                        .occlude()
                        .pb(px(theme.spacing.sm - 2.0))
                        .child(content),
                )),
        )
        .priority(1)
        .into_any_element(),
    )
}

/// Places `content` at a point, which is what a context menu needs.
pub fn at(
    id: impl Into<ElementId>,
    theme: &Theme,
    position: Point<Pixels>,
    content: AnyElement,
) -> AnyElement {
    gpui::deferred(
        gpui::anchored()
            .position(position)
            .anchor(Anchor::TopLeft)
            .snap_to_window_with_margin(px(theme.spacing.sm))
            .child(motion::menu_in(id, theme, div().occlude().child(content))),
    )
    .priority(1)
    .into_any_element()
}

/// Places `content` in the middle of the window, over a scrim.
pub fn modal(
    id: impl Into<ElementId>,
    theme: &Theme,
    viewport: gpui::Size<Pixels>,
    content: AnyElement,
) -> AnyElement {
    gpui::deferred(
        gpui::anchored()
            .position(gpui::point(px(0.0), px(0.0)))
            .child(
                div()
                    .occlude()
                    .w(viewport.width)
                    .h(viewport.height)
                    .bg(gpui::black().opacity(0.6))
                    .flex()
                    .items_center()
                    .justify_center()
                    .child(motion::dialog_in(id, theme, div().child(content))),
            ),
    )
    .priority(2)
    .into_any_element()
}

/// One row of a menu-like surface, with the shared height, hover wash, and
/// refusal treatment.
pub fn menu_row(theme: &Theme, selected: bool, highlighted: bool) -> gpui::Div {
    div()
        .flex()
        .flex_row()
        .items_center()
        .gap(px(10.0))
        .px(px(theme.spacing.sm))
        .py(px(6.0))
        .rounded(px(theme.radii.control))
        .when(selected, |element| {
            element
                .bg(theme.colors.selected)
                .shadow(theme.selected_ring())
        })
        .when(!selected && highlighted, |element| {
            element.bg(theme.colors.hover)
        })
        .when(!selected && !highlighted, |element| {
            element.hover(|style| style.bg(theme.colors.hover))
        })
}

/// A menu row's visible label, including the foreground transition for a
/// pointer anywhere over the row rather than only over the glyphs themselves.
pub fn menu_label(
    theme: &Theme,
    label: impl Into<SharedString>,
    selected: bool,
    highlighted: bool,
    hover_group: SharedString,
) -> gpui::Div {
    text(theme, TypeScale::Label, label)
        .text_color(if selected || highlighted {
            theme.colors.text
        } else {
            theme.colors.text_muted
        })
        .when(!selected && !highlighted, |element| {
            element.group_hover(hover_group, |style| style.text_color(theme.colors.text))
        })
}

/// A section label inside a menu-like surface.
pub fn heading(theme: &Theme, label: &str) -> gpui::Div {
    div()
        .px(px(theme.spacing.sm))
        .pb(px(theme.spacing.xs))
        .pt(px(6.0))
        .child(
            text(
                theme,
                TypeScale::Caption,
                SharedString::from(tracked_upper(label)),
            )
            .text_color(theme.colors.text_muted.opacity(0.6)),
        )
}

/// The hairline between two groups of menu rows.
pub fn separator(theme: &Theme) -> gpui::Div {
    div()
        .h(px(1.0))
        .mx(px(-theme.spacing.xs))
        .my(px(theme.spacing.xs))
        .bg(theme.colors.hairline)
}

/// The shortcut cap a menu row carries on its trailing edge.
pub fn key_cap(theme: &Theme, label: impl Into<SharedString>) -> gpui::Div {
    div()
        .h(px(22.0))
        .px(px(5.0))
        .rounded(px(theme.radii.small))
        .flex()
        .items_center()
        .justify_center()
        .bg(theme.colors.hover.opacity(0.38))
        .child(text(theme, TypeScale::Code, label.into()).text_tone(theme, TextTone::Muted))
}

/// The surface a modal draws itself on.
pub fn dialog_card(theme: &Theme) -> gpui::Div {
    div()
        .w(px(360.0))
        .p(px(theme.spacing.xl - theme.spacing.xs))
        .rounded(px(theme.radii.dialog))
        .bg(theme.colors.overlay)
        .elevation(theme, Elevation::Modal)
        .flex()
        .flex_col()
        .text_color(theme.colors.text)
}

/// The one question a modal is asking.
pub fn dialog_title(theme: &Theme, title: impl Into<SharedString>) -> gpui::Div {
    text(theme, TypeScale::Title, title.into())
}

/// The detail under a modal's question.
pub fn dialog_body(theme: &Theme, body: impl Into<SharedString>) -> gpui::Div {
    text(theme, TypeScale::Body, body.into())
        .mt(px(theme.spacing.sm))
        .text_tone(theme, TextTone::Muted)
}

/// Lays a trigger out with the slot an anchored overlay hangs from.
///
/// A surface anchors to where its slot sits, so the slot goes under the
/// trigger for a surface that opens downwards and above it for one that opens
/// upwards. The slot takes no space of its own.
pub fn anchored_slot(
    placement: Placement,
    trigger: AnyElement,
    overlay: Option<AnyElement>,
) -> gpui::Div {
    let slot = div().relative().children(overlay);
    // The trigger sizes to itself: one stretched to its container would claim
    // to be a full-width control.
    let frame = div().flex().flex_col().items_start();
    match placement {
        Placement::Above => frame.child(slot).child(trigger),
        _ => frame.child(trigger).child(slot),
    }
}

/// What a popover reports. The owner decides what any of it means.
///
/// A dismissal is always followed by [`PopoverEvent::Closed`], so a subscriber
/// that only cares that the surface went away has one event to watch.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PopoverEvent {
    Opened,
    /// The surface was waved away, by escape or by a click outside it.
    Dismissed,
    Closed,
}

impl EventEmitter<PopoverEvent> for Popover {}

/// Builds the popover body for one frame.
type Content = Rc<dyn Fn(&mut Window, &mut App) -> AnyElement>;

/// A surface anchored to a trigger, holding whatever the caller puts in it.
///
/// Open state and the element that had the keyboard before opening both
/// outlive a frame, so a popover is a view rather than a builder. The body is
/// a callback instead of a stored element because an `AnyElement` can be
/// consumed once, while an open popover re-renders for as long as it stays
/// open.
pub struct Popover {
    ident: Ident,
    focus_handle: FocusHandle,
    trigger_focus: FocusHandle,
    trigger: SharedString,
    trigger_icon: Option<Icon>,
    content: Option<Content>,
    placement: Placement,
    dismissable: bool,
    open: bool,
    /// Set by `open`, cleared by the first frame that can act on it.
    pending_focus: bool,
    trap: FocusTrap,
}

impl std::fmt::Debug for Popover {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("Popover")
            .field("ident", &self.ident)
            .field("trigger", &self.trigger)
            .field("has_content", &self.content.is_some())
            .field("placement", &self.placement)
            .field("dismissable", &self.dismissable)
            .field("open", &self.open)
            .finish()
    }
}

impl Popover {
    pub fn new(ident: impl Into<Ident>, _window: &mut Window, cx: &mut Context<Self>) -> Self {
        Self {
            ident: ident.into(),
            focus_handle: cx.focus_handle(),
            trigger_focus: cx.focus_handle(),
            trigger: SharedString::default(),
            trigger_icon: None,
            content: None,
            placement: Placement::Below,
            dismissable: true,
            open: false,
            pending_focus: false,
            trap: FocusTrap::new(),
        }
    }

    /// The label of the control that opens the surface.
    pub fn trigger(mut self, label: impl Into<SharedString>) -> Self {
        self.trigger = label.into();
        self
    }

    pub fn trigger_icon(mut self, icon: Icon) -> Self {
        self.trigger_icon = Some(icon);
        self
    }

    /// Supplies the body, rebuilt on every frame the popover is open.
    pub fn content(
        mut self,
        content: impl Fn(&mut Window, &mut App) -> AnyElement + 'static,
    ) -> Self {
        self.content = Some(Rc::new(content));
        self
    }

    pub fn placement(mut self, placement: Placement) -> Self {
        self.placement = placement;
        self
    }

    /// Whether escape and a click outside close the surface. A popover that is
    /// not dismissable installs neither handler.
    pub fn dismissable(mut self, dismissable: bool) -> Self {
        self.dismissable = dismissable;
        self
    }

    pub fn is_open(&self) -> bool {
        self.open
    }

    pub fn is_dismissable(&self) -> bool {
        self.dismissable
    }

    pub fn open(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        if self.open {
            return;
        }
        self.open = true;
        self.pending_focus = true;
        self.trap.engage(window, cx);
        cx.emit(PopoverEvent::Opened);
        cx.notify();
    }

    /// Closes the surface and gives the keyboard back to the trigger.
    pub fn close(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        if !self.open {
            return;
        }
        self.open = false;
        self.pending_focus = false;
        self.trap.release(window, cx);
        self.trigger_focus.focus(window, cx);
        cx.emit(PopoverEvent::Closed);
        cx.notify();
    }

    pub fn toggle(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        if self.open {
            self.dismiss(window, cx);
        } else {
            self.open(window, cx);
        }
    }

    /// Reports a wave-away. A popover that is not dismissable cannot be waved
    /// away even by a host calling this directly.
    pub fn dismiss(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        if !self.open || !self.dismissable {
            return;
        }
        cx.emit(PopoverEvent::Dismissed);
        self.close(window, cx);
    }

    fn on_dismiss_key(
        &mut self,
        event: &KeyDownEvent,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if !self.open || event.keystroke.key.as_str() != "escape" {
            return;
        }
        self.dismiss(window, cx);
        cx.stop_propagation();
    }
}

impl Focusable for Popover {
    fn focus_handle(&self, _cx: &App) -> FocusHandle {
        self.focus_handle.clone()
    }
}

impl Render for Popover {
    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        let theme = cx.theme().clone();
        let popover = cx.entity().downgrade();
        let trigger = Button::new(self.ident.child("trigger"))
            .label(self.trigger.clone())
            .secondary()
            .track_focus(&self.trigger_focus)
            .when_some(self.trigger_icon, |button, glyph| button.icon(glyph))
            .on_click(move |window, cx| {
                popover
                    .update(cx, |popover, cx| popover.toggle(window, cx))
                    .ok();
            })
            .into_any_element();

        let overlay = self.open.then(|| {
            if self.pending_focus {
                // The handle can only take focus once this frame has put it in
                // the dispatch tree, which is why opening records the intent.
                self.pending_focus = false;
                self.focus_handle.focus(window, cx);
            }
            let body = self.content.clone().map(|content| content(window, cx));
            let mut card = surface(&theme, Elevation::Overlay)
                .p_token(&theme, Space::Sm)
                .track_focus(&self.focus_handle);
            if self.dismissable {
                card = card
                    .on_key_down(cx.listener(Self::on_dismiss_key))
                    .on_mouse_down_out(cx.listener(|popover, _, window, cx| {
                        popover.dismiss(window, cx);
                    }));
            }
            let card = card.children(body).semantic_in(
                cx,
                NodeSpec::new(self.ident.child("surface").semantic_id(), Role::Group)
                    .parent(self.ident.semantic_id())
                    .focus(&self.focus_handle),
            );
            Overlay::new(self.ident.child("overlay"))
                .placement(self.placement)
                .child(card)
                .into_any_element()
        });

        anchored_slot(self.placement, trigger, overlay).semantic_in(
            cx,
            NodeSpec::new(self.ident.semantic_id(), Role::Group).expanded(self.open),
        )
    }
}

/// Upper-cases a section label and opens its letter spacing, which is the one
/// place in the library that shouts.
pub fn tracked_upper(label: &str) -> String {
    let mut output = String::with_capacity(label.len() * 2);
    for (index, character) in label.to_uppercase().chars().enumerate() {
        if index > 0 {
            output.push('\u{200A}');
        }
        output.push(character);
    }
    output
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn navigation_wraps_and_handles_empty_lists() {
        assert_eq!(step(None, 0, 1), None);
        assert_eq!(step(None, 3, 1), Some(0));
        assert_eq!(step(None, 3, -1), Some(2));
        assert_eq!(step(Some(2), 3, 1), Some(0));
        assert_eq!(step(Some(0), 3, -1), Some(2));
    }

    #[test]
    fn filtering_prefers_prefixes_and_is_stable() {
        let labels = ["main", "feature/main-sync", "master", "dev"];
        assert_eq!(filter_indices("ma", &labels), vec![0, 2, 1]);
        assert_eq!(filter_indices("", &labels), vec![0, 1, 2, 3]);
    }

    #[test]
    fn key_classification_keeps_modified_enter_distinct() {
        assert_eq!(classify_key("enter", false, false), MenuKey::Enter);
        assert_eq!(classify_key("enter", true, false), MenuKey::ModifiedEnter);
        assert_eq!(classify_key("escape", false, false), MenuKey::Escape);
    }

    #[test]
    fn a_submenu_is_entered_and_left_sideways() {
        assert_eq!(classify_key("right", false, false), MenuKey::Right);
        assert_eq!(classify_key("left", false, false), MenuKey::Left);
    }

    #[test]
    fn ranking_prefers_a_prefix_then_a_word_then_a_subsequence() {
        assert_eq!(match_rank("com", "Command palette"), Some(0));
        assert_eq!(match_rank("pal", "Command palette"), Some(1));
        assert_eq!(match_rank("mmand", "Command palette"), Some(2));
        assert_eq!(match_rank("cmp", "Command palette"), Some(3));
        assert_eq!(match_rank("zz", "Command palette"), None);
    }

    #[test]
    fn filtering_orders_literal_matches_ahead_of_a_subsequence() {
        let labels = ["Set theme", "Reset zoom", "Show settings", "Save file"];
        assert_eq!(filter_indices("se", &labels), vec![0, 2, 1, 3]);
    }

    #[test]
    fn type_ahead_wraps_and_skips_entries_it_cannot_land_on() {
        let labels = [
            Some("Copy"),
            None,
            Some("Cut"),
            Some("Paste"),
            Some("Copy path"),
        ];
        assert_eq!(jump_to(&labels, None, 'c'), Some(0));
        assert_eq!(jump_to(&labels, Some(0), 'c'), Some(2));
        assert_eq!(jump_to(&labels, Some(2), 'c'), Some(4));
        assert_eq!(jump_to(&labels, Some(4), 'c'), Some(0));
        assert_eq!(jump_to(&labels, None, 'z'), None);
    }

    #[test]
    fn only_an_unmodified_letter_is_type_ahead() {
        let none = gpui::Modifiers::none();
        assert_eq!(typed_letter("s", none), Some('s'));
        assert_eq!(typed_letter("escape", none), None);
        assert_eq!(typed_letter("s", gpui::Modifiers::command()), None);
    }
}