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};
pub struct Tooltip {
text: SharedString,
shortcut: Option<SharedString>,
palette: Palette,
}
#[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,
}
}
pub fn shortcut(mut self, shortcut: impl Into<SharedString>) -> Self {
self.shortcut = Some(shortcut.into());
self
}
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 {
pub fn text(
text: impl Into<SharedString>,
palette: Palette,
) -> impl Fn(&mut Window, &mut App) -> AnyView + 'static {
Hint::new(text).tooltip(palette)
}
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)),
)
}
}
#[derive(Clone, Debug)]
pub struct Command {
pub id: SharedString,
pub label: SharedString,
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
}
}
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;
}
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;
}
}
Some(score - (candidate.len() as i32 / 8))
}
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()
}
pub const COMMAND_ROW_HEIGHT: f32 = 30.0;
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 {
let on_activate = Rc::new(on_activate);
let state = view.control_state();
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());
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(),
);
}
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>)>;
#[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)
}
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();
}
}))
}
#[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);
});
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()
.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()
.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"),
)
}),
)
.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);
}
}