From eb2cce98a715ae078579efdf90a9776604649da9 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 1 May 2023 16:40:57 +0200 Subject: [PATCH] Move `dispatch_action_any_action_at` to `AsyncAppContext` --- crates/command_palette/src/command_palette.rs | 8 +-- crates/context_menu/src/context_menu.rs | 27 ++++++---- crates/gpui/src/app.rs | 51 ++++++------------- 3 files changed, 37 insertions(+), 49 deletions(-) diff --git a/crates/command_palette/src/command_palette.rs b/crates/command_palette/src/command_palette.rs index 1d9ac62c2c..5b1559bd29 100644 --- a/crates/command_palette/src/command_palette.rs +++ b/crates/command_palette/src/command_palette.rs @@ -167,9 +167,11 @@ impl PickerDelegate for CommandPaletteDelegate { let focused_view_id = self.focused_view_id; let action_ix = self.matches[self.selected_ix].candidate_id; let action = self.actions.remove(action_ix).action; - cx.defer(move |_, cx| { - cx.dispatch_any_action_at(window_id, focused_view_id, action); - }); + cx.app_context() + .spawn(move |mut cx| async move { + cx.dispatch_action(window_id, focused_view_id, action.as_ref()) + }) + .detach_and_log_err(cx); } cx.emit(PickerEvent::Dismiss); } diff --git a/crates/context_menu/src/context_menu.rs b/crates/context_menu/src/context_menu.rs index 3a3ca9b66b..e0429bd01b 100644 --- a/crates/context_menu/src/context_menu.rs +++ b/crates/context_menu/src/context_menu.rs @@ -227,11 +227,13 @@ impl ContextMenu { match action { ContextMenuItemAction::Action(action) => { let window_id = cx.window_id(); - cx.dispatch_any_action_at( - window_id, - self.parent_view_id, - action.boxed_clone(), - ); + let view_id = self.parent_view_id; + let action = action.boxed_clone(); + cx.app_context() + .spawn(|mut cx| async move { + cx.dispatch_action(window_id, view_id, action.as_ref()) + }) + .detach_and_log_err(cx); } ContextMenuItemAction::Handler(handler) => handler(cx), } @@ -459,11 +461,16 @@ impl ContextMenu { let window_id = cx.window_id(); match &action { ContextMenuItemAction::Action(action) => { - cx.dispatch_any_action_at( - window_id, - view_id, - action.boxed_clone(), - ); + let action = action.boxed_clone(); + cx.app_context() + .spawn(|mut cx| async move { + cx.dispatch_action( + window_id, + view_id, + action.as_ref(), + ) + }) + .detach_and_log_err(cx); } ContextMenuItemAction::Handler(handler) => handler(cx), } diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index 02d6c1a2ac..c757e7f383 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -309,6 +309,20 @@ impl AsyncAppContext { self.0.borrow_mut().update_window(window_id, callback) } + pub fn dispatch_action( + &mut self, + window_id: usize, + view_id: usize, + action: &dyn Action, + ) -> Result<()> { + self.0 + .borrow_mut() + .update_window(window_id, |window| { + window.handle_dispatch_action_from_effect(Some(view_id), action); + }) + .ok_or_else(|| anyhow!("window not found")) + } + pub fn add_model(&mut self, build_model: F) -> ModelHandle where T: Entity, @@ -1619,17 +1633,7 @@ impl AppContext { Effect::RefreshWindows => { refreshing = true; } - Effect::DispatchActionFrom { - window_id, - view_id, - action, - } => { - self.handle_dispatch_action_from_effect( - window_id, - Some(view_id), - action.as_ref(), - ); - } + Effect::ActionDispatchNotification { action_id } => { self.handle_action_dispatch_notification_effect(action_id) } @@ -1745,19 +1749,6 @@ impl AppContext { self.pending_effects.push_back(Effect::RefreshWindows); } - pub fn dispatch_any_action_at( - &mut self, - window_id: usize, - view_id: usize, - action: Box, - ) { - self.pending_effects.push_back(Effect::DispatchActionFrom { - window_id, - view_id, - action, - }); - } - fn perform_window_refresh(&mut self) { let window_ids = self.windows.keys().cloned().collect::>(); for window_id in window_ids { @@ -2155,11 +2146,6 @@ pub enum Effect { result: MatchResult, }, RefreshWindows, - DispatchActionFrom { - window_id: usize, - view_id: usize, - action: Box, - }, ActionDispatchNotification { action_id: TypeId, }, @@ -2248,13 +2234,6 @@ impl Debug for Effect { .field("view_id", view_id) .field("subscription_id", subscription_id) .finish(), - Effect::DispatchActionFrom { - window_id, view_id, .. - } => f - .debug_struct("Effect::DispatchActionFrom") - .field("window_id", window_id) - .field("view_id", view_id) - .finish(), Effect::ActionDispatchNotification { action_id, .. } => f .debug_struct("Effect::ActionDispatchNotification") .field("action_id", action_id)