Allow clicking on commands in the command palette

This commit is contained in:
Conrad Irwin 2023-11-13 21:42:27 -07:00
parent 4ef95f05e8
commit ad017a5df5
5 changed files with 104 additions and 25 deletions

View file

@ -1,7 +1,7 @@
use editor::Editor;
use gpui::{
div, uniform_list, Component, Div, ParentElement, Render, StatelessInteractive, Styled, Task,
UniformListScrollHandle, View, ViewContext, VisualContext, WindowContext,
div, uniform_list, Component, Div, MouseButton, ParentElement, Render, StatelessInteractive,
Styled, Task, UniformListScrollHandle, View, ViewContext, VisualContext, WindowContext,
};
use std::{cmp, sync::Arc};
use ui::{prelude::*, v_stack, Divider, Label, LabelColor};
@ -11,6 +11,7 @@ pub struct Picker<D: PickerDelegate> {
scroll_handle: UniformListScrollHandle,
editor: View<Editor>,
pending_update_matches: Option<Task<()>>,
confirm_on_update: Option<bool>,
}
pub trait PickerDelegate: Sized + 'static {
@ -44,9 +45,10 @@ impl<D: PickerDelegate> Picker<D> {
cx.subscribe(&editor, Self::on_input_editor_event).detach();
let mut this = Self {
delegate,
editor,
scroll_handle: UniformListScrollHandle::new(),
pending_update_matches: None,
editor,
confirm_on_update: None,
};
this.update_matches("".to_string(), cx);
this
@ -101,11 +103,26 @@ impl<D: PickerDelegate> Picker<D> {
}
fn confirm(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
self.delegate.confirm(false, cx);
if self.pending_update_matches.is_some() {
self.confirm_on_update = Some(false)
} else {
self.delegate.confirm(false, cx);
}
}
fn secondary_confirm(&mut self, _: &menu::SecondaryConfirm, cx: &mut ViewContext<Self>) {
self.delegate.confirm(true, cx);
if self.pending_update_matches.is_some() {
self.confirm_on_update = Some(true)
} else {
self.delegate.confirm(true, cx);
}
}
fn handle_click(&mut self, ix: usize, secondary: bool, cx: &mut ViewContext<Self>) {
cx.stop_propagation();
cx.prevent_default();
self.delegate.set_selected_index(ix, cx);
self.delegate.confirm(secondary, cx);
}
fn on_input_editor_event(
@ -136,6 +153,9 @@ impl<D: PickerDelegate> Picker<D> {
let index = self.delegate.selected_index();
self.scroll_handle.scroll_to_item(index);
self.pending_update_matches = None;
if let Some(secondary) = self.confirm_on_update.take() {
self.delegate.confirm(secondary, cx);
}
cx.notify();
}
}
@ -173,7 +193,22 @@ impl<D: PickerDelegate> Render for Picker<D> {
let selected_ix = this.delegate.selected_index();
visible_range
.map(|ix| {
this.delegate.render_match(ix, ix == selected_ix, cx)
div()
.on_mouse_down(
MouseButton::Left,
move |this: &mut Self, event, cx| {
this.handle_click(
ix,
event.modifiers.command,
cx,
)
},
)
.child(this.delegate.render_match(
ix,
ix == selected_ix,
cx,
))
})
.collect()
}
@ -186,10 +221,11 @@ impl<D: PickerDelegate> Render for Picker<D> {
})
.when(self.delegate.match_count() == 0, |el| {
el.child(
v_stack()
.p_1()
.grow()
.child(Label::new("No matches").color(LabelColor::Muted)),
v_stack().p_1().grow().child(
div()
.px_1()
.child(Label::new("No matches").color(LabelColor::Muted)),
),
)
})
}