use super::super::craft::{press, sans};
use super::super::press::{Handler, focus, hover, icon, rem, ring, track_hover};
use crate::{
components::{
dropdown_menu::{DropdownMenu, DropdownMenuKind, DropdownMenuSize},
input::InputState,
input_group::{
AddonAlign, InputGroup, InputGroupAddon, InputGroupInput, input_group_button,
},
kbd::Kbd,
menu::{self, MenuItem, MenuSize},
overlay,
},
icons::{CarbonIcon, Icon},
theme::color,
tokens::{ColorRole, SIZE_LG, SIZE_MD, SIZE_SM},
};
use gpui_kit::{
AnyElement, App, AppContext as _, ElementId, Entity, Focusable as _, FontWeight, Hsla,
InteractiveElement as _, IntoElement, KeyDownEvent, ParentElement, RenderOnce, ScrollHandle,
SharedString, StatefulInteractiveElement as _, Styled, Subscription, Window,
component::Sizable as _,
component::input::{Input, InputEvent},
div,
prelude::FluentBuilder as _,
px,
};
use std::rc::Rc;
pub struct PaletteItem {
title: SharedString,
subtitle: Vec<SharedString>,
icon: Option<Icon>,
leading: Option<AnyElement>,
shortcut: Option<SharedString>,
on_select: Option<Handler>,
}
impl PaletteItem {
pub fn new(title: impl Into<SharedString>) -> Self {
Self {
title: title.into(),
subtitle: Vec::new(),
icon: None,
leading: None,
shortcut: None,
on_select: None,
}
}
pub fn subtitle<S: Into<SharedString>>(mut self, parts: impl IntoIterator<Item = S>) -> Self {
self.subtitle = parts.into_iter().map(Into::into).collect();
self
}
pub fn icon(mut self, icon: impl Into<Icon>) -> Self {
self.icon = Some(icon.into());
self
}
pub fn leading(mut self, leading: impl IntoElement) -> Self {
self.leading = Some(leading.into_any_element());
self
}
pub fn shortcut(mut self, shortcut: impl Into<SharedString>) -> Self {
self.shortcut = Some(shortcut.into());
self
}
pub fn on_select(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_select = Some(Rc::new(f));
self
}
}
pub struct PaletteGroup {
label: SharedString,
items: Vec<PaletteItem>,
}
impl PaletteGroup {
pub fn new(
label: impl Into<SharedString>,
items: impl IntoIterator<Item = PaletteItem>,
) -> Self {
Self {
label: label.into(),
items: items.into_iter().collect(),
}
}
}
pub fn palette_matches(title: &str, subtitle: &[SharedString], query: &str) -> bool {
let sub: Vec<&str> = subtitle.iter().map(|s| s.as_ref()).collect();
format!("{title} {}", sub.join(","))
.to_lowercase()
.contains(&query.to_lowercase())
}
pub fn palette_step(key: &str, active: usize, len: usize) -> Option<usize> {
match key {
"down" if len > 0 => Some((active + 1).min(len - 1)),
"up" => Some(active.saturating_sub(1)),
_ => None,
}
}
struct PaletteState {
input: Entity<InputState>,
active: usize,
focused_once: bool,
scroll: ScrollHandle,
_subscription: Subscription,
}
#[derive(IntoElement)]
pub struct CommandPalette {
id: ElementId,
groups: Vec<PaletteGroup>,
placeholder: SharedString,
empty: SharedString,
escape_key: SharedString,
hints: [SharedString; 3],
auto_focus: bool,
}
impl CommandPalette {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
groups: Vec::new(),
placeholder: "Search threads, projects and commands".into(),
empty: "No matching commands, projects, or threads.".into(),
escape_key: "Esc".into(),
hints: ["Navigate".into(), "Open".into(), "Open in new tab".into()],
auto_focus: true,
}
}
pub fn group(mut self, group: PaletteGroup) -> Self {
self.groups.push(group);
self
}
pub fn placeholder(mut self, text: impl Into<SharedString>) -> Self {
self.placeholder = text.into();
self
}
pub fn empty(mut self, text: impl Into<SharedString>) -> Self {
self.empty = text.into();
self
}
pub fn auto_focus(mut self, auto_focus: bool) -> Self {
self.auto_focus = auto_focus;
self
}
pub fn hints(mut self, hints: [SharedString; 3]) -> Self {
self.hints = hints;
self
}
}
fn kbd_row(keys: &[&'static str], text: SharedString) -> impl IntoElement {
div()
.flex()
.items_center()
.gap(px(2.))
.children(keys.iter().map(|k| Kbd::new(*k)))
.child(div().ml(px(2.)).child(text))
}
pub fn palette_row_colors(on: bool) -> (Hsla, Option<Hsla>) {
if on {
(
color(ColorRole::LayerSelected01),
Some(color(ColorRole::BorderInteractive)),
)
} else {
(gpui_kit::transparent_black(), None)
}
}
impl RenderOnce for CommandPalette {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id;
let state = window.use_keyed_state((id.clone(), "palette"), cx, |window, cx| {
let input = cx.new(|cx| InputState::new(window, cx));
let _subscription = cx.subscribe(&input, |this: &mut PaletteState, _, event, cx| {
if matches!(event, InputEvent::Change) {
this.active = 0;
cx.notify();
}
});
PaletteState {
input,
active: 0,
focused_once: false,
scroll: ScrollHandle::new(),
_subscription,
}
});
let input = state.read(cx).input.clone();
if self.auto_focus && !state.read(cx).focused_once {
let handle = input.read(cx).focus_handle(cx);
window.focus(&handle, cx);
state.update(cx, |s, _| s.focused_once = true);
}
let query = input.read(cx).value().to_string();
let shown: Vec<PaletteGroup> = self
.groups
.into_iter()
.map(|g| PaletteGroup {
label: g.label,
items: g
.items
.into_iter()
.filter(|it| palette_matches(&it.title, &it.subtitle, &query))
.collect(),
})
.filter(|g| !g.items.is_empty())
.collect();
let handlers: Vec<Option<Handler>> = shown
.iter()
.flat_map(|g| g.items.iter().map(|it| it.on_select.clone()))
.collect();
let len = handlers.len();
let active = state.read(cx).active.min(len.saturating_sub(1));
let scroll = state.read(cx).scroll.clone();
let mut rows: Vec<AnyElement> = Vec::new();
let mut item_rows: Vec<usize> = Vec::new();
let mut flat = 0usize;
for group in shown {
rows.push(
sans(div(), 12., 400)
.pt(rem(12.))
.px(rem(16.))
.pb(rem(4.))
.text_color(color(ColorRole::TextSecondary))
.child(group.label)
.into_any_element(),
);
for item in group.items {
let me = flat;
flat += 1;
item_rows.push(rows.len());
let on = me == active;
let (bg, bar) = palette_row_colors(on);
let lead: Option<AnyElement> = item.leading.or_else(|| {
item.icon.map(|i| {
i.size_length(rem(16.))
.color(color(ColorRole::IconSecondary))
.into_any_element()
})
});
let subtitle = (!item.subtitle.is_empty()).then(|| {
let mut parts: Vec<AnyElement> = Vec::new();
for (i, s) in item.subtitle.into_iter().enumerate() {
if i > 0 {
parts.push(
div()
.px(px(2.))
.text_color(color(ColorRole::TextHelper))
.child("·")
.into_any_element(),
);
}
parts.push(div().child(s).into_any_element());
}
sans(div(), 12., 400)
.flex()
.text_color(color(ColorRole::TextSecondary))
.children(parts)
});
let shortcut = item.shortcut.map(|s| {
let keys: Vec<String> = s.split(' ').map(str::to_string).collect();
div()
.flex()
.flex_none()
.gap(px(2.))
.children(keys.into_iter().map(Kbd::new))
});
let hover_state = state.clone();
let select = item.on_select.clone();
rows.push(
div()
.id((id.clone(), SharedString::from(format!("item-{me}"))))
.relative()
.flex()
.items_center()
.gap(rem(12.))
.min_h(SIZE_MD.rems())
.py(rem(4.))
.px(rem(16.))
.bg(bg)
.cursor_pointer()
.on_hover(move |over, _, cx| {
if *over {
hover_state.update(cx, |s, cx| {
if s.active != me {
s.active = me;
cx.notify();
}
})
}
})
.on_click(move |_, w, cx| {
if let Some(f) = &select {
f(w, cx)
}
})
.when_some(bar, |d, c| {
d.child(div().absolute().left_0().top_0().bottom_0().w(px(3.)).bg(c))
})
.child(
div()
.flex()
.flex_none()
.w(rem(20.))
.justify_center()
.children(lead),
)
.child(
div()
.flex()
.flex_col()
.flex_1()
.min_w_0()
.child(
sans(div(), 14., 400)
.text_color(color(ColorRole::TextPrimary))
.truncate()
.child(item.title),
)
.children(subtitle),
)
.children(shortcut)
.into_any_element(),
);
}
}
let on_key = {
let state = state.clone();
let handlers = handlers.clone();
let scroll = scroll.clone();
let item_rows = item_rows.clone();
move |event: &KeyDownEvent, window: &mut Window, cx: &mut App| {
let key = event.keystroke.key.as_str();
let current = state.read(cx).active.min(len.saturating_sub(1));
if let Some(next) = palette_step(key, current, len) {
state.update(cx, |s, cx| {
s.active = next;
cx.notify();
});
if let Some(row) = item_rows.get(next) {
scroll.scroll_to_item(*row);
}
cx.stop_propagation();
} else if key == "enter" {
if let Some(Some(f)) = handlers.get(current) {
f(window, cx);
}
cx.stop_propagation();
}
}
};
let list = if len == 0 {
div()
.pb(rem(8.))
.child(
sans(div(), 14., 400)
.py(rem(32.))
.px(rem(16.))
.flex()
.justify_center()
.text_color(color(ColorRole::TextSecondary))
.child(self.empty),
)
.into_any_element()
} else {
div()
.id((id.clone(), "list"))
.flex()
.flex_col()
.max_h(rem(320.))
.overflow_y_scroll()
.track_scroll(&scroll)
.pb(rem(8.))
.children(rows)
.into_any_element()
};
let [navigate, open, new_tab] = self.hints;
div()
.id(id.clone())
.flex()
.flex_col()
.w_full()
.max_w(rem(640.))
.bg(color(ColorRole::Layer01))
.shadow(overlay::floating_shadow())
.on_key_down(on_key)
.child(
div()
.relative()
.flex()
.items_center()
.gap(rem(12.))
.h(SIZE_LG.rems())
.px(rem(16.))
.border_b_1()
.border_color(color(ColorRole::BorderSubtle01))
.child(icon(
CarbonIcon::Search,
20.,
color(ColorRole::IconSecondary),
))
.child(
div()
.relative()
.flex()
.flex_1()
.min_w_0()
.items_center()
.when(query.is_empty(), |d| {
d.child(
sans(div(), 16., 400)
.absolute()
.left_0()
.text_color(color(ColorRole::TextPlaceholder))
.child(self.placeholder.clone()),
)
})
.child(
Input::new(&input)
.appearance(false)
.with_size(gpui_kit::component::Size::XSmall)
.aria_label(self.placeholder.clone())
.p_0()
.w_full()
.text_size(rem(16.))
.text_color(color(ColorRole::TextPrimary)),
),
)
.child(Kbd::new(self.escape_key)),
)
.child(list)
.child(
sans(div(), 12., 400)
.flex()
.flex_wrap()
.gap(rem(16.))
.py(rem(8.))
.px(rem(16.))
.border_t_1()
.border_color(color(ColorRole::BorderSubtle01))
.text_color(color(ColorRole::TextSecondary))
.child(kbd_row(&["↑", "↓"], navigate))
.child(kbd_row(&["↵"], open))
.child(kbd_row(&["⌘", "↵"], new_tab)),
)
}
}
pub struct ListFilter {
label: SharedString,
value: Option<SharedString>,
menu: Vec<MenuItem>,
on_click: Option<Handler>,
}
impl ListFilter {
pub fn new(label: impl Into<SharedString>) -> Self {
Self {
label: label.into(),
value: None,
menu: Vec::new(),
on_click: None,
}
}
pub fn value(mut self, value: impl Into<SharedString>) -> Self {
self.value = Some(value.into());
self
}
pub fn menu(mut self, items: impl IntoIterator<Item = MenuItem>) -> Self {
self.menu = items.into_iter().collect();
self
}
pub fn on_click(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_click = Some(Rc::new(f));
self
}
}
pub fn filter_colors(set: bool, hovered: bool) -> (Hsla, Hsla) {
let bg = if hovered {
ColorRole::LayerHover01
} else if set {
ColorRole::LayerSelected01
} else {
ColorRole::Layer01
};
let fg = if set || hovered {
ColorRole::TextPrimary
} else {
ColorRole::TextSecondary
};
(color(bg), color(fg))
}
type ViewChange = Rc<dyn Fn(SharedString, &mut Window, &mut App)>;
#[derive(IntoElement)]
pub struct ListToolbar {
id: ElementId,
placeholder: SharedString,
search_width: f32,
nowrap_filters: bool,
filters: Vec<ListFilter>,
views: Vec<SharedString>,
view: Option<SharedString>,
on_view_change: Option<ViewChange>,
actions: Vec<AnyElement>,
search: Option<Entity<InputState>>,
labels: [SharedString; 3],
}
impl ListToolbar {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
placeholder: "Search".into(),
search_width: 256.,
nowrap_filters: false,
filters: Vec::new(),
views: Vec::new(),
view: None,
on_view_change: None,
actions: Vec::new(),
search: None,
labels: [
"Saved views".into(),
"Save current view…".into(),
"Clear".into(),
],
}
}
pub fn search_width(mut self, width: f32) -> Self {
self.search_width = width;
self
}
pub fn nowrap_filters(mut self, nowrap: bool) -> Self {
self.nowrap_filters = nowrap;
self
}
pub fn placeholder(mut self, text: impl Into<SharedString>) -> Self {
self.placeholder = text.into();
self
}
pub fn filter(mut self, filter: ListFilter) -> Self {
self.filters.push(filter);
self
}
pub fn views<S: Into<SharedString>>(mut self, views: impl IntoIterator<Item = S>) -> Self {
self.views = views.into_iter().map(Into::into).collect();
self
}
pub fn view(mut self, view: impl Into<SharedString>) -> Self {
self.view = Some(view.into());
self
}
pub fn on_view_change(
mut self,
f: impl Fn(SharedString, &mut Window, &mut App) + 'static,
) -> Self {
self.on_view_change = Some(Rc::new(f));
self
}
pub fn action(mut self, action: impl IntoElement) -> Self {
self.actions.push(action.into_any_element());
self
}
pub fn search_state(mut self, state: Entity<InputState>) -> Self {
self.search = Some(state);
self
}
pub fn labels(mut self, labels: [SharedString; 3]) -> Self {
self.labels = labels;
self
}
}
impl RenderOnce for ListToolbar {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id;
let search = self.search.unwrap_or_else(|| {
window
.use_keyed_state((id.clone(), "search"), cx, |window, cx| {
cx.new(|cx| InputState::new(window, cx).clean_on_escape())
})
.read(cx)
.clone()
});
let [views_label, save_label, clear_label] = self.labels;
let has_text = !search.read(cx).value().is_empty();
let mut group = InputGroup::new((id.clone(), "search-field"))
.addon(InputGroupAddon::new().child(Icon::carbon(CarbonIcon::Search)))
.input(InputGroupInput::new(&search).placeholder(self.placeholder));
if has_text {
let clear = search.clone();
group = group.addon(
InputGroupAddon::new().align(AddonAlign::InlineEnd).child(
input_group_button((id.clone(), "clear"), clear_label)
.icon(CarbonIcon::Close)
.icon_only(true)
.on_click(move |_, w, cx| clear.update(cx, |s, cx| s.set_value("", w, cx))),
),
);
}
let mut chips: Vec<AnyElement> = Vec::new();
for (i, f) in self.filters.into_iter().enumerate() {
let chip_id: ElementId = (id.clone(), SharedString::from(format!("filter-{i}"))).into();
let (hovered, store) = hover((chip_id.clone(), "hover"), window, cx);
let (handle, focused) = focus((chip_id.clone(), "focus"), window, cx);
let set = f.value.is_some();
let (bg, fg) = filter_colors(set, hovered);
let has_menu = !f.menu.is_empty();
let menu_label = f.label.clone();
let chip = sans(press(chip_id.clone(), &handle), 12., 400)
.flex()
.items_center()
.gap(rem(4.))
.h(SIZE_SM.rems())
.px(rem(8.))
.border_1()
.border_color(gpui_kit::transparent_black())
.bg(bg)
.text_color(fg)
.on_hover(track_hover(store))
.when_some(f.on_click.filter(|_| !has_menu), |b, h| {
b.on_click(move |_, w, cx| h(w, cx))
})
.child(f.label)
.children(f.value.map(|v| {
div()
.ml(rem(-4.))
.font_weight(FontWeight::SEMIBOLD)
.child(format!(": {v}"))
}))
.child(icon(CarbonIcon::ChevronDown, 14., fg))
.when(focused, |b| b.child(ring()));
if has_menu {
let openness = overlay::openness(&chip_id, None, false, None, window, cx);
let open = openness.open;
let set_open = openness.setter();
let trigger = overlay::trigger_wrapper(
(chip_id.clone(), "trigger"),
chip.into_any_element(),
{
let set_open = set_open.clone();
Rc::new(move |w, cx| set_open(!open, w, cx))
},
)
.into_any_element();
chips.push(
menu::anchored_menu(
&chip_id,
trigger,
menu::MenuSpec {
items: f.menu,
size: MenuSize::Sm,
label: menu_label,
open,
set_open,
min_width: None,
on_unhandled_key: None,
},
window,
cx,
)
.into_any_element(),
);
} else {
chips.push(chip.into_any_element());
}
}
let views = (!self.views.is_empty()).then(|| {
let on_change = self.on_view_change.clone();
let on_save = on_change.clone();
let save_text = save_label.clone();
DropdownMenu::new((id.clone(), "views"))
.label(self.view.clone().unwrap_or(views_label))
.kind(DropdownMenuKind::Ghost)
.size(DropdownMenuSize::Md)
.items(self.views.into_iter().map(|v| {
let on_change = on_change.clone();
MenuItem::item(v.clone()).on_select(move |w, cx| {
if let Some(f) = &on_change {
f(v.clone(), w, cx)
}
})
}))
.item(MenuItem::divider())
.item(MenuItem::item(save_label).on_select(move |w, cx| {
if let Some(f) = &on_save {
f(save_text.clone(), w, cx)
}
}))
});
div()
.flex()
.flex_wrap()
.items_center()
.gap(rem(8.))
.py(rem(8.))
.child(
div()
.w(rem(self.search_width))
.max_w_full()
.flex_none()
.child(group),
)
.child(
div()
.flex()
.flex_1()
.when(!self.nowrap_filters, |d| d.flex_wrap())
.gap(rem(8.))
.children(chips),
)
.children(views)
.children(self.actions)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tokens::CarbonTheme;
fn hex(c: Hsla) -> String {
let c = c.to_rgb();
let b = |v: f32| (v * 255.).round() as u8;
format!("#{:02x}{:02x}{:02x}", b(c.r), b(c.g), b(c.b))
}
#[test]
fn palette_filters_and_steps_like_shell_jsx() {
let sub = [
SharedString::from("carbon-ui"),
SharedString::from("Working"),
];
assert!(palette_matches("Add dark theme tokens", &sub, "DARK"));
assert!(palette_matches("Add dark theme tokens", &sub, "working"));
assert!(!palette_matches("Settings", &[], "terminal"));
assert_eq!(palette_step("down", 2, 3), Some(2));
assert_eq!(palette_step("down", 0, 3), Some(1));
assert_eq!(palette_step("up", 0, 3), Some(0));
assert_eq!(palette_step("enter", 0, 3), None);
}
#[test]
fn filters_follow_the_last_t3_css_rule() {
crate::theme::tests_set(CarbonTheme::White);
let (bg, fg) = filter_colors(false, false);
assert_eq!((hex(bg), hex(fg)), ("#f4f4f4".into(), "#525252".into()));
let (bg, fg) = filter_colors(true, false);
assert_eq!((hex(bg), hex(fg)), ("#e0e0e0".into(), "#161616".into()));
let (bg, _) = filter_colors(true, true);
assert_eq!(hex(bg), "#e8e8e8");
let (bg, bar) = palette_row_colors(true);
assert_eq!(
(hex(bg), bar.map(hex)),
("#e0e0e0".into(), Some("#0f62fe".into()))
);
}
}