From 183b9ef8095efd3572b1c3becc576d70f382483c Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Fri, 14 Apr 2023 11:56:31 +0200 Subject: [PATCH] Make full-screen and window bounds callbacks take a WindowContext --- crates/gpui/src/app.rs | 116 +++++------------------- crates/gpui/src/app/window.rs | 73 ++++++++++++++- crates/vim/src/test/vim_test_context.rs | 4 +- crates/vim/src/vim.rs | 5 +- crates/zed/src/zed.rs | 3 +- 5 files changed, 98 insertions(+), 103 deletions(-) diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index fbd83625a5..1a90e2f2da 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -500,10 +500,10 @@ type FocusObservationCallback = Box bool> type ReleaseObservationCallback = Box; type ActionObservationCallback = Box; type WindowActivationCallback = Box bool>; -type WindowFullscreenCallback = Box bool>; -type WindowBoundsCallback = Box bool>; +type WindowFullscreenCallback = Box bool>; +type WindowBoundsCallback = Box bool>; type KeystrokeCallback = - Box>, &mut AppContext) -> bool>; + Box>, &mut WindowContext) -> bool>; type ActiveLabeledTasksCallback = Box bool>; type DeserializeActionCallback = fn(json: &str) -> anyhow::Result>; type WindowShouldCloseSubscriptionCallback = Box bool>; @@ -1067,72 +1067,7 @@ impl AppContext { ) } - fn observe_window_activation(&mut self, window_id: usize, callback: F) -> Subscription - where - F: 'static + FnMut(bool, &mut WindowContext) -> bool, - { - let subscription_id = post_inc(&mut self.next_subscription_id); - self.pending_effects - .push_back(Effect::WindowActivationObservation { - window_id, - subscription_id, - callback: Box::new(callback), - }); - Subscription::WindowActivationObservation( - self.window_activation_observations - .subscribe(window_id, subscription_id), - ) - } - - fn observe_fullscreen(&mut self, window_id: usize, callback: F) -> Subscription - where - F: 'static + FnMut(bool, &mut AppContext) -> bool, - { - let subscription_id = post_inc(&mut self.next_subscription_id); - self.pending_effects - .push_back(Effect::WindowFullscreenObservation { - window_id, - subscription_id, - callback: Box::new(callback), - }); - Subscription::WindowActivationObservation( - self.window_activation_observations - .subscribe(window_id, subscription_id), - ) - } - - fn observe_window_bounds(&mut self, window_id: usize, callback: F) -> Subscription - where - F: 'static + FnMut(WindowBounds, Uuid, &mut AppContext) -> bool, - { - let subscription_id = post_inc(&mut self.next_subscription_id); - self.pending_effects - .push_back(Effect::WindowBoundsObservation { - window_id, - subscription_id, - callback: Box::new(callback), - }); - Subscription::WindowBoundsObservation( - self.window_bounds_observations - .subscribe(window_id, subscription_id), - ) - } - - pub fn observe_keystrokes(&mut self, window_id: usize, callback: F) -> Subscription - where - F: 'static - + FnMut(&Keystroke, &MatchResult, Option<&Box>, &mut AppContext) -> bool, - { - let subscription_id = post_inc(&mut self.next_subscription_id); - self.keystroke_observations - .add_callback(window_id, subscription_id, Box::new(callback)); - Subscription::KeystrokeObservation( - self.keystroke_observations - .subscribe(window_id, subscription_id), - ) - } - - pub fn observe_active_labeled_tasks(&mut self, callback: F) -> Subscription + fn observe_active_labeled_tasks(&mut self, callback: F) -> Subscription where F: 'static + FnMut(&mut AppContext) -> bool, { @@ -1906,10 +1841,10 @@ impl AppContext { handled_by: Option>, result: MatchResult, ) { - self.update(|this| { - let mut observations = this.keystroke_observations.clone(); + self.update_window(window_id, |cx| { + let mut observations = cx.keystroke_observations.clone(); observations.emit(window_id, move |callback| { - callback(&keystroke, &result, handled_by.as_ref(), this) + callback(&keystroke, &result, handled_by.as_ref(), cx) }); }); } @@ -3266,9 +3201,8 @@ impl<'a, 'b, 'c, V: View> ViewContext<'a, 'b, 'c, V> { F: 'static + FnMut(&mut V, bool, &mut ViewContext), { let observer = self.weak_handle(); - let window_id = self.window_id; self.window_context - .observe_window_activation(window_id, move |active, cx| { + .observe_window_activation(move |active, cx| { if let Some(observer) = observer.upgrade(cx) { observer.update(cx, |observer, cx| { callback(observer, active, cx); @@ -3285,18 +3219,16 @@ impl<'a, 'b, 'c, V: View> ViewContext<'a, 'b, 'c, V> { F: 'static + FnMut(&mut V, bool, &mut ViewContext), { let observer = self.weak_handle(); - let window_id = self.window_id; - self.window_context - .observe_fullscreen(window_id, move |active, cx| { - if let Some(observer) = observer.upgrade(cx) { - observer.update(cx, |observer, cx| { - callback(observer, active, cx); - }); - true - } else { - false - } - }) + self.window_context.observe_fullscreen(move |active, cx| { + if let Some(observer) = observer.upgrade(cx) { + observer.update(cx, |observer, cx| { + callback(observer, active, cx); + }); + true + } else { + false + } + }) } pub fn observe_keystrokes(&mut self, mut callback: F) -> Subscription @@ -3311,10 +3243,8 @@ impl<'a, 'b, 'c, V: View> ViewContext<'a, 'b, 'c, V> { ) -> bool, { let observer = self.weak_handle(); - let window_id = self.window_id; - self.window_context.observe_keystrokes( - window_id, - move |keystroke, result, handled_by, cx| { + self.window_context + .observe_keystrokes(move |keystroke, result, handled_by, cx| { if let Some(observer) = observer.upgrade(cx) { observer.update(cx, |observer, cx| { callback(observer, keystroke, handled_by, result, cx); @@ -3323,8 +3253,7 @@ impl<'a, 'b, 'c, V: View> ViewContext<'a, 'b, 'c, V> { } else { false } - }, - ) + }) } pub fn observe_window_bounds(&mut self, mut callback: F) -> Subscription @@ -3332,9 +3261,8 @@ impl<'a, 'b, 'c, V: View> ViewContext<'a, 'b, 'c, V> { F: 'static + FnMut(&mut V, WindowBounds, Uuid, &mut ViewContext), { let observer = self.weak_handle(); - let window_id = self.window_id; self.window_context - .observe_window_bounds(window_id, move |bounds, display, cx| { + .observe_window_bounds(move |bounds, display, cx| { if let Some(observer) = observer.upgrade(cx) { observer.update(cx, |observer, cx| { callback(observer, bounds, display, cx); diff --git a/crates/gpui/src/app/window.rs b/crates/gpui/src/app/window.rs index c9008b551e..106ec2b32e 100644 --- a/crates/gpui/src/app/window.rs +++ b/crates/gpui/src/app/window.rs @@ -15,8 +15,8 @@ use crate::{ util::post_inc, Action, AnyView, AnyViewHandle, AnyWeakViewHandle, AppContext, Drawable, Effect, Entity, ModelContext, ModelHandle, MouseRegion, MouseRegionId, ParentId, ReadModel, ReadView, - SceneBuilder, UpdateModel, UpdateView, UpgradeViewHandle, View, ViewContext, ViewHandle, - WeakViewHandle, WindowInvalidation, + SceneBuilder, Subscription, UpdateModel, UpdateView, UpgradeViewHandle, View, ViewContext, + ViewHandle, WeakViewHandle, WindowInvalidation, }; use anyhow::{anyhow, bail, Result}; use collections::{HashMap, HashSet}; @@ -239,6 +239,75 @@ impl<'a: 'b, 'b> WindowContext<'a, 'b> { Some(result) } + pub(crate) fn observe_window_activation(&mut self, callback: F) -> Subscription + where + F: 'static + FnMut(bool, &mut WindowContext) -> bool, + { + let window_id = self.window_id; + let subscription_id = post_inc(&mut self.next_subscription_id); + self.pending_effects + .push_back(Effect::WindowActivationObservation { + window_id, + subscription_id, + callback: Box::new(callback), + }); + Subscription::WindowActivationObservation( + self.window_activation_observations + .subscribe(window_id, subscription_id), + ) + } + + pub(crate) fn observe_fullscreen(&mut self, callback: F) -> Subscription + where + F: 'static + FnMut(bool, &mut WindowContext) -> bool, + { + let window_id = self.window_id; + let subscription_id = post_inc(&mut self.next_subscription_id); + self.pending_effects + .push_back(Effect::WindowFullscreenObservation { + window_id, + subscription_id, + callback: Box::new(callback), + }); + Subscription::WindowActivationObservation( + self.window_activation_observations + .subscribe(window_id, subscription_id), + ) + } + + pub(crate) fn observe_window_bounds(&mut self, callback: F) -> Subscription + where + F: 'static + FnMut(WindowBounds, Uuid, &mut WindowContext) -> bool, + { + let window_id = self.window_id; + let subscription_id = post_inc(&mut self.next_subscription_id); + self.pending_effects + .push_back(Effect::WindowBoundsObservation { + window_id, + subscription_id, + callback: Box::new(callback), + }); + Subscription::WindowBoundsObservation( + self.window_bounds_observations + .subscribe(window_id, subscription_id), + ) + } + + pub fn observe_keystrokes(&mut self, callback: F) -> Subscription + where + F: 'static + + FnMut(&Keystroke, &MatchResult, Option<&Box>, &mut WindowContext) -> bool, + { + let window_id = self.window_id; + let subscription_id = post_inc(&mut self.next_subscription_id); + self.keystroke_observations + .add_callback(window_id, subscription_id, Box::new(callback)); + Subscription::KeystrokeObservation( + self.keystroke_observations + .subscribe(window_id, subscription_id), + ) + } + /// Return keystrokes that would dispatch the given action on the given view. pub(crate) fn keystrokes_for_action( &mut self, diff --git a/crates/vim/src/test/vim_test_context.rs b/crates/vim/src/test/vim_test_context.rs index f5614b4b47..2e668292e8 100644 --- a/crates/vim/src/test/vim_test_context.rs +++ b/crates/vim/src/test/vim_test_context.rs @@ -31,11 +31,9 @@ impl<'a> VimTestContext<'a> { }); }); - let window_id = cx.window_id; - // Setup search toolbars and keypress hook cx.update_workspace(|workspace, cx| { - observe_keystrokes(window_id, cx); + observe_keystrokes(cx); workspace.active_pane().update(cx, |pane, cx| { pane.toolbar().update(cx, |toolbar, cx| { let buffer_search_bar = cx.add_view(BufferSearchBar::new); diff --git a/crates/vim/src/vim.rs b/crates/vim/src/vim.rs index b553596421..5f6c6435df 100644 --- a/crates/vim/src/vim.rs +++ b/crates/vim/src/vim.rs @@ -16,6 +16,7 @@ use collections::CommandPaletteFilter; use editor::{Bias, Cancel, Editor, EditorMode}; use gpui::{ actions, impl_actions, AppContext, Subscription, ViewContext, ViewHandle, WeakViewHandle, + WindowContext, }; use language::CursorShape; use motion::Motion; @@ -95,8 +96,8 @@ pub fn init(cx: &mut AppContext) { .detach(); } -pub fn observe_keystrokes(window_id: usize, cx: &mut AppContext) { - cx.observe_keystrokes(window_id, |_keystroke, _result, handled_by, cx| { +pub fn observe_keystrokes(cx: &mut WindowContext) { + cx.observe_keystrokes(|_keystroke, _result, handled_by, cx| { if let Some(handled_by) = handled_by { // Keystroke is handled by the vim system, so continue forward // Also short circuit if it is the special cancel action diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs index 19401d5312..6ac9b190d2 100644 --- a/crates/zed/src/zed.rs +++ b/crates/zed/src/zed.rs @@ -333,8 +333,7 @@ pub fn initialize_workspace( auto_update::notify_of_any_new_update(cx.weak_handle(), cx); - let window_id = cx.window_id(); - vim::observe_keystrokes(window_id, cx); + vim::observe_keystrokes(cx); cx.on_window_should_close(|workspace, cx| { if let Some(task) = workspace.close(&Default::default(), cx) {