Allow toggling other modals from the command palette

This commit is contained in:
Max Brunsfeld 2022-04-18 11:33:15 -07:00
parent f5377c2f50
commit 11eba96cb8
2 changed files with 28 additions and 9 deletions

View File

@ -27,6 +27,11 @@ pub struct CommandPalette {
pub enum Event { pub enum Event {
Dismissed, Dismissed,
Confirmed {
window_id: usize,
focused_view_id: usize,
action: Box<dyn Action>,
},
} }
struct Command { struct Command {
@ -81,8 +86,18 @@ impl CommandPalette {
cx: &mut ViewContext<Workspace>, cx: &mut ViewContext<Workspace>,
) { ) {
match event { match event {
Event::Dismissed => { Event::Dismissed => workspace.dismiss_modal(cx),
Event::Confirmed {
window_id,
focused_view_id,
action,
} => {
let window_id = *window_id;
let focused_view_id = *focused_view_id;
let action = (*action).boxed_clone();
workspace.dismiss_modal(cx); workspace.dismiss_modal(cx);
cx.as_mut()
.defer(move |cx| cx.dispatch_action_at(window_id, focused_view_id, &*action))
} }
} }
} }
@ -174,16 +189,16 @@ impl PickerDelegate for CommandPalette {
fn confirm(&mut self, cx: &mut ViewContext<Self>) { fn confirm(&mut self, cx: &mut ViewContext<Self>) {
if !self.matches.is_empty() { if !self.matches.is_empty() {
let window_id = cx.window_id();
let action_ix = self.matches[self.selected_ix].candidate_id; let action_ix = self.matches[self.selected_ix].candidate_id;
cx.dispatch_action_at( cx.emit(Event::Confirmed {
window_id, window_id: cx.window_id(),
self.focused_view_id, focused_view_id: self.focused_view_id,
self.actions[action_ix].action.as_ref(), action: self.actions.remove(action_ix).action,
) });
} } else {
cx.emit(Event::Dismissed); cx.emit(Event::Dismissed);
} }
}
fn render_match(&self, ix: usize, selected: bool, cx: &gpui::AppContext) -> gpui::ElementBox { fn render_match(&self, ix: usize, selected: bool, cx: &gpui::AppContext) -> gpui::ElementBox {
let mat = &self.matches[ix]; let mat = &self.matches[ix];

View File

@ -20,6 +20,7 @@ pub struct Picker<D: PickerDelegate> {
list_state: UniformListState, list_state: UniformListState,
update_task: Option<Task<()>>, update_task: Option<Task<()>>,
max_size: Vector2F, max_size: Vector2F,
confirmed: bool,
} }
pub trait PickerDelegate: View { pub trait PickerDelegate: View {
@ -138,6 +139,7 @@ impl<D: PickerDelegate> Picker<D> {
update_task: None, update_task: None,
delegate, delegate,
max_size: vec2f(500., 420.), max_size: vec2f(500., 420.),
confirmed: false,
}; };
cx.defer(|this, cx| this.update_matches(cx)); cx.defer(|this, cx| this.update_matches(cx));
this this
@ -156,7 +158,7 @@ impl<D: PickerDelegate> Picker<D> {
) { ) {
match event { match event {
editor::Event::BufferEdited { .. } => self.update_matches(cx), editor::Event::BufferEdited { .. } => self.update_matches(cx),
editor::Event::Blurred => { editor::Event::Blurred if !self.confirmed => {
if let Some(delegate) = self.delegate.upgrade(cx) { if let Some(delegate) = self.delegate.upgrade(cx) {
delegate.update(cx, |delegate, cx| { delegate.update(cx, |delegate, cx| {
delegate.dismiss(cx); delegate.dismiss(cx);
@ -204,6 +206,7 @@ impl<D: PickerDelegate> Picker<D> {
pub fn select_index(&mut self, action: &SelectIndex, cx: &mut ViewContext<Self>) { pub fn select_index(&mut self, action: &SelectIndex, cx: &mut ViewContext<Self>) {
if let Some(delegate) = self.delegate.upgrade(cx) { if let Some(delegate) = self.delegate.upgrade(cx) {
let index = action.0; let index = action.0;
self.confirmed = true;
delegate.update(cx, |delegate, cx| { delegate.update(cx, |delegate, cx| {
delegate.set_selected_index(index, cx); delegate.set_selected_index(index, cx);
delegate.confirm(cx); delegate.confirm(cx);
@ -256,6 +259,7 @@ impl<D: PickerDelegate> Picker<D> {
fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) { fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
if let Some(delegate) = self.delegate.upgrade(cx) { if let Some(delegate) = self.delegate.upgrade(cx) {
self.confirmed = true;
delegate.update(cx, |delegate, cx| delegate.confirm(cx)); delegate.update(cx, |delegate, cx| delegate.confirm(cx));
} }
} }