Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Improve deactivate simulation
Conrad Irwinfff415e3e908
diff --git a/crates/editor/src/link_go_to_definition.rs b/crates/editor/src/link_go_to_definition.rsindex 42f502daeda..04dcf930150 100644--- a/crates/editor/src/link_go_to_definition.rs+++ b/crates/editor/src/link_go_to_definition.rs@@ -930,10 +930,7 @@ mod tests {fn do_work() { «test»(); }"});- // Deactivating the window dismisses the highlight- cx.update_workspace(|workspace, cx| {- workspace.on_window_activation_changed(cx);- });+ cx.cx.cx.deactivate_window();cx.assert_editor_text_highlights::<LinkGoToDefinitionState>(indoc! {"fn test() { do_work(); }fn do_work() { test(); }diff --git a/crates/gpui/src/app/test_context.rs b/crates/gpui/src/app/test_context.rsindex dfbffaf2e59..a5f66be09b2 100644--- a/crates/gpui/src/app/test_context.rs+++ b/crates/gpui/src/app/test_context.rs@@ -519,16 +519,11 @@ impl<'a> VisualTestContext<'a> {self.cx.simulate_input(self.window, input)}- pub fn simulate_activation(&mut self) {- self.cx- .test_window(self.window)- .simulate_active_status_change(true)- }-- pub fn simulate_deactivation(&mut self) {- self.cx- .test_window(self.window)- .simulate_active_status_change(false)+ pub fn deactivate_window(&mut self) {+ if Some(self.window) == self.test_platform.active_window() {+ self.test_platform.set_active_window(None)+ }+ self.background_executor.run_until_parked();}}diff --git a/crates/gpui/src/platform/test/platform.rs b/crates/gpui/src/platform/test/platform.rsindex 889e8b971e9..111fb839211 100644--- a/crates/gpui/src/platform/test/platform.rs+++ b/crates/gpui/src/platform/test/platform.rs@@ -79,6 +79,28 @@ impl TestPlatform {self.prompts.borrow_mut().multiple_choice.push_back(tx);rx}++ pub(crate) fn set_active_window(&self, window: Option<TestWindow>) {+ let executor = self.foreground_executor().clone();+ let previous_window = self.active_window.borrow_mut().take();+ *self.active_window.borrow_mut() = window.clone();++ executor+ .spawn(async move {+ if let Some(previous_window) = previous_window {+ if let Some(window) = window.as_ref() {+ if Arc::ptr_eq(&previous_window.0, &window.0) {+ return;+ }+ }+ previous_window.simulate_active_status_change(false);+ }+ if let Some(window) = window {+ window.simulate_active_status_change(true);+ }+ })+ .detach();+ }}// todo!("implement out what our tests needed in GPUI 1")diff --git a/crates/gpui/src/platform/test/window.rs b/crates/gpui/src/platform/test/window.rsindex eee2cf93c76..e0128d21297 100644--- a/crates/gpui/src/platform/test/window.rs+++ b/crates/gpui/src/platform/test/window.rs@@ -1,6 +1,6 @@use crate::{px, AnyWindowHandle, AtlasKey, AtlasTextureId, AtlasTile, Bounds, InputEvent, KeyDownEvent,- Keystroke, Pixels, Platform, PlatformAtlas, PlatformDisplay, PlatformInputHandler,+ Keystroke, Pixels, PlatformAtlas, PlatformDisplay, PlatformInputHandler,PlatformWindow, Point, Size, TestPlatform, TileId, WindowAppearance, WindowBounds,WindowOptions,};@@ -76,7 +76,7 @@ impl TestWindow {self.0.lock().resize_callback = Some(callback);}- pub fn simulate_active_status_change(&self, active: bool) {+ pub(crate) fn simulate_active_status_change(&self, active: bool) {let mut lock = self.0.lock();let Some(mut callback) = lock.active_status_change_callback.take() else {return;@@ -184,33 +184,12 @@ impl PlatformWindow for TestWindow {}fn activate(&self) {- let this = self.clone();- let executor = self- .0+ self.0.lock().platform.upgrade().unwrap()- .foreground_executor()- .clone();-- executor- .spawn(async move {- let state = this.0.lock();- let platform = state.platform.upgrade().unwrap();- let previous_window = platform.active_window.borrow_mut().replace(this.clone());- drop(state);- drop(platform);- if let Some(previous_window) = previous_window {- if Arc::ptr_eq(&previous_window.0, &this.0) {- return;- }- previous_window.simulate_active_status_change(false);- }-- this.simulate_active_status_change(true);- })- .detach();+ .set_active_window(Some(self.clone()))}fn set_title(&mut self, title: &str) {diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rsindex 6c31366f93c..37bf224d4f3 100644--- a/crates/workspace/src/workspace.rs+++ b/crates/workspace/src/workspace.rs@@ -4763,8 +4763,7 @@ mod tests {});// Deactivating the window saves the file.- cx.simulate_deactivation();- cx.executor().run_until_parked();+ cx.deactivate_window();item.update(cx, |item, _| assert_eq!(item.save_count, 1));// Autosave on focus change.@@ -4784,14 +4783,13 @@ mod tests {item.update(cx, |item, _| assert_eq!(item.save_count, 2));// Deactivating the window still saves the file.- cx.simulate_activation();+ cx.update(|cx| cx.activate_window());item.update(cx, |item, cx| {cx.focus_self();item.is_dirty = true;});- cx.simulate_deactivation();+ cx.deactivate_window();- cx.executor().run_until_parked();item.update(cx, |item, _| assert_eq!(item.save_count, 3));// Autosave after delay.