vampir 0.1.2

Lit widget toolkit for GPUI: buttons, fields, pop-ups, menus and a text input, over a small OKLCH palette.
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
//! Things that float above the view: tooltips and a command palette, and
//! the search field with results that the palette is built from.

use std::rc::Rc;

use gpui::{
    AnyElement, AnyView, App, Context, Div, ElementId, Entity, FontWeight, InteractiveElement,
    MouseButton, MouseDownEvent, Render, SharedString, Window, deferred, div, prelude::*, px,
};

use crate::controls::search_field;
use crate::easing::{ease_out_cubic, progress};
use crate::keyboard::{self, Dismiss, Key, Orientation};
use crate::lighting;
use crate::palette::Palette;
use crate::state::{COMBO_REVEAL, ComboId, ControlHost, MOVE, SWITCH_SLIDE};
use crate::text_input::{self as text, TextInput};

// ---- Tooltip ----------------------------------------------------------------

/// A tooltip's content: one line, optionally with an accelerator after it.
///
/// GPUI already owns the hard part, which is deciding when a tooltip should
/// appear and where it fits. This is only what it looks like, handed to
/// `.tooltip(..)` on any interactive element.
pub struct Tooltip {
    text: SharedString,
    shortcut: Option<SharedString>,
    palette: Palette,
}

/// What a tooltip says: a label, and optionally the shortcut after it.
///
/// A plain `&str` converts into one, so a control that takes a `Hint` can
/// be handed `"Undo"`; `Hint::new("Favourite").shortcut(display("secondary-d"))`
/// adds the accelerator. Purely a label: binding the key is the host's
/// business.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Hint {
    pub text: SharedString,
    pub shortcut: Option<SharedString>,
}

impl Hint {
    pub fn new(text: impl Into<SharedString>) -> Self {
        Self {
            text: text.into(),
            shortcut: None,
        }
    }

    /// The accelerator shown after the label.
    pub fn shortcut(mut self, shortcut: impl Into<SharedString>) -> Self {
        self.shortcut = Some(shortcut.into());
        self
    }

    /// Builds the callback `.tooltip(..)` wants.
    pub fn tooltip(self, palette: Palette) -> impl Fn(&mut Window, &mut App) -> AnyView + 'static {
        move |_window, cx| {
            let hint = self.clone();
            cx.new(move |_cx| Tooltip {
                text: hint.text,
                shortcut: hint.shortcut,
                palette,
            })
            .into()
        }
    }
}

impl From<&str> for Hint {
    fn from(text: &str) -> Self {
        Hint::new(text.to_string())
    }
}

impl From<String> for Hint {
    fn from(text: String) -> Self {
        Hint::new(text)
    }
}

impl From<SharedString> for Hint {
    fn from(text: SharedString) -> Self {
        Hint::new(text)
    }
}

impl Tooltip {
    /// Builds the callback `.tooltip(..)` wants, for any interactive element
    /// of the host's own. GPUI allows one tooltip per element.
    ///
    /// ```ignore
    /// div().id("thumbnail").child(image).tooltip(Tooltip::text("Open", palette))
    /// ```
    pub fn text(
        text: impl Into<SharedString>,
        palette: Palette,
    ) -> impl Fn(&mut Window, &mut App) -> AnyView + 'static {
        Hint::new(text).tooltip(palette)
    }

    /// The same, with an accelerator shown after the label.
    pub fn with_shortcut(
        text: impl Into<SharedString>,
        shortcut: impl Into<SharedString>,
        palette: Palette,
    ) -> impl Fn(&mut Window, &mut App) -> AnyView + 'static {
        Hint::new(text).shortcut(shortcut).tooltip(palette)
    }
}

impl Render for Tooltip {
    fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
        let palette = self.palette;
        let dark = palette.is_dark;
        let fill = if dark {
            palette.soft_fill
        } else {
            palette.field_surface
        };
        div()
            .px(px(8.0))
            .py(px(4.0))
            .flex()
            .items_center()
            .gap(px(8.0))
            .rounded(px(5.0))
            .bg(fill)
            .border_1()
            .border_color(lighting::rim(fill, dark))
            .shadow(lighting::panel(dark))
            .text_size(px(11.5))
            .text_color(palette.text_primary)
            .whitespace_nowrap()
            .child(self.text.clone())
            .children(
                self.shortcut
                    .clone()
                    .map(|shortcut| div().text_color(palette.text_secondary).child(shortcut)),
            )
    }
}

// ---- Command palette --------------------------------------------------------

/// One entry in a [`command_palette`].
#[derive(Clone, Debug)]
pub struct Command {
    /// Comes back to the activation callback.
    pub id: SharedString,
    pub label: SharedString,
    /// Where the command lives, shown quietly before the label.
    pub group: Option<SharedString>,
    pub shortcut: Option<SharedString>,
}

impl Command {
    pub fn new(id: impl Into<SharedString>, label: impl Into<SharedString>) -> Self {
        Self {
            id: id.into(),
            label: label.into(),
            group: None,
            shortcut: None,
        }
    }

    pub fn group(mut self, group: impl Into<SharedString>) -> Self {
        self.group = Some(group.into());
        self
    }

    pub fn shortcut(mut self, shortcut: impl Into<SharedString>) -> Self {
        self.shortcut = Some(shortcut.into());
        self
    }
}

/// How well a query matches a candidate, or `None` if it does not.
///
/// Subsequence matching, the way every fuzzy finder works: the query's
/// characters must appear in order, but not together. The score rewards
/// matches that start a word and matches that run on from the last one, so
/// "opf" ranks "Open File" above "Optional Prefix".
pub fn fuzzy_score(query: &str, candidate: &str) -> Option<i32> {
    if query.is_empty() {
        return Some(0);
    }
    let candidate_lower = candidate.to_lowercase();
    let mut haystack = candidate_lower.char_indices().peekable();
    let mut score = 0i32;
    let mut last_index: Option<usize> = None;

    for needle in query.to_lowercase().chars() {
        if needle.is_whitespace() {
            continue;
        }
        loop {
            let (index, ch) = haystack.next()?;
            if ch != needle {
                continue;
            }
            // Starting a word is the strongest signal that this is the
            // match the person meant.
            let starts_word = index == 0
                || candidate_lower[..index]
                    .chars()
                    .next_back()
                    .is_some_and(|previous| previous == ' ' || previous == '-' || previous == '_');
            score += if starts_word { 12 } else { 2 };
            if last_index == Some(index.saturating_sub(1)) {
                score += 6;
            }
            last_index = Some(index);
            break;
        }
    }
    // Among equally good matches, the shortest candidate is the one that
    // most nearly *is* the query.
    Some(score - (candidate.len() as i32 / 8))
}

/// Commands matching a query, best first. Ties keep their original order,
/// so a host's own ranking survives where the score cannot separate two.
pub fn fuzzy_filter(query: &str, commands: &[Command]) -> Vec<Command> {
    let mut scored: Vec<(i32, usize, Command)> = commands
        .iter()
        .enumerate()
        .filter_map(|(index, command)| {
            let haystack = match &command.group {
                Some(group) => format!("{group} {}", command.label),
                None => command.label.to_string(),
            };
            fuzzy_score(query, &haystack).map(|score| (score, index, command.clone()))
        })
        .collect();
    scored.sort_by(|a, b| b.0.cmp(&a.0).then(a.1.cmp(&b.1)));
    scored.into_iter().map(|(_, _, command)| command).collect()
}

/// Height of one row in a [`command_list`].
pub const COMMAND_ROW_HEIGHT: f32 = 30.0;

/// A list of matching commands, one row each, with one highlighted.
///
/// The rows on their own, for a host that filters and moves the highlight
/// itself; [`search_list`] is these under a search field with the keys
/// wired, and [`command_palette`] is that in an overlay. Each row reports
/// its `Command::id` when clicked.
pub fn command_list<V: ControlHost>(
    id: &'static str,
    matches: &[Command],
    highlighted: usize,
    palette: Palette,
    view: &V,
    cx: &mut Context<V>,
    on_activate: impl Fn(&mut V, SharedString, &mut Window, &mut Context<V>) + 'static,
) -> Div {
    // A concrete `Div` rather than `impl IntoElement`: in the 2024 edition an
    // opaque return captures every input lifetime, which would keep `cx`
    // borrowed for as long as the list lived and stop the caller building
    // anything else with it.
    let on_activate = Rc::new(on_activate);
    let state = view.control_state();
    // A list that has just appeared arrives whole, with whatever brought it.
    // Once it is up, rows the query lets through fade in at their place and
    // rows that stay slide to their new one, so typing reads as the list
    // being sifted rather than replaced.
    let fresh = !state.present(id);
    let mut rows: Vec<AnyElement> = Vec::with_capacity(matches.len());
    for (index, command) in matches.iter().enumerate() {
        let cx: &mut Context<V> = &mut *cx;
        let on_activate = on_activate.clone();
        let command_id = command.id.clone();
        let active = index == highlighted;
        let key = |what: &str| ElementId::Name(format!("{id}-{what}-{command_id}").into());
        // Keyed by the command rather than the row, so the highlight and the
        // fade belong to the command as it moves.
        let on = state.blend(&key("lit"), active, SWITCH_SLIDE);
        let shown = if fresh {
            state.tween(key("shown"), 1.0, SWITCH_SLIDE)
        } else {
            state.tween_from(key("shown"), 0.0, 1.0, SWITCH_SLIDE)
        };
        let place = index as f32 * (COMMAND_ROW_HEIGHT + 1.0);
        let offset = state.tween(key("place"), place, MOVE) - place;
        rows.push(
            div()
                .id(ElementId::NamedInteger(
                    format!("{id}-command").into(),
                    index as u64,
                ))
                .relative()
                .top(px(offset))
                .opacity(shown)
                .h(px(COMMAND_ROW_HEIGHT))
                .flex_none()
                .w_full()
                .px(px(9.0))
                .flex()
                .items_center()
                .gap(px(8.0))
                .rounded(px(5.0))
                .cursor_pointer()
                .text_size(px(12.5))
                .text_color(crate::color::lerp(
                    palette.text_primary,
                    palette.control_label,
                    on,
                ))
                .when(on > 0.01, |el| {
                    el.bg(lighting::lit_at(palette.control_fill, 0.08, on))
                })
                .when(!active, |el| {
                    el.hover(move |style| style.bg(palette.row_hover))
                })
                .on_click(cx.listener(move |this, _event, window, cx| {
                    on_activate(this, command_id.clone(), window, cx);
                    cx.notify();
                }))
                .children(command.group.clone().map(|group| {
                    div()
                        .flex_none()
                        .text_color(palette.text_secondary)
                        .child(group)
                }))
                .child(
                    div()
                        .flex_1()
                        .overflow_hidden()
                        .child(command.label.clone()),
                )
                .children(command.shortcut.clone().map(|shortcut| {
                    div()
                        .flex_none()
                        .text_size(px(11.5))
                        .text_color(palette.text_secondary)
                        .child(shortcut)
                }))
                .into_any_element(),
        );
    }
    // Clipped, so a row sliding up from where it was emerges from the list's
    // edge instead of crossing whatever sits below it.
    div()
        .flex()
        .flex_col()
        .gap(px(1.0))
        .overflow_hidden()
        .children(rows)
}

type Activate<V> = Rc<dyn Fn(&mut V, SharedString, &mut Window, &mut Context<V>)>;

/// A search field with the rows that match it underneath, and the keys
/// between the two: Up and Down move the highlight, Enter picks it.
///
/// The host owns the query input and the items; this filters the items by
/// the query with [`fuzzy_filter`], keeps the keyboard's row in
/// [`ControlState`](crate::ControlState) under `id`, and reports the picked
/// `Command::id` — from a click and from Enter alike. The keys arrive as the
/// field's own actions: a single-line field has no use for Up, Down or an
/// Enter it has nothing to submit to, so it passes them on, and they are
/// caught here on the element that holds the field — which is also why they
/// only work while the field has the keyboard.
#[allow(clippy::too_many_arguments)]
pub fn search_list<V: ControlHost>(
    id: &'static str,
    query: &Entity<TextInput>,
    items: &[Command],
    palette: Palette,
    view: &V,
    window: &Window,
    cx: &mut Context<V>,
    on_activate: impl Fn(&mut V, SharedString, &mut Window, &mut Context<V>) + 'static,
) -> Div {
    let on_activate: Activate<V> = Rc::new(on_activate);
    let text = query.read(cx).text();
    let matches = fuzzy_filter(&text, items);
    let highlighted = view
        .control_state()
        .list_highlight(id, &text)
        .min(matches.len().saturating_sub(1));
    let rows = {
        let on_activate = on_activate.clone();
        command_list(
            id,
            &matches,
            highlighted,
            palette,
            view,
            cx,
            move |view, id, window, cx| on_activate(view, id, window, cx),
        )
    };
    let ids = matches.iter().map(|command| command.id.clone()).collect();
    let list = div()
        .flex()
        .flex_col()
        .gap(px(6.0))
        .child(search_field(id, query, palette, window, cx))
        .child(rows);
    list_keys(list, id, ids, cx, on_activate)
}

/// Puts a filtering list's keys on the element that holds its query field:
/// Up and Down move the highlight through `ids`, Enter picks the
/// highlighted one.
fn list_keys<V: ControlHost, E: InteractiveElement>(
    element: E,
    id: &'static str,
    ids: Vec<SharedString>,
    cx: &mut Context<V>,
    on_activate: Activate<V>,
) -> E {
    let count = ids.len();
    let step = move |this: &mut V, key: Key, cx: &mut Context<V>| {
        let state = this.control_state();
        let here = state.list_position(id).min(count.saturating_sub(1));
        if let Some(moved) = keyboard::step(key, Orientation::Vertical, here, count) {
            state.highlight_list(id, moved);
            cx.notify();
        }
    };
    let step_down = step;
    element
        .on_action(cx.listener(move |this, _: &text::Up, _window, cx| {
            step(this, Key::Up, cx);
        }))
        .on_action(cx.listener(move |this, _: &text::Down, _window, cx| {
            step_down(this, Key::Down, cx);
        }))
        .on_action(cx.listener(move |this, _: &text::Enter, window, cx| {
            let here = this
                .control_state()
                .list_position(id)
                .min(count.saturating_sub(1));
            if let Some(picked) = ids.get(here) {
                on_activate(this, picked.clone(), window, cx);
                cx.notify();
            }
        }))
}

/// Centred overlay with a filter field and a list of matching commands.
///
/// Open it with [`ControlState::open_palette`](crate::ControlState::open_palette)
/// — or [`toggle_palette`](crate::ControlState::toggle_palette), from
/// whatever shortcut the host gives it; while `id` is not open this returns
/// nothing. The host owns the query input and the commands. The palette
/// filters them with [`fuzzy_filter`], moves the highlight with Up and Down,
/// runs the highlighted command on Enter or a clicked one, and closes on
/// Escape or a press on the scrim. It closes before a command runs, handing
/// the keyboard back where it was, so a command that opens something starts
/// from the right place.
#[allow(clippy::too_many_arguments)]
pub fn command_palette<V: ControlHost>(
    id: ComboId,
    query: &Entity<TextInput>,
    commands: &[Command],
    palette: Palette,
    view: &V,
    window: &Window,
    cx: &mut Context<V>,
    on_activate: impl Fn(&mut V, SharedString, &mut Window, &mut Context<V>) + 'static,
) -> Option<impl IntoElement> {
    let state = view.control_state();
    let opened_at = state
        .palette_overlay
        .as_ref()
        .filter(|open| open.id == id)?
        .opened_at;
    let dark = palette.is_dark;
    let on_activate: Activate<V> = Rc::new(move |view: &mut V, picked, window, cx| {
        view.control_state_mut().close_palette(window, cx);
        on_activate(view, picked, window, cx);
    });
    // The panel arrives the way a menu does: the same reveal, a short drift
    // down into place.
    let reveal = ease_out_cubic(progress(opened_at, state.scaled(COMBO_REVEAL)));
    let list_scroll = state.scroll(ElementId::Name(format!("{id}-list").into()));
    let fill = if dark {
        palette.soft_fill
    } else {
        palette.field_surface
    };

    let text = query.read(cx).text();
    let matches = fuzzy_filter(&text, commands);
    let highlighted = state
        .list_highlight(id, &text)
        .min(matches.len().saturating_sub(1));
    let empty = matches.is_empty();
    let rows = {
        let on_activate = on_activate.clone();
        command_list(
            id,
            &matches,
            highlighted,
            palette,
            view,
            cx,
            move |view, id, window, cx| on_activate(view, id, window, cx),
        )
    };
    let ids = matches.iter().map(|command| command.id.clone()).collect();

    let panel = div()
        // A little above centre: the list grows downward, and a palette
        // pinned to the middle ends up low on the screen as soon as it has
        // results.
        .mt(gpui::relative(0.16))
        .relative()
        .top(px(-6.0 * (1.0 - reveal)))
        .opacity(reveal)
        .w(px(520.0))
        .max_w(gpui::relative(0.9))
        .flex()
        .flex_col()
        .gap(px(6.0))
        .p(px(8.0))
        .rounded(px(10.0))
        .bg(fill)
        .border_1()
        .border_color(lighting::rim(fill, dark))
        .shadow(lighting::panel(dark))
        .occlude()
        // Once `bind_keys` has run, Escape arrives as this action and never
        // as a key. The query field has the keyboard, and this is the first
        // thing above it.
        .on_action(cx.listener(move |this, _: &Dismiss, window, cx| {
            this.control_state_mut().close_palette(window, cx);
            cx.notify();
        }))
        .child(search_field(id, query, palette, window, cx))
        .child(
            div()
                .relative()
                .child(
                    div()
                        .id(ElementId::Name(format!("{id}-list").into()))
                        .max_h(px(340.0))
                        .flex()
                        .flex_col()
                        .gap(px(1.0))
                        .overflow_y_scroll()
                        .restrict_scroll_to_axis()
                        .track_scroll(&list_scroll)
                        .child(rows)
                        .when(empty, |el| {
                            el.child(
                                div()
                                    .h(px(30.0))
                                    .px(px(9.0))
                                    .flex()
                                    .items_center()
                                    .text_size(px(12.5))
                                    .font_weight(FontWeight::NORMAL)
                                    .text_color(palette.text_secondary)
                                    .child("No matching commands"),
                            )
                        }),
                )
                // A long list fades at the edge that has more past it,
                // rather than slicing a row in half.
                .children(crate::scroll::scroll_fades(
                    state,
                    id,
                    &list_scroll,
                    crate::scroll::ScrollAxis::Vertical,
                    fill,
                    fill,
                )),
        );
    let panel = list_keys(panel, id, ids, cx, on_activate);

    Some(
        deferred(
            div()
                .id(ElementId::Name(format!("{id}-scrim").into()))
                .absolute()
                .inset_0()
                .flex()
                .flex_col()
                .items_center()
                .bg(crate::color::with_alpha(
                    palette.scrim(),
                    palette.scrim().alpha * reveal,
                ))
                .occlude()
                .on_mouse_down(
                    MouseButton::Left,
                    cx.listener(move |this, _event: &MouseDownEvent, window, cx| {
                        this.control_state_mut().close_palette(window, cx);
                        cx.notify();
                    }),
                )
                .child(panel),
        )
        .with_priority(180),
    )
}

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

    #[test]
    fn word_starts_outrank_scattered_letters() {
        let open_file = fuzzy_score("opf", "Open File").expect("matches");
        let scattered = fuzzy_score("opf", "Optional Prefix").expect("matches");
        assert!(
            open_file > scattered,
            "expected Open File ({open_file}) above Optional Prefix ({scattered})"
        );
    }

    #[test]
    fn a_query_that_is_not_a_subsequence_does_not_match() {
        assert!(fuzzy_score("zzz", "Open File").is_none());
        assert!(fuzzy_score("elif", "Open File").is_none());
    }

    #[test]
    fn an_empty_query_keeps_everything_in_its_original_order() {
        let commands = vec![Command::new("b", "Second"), Command::new("a", "First")];
        let filtered = fuzzy_filter("", &commands);
        let ids: Vec<&str> = filtered.iter().map(|command| command.id.as_ref()).collect();
        assert_eq!(ids, vec!["b", "a"]);
    }

    #[test]
    fn the_group_is_searched_along_with_the_label() {
        let commands = vec![Command::new("save", "Save").group("File")];
        assert_eq!(fuzzy_filter("file sa", &commands).len(), 1);
    }
}