Start work on a command palette
This commit is contained in:
parent
99f8466cb5
commit
4630071f58
11 changed files with 487 additions and 26 deletions
24
crates/command_palette/Cargo.toml
Normal file
24
crates/command_palette/Cargo.toml
Normal file
|
@ -0,0 +1,24 @@
|
|||
[package]
|
||||
name = "command_palette"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
path = "src/command_palette.rs"
|
||||
doctest = false
|
||||
|
||||
[dependencies]
|
||||
editor = { path = "../editor" }
|
||||
fuzzy = { path = "../fuzzy" }
|
||||
gpui = { path = "../gpui" }
|
||||
settings = { path = "../settings" }
|
||||
util = { path = "../util" }
|
||||
theme = { path = "../theme" }
|
||||
workspace = { path = "../workspace" }
|
||||
|
||||
[dev-dependencies]
|
||||
gpui = { path = "../gpui", features = ["test-support"] }
|
||||
serde_json = { version = "1.0.64", features = ["preserve_order"] }
|
||||
workspace = { path = "../workspace", features = ["test-support"] }
|
||||
ctor = "0.1"
|
||||
env_logger = "0.8"
|
179
crates/command_palette/src/command_palette.rs
Normal file
179
crates/command_palette/src/command_palette.rs
Normal file
|
@ -0,0 +1,179 @@
|
|||
use std::cmp;
|
||||
|
||||
use fuzzy::{StringMatch, StringMatchCandidate};
|
||||
use gpui::{
|
||||
actions,
|
||||
elements::{ChildView, Label},
|
||||
Action, Element, Entity, MutableAppContext, View, ViewContext, ViewHandle,
|
||||
};
|
||||
use selector::{SelectorModal, SelectorModalDelegate};
|
||||
use settings::Settings;
|
||||
use workspace::Workspace;
|
||||
|
||||
mod selector;
|
||||
|
||||
pub fn init(cx: &mut MutableAppContext) {
|
||||
cx.add_action(CommandPalette::toggle);
|
||||
selector::init::<CommandPalette>(cx);
|
||||
}
|
||||
|
||||
actions!(command_palette, [Toggle]);
|
||||
|
||||
pub struct CommandPalette {
|
||||
selector: ViewHandle<SelectorModal<Self>>,
|
||||
actions: Vec<(&'static str, Box<dyn Action>)>,
|
||||
matches: Vec<StringMatch>,
|
||||
selected_ix: usize,
|
||||
focused_view_id: usize,
|
||||
}
|
||||
|
||||
pub enum Event {
|
||||
Dismissed,
|
||||
}
|
||||
|
||||
impl CommandPalette {
|
||||
pub fn new(
|
||||
focused_view_id: usize,
|
||||
actions: Vec<(&'static str, Box<dyn Action>)>,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) -> Self {
|
||||
let this = cx.weak_handle();
|
||||
let selector = cx.add_view(|cx| SelectorModal::new(this, cx));
|
||||
Self {
|
||||
selector,
|
||||
actions,
|
||||
matches: vec![],
|
||||
selected_ix: 0,
|
||||
focused_view_id,
|
||||
}
|
||||
}
|
||||
|
||||
fn toggle(_: &mut Workspace, _: &Toggle, cx: &mut ViewContext<Workspace>) {
|
||||
let workspace = cx.handle();
|
||||
let window_id = cx.window_id();
|
||||
let focused_view_id = cx.focused_view_id(window_id).unwrap_or(workspace.id());
|
||||
|
||||
cx.as_mut().defer(move |cx| {
|
||||
let actions = cx.available_actions(window_id, focused_view_id);
|
||||
workspace.update(cx, |workspace, cx| {
|
||||
workspace.toggle_modal(cx, |cx, _| {
|
||||
let selector = cx.add_view(|cx| Self::new(focused_view_id, actions, cx));
|
||||
cx.subscribe(&selector, Self::on_event).detach();
|
||||
selector
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn on_event(
|
||||
workspace: &mut Workspace,
|
||||
_: ViewHandle<Self>,
|
||||
event: &Event,
|
||||
cx: &mut ViewContext<Workspace>,
|
||||
) {
|
||||
match event {
|
||||
Event::Dismissed => {
|
||||
workspace.dismiss_modal(cx);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Entity for CommandPalette {
|
||||
type Event = Event;
|
||||
}
|
||||
|
||||
impl View for CommandPalette {
|
||||
fn ui_name() -> &'static str {
|
||||
"CommandPalette"
|
||||
}
|
||||
|
||||
fn render(&mut self, _: &mut gpui::RenderContext<'_, Self>) -> gpui::ElementBox {
|
||||
ChildView::new(self.selector.clone()).boxed()
|
||||
}
|
||||
|
||||
fn on_focus(&mut self, cx: &mut ViewContext<Self>) {
|
||||
cx.focus(&self.selector);
|
||||
}
|
||||
}
|
||||
|
||||
impl SelectorModalDelegate for CommandPalette {
|
||||
fn match_count(&self) -> usize {
|
||||
self.matches.len()
|
||||
}
|
||||
|
||||
fn selected_index(&self) -> usize {
|
||||
self.selected_ix
|
||||
}
|
||||
|
||||
fn set_selected_index(&mut self, ix: usize) {
|
||||
self.selected_ix = ix;
|
||||
}
|
||||
|
||||
fn update_matches(
|
||||
&mut self,
|
||||
query: String,
|
||||
cx: &mut gpui::ViewContext<Self>,
|
||||
) -> gpui::Task<()> {
|
||||
let candidates = self
|
||||
.actions
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(ix, (name, _))| StringMatchCandidate {
|
||||
id: ix,
|
||||
string: name.to_string(),
|
||||
char_bag: name.chars().collect(),
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
cx.spawn(move |this, mut cx| async move {
|
||||
let matches = fuzzy::match_strings(
|
||||
&candidates,
|
||||
&query,
|
||||
true,
|
||||
10000,
|
||||
&Default::default(),
|
||||
cx.background(),
|
||||
)
|
||||
.await;
|
||||
this.update(&mut cx, |this, _| {
|
||||
this.matches = matches;
|
||||
if this.matches.is_empty() {
|
||||
this.selected_ix = 0;
|
||||
} else {
|
||||
this.selected_ix = cmp::min(this.selected_ix, this.matches.len() - 1);
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
fn dismiss(&mut self, cx: &mut ViewContext<Self>) {
|
||||
cx.emit(Event::Dismissed);
|
||||
}
|
||||
|
||||
fn confirm(&mut self, cx: &mut ViewContext<Self>) {
|
||||
if !self.matches.is_empty() {
|
||||
let window_id = cx.window_id();
|
||||
let action_ix = self.matches[self.selected_ix].candidate_id;
|
||||
cx.dispatch_action_at(
|
||||
window_id,
|
||||
self.focused_view_id,
|
||||
self.actions[action_ix].1.as_ref(),
|
||||
)
|
||||
}
|
||||
cx.emit(Event::Dismissed);
|
||||
}
|
||||
|
||||
fn render_match(&self, ix: usize, selected: bool, cx: &gpui::AppContext) -> gpui::ElementBox {
|
||||
let settings = cx.global::<Settings>();
|
||||
let theme = &settings.theme.selector;
|
||||
let style = if selected {
|
||||
&theme.active_item
|
||||
} else {
|
||||
&theme.item
|
||||
};
|
||||
Label::new(self.matches[ix].string.clone(), style.label.clone())
|
||||
.contained()
|
||||
.with_style(style.container)
|
||||
.boxed()
|
||||
}
|
||||
}
|
222
crates/command_palette/src/selector.rs
Normal file
222
crates/command_palette/src/selector.rs
Normal file
|
@ -0,0 +1,222 @@
|
|||
use editor::Editor;
|
||||
use gpui::{
|
||||
elements::{
|
||||
ChildView, Flex, FlexItem, Label, ParentElement, ScrollTarget, UniformList,
|
||||
UniformListState,
|
||||
},
|
||||
keymap, AppContext, Axis, Element, ElementBox, Entity, MutableAppContext, RenderContext, Task,
|
||||
View, ViewContext, ViewHandle, WeakViewHandle,
|
||||
};
|
||||
use settings::Settings;
|
||||
use std::cmp;
|
||||
use workspace::menu::{Cancel, Confirm, SelectFirst, SelectLast, SelectNext, SelectPrev};
|
||||
|
||||
pub fn init<D: SelectorModalDelegate>(cx: &mut MutableAppContext) {
|
||||
cx.add_action(SelectorModal::<D>::select_first);
|
||||
cx.add_action(SelectorModal::<D>::select_last);
|
||||
cx.add_action(SelectorModal::<D>::select_next);
|
||||
cx.add_action(SelectorModal::<D>::select_prev);
|
||||
cx.add_action(SelectorModal::<D>::confirm);
|
||||
cx.add_action(SelectorModal::<D>::cancel);
|
||||
}
|
||||
|
||||
pub struct SelectorModal<D: SelectorModalDelegate> {
|
||||
delegate: WeakViewHandle<D>,
|
||||
query_editor: ViewHandle<Editor>,
|
||||
list_state: UniformListState,
|
||||
}
|
||||
|
||||
pub trait SelectorModalDelegate: View {
|
||||
fn match_count(&self) -> usize;
|
||||
fn selected_index(&self) -> usize;
|
||||
fn set_selected_index(&mut self, ix: usize);
|
||||
fn update_matches(&mut self, query: String, cx: &mut ViewContext<Self>) -> Task<()>;
|
||||
fn confirm(&mut self, cx: &mut ViewContext<Self>);
|
||||
fn dismiss(&mut self, cx: &mut ViewContext<Self>);
|
||||
fn render_match(&self, ix: usize, selected: bool, cx: &AppContext) -> ElementBox;
|
||||
}
|
||||
|
||||
impl<D: SelectorModalDelegate> Entity for SelectorModal<D> {
|
||||
type Event = ();
|
||||
}
|
||||
|
||||
impl<D: SelectorModalDelegate> View for SelectorModal<D> {
|
||||
fn ui_name() -> &'static str {
|
||||
"SelectorModal"
|
||||
}
|
||||
|
||||
fn render(&mut self, cx: &mut RenderContext<Self>) -> gpui::ElementBox {
|
||||
let settings = cx.global::<Settings>();
|
||||
Flex::new(Axis::Vertical)
|
||||
.with_child(
|
||||
ChildView::new(&self.query_editor)
|
||||
.contained()
|
||||
.with_style(settings.theme.selector.input_editor.container)
|
||||
.boxed(),
|
||||
)
|
||||
.with_child(
|
||||
FlexItem::new(self.render_matches(cx))
|
||||
.flex(1., false)
|
||||
.boxed(),
|
||||
)
|
||||
.contained()
|
||||
.with_style(settings.theme.selector.container)
|
||||
.constrained()
|
||||
.with_max_width(500.0)
|
||||
.with_max_height(420.0)
|
||||
.aligned()
|
||||
.top()
|
||||
.named("selector")
|
||||
}
|
||||
|
||||
fn keymap_context(&self, _: &AppContext) -> keymap::Context {
|
||||
let mut cx = Self::default_keymap_context();
|
||||
cx.set.insert("menu".into());
|
||||
cx
|
||||
}
|
||||
|
||||
fn on_focus(&mut self, cx: &mut ViewContext<Self>) {
|
||||
cx.focus(&self.query_editor);
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: SelectorModalDelegate> SelectorModal<D> {
|
||||
pub fn new(delegate: WeakViewHandle<D>, cx: &mut ViewContext<Self>) -> Self {
|
||||
let query_editor = cx.add_view(|cx| {
|
||||
Editor::single_line(Some(|theme| theme.selector.input_editor.clone()), cx)
|
||||
});
|
||||
cx.subscribe(&query_editor, Self::on_query_editor_event)
|
||||
.detach();
|
||||
|
||||
Self {
|
||||
delegate,
|
||||
query_editor,
|
||||
list_state: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
fn render_matches(&self, cx: &AppContext) -> ElementBox {
|
||||
let delegate = self.delegate.clone();
|
||||
let match_count = if let Some(delegate) = delegate.upgrade(cx) {
|
||||
delegate.read(cx).match_count()
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
if match_count == 0 {
|
||||
let settings = cx.global::<Settings>();
|
||||
return Label::new(
|
||||
"No matches".into(),
|
||||
settings.theme.selector.empty.label.clone(),
|
||||
)
|
||||
.contained()
|
||||
.with_style(settings.theme.selector.empty.container)
|
||||
.named("empty matches");
|
||||
}
|
||||
|
||||
UniformList::new(
|
||||
self.list_state.clone(),
|
||||
match_count,
|
||||
move |mut range, items, cx| {
|
||||
let cx = cx.as_ref();
|
||||
let delegate = delegate.upgrade(cx).unwrap();
|
||||
let delegate = delegate.read(cx);
|
||||
let selected_ix = delegate.selected_index();
|
||||
range.end = cmp::min(range.end, delegate.match_count());
|
||||
items.extend(range.map(move |ix| delegate.render_match(ix, ix == selected_ix, cx)));
|
||||
},
|
||||
)
|
||||
.contained()
|
||||
.with_margin_top(6.0)
|
||||
.named("matches")
|
||||
}
|
||||
|
||||
fn on_query_editor_event(
|
||||
&mut self,
|
||||
_: ViewHandle<Editor>,
|
||||
event: &editor::Event,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) {
|
||||
if let Some(delegate) = self.delegate.upgrade(cx) {
|
||||
match event {
|
||||
editor::Event::BufferEdited { .. } => {
|
||||
let query = self.query_editor.read(cx).text(cx);
|
||||
let update = delegate.update(cx, |d, cx| d.update_matches(query, cx));
|
||||
cx.spawn(|this, mut cx| async move {
|
||||
update.await;
|
||||
this.update(&mut cx, |_, cx| cx.notify());
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
editor::Event::Blurred => delegate.update(cx, |delegate, cx| {
|
||||
delegate.dismiss(cx);
|
||||
}),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn select_first(&mut self, _: &SelectFirst, cx: &mut ViewContext<Self>) {
|
||||
if let Some(delegate) = self.delegate.upgrade(cx) {
|
||||
let index = 0;
|
||||
delegate.update(cx, |delegate, _| delegate.set_selected_index(0));
|
||||
self.list_state.scroll_to(ScrollTarget::Show(index));
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
|
||||
fn select_last(&mut self, _: &SelectLast, cx: &mut ViewContext<Self>) {
|
||||
if let Some(delegate) = self.delegate.upgrade(cx) {
|
||||
let index = delegate.update(cx, |delegate, _| {
|
||||
let match_count = delegate.match_count();
|
||||
let index = if match_count > 0 { match_count - 1 } else { 0 };
|
||||
delegate.set_selected_index(index);
|
||||
index
|
||||
});
|
||||
self.list_state.scroll_to(ScrollTarget::Show(index));
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
|
||||
fn select_next(&mut self, _: &SelectNext, cx: &mut ViewContext<Self>) {
|
||||
if let Some(delegate) = self.delegate.upgrade(cx) {
|
||||
let index = delegate.update(cx, |delegate, _| {
|
||||
let mut selected_index = delegate.selected_index();
|
||||
if selected_index + 1 < delegate.match_count() {
|
||||
selected_index += 1;
|
||||
delegate.set_selected_index(selected_index);
|
||||
}
|
||||
selected_index
|
||||
});
|
||||
self.list_state.scroll_to(ScrollTarget::Show(index));
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
|
||||
fn select_prev(&mut self, _: &SelectPrev, cx: &mut ViewContext<Self>) {
|
||||
if let Some(delegate) = self.delegate.upgrade(cx) {
|
||||
let index = delegate.update(cx, |delegate, _| {
|
||||
let mut selected_index = delegate.selected_index();
|
||||
if selected_index > 0 {
|
||||
selected_index -= 1;
|
||||
delegate.set_selected_index(selected_index);
|
||||
}
|
||||
selected_index
|
||||
});
|
||||
self.list_state.scroll_to(ScrollTarget::Show(index));
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
|
||||
fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
|
||||
if let Some(delegate) = self.delegate.upgrade(cx) {
|
||||
delegate.update(cx, |delegate, cx| delegate.confirm(cx));
|
||||
}
|
||||
}
|
||||
|
||||
fn cancel(&mut self, _: &Cancel, cx: &mut ViewContext<Self>) {
|
||||
if let Some(delegate) = self.delegate.upgrade(cx) {
|
||||
delegate.update(cx, |delegate, cx| delegate.dismiss(cx));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue