use crate::{
components::{Kbd, KbdGroup},
density,
icons::{CarbonIcon, Icon},
t3::craft::{rem, sans},
theme::color,
tokens::{self, ColorRole},
u0::primitives::{HighlightedText, IconButton, fuzzy_match, parts::editor},
};
use gpui_kit::{
AnyElement, App, AppContext as _, BoxShadow, CursorStyle, Div, ElementId, Entity, Hsla,
InteractiveElement as _, IntoElement, KeyDownEvent, ParentElement, RenderOnce, SharedString,
StatefulInteractiveElement as _, Styled, Window, component::input::InputState, div, point,
prelude::FluentBuilder as _, px,
};
use std::rc::Rc;
pub fn row_look(active: bool, hovered: bool) -> (Option<Hsla>, bool) {
if active {
(Some(color(ColorRole::LayerSelected01)), true)
} else if hovered {
(Some(color(ColorRole::LayerHover01)), false)
} else {
(None, false)
}
}
fn row(id: ElementId, active: bool, hovered: bool) -> gpui_kit::Stateful<Div> {
let (bg, bar) = row_look(active, hovered);
sans(div(), 14., 400)
.id(id)
.relative()
.flex()
.items_center()
.gap(tokens::SPACING_03)
.h(density::current().row())
.px(tokens::SPACING_04)
.text_color(color(ColorRole::TextPrimary))
.cursor(CursorStyle::PointingHand)
.when_some(bg, |d, c| d.bg(c))
.when(bar, |d| {
d.child(
div()
.absolute()
.top_0()
.bottom_0()
.left_0()
.w(px(2.))
.bg(color(ColorRole::BorderInteractive)),
)
})
}
fn dim_icon(name: CarbonIcon) -> Icon {
Icon::carbon(name)
.size_length(rem(16.))
.color(color(ColorRole::IconSecondary))
}
fn hover_state(
id: ElementId,
window: &mut Window,
cx: &mut App,
) -> (bool, impl Fn(&bool, &mut Window, &mut App) + 'static) {
let store = window.use_keyed_state(id, cx, |_, _| false);
let over = *store.read(cx);
(over, move |h: &bool, _: &mut Window, cx: &mut App| {
store.update(cx, |v, cx| {
if *v != *h {
*v = *h;
cx.notify();
}
})
})
}
type Handler = Rc<dyn Fn(&mut Window, &mut App)>;
#[derive(IntoElement)]
pub struct PaletteItem {
id: ElementId,
icon: Option<CarbonIcon>,
title: SharedString,
detail: Option<SharedString>,
shortcut: Option<SharedString>,
chevron: bool,
active: bool,
on_select: Option<Handler>,
}
impl PaletteItem {
pub fn new(id: impl Into<ElementId>, title: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
icon: None,
title: title.into(),
detail: None,
shortcut: None,
chevron: false,
active: false,
on_select: None,
}
}
pub fn icon(mut self, icon: CarbonIcon) -> Self {
self.icon = Some(icon);
self
}
pub fn detail(mut self, detail: impl Into<SharedString>) -> Self {
self.detail = Some(detail.into());
self
}
pub fn shortcut(mut self, shortcut: impl Into<SharedString>) -> Self {
self.shortcut = Some(shortcut.into());
self
}
pub fn chevron(mut self, chevron: bool) -> Self {
self.chevron = chevron;
self
}
pub fn active(mut self, active: bool) -> Self {
self.active = active;
self
}
pub fn on_select(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_select = Some(Rc::new(f));
self
}
}
impl RenderOnce for PaletteItem {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let (over, on_hover) = hover_state((self.id.clone(), "hover").into(), window, cx);
let select = self.on_select.clone();
row(self.id, self.active, over)
.on_hover(on_hover)
.on_click(move |_, w, cx| {
if let Some(f) = &select {
f(w, cx);
}
})
.when_some(self.icon, |d, i| d.child(dim_icon(i).flex_none()))
.child(div().flex_1().min_w_0().truncate().child(self.title))
.when_some(self.detail, |d, t| {
d.child(
sans(div(), 12., 400)
.flex_none()
.text_color(color(ColorRole::TextSecondary))
.child(t),
)
})
.when_some(self.shortcut, |d, s| {
d.child(KbdGroup::new().children(s.split(' ').map(|k| Kbd::new(k.to_string()))))
})
.when(self.chevron, |d| {
d.child(dim_icon(CarbonIcon::ChevronRight).flex_none())
})
}
}
pub fn file_result_hits(path: &str, query: &str) -> (String, String, Vec<usize>, Vec<usize>) {
let (dir, file) = match path.rfind('/') {
Some(i) => (&path[..=i], &path[i + 1..]),
None => ("", path),
};
let n = file.chars().count();
let hits = fuzzy_match(&format!("{file}{dir}"), query).unwrap_or_default();
let (fh, dh): (Vec<usize>, Vec<usize>) = hits.into_iter().partition(|&x| x < n);
(
file.to_string(),
dir.to_string(),
fh,
dh.into_iter().map(|x| x - n).collect(),
)
}
#[derive(IntoElement)]
pub struct FileResultRow {
id: ElementId,
path: SharedString,
query: SharedString,
active: bool,
kind: Option<SharedString>,
on_select: Option<Handler>,
}
impl FileResultRow {
pub fn new(
id: impl Into<ElementId>,
path: impl Into<SharedString>,
query: impl Into<SharedString>,
) -> Self {
Self {
id: id.into(),
path: path.into(),
query: query.into(),
active: false,
kind: None,
on_select: None,
}
}
pub fn active(mut self, active: bool) -> Self {
self.active = active;
self
}
pub fn kind(mut self, kind: impl Into<SharedString>) -> Self {
self.kind = Some(kind.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
}
}
impl RenderOnce for FileResultRow {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let (over, on_hover) = hover_state((self.id.clone(), "hover").into(), window, cx);
let (file, dir, fh, dh) = file_result_hits(&self.path, &self.query);
let select = self.on_select.clone();
row(self.id, self.active, over)
.on_hover(on_hover)
.on_click(move |_, w, cx| {
if let Some(f) = &select {
f(w, cx);
}
})
.child(dim_icon(CarbonIcon::Document).flex_none())
.child(
div()
.flex_none()
.whitespace_nowrap()
.child(HighlightedText::new(file).indices(fh)),
)
.child(
sans(div(), 12., 400)
.flex_1()
.min_w_0()
.overflow_hidden()
.whitespace_nowrap()
.text_ellipsis_start()
.text_color(color(ColorRole::TextSecondary))
.child(HighlightedText::new(dir).indices(dh)),
)
.when_some(self.kind, |d, k| {
d.child(
sans(div(), 12., 400)
.flex_none()
.text_color(color(ColorRole::TextSecondary))
.child(k),
)
})
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PaletteLevel {
pub title: SharedString,
pub placeholder: Option<SharedString>,
}
impl PaletteLevel {
pub fn new(title: impl Into<SharedString>) -> Self {
Self {
title: title.into(),
placeholder: None,
}
}
pub fn placeholder(mut self, placeholder: impl Into<SharedString>) -> Self {
self.placeholder = Some(placeholder.into());
self
}
}
pub fn crumb(stack: &[PaletteLevel]) -> Option<String> {
(stack.len() > 1).then(|| {
stack[1..]
.iter()
.map(|p| p.title.to_string())
.collect::<Vec<_>>()
.join(" › ")
})
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PaletteKey {
Back,
Close,
Pass,
}
pub fn palette_key(key: &str, depth: usize, empty: bool) -> PaletteKey {
match key {
"escape" if depth > 1 => PaletteKey::Back,
"escape" => PaletteKey::Close,
"backspace" if empty && depth > 1 => PaletteKey::Back,
_ => PaletteKey::Pass,
}
}
#[derive(Clone)]
pub struct PaletteNav {
pub page: PaletteLevel,
pub depth: usize,
pub query: SharedString,
stack: Entity<Vec<PaletteLevel>>,
input: Entity<InputState>,
}
impl PaletteNav {
pub fn push(&self, page: PaletteLevel, window: &mut Window, cx: &mut App) {
self.stack.update(cx, |s, cx| {
s.push(page);
cx.notify();
});
fresh_input(&self.input, window, cx);
}
pub fn input_focus(&self, cx: &App) -> gpui_kit::FocusHandle {
gpui_kit::Focusable::focus_handle(self.input.read(cx), cx)
}
}
fn fresh_input(input: &Entity<InputState>, window: &mut Window, cx: &mut App) {
input.update(cx, |s, cx| s.set_value("", window, cx));
let focus = gpui_kit::Focusable::focus_handle(input.read(cx), cx);
focus.focus(window, cx);
}
#[derive(Clone)]
pub struct PalettePageTexts {
pub root: SharedString,
pub placeholder: SharedString,
pub back_to: Rc<dyn Fn(&str) -> SharedString>,
pub open: SharedString,
pub back: SharedString,
pub close: SharedString,
}
impl Default for PalettePageTexts {
fn default() -> Self {
Self {
root: "Commands".into(),
placeholder: "Type a command or search".into(),
back_to: Rc::new(|t| format!("Back to {t}").into()),
open: "Open".into(),
back: "Back".into(),
close: "Close".into(),
}
}
}
type BackHandler = Rc<dyn Fn(&mut Window, &mut App) -> bool>;
type Content = Rc<dyn Fn(&PaletteNav, &mut Window, &mut App) -> Vec<AnyElement>>;
#[derive(IntoElement)]
pub struct PalettePage {
id: ElementId,
pages: Vec<PaletteLevel>,
content: Option<Content>,
on_back: Option<Handler>,
on_close: Option<Handler>,
texts: PalettePageTexts,
auto_focus: bool,
}
impl PalettePage {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
pages: Vec::new(),
content: None,
on_back: None,
on_close: None,
texts: PalettePageTexts::default(),
auto_focus: true,
}
}
pub fn auto_focus(mut self, auto_focus: bool) -> Self {
self.auto_focus = auto_focus;
self
}
pub fn pages(mut self, pages: impl IntoIterator<Item = PaletteLevel>) -> Self {
self.pages = pages.into_iter().collect();
self
}
pub fn content(
mut self,
f: impl Fn(&PaletteNav, &mut Window, &mut App) -> Vec<AnyElement> + 'static,
) -> Self {
self.content = Some(Rc::new(f));
self
}
pub fn on_back(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_back = Some(Rc::new(f));
self
}
pub fn on_close(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_close = Some(Rc::new(f));
self
}
pub fn texts(mut self, texts: PalettePageTexts) -> Self {
self.texts = texts;
self
}
}
fn palette_shadow() -> Vec<BoxShadow> {
vec![
BoxShadow::new(px(0.), px(2.), color(ColorRole::Shadow)).blur_radius(px(6.)),
BoxShadow {
color: color(ColorRole::BorderSubtle01),
offset: point(px(0.), px(0.)),
blur_radius: px(0.),
spread_radius: px(1.),
inset: false,
},
]
}
fn hint(key: &'static str, label: SharedString) -> Div {
div()
.flex()
.items_center()
.gap(tokens::SPACING_02)
.child(Kbd::new(key))
.child(label)
}
impl RenderOnce for PalettePage {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let initial = if self.pages.is_empty() {
vec![PaletteLevel::new(self.texts.root.clone())]
} else {
self.pages.clone()
};
let stack = window.use_keyed_state((id.clone(), "stack"), cx, move |_, _| initial);
let auto_focus = self.auto_focus;
let input = window.use_keyed_state((id.clone(), "input"), cx, move |window, cx| {
let input = cx.new(|cx| InputState::new(window, cx));
if auto_focus {
let focus = gpui_kit::Focusable::focus_handle(input.read(cx), cx);
window.defer(cx, move |window, cx| focus.focus(window, cx));
}
input
});
let input = input.read(cx).clone();
let pages = stack.read(cx).clone();
let depth = pages.len();
let current = pages
.last()
.cloned()
.unwrap_or(PaletteLevel::new(self.texts.root.clone()));
let query = input.read(cx).value();
let nav = PaletteNav {
page: current.clone(),
depth,
query: query.clone(),
stack: stack.clone(),
input: input.clone(),
};
let results = self
.content
.as_ref()
.map(|f| f(&nav, window, cx))
.unwrap_or_default();
let back: BackHandler = {
let stack = stack.clone();
let input = input.clone();
let on_back = self.on_back.clone();
Rc::new(move |window, cx| {
if stack.read(cx).len() <= 1 {
return false;
}
stack.update(cx, |s, cx| {
s.pop();
cx.notify();
});
fresh_input(&input, window, cx);
if let Some(f) = &on_back {
f(window, cx);
}
true
})
};
let lead: AnyElement = if depth > 1 {
let back = back.clone();
IconButton::new(
(id.clone(), "back"),
CarbonIcon::ArrowLeft,
(self.texts.back_to)(&pages[depth - 2].title),
)
.on_click(move |_, w, cx| {
back(w, cx);
})
.into_any_element()
} else {
dim_icon(CarbonIcon::Search).flex_none().into_any_element()
};
let placeholder = query.is_empty().then(|| {
sans(div(), 16., 400)
.absolute()
.left_0()
.top_0()
.bottom_0()
.flex()
.items_center()
.text_color(color(ColorRole::TextPlaceholder))
.child(
current
.placeholder
.clone()
.unwrap_or(self.texts.placeholder.clone()),
)
});
let keys = {
let back = back.clone();
let input = input.clone();
let close = self.on_close.clone();
move |e: &KeyDownEvent, window: &mut Window, cx: &mut App| {
let focus = gpui_kit::Focusable::focus_handle(input.read(cx), cx);
if !focus.is_focused(window) {
return;
}
let empty = input.read(cx).value().is_empty();
let depth = stack.read(cx).len();
match palette_key(e.keystroke.key.as_str(), depth, empty) {
PaletteKey::Back => {
back(window, cx);
cx.stop_propagation();
}
PaletteKey::Close => {
if let Some(f) = &close {
f(window, cx);
}
cx.stop_propagation();
}
PaletteKey::Pass => {}
}
}
};
let bar = div()
.relative()
.flex()
.items_center()
.gap(tokens::SPACING_03)
.h(tokens::PALETTE_INPUT_HEIGHT)
.px(tokens::SPACING_04)
.border_b_1()
.border_color(color(ColorRole::BorderSubtle01))
.child(lead)
.when_some(crumb(&pages), |d, c| {
d.child(
sans(div(), 12., 400)
.flex_none()
.line_height(rem(20.))
.px(tokens::SPACING_03)
.bg(color(ColorRole::LayerAccent01))
.text_color(color(ColorRole::TextSecondary))
.child(c),
)
})
.child(
div()
.relative()
.flex()
.items_center()
.flex_1()
.min_w_0()
.h_full()
.children(placeholder)
.child(editor(&input).text_size(rem(16.)).line_height(rem(22.))),
)
.child(Kbd::new("Esc"));
let foot = sans(div(), 12., 400)
.flex()
.gap(tokens::SPACING_05)
.py(tokens::SPACING_02)
.px(tokens::SPACING_04)
.border_t_1()
.border_color(color(ColorRole::BorderSubtle01))
.text_color(color(ColorRole::TextSecondary))
.child(hint("↵", self.texts.open.clone()))
.child(if depth > 1 {
hint("⌫", self.texts.back.clone())
} else {
hint("Esc", self.texts.close.clone())
});
div()
.id(id.clone())
.flex()
.flex_col()
.w_full()
.max_w(rem(640.))
.bg(color(ColorRole::Layer01))
.shadow(palette_shadow())
.capture_key_down(keys)
.child(bar)
.child(
div()
.id((id, "body"))
.flex()
.flex_col()
.max_h(rem(320.))
.overflow_y_scroll()
.py(tokens::SPACING_02)
.children(results),
)
.child(foot)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{CarbonTheme, theme::color_in};
#[test]
fn rows_follow_g_pitem() {
crate::theme::tests_set(CarbonTheme::White);
let c = |r| color_in(CarbonTheme::White, r);
assert_eq!(
row_look(true, false),
(Some(c(ColorRole::LayerSelected01)), true)
);
assert_eq!(
row_look(false, true),
(Some(c(ColorRole::LayerHover01)), false)
);
assert_eq!(row_look(false, false), (None, false));
}
#[test]
fn file_hits_light_each_query_char_once() {
let (file, dir, fh, dh) = file_result_hits(
"packages/web/src/components/sidebar/SidebarThreadRow.tsx",
"sbtr",
);
assert_eq!(file, "SidebarThreadRow.tsx");
assert_eq!(dir, "packages/web/src/components/sidebar/");
assert_eq!(fh, vec![0, 4, 7, 9]);
assert!(dh.is_empty());
let (_, _, fh, dh) = file_result_hits("README.md", "rd");
assert_eq!((fh, dh), (vec![0, 3], vec![]));
let (_, _, fh, dh) = file_result_hits("apps/x.ts", "xa");
assert_eq!((fh, dh), (vec![0], vec![0]));
}
#[test]
fn keys_and_crumbs() {
assert_eq!(palette_key("escape", 2, false), PaletteKey::Back);
assert_eq!(palette_key("escape", 1, true), PaletteKey::Close);
assert_eq!(palette_key("backspace", 2, true), PaletteKey::Back);
assert_eq!(palette_key("backspace", 2, false), PaletteKey::Pass);
assert_eq!(palette_key("backspace", 1, true), PaletteKey::Pass);
let stack = [
PaletteLevel::new("Commands"),
PaletteLevel::new("Switch agent"),
PaletteLevel::new("Claude"),
];
assert_eq!(crumb(&stack).as_deref(), Some("Switch agent › Claude"));
assert_eq!(crumb(&stack[..1]), None);
}
}