mirror of
https://github.com/zed-industries/zed.git
synced 2024-11-08 07:35:01 +03:00
Allow toggling other modals from the command palette
This commit is contained in:
parent
f5377c2f50
commit
11eba96cb8
@ -27,6 +27,11 @@ pub struct CommandPalette {
|
||||
|
||||
pub enum Event {
|
||||
Dismissed,
|
||||
Confirmed {
|
||||
window_id: usize,
|
||||
focused_view_id: usize,
|
||||
action: Box<dyn Action>,
|
||||
},
|
||||
}
|
||||
|
||||
struct Command {
|
||||
@ -81,8 +86,18 @@ impl CommandPalette {
|
||||
cx: &mut ViewContext<Workspace>,
|
||||
) {
|
||||
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);
|
||||
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>) {
|
||||
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].action.as_ref(),
|
||||
)
|
||||
}
|
||||
cx.emit(Event::Confirmed {
|
||||
window_id: cx.window_id(),
|
||||
focused_view_id: self.focused_view_id,
|
||||
action: self.actions.remove(action_ix).action,
|
||||
});
|
||||
} else {
|
||||
cx.emit(Event::Dismissed);
|
||||
}
|
||||
}
|
||||
|
||||
fn render_match(&self, ix: usize, selected: bool, cx: &gpui::AppContext) -> gpui::ElementBox {
|
||||
let mat = &self.matches[ix];
|
||||
|
@ -20,6 +20,7 @@ pub struct Picker<D: PickerDelegate> {
|
||||
list_state: UniformListState,
|
||||
update_task: Option<Task<()>>,
|
||||
max_size: Vector2F,
|
||||
confirmed: bool,
|
||||
}
|
||||
|
||||
pub trait PickerDelegate: View {
|
||||
@ -138,6 +139,7 @@ impl<D: PickerDelegate> Picker<D> {
|
||||
update_task: None,
|
||||
delegate,
|
||||
max_size: vec2f(500., 420.),
|
||||
confirmed: false,
|
||||
};
|
||||
cx.defer(|this, cx| this.update_matches(cx));
|
||||
this
|
||||
@ -156,7 +158,7 @@ impl<D: PickerDelegate> Picker<D> {
|
||||
) {
|
||||
match event {
|
||||
editor::Event::BufferEdited { .. } => self.update_matches(cx),
|
||||
editor::Event::Blurred => {
|
||||
editor::Event::Blurred if !self.confirmed => {
|
||||
if let Some(delegate) = self.delegate.upgrade(cx) {
|
||||
delegate.update(cx, |delegate, 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>) {
|
||||
if let Some(delegate) = self.delegate.upgrade(cx) {
|
||||
let index = action.0;
|
||||
self.confirmed = true;
|
||||
delegate.update(cx, |delegate, cx| {
|
||||
delegate.set_selected_index(index, cx);
|
||||
delegate.confirm(cx);
|
||||
@ -256,6 +259,7 @@ impl<D: PickerDelegate> Picker<D> {
|
||||
|
||||
fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
|
||||
if let Some(delegate) = self.delegate.upgrade(cx) {
|
||||
self.confirmed = true;
|
||||
delegate.update(cx, |delegate, cx| delegate.confirm(cx));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user