gpui-cn 0.5.0

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
//! A command palette: a search field over a filtered list of commands,
//! with groups, the commands' shortcuts, and keyboard navigation, as
//! shadcn's `Command`.
//!
//! [`CommandState`] holds the commands, the query, and the highlight;
//! [`Command`] draws them.

mod item;

use std::rc::Rc;

use gpui_kit::{
    AnyElement, App, AppContext as _, Context, ElementId, Entity, EventEmitter, FocusHandle,
    Focusable, InteractiveElement as _, IntoElement, KeyBinding, ListAlignment, ListState,
    ParentElement as _, RenderOnce, Role, SharedString, StatefulInteractiveElement as _,
    StyleRefinement, Styled, Subscription, Task, Window,
    base::{
        StyledExt as _, TestSupportExt as _,
        actions::{Cancel, Confirm, SelectDown, SelectUp},
        input::{InputEvent, InputState},
        v_flex,
    },
    div,
    prelude::FluentBuilder as _,
};

use item::Row;
pub use item::{CommandEntry, CommandGroup, CommandItem, CommandRow};

use crate::{
    ActiveTheme as _, ScrollArea, Theme,
    menu::{
        MenuLook, label_block, line_slot, row_frame, row_line, search_row, search_style, separator,
        shortcut,
    },
};

/// What a palette shows while no command matches, as shadcn's demo.
const DEFAULT_EMPTY: &str = "No results.";

/// The key context of a palette, which takes Up, Down, Enter, and Escape
/// the search field passes on.
const CONTEXT: &str = "GpuiCnCommand";

pub(crate) fn init(cx: &mut App) {
    cx.bind_keys([
        KeyBinding::new("up", SelectUp, Some(CONTEXT)),
        KeyBinding::new("down", SelectDown, Some(CONTEXT)),
        KeyBinding::new("enter", Confirm { secondary: false }, Some(CONTEXT)),
        KeyBinding::new("escape", Cancel, Some(CONTEXT)),
    ]);
}

/// A search the application runs itself: given the query, it returns the
/// commands to show, when it has them. See
/// [`CommandState::with_search_handler`].
pub type CommandSearchHandler =
    Rc<dyn Fn(SharedString, &mut Context<CommandState>) -> Task<Vec<CommandEntry>>>;

/// A chosen command's action and the element it goes to.
type Pending = (Box<dyn gpui_kit::Action>, FocusHandle);

/// What a [`CommandState`] reports.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CommandEvent {
    /// The query changed. The payload is the new query.
    QueryChanged(SharedString),
    /// A command was chosen, after its action was dispatched. The payload
    /// is its key.
    Confirmed(SharedString),
    /// Enter was pressed while no command matched the query. The payload
    /// is the query, such as a path or a URL to open.
    Submitted(SharedString),
}

/// The commands of a [`Command`] palette, its query, and its highlight.
///
/// Owned by the view that shows the palette, which observes it so a
/// change renders. The query filters the commands by label and keywords
/// as a fuzzy subsequence, ignoring case, and puts the highlight on the first match; Up and Down
/// move it, and Enter chooses it. Choosing a command, or submitting a
/// query, clears the query for the next time.
pub struct CommandState {
    input: Entity<InputState>,
    entries: Vec<CommandEntry>,
    rows: Vec<Row>,
    highlighted: Option<usize>,
    list: ListState,
    handler: Option<CommandSearchHandler>,
    /// The running search. Dropping it cancels it, so a newer query
    /// cancels the search before it and no stale rows arrive.
    search: Option<Task<()>>,
    _subscriptions: Vec<Subscription>,
}

impl EventEmitter<CommandEvent> for CommandState {}

/// The search field's focus, which holds the keyboard for the palette.
impl Focusable for CommandState {
    fn focus_handle(&self, cx: &App) -> FocusHandle {
        self.input.read(cx).focus_handle(cx)
    }
}

impl CommandState {
    /// A palette with no commands, whose search field shows `placeholder`.
    pub fn new(
        placeholder: impl Into<SharedString>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Self {
        let style = search_style(cx.theme());
        let input = cx.new(|cx| {
            let mut input = InputState::new(window, cx).placeholder(placeholder);
            input.set_editor_style(style);
            input
        });
        let subscription = cx.subscribe(&input, |this, input, event: &InputEvent, cx| {
            if matches!(event, InputEvent::Change) {
                let query = input.read(cx).value();
                this.query_changed(query, cx);
            }
        });
        let overdraw = cx.theme().metrics.menu_max_height;
        Self {
            input,
            entries: Vec::new(),
            rows: Vec::new(),
            highlighted: None,
            list: ListState::new(0, ListAlignment::Top, overdraw),
            handler: None,
            search: None,
            _subscriptions: vec![subscription],
        }
    }

    /// Seeds the commands.
    pub fn with_entries(
        mut self,
        entries: impl IntoIterator<Item = impl Into<CommandEntry>>,
    ) -> Self {
        self.entries = entries.into_iter().map(Into::into).collect();
        self.refilter("");
        self
    }

    /// Answers the query with `handler` in place of the local filter:
    /// for each change, and for the empty query when set, it gets the
    /// query and returns a task with the commands to show. The task can
    /// match on a background thread, fuzzily, or ask a server; a newer
    /// query cancels the task before it. The commands shown stay until
    /// the answer arrives.
    ///
    /// ```
    /// use gpui_cn::{CommandEntry, CommandItem, CommandState};
    /// use gpui_kit::{AppContext as _, Context, Window};
    ///
    /// fn files(window: &mut Window, cx: &mut Context<CommandState>) -> CommandState {
    ///     CommandState::new("Open any file", window, cx).with_search_handler(|query, cx| {
    ///         let paths = cx.background_spawn(async move {
    ///             // Any search: fuzzy, indexed, or a request.
    ///             vec![format!("src/{query}.rs")]
    ///         });
    ///         cx.spawn(async move |_, _| {
    ///             paths
    ///                 .await
    ///                 .into_iter()
    ///                 .map(|path| CommandEntry::from(CommandItem::new(path.clone(), path)))
    ///                 .collect()
    ///         })
    ///     }, cx)
    /// }
    /// ```
    pub fn with_search_handler(
        mut self,
        handler: impl Fn(SharedString, &mut Context<Self>) -> Task<Vec<CommandEntry>> + 'static,
        cx: &mut Context<Self>,
    ) -> Self {
        self.handler = Some(Rc::new(handler));
        self.run_search(SharedString::default(), cx);
        self
    }

    /// Whether a search handler is still answering the query.
    pub fn is_searching(&self) -> bool {
        self.search.is_some()
    }

    /// Replaces the commands, as an answer to the query. The highlight
    /// stays on the command with the same key when there is one, and a
    /// search handler's answer still on its way is dropped, so it cannot
    /// replace these commands.
    pub fn set_entries(
        &mut self,
        entries: impl IntoIterator<Item = impl Into<CommandEntry>>,
        cx: &mut Context<Self>,
    ) {
        self.search = None;
        let highlighted = self.highlighted();
        self.entries = entries.into_iter().map(Into::into).collect();
        let query = self.query(cx);
        self.refilter(&query);
        if let Some(key) = highlighted
            && let Some(row) = self.row_of(&key)
        {
            self.highlight(row);
        }
        cx.notify();
    }

    /// The search query.
    pub fn query(&self, cx: &App) -> SharedString {
        self.input.read(cx).value()
    }

    /// Replaces the search query.
    pub fn set_query(
        &mut self,
        query: impl Into<SharedString>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let query = query.into();
        if self.query(cx) == query {
            return;
        }
        self.input
            .update(cx, |input, cx| input.set_value(query.clone(), window, cx));
        // A value set from code does not report a change, so the rows
        // follow it here.
        self.query_changed(query, cx);
    }

    /// The query changed: the handler answers it, else the local filter.
    fn query_changed(&mut self, query: SharedString, cx: &mut Context<Self>) {
        if self.handler.is_some() {
            self.run_search(query.clone(), cx);
        } else {
            self.refilter(&query);
        }
        cx.emit(CommandEvent::QueryChanged(query));
        cx.notify();
    }

    /// Asks the handler for the commands of `query`, and shows them when
    /// they arrive unless a newer query has replaced the search.
    fn run_search(&mut self, query: SharedString, cx: &mut Context<Self>) {
        let Some(handler) = self.handler.clone() else {
            return;
        };
        let answer = handler(query, cx);
        // The rows answer the query before this one until the new answer
        // arrives, so Enter submits what was typed rather than choose one.
        self.highlighted = None;
        self.search = Some(cx.spawn(async move |this, cx| {
            let entries = answer.await;
            this.update(cx, |this, cx| {
                this.search = None;
                this.entries = entries;
                this.refilter("");
                cx.notify();
            })
            .ok();
        }));
    }

    /// The key of the command the keyboard or the pointer is on.
    pub fn highlighted(&self) -> Option<SharedString> {
        match self.rows.get(self.highlighted?)? {
            Row::Item(item) => Some(item.key().clone()),
            Row::Heading(_) | Row::Separator => None,
        }
    }

    /// How many commands match the query.
    pub fn matched_count(&self) -> usize {
        self.rows
            .iter()
            .filter(|row| matches!(row, Row::Item(_)))
            .count()
    }

    /// Filters the rows for `query` and puts the highlight on the first
    /// command that can be chosen.
    fn refilter(&mut self, query: &str) {
        // A handler's commands are the answer to the query already.
        let query = if self.handler.is_none() {
            query.trim().to_string()
        } else {
            String::new()
        };
        self.rows = item::rows(&self.entries, &query);
        self.list.reset(self.rows.len());
        self.highlighted = None;
        if let Some(first) = (0..self.rows.len()).find(|row| self.is_selectable(*row)) {
            self.highlight(first);
        }
    }

    fn row_of(&self, key: &SharedString) -> Option<usize> {
        self.rows.iter().position(|row| match row {
            Row::Item(item) => item.key() == key && !item.is_disabled(),
            Row::Heading(_) | Row::Separator => false,
        })
    }

    fn is_selectable(&self, row: usize) -> bool {
        matches!(self.rows.get(row), Some(Row::Item(item)) if !item.is_disabled())
    }

    fn highlight(&mut self, row: usize) {
        self.highlighted = Some(row);
        self.list.scroll_to_reveal_item(row);
    }

    /// Moves the highlight one command up or down, wrapping at the ends.
    fn move_highlight(&mut self, delta: isize, cx: &mut Context<Self>) {
        let count = self.rows.len() as isize;
        if count == 0 {
            return;
        }
        let mut row = self
            .highlighted
            .map(|row| row as isize)
            .unwrap_or(if delta > 0 { -1 } else { count });
        for _ in 0..count {
            row = (row + delta).rem_euclid(count);
            if self.is_selectable(row as usize) {
                self.highlight(row as usize);
                cx.notify();
                return;
            }
        }
    }

    /// The pointer moved onto `row`.
    fn hover_row(&mut self, row: usize, cx: &mut Context<Self>) {
        if self.is_selectable(row) && self.highlighted != Some(row) {
            self.highlighted = Some(row);
            cx.notify();
        }
    }

    /// Chooses `row`: clears the query for the next time, reports the
    /// command, and hands back its action and where it goes, to dispatch
    /// once the state is released.
    fn choose(
        &mut self,
        row: usize,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<Pending> {
        let Some(Row::Item(item)) = self.rows.get(row).cloned() else {
            return None;
        };
        if item.is_disabled() {
            return None;
        }
        let pending = item.dispatched_action().map(|action| {
            let context = item
                .own_action_context()
                .cloned()
                .unwrap_or_else(|| self.input.read(cx).focus_handle(cx));
            (action.boxed_clone(), context)
        });
        self.set_query("", window, cx);
        cx.emit(CommandEvent::Confirmed(item.key().clone()));
        pending
    }

    /// Enter: chooses the highlighted command, or submits the query when
    /// no command matches it.
    fn confirm(&mut self, window: &mut Window, cx: &mut Context<Self>) -> Option<Pending> {
        match self.highlighted {
            Some(row) => self.choose(row, window, cx),
            None => {
                let query = self.query(cx);
                if !query.trim().is_empty() {
                    self.set_query("", window, cx);
                    cx.emit(CommandEvent::Submitted(query));
                }
                None
            }
        }
    }

    /// Chooses as the pointer or the keyboard does, then dispatches the
    /// command's action once the state is released, so a handler that
    /// reads or updates the palette can.
    fn activate(
        this: &Entity<Self>,
        choose: impl FnOnce(&mut Self, &mut Window, &mut Context<Self>) -> Option<Pending>,
        window: &mut Window,
        cx: &mut App,
    ) {
        if let Some((action, context)) = this.update(cx, |state, cx| choose(state, window, cx)) {
            context.dispatch_action(action.as_ref(), window, cx);
        }
    }
}

/// A command palette on a [`CommandState`], as shadcn's `Command`: the
/// search field, a hairline, and the matching commands in a scroll
/// region, drawn with a menu's rows.
///
/// ```
/// use gpui_cn::{Command, CommandItem, CommandState};
/// use gpui_kit::{AppContext as _, Context, Entity, IntoElement, Window};
///
/// fn palette(window: &mut Window, cx: &mut Context<()>) -> Entity<CommandState> {
///     cx.new(|cx| {
///         CommandState::new("Type a command or search...", window, cx)
///             .with_entries([CommandItem::new("calendar", "Calendar")])
///     })
/// }
///
/// fn view(state: &Entity<CommandState>) -> impl IntoElement {
///     Command::new("palette", state)
/// }
/// ```
///
/// The palette draws its own frame; inside a [`Popover`](crate::Popover)
/// or another frame, turn it off with [`bordered`](Self::bordered). The
/// id keys the rows, so it must be stable across frames.
#[derive(IntoElement)]
pub struct Command {
    id: ElementId,
    state: Entity<CommandState>,
    bordered: bool,
    empty: SharedString,
    style: StyleRefinement,
}

impl Command {
    /// A palette on `state`, with a stable id.
    pub fn new(id: impl Into<ElementId>, state: &Entity<CommandState>) -> Self {
        Self {
            id: id.into(),
            state: state.clone(),
            bordered: true,
            empty: DEFAULT_EMPTY.into(),
            style: StyleRefinement::default(),
        }
    }

    /// Whether the palette draws its own frame. On by default.
    pub fn bordered(mut self, bordered: bool) -> Self {
        self.bordered = bordered;
        self
    }

    /// The text shown while no command matches. The default is shadcn's
    /// "No results."
    pub fn empty(mut self, text: impl Into<SharedString>) -> Self {
        self.empty = text.into();
        self
    }
}

impl Styled for Command {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl RenderOnce for Command {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let look = MenuLook::of(cx.theme(), window.rem_size());
        let pointer_cursors = Theme::global(cx).pointer_cursors;
        let state = self.state;
        let (input, list, empty) = {
            let state = state.read(cx);
            (
                state.input.clone(),
                state.list.clone(),
                state.rows.is_empty(),
            )
        };
        let child = |name: &'static str| ElementId::NamedChild(self.id.clone().into(), name.into());
        let body = if empty {
            div()
                .py_6()
                .text_center()
                .text_color(look.muted_foreground)
                .child(self.empty)
                .into_any_element()
        } else {
            let rows = Rows {
                id: self.id.clone(),
                state: state.clone(),
                look: look.clone(),
                pointer_cursors,
                action_context: input.read(cx).focus_handle(cx),
            };
            ScrollArea::list(
                child("rows"),
                &list,
                look.row_height,
                move |row, window, cx| rows.render(row, window, cx),
            )
            .max_h(look.rows_max_height(true))
            .into_any_element()
        };
        let update = |f: fn(&mut CommandState, &mut Context<CommandState>)| {
            let state = state.clone();
            move |cx: &mut App| state.update(cx, f)
        };
        let (up, down) = (
            update(|state, cx| state.move_highlight(-1, cx)),
            update(|state, cx| state.move_highlight(1, cx)),
        );
        v_flex()
            .id(self.id.clone())
            .test_support()
            .key_context(CONTEXT)
            .min_w(look.search_min_width)
            .max_w(look.max_width)
            .text_size(look.text_size)
            .line_height(look.line_height)
            .text_color(look.foreground)
            .when(self.bordered, |this| {
                this.p(look.padding)
                    .rounded(look.radius)
                    .bg(look.surface)
                    .border_1()
                    .border_color(look.border)
            })
            .refine_style(&self.style)
            .on_action(move |_: &SelectUp, _, cx| up(cx))
            .on_action(move |_: &SelectDown, _, cx| down(cx))
            .on_action({
                let state = state.clone();
                move |_: &Confirm, window, cx| {
                    CommandState::activate(
                        &state,
                        |state, window, cx| state.confirm(window, cx),
                        window,
                        cx,
                    );
                }
            })
            .on_action({
                let state = state.clone();
                // Escape clears a query first; with none it goes on, to
                // the popover or dialog around the palette.
                move |_: &Cancel, window, cx| {
                    if state.read(cx).query(cx).is_empty() {
                        cx.propagate();
                    } else {
                        state.update(cx, |state, cx| state.set_query("", window, cx));
                    }
                }
            })
            .child(
                search_row(&input, &look, window, cx)
                    .id(child("search"))
                    .test_support(),
            )
            .child(separator(&look))
            .child(body)
    }
}

/// What the rows are drawn from, owned by the list's row builder, which
/// outlives the render that made it.
struct Rows {
    id: ElementId,
    state: Entity<CommandState>,
    look: MenuLook,
    pointer_cursors: bool,
    /// Where the rows' actions go when an item names no context of its
    /// own, whose bindings their shortcuts show: the search field.
    action_context: FocusHandle,
}

impl Rows {
    fn render(&self, row: usize, window: &mut Window, cx: &mut App) -> AnyElement {
        let look = &self.look;
        let (entry, highlighted, query) = {
            let state = self.state.read(cx);
            let Some(entry) = state.rows.get(row).cloned() else {
                return div().into_any_element();
            };
            (entry, state.highlighted == Some(row), state.query(cx))
        };
        let context = &self.action_context;
        let item = match entry {
            Row::Separator => return separator(look),
            Row::Heading(text) => {
                return div()
                    .w_full()
                    .h(look.row_height)
                    .px(look.row_padding)
                    .flex()
                    .items_center()
                    .text_color(look.muted_foreground)
                    .child(text)
                    .into_any_element();
            }
            Row::Item(item) => item,
        };
        let disabled = item.is_disabled();
        let foreground = if disabled {
            look.muted_foreground
        } else {
            look.foreground
        };
        let shortcut = item.dispatched_action().and_then(|action| {
            shortcut(action, item.own_action_context().unwrap_or(context), window)
        });
        let row_id = ElementId::NamedChild(self.id.clone().into(), item.key().clone());
        let hover = self.state.clone();
        let choose = self.state.clone();
        let touch = look.touch;
        row_frame(look, highlighted && !disabled)
            .id(row_id)
            .test_support()
            .role(Role::ListBoxOption)
            .aria_label(item.label().clone())
            .when_some(shortcut.clone(), |this, shortcut| {
                this.aria_keyshortcuts(shortcut)
            })
            .when(highlighted && !disabled, |this| {
                this.aria_active_descendant()
            })
            .text_color(foreground)
            .map(|this| {
                if !disabled && self.pointer_cursors {
                    this.cursor_pointer()
                } else {
                    this.cursor_default()
                }
            })
            .map(|this| match item.renderer() {
                Some(render) => this.child(render(
                    CommandRow {
                        highlighted,
                        disabled,
                    },
                    window,
                    cx,
                )),
                None => this.child(
                    row_line()
                        .children(item.leading_icon().cloned().map(|icon| {
                            line_slot(look)
                                .w_4()
                                .justify_center()
                                .child(icon.size_4().text_color(foreground))
                        }))
                        .child(div().flex_1().min_w_0().child(label_block(
                            item.label().clone(),
                            &crate::fuzzy::matched(&query, item.label()).unwrap_or_default(),
                            None,
                            look,
                        )))
                        .children(shortcut.map(|shortcut| {
                            line_slot(look)
                                .ml_4()
                                .text_color(look.description)
                                .child(shortcut)
                        })),
                ),
            })
            // A row that scrolls under a resting pointer does not take the
            // keyboard's highlight; a finger has no hover.
            .when(!touch, |this| {
                this.on_mouse_move(move |_, _, cx| {
                    hover.update(cx, |state, cx| state.hover_row(row, cx));
                })
            })
            .on_click(move |_, window, cx| {
                CommandState::activate(
                    &choose,
                    |state, window, cx| state.choose(row, window, cx),
                    window,
                    cx,
                );
            })
            .into_any_element()
    }
}