refactor: consolidate shared ui helpers and prune unused code
This commit is contained in:
@@ -30,7 +30,7 @@ use gpui::prelude::FluentBuilder;
|
||||
use gpui::{
|
||||
AnyElement, App, Context, ElementId, Entity, InteractiveElement as _, IntoElement, KeyBinding,
|
||||
MouseButton, ParentElement as _, Render, Role, SharedString, StatefulInteractiveElement as _,
|
||||
StyleRefinement, Styled as _, Subscription, UniformListScrollHandle, Window, actions, canvas,
|
||||
StyleRefinement, Styled as _, Subscription, UniformListScrollHandle, Window, actions,
|
||||
deferred, div, relative, rems, uniform_list,
|
||||
};
|
||||
|
||||
@@ -51,20 +51,10 @@ actions!(
|
||||
/// The key context the palette's panel declares.
|
||||
pub const COMMAND_PALETTE_CONTEXT: &str = "CommandPalette";
|
||||
|
||||
/// Binds the palette's four keys: down, up, enter, and escape.
|
||||
pub fn bind_command_palette_keys(cx: &mut App) {
|
||||
let under_input = Some("CommandPalette > Input");
|
||||
cx.bind_keys([
|
||||
KeyBinding::new("down", HighlightNext, under_input),
|
||||
KeyBinding::new("up", HighlightPrevious, under_input),
|
||||
KeyBinding::new("enter", Choose, under_input),
|
||||
KeyBinding::new("escape", Dismiss, under_input),
|
||||
KeyBinding::new("down", HighlightNext, Some(COMMAND_PALETTE_CONTEXT)),
|
||||
KeyBinding::new("up", HighlightPrevious, Some(COMMAND_PALETTE_CONTEXT)),
|
||||
KeyBinding::new("enter", Choose, Some(COMMAND_PALETTE_CONTEXT)),
|
||||
KeyBinding::new("escape", Dismiss, Some(COMMAND_PALETTE_CONTEXT)),
|
||||
]);
|
||||
}
|
||||
/// Filters and ranks the commands for a query.
|
||||
pub type CommandMatcher = Rc<dyn Fn(&str, &[Command]) -> Vec<Command>>;
|
||||
/// Runs the chosen command.
|
||||
pub type CommandHandler = Rc<dyn Fn(&Command, &mut Window, &mut App)>;
|
||||
|
||||
/// A command the palette can run: a stable id and the label the matcher sees.
|
||||
#[derive(Clone)]
|
||||
@@ -117,11 +107,6 @@ impl Command {
|
||||
// Command is a data builder, not an element, adopt the combinators explicitly.
|
||||
impl FluentBuilder for Command {}
|
||||
|
||||
/// Filters and ranks the commands for a query.
|
||||
pub type CommandMatcher = Rc<dyn Fn(&str, &[Command]) -> Vec<Command>>;
|
||||
/// Runs the chosen command.
|
||||
pub type CommandHandler = Rc<dyn Fn(&Command, &mut Window, &mut App)>;
|
||||
|
||||
/// Builder for a command palette, consumed by [`CommandPaletteState::new`].
|
||||
#[must_use]
|
||||
pub struct CommandPalette {
|
||||
@@ -525,18 +510,10 @@ impl Render for CommandPaletteState {
|
||||
.placeholder(self.placeholder.clone()),
|
||||
)
|
||||
.child(rows)
|
||||
.child(
|
||||
canvas(|_, _, _| {}, {
|
||||
let focus = query_focus.clone();
|
||||
move |_, _, window, cx| {
|
||||
if !focus.contains_focused(window, cx) {
|
||||
window.focus(&focus, cx);
|
||||
}
|
||||
}
|
||||
})
|
||||
.absolute()
|
||||
.inset_0(),
|
||||
);
|
||||
.child(crate::components::menu::focus_capture(
|
||||
query_focus.clone(),
|
||||
move |window, cx| !query_focus.contains_focused(window, cx),
|
||||
));
|
||||
|
||||
let scrim = div()
|
||||
.absolute()
|
||||
@@ -566,3 +543,18 @@ impl Render for CommandPaletteState {
|
||||
.into_any_element()
|
||||
}
|
||||
}
|
||||
|
||||
/// Binds the palette's four keys: down, up, enter, and escape.
|
||||
pub fn bind_command_palette_keys(cx: &mut App) {
|
||||
let under_input = Some("CommandPalette > Input");
|
||||
cx.bind_keys([
|
||||
KeyBinding::new("down", HighlightNext, under_input),
|
||||
KeyBinding::new("up", HighlightPrevious, under_input),
|
||||
KeyBinding::new("enter", Choose, under_input),
|
||||
KeyBinding::new("escape", Dismiss, under_input),
|
||||
KeyBinding::new("down", HighlightNext, Some(COMMAND_PALETTE_CONTEXT)),
|
||||
KeyBinding::new("up", HighlightPrevious, Some(COMMAND_PALETTE_CONTEXT)),
|
||||
KeyBinding::new("enter", Choose, Some(COMMAND_PALETTE_CONTEXT)),
|
||||
KeyBinding::new("escape", Dismiss, Some(COMMAND_PALETTE_CONTEXT)),
|
||||
]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user