Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
workspace: Update window title when switching active workspace (#58401)
Lukas Wirtha32999e00b01
diff --git a/crates/workspace/src/multi_workspace.rs b/crates/workspace/src/multi_workspace.rsindex ed652ef3745..3d87e65d11b 100644--- a/crates/workspace/src/multi_workspace.rs+++ b/crates/workspace/src/multi_workspace.rs@@ -11,9 +11,10 @@ use project::{DisableAiSettings, Project};use remote::RemoteConnectionOptions;use settings::Settings;pub use settings::SidebarSide;+use std::cell::Cell;use std::future::Future;-use std::path::PathBuf;+use std::rc::Rc;use ui::prelude::*;use util::ResultExt;use util::path_list::PathList;@@ -288,6 +289,13 @@ pub struct MultiWorkspace {retained_workspaces: Vec<Entity<Workspace>>,project_groups: Vec<ProjectGroupState>,active_workspace: Entity<Workspace>,+ /// Source of truth for which workspace is presented in this window, shared+ /// with each member `Workspace` so they can tell whether they own the+ /// platform window's title and edited indicator. This only exists to prevent+ /// Workspaces from having to read their parent MultiWorkspace to check+ /// chrome ownership, as that might cause a double lease. Kept in sync with+ /// `active_workspace`.+ active_workspace_id: Rc<Cell<EntityId>>,sidebar: Option<Box<dyn SidebarHandle>>,sidebar_open: bool,sidebar_overlay: Option<AnyView>,@@ -337,14 +345,16 @@ impl MultiWorkspace {});Self::subscribe_to_workspace(&workspace, window, cx);let weak_self = cx.weak_entity();+ let active_workspace_id = Rc::new(Cell::new(workspace.entity_id()));workspace.update(cx, |workspace, cx| {- workspace.set_multi_workspace(weak_self, cx);+ workspace.set_multi_workspace(weak_self, active_workspace_id.clone(), cx);});Self {window_id: window.window_handle().window_id(),retained_workspaces: Vec::new(),project_groups: Vec::new(),active_workspace: workspace,+ active_workspace_id,sidebar: None,sidebar_open: false,sidebar_overlay: None,@@ -745,8 +755,9 @@ impl MultiWorkspace {) {Self::subscribe_to_workspace(workspace, window, cx);let weak_self = cx.weak_entity();+ let active_workspace_id = self.active_workspace_id.clone();workspace.update(cx, |workspace, cx| {- workspace.set_multi_workspace(weak_self, cx);+ workspace.set_multi_workspace(weak_self, active_workspace_id, cx);});let entity = cx.entity();@@ -1488,6 +1499,10 @@ impl MultiWorkspace {}self.active_workspace = workspace;+ // Publish the new active workspace before anyone reads the shared cell+ // to decide who owns the window chrome.+ self.active_workspace_id+ .set(self.active_workspace.entity_id());let active_key = self.active_workspace.read(cx).project_group_key(cx);if let Some(group) = self.project_groups.iter_mut().find(|g| g.key == active_key) {@@ -1498,6 +1513,14 @@ impl MultiWorkspace {self.detach_workspace(&old_active_workspace, cx);}+ // The platform window is shared across all workspaces in this window.+ // The previously-active workspace left the title and edited indicator+ // reflecting its own state, so re-apply them from the newly-active+ // workspace (which is now the chrome owner per `owns_window_chrome`).+ self.active_workspace.update(cx, |workspace, cx| {+ workspace.refresh_window_state(window, cx);+ });+cx.emit(MultiWorkspaceEvent::ActiveWorkspaceChanged { source_workspace });self.serialize(cx);self.focus_active_workspace(window, cx);diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rsindex f9534d838a1..a1b588988b1 100644--- a/crates/workspace/src/workspace.rs+++ b/crates/workspace/src/workspace.rs@@ -123,7 +123,7 @@ pub use status_bar::{HideStatusItem, StatusItemView, add_hide_button_entry};use std::{any::TypeId,borrow::Cow,- cell::RefCell,+ cell::{Cell, RefCell},cmp,collections::VecDeque,env,@@ -1409,6 +1409,13 @@ pub struct Workspace {_panels_task: Option<Task<Result<()>>>,sidebar_focus_handle: Option<FocusHandle>,multi_workspace: Option<WeakEntity<MultiWorkspace>>,+ /// Shared with the parent `MultiWorkspace` and any sibling workspaces: holds+ /// the id of the single workspace currently presented in this OS window.+ /// `MultiWorkspace` is the only writer; workspaces only read it to decide+ /// whether they may write the shared window's title and edited indicator. We+ /// use this instead of going through the `multi_workspace` field to avoid+ /// reading it as we might end up in a double lease otherwise.+ active_workspace_id: Option<Rc<Cell<EntityId>>>,active_worktree_creation: ActiveWorktreeCreation,deferred_save_items: Vec<Box<dyn WeakItemHandle>>,}@@ -1853,6 +1860,7 @@ impl Workspace {removing: false,sidebar_focus_handle: None,multi_workspace,+ active_workspace_id: None,active_worktree_creation: ActiveWorktreeCreation::default(),open_in_dev_container: false,_dev_container_task: None,@@ -2583,12 +2591,14 @@ impl Workspace {pub fn set_multi_workspace(&mut self,multi_workspace: WeakEntity<MultiWorkspace>,+ active_workspace_id: Rc<Cell<EntityId>>,cx: &mut App,) {self.status_bar.update(cx, |status_bar, cx| {status_bar.set_multi_workspace(multi_workspace.clone(), cx);});self.multi_workspace = Some(multi_workspace);+ self.active_workspace_id = Some(active_workspace_id);}pub fn app_state(&self) -> &Arc<AppState> {@@ -6098,7 +6108,31 @@ impl Workspace {self.update_window_title(window, cx);}+ /// Whether this workspace may write the platform window's title and edited+ /// indicator.+ ///+ /// In a multi-workspace window several workspaces share one platform+ /// window, so only the active one is allowed to write that chrome —+ /// otherwise a background workspace's project/item events would clobber the+ /// active workspace's title. `MultiWorkspace` publishes the active+ /// workspace's id into the shared `active_workspace_id` cell, which we+ /// simply compare against our own id. A workspace with no shared cell (e.g.+ /// a plain test window) owns its window unconditionally.+ fn owns_window_chrome(&self) -> bool {+ match &self.active_workspace_id {+ Some(active_workspace_id) => active_workspace_id.get() == self.weak_self.entity_id(),+ None => true,+ }+ }+fn update_window_title(&mut self, window: &mut Window, cx: &mut App) {+ if !self.owns_window_chrome() {+ return;+ }+ self.apply_window_title(window, cx);+ }++ fn apply_window_title(&mut self, window: &mut Window, cx: &mut App) {let project = self.project().read(cx);let mut title = String::new();@@ -6158,14 +6192,45 @@ impl Workspace {self.last_window_title = Some(title);}+ fn is_window_edited(&self, cx: &App) -> bool {+ !self.project.read(cx).is_disconnected(cx) && !self.dirty_items.is_empty()+ }+fn update_window_edited(&mut self, window: &mut Window, cx: &mut App) {- let is_edited = !self.project.read(cx).is_disconnected(cx) && !self.dirty_items.is_empty();+ if !self.owns_window_chrome() {+ return;+ }+ let is_edited = self.is_window_edited(cx);if is_edited != self.window_edited {self.window_edited = is_edited;window.set_window_edited(self.window_edited)}}+ /// Re-applies this workspace's title and edited indicator to the platform+ /// window, bypassing the change-detection caches.+ ///+ /// Several workspaces can share a single platform window (a multi-workspace+ /// window). The `last_window_title`/`window_edited` caches assume the+ /// window's title and edited state reflect *this* workspace, but after the+ /// active workspace changes those values may have been set by a different+ /// workspace. Clearing the caches forces the values to be re-applied so the+ /// window reflects the newly-active workspace.+ ///+ /// This is only ever invoked for the workspace that just became active, so+ /// it writes directly rather than going through `update_window_title` /+ /// `update_window_edited`, whose change-detection caches would otherwise+ /// suppress the write when this workspace's computed title/edited state+ /// matches what *it* last set (even though the shared window currently+ /// shows a different workspace's state).+ pub fn refresh_window_state(&mut self, window: &mut Window, cx: &mut App) {+ self.last_window_title = None;+ self.apply_window_title(window, cx);++ self.window_edited = self.is_window_edited(cx);+ window.set_window_edited(self.window_edited);+ }+fn update_item_dirty_state(&mut self,item: &dyn ItemHandle,@@ -15811,6 +15876,93 @@ mod tests {});}+ #[gpui::test]+ async fn test_window_title_follows_active_workspace(cx: &mut TestAppContext) {+ init_test(cx);+ let fs = FakeFs::new(cx.executor());+ fs.insert_tree(path!("/root1"), json!({ "a.txt": "" }))+ .await;+ fs.insert_tree(path!("/root2"), json!({ "b.txt": "" }))+ .await;++ let project_a = Project::test(fs.clone(), [path!("/root1").as_ref()], cx).await;+ let project_b = Project::test(fs, [path!("/root2").as_ref()], cx).await;++ let multi_workspace_handle =+ cx.add_window(|window, cx| MultiWorkspace::test_new(project_a.clone(), window, cx));+ let workspace_a = multi_workspace_handle+ .read_with(cx, |mw, _| mw.workspace().clone())+ .unwrap();++ let cx = &mut VisualTestContext::from_window(multi_workspace_handle.into(), cx);+ cx.run_until_parked();+ assert_eq!(cx.window_title().as_deref(), Some("root1"));++ // Activating a second workspace must update the shared window's title.+ multi_workspace_handle+ .update(cx, |mw, window, cx| {+ mw.test_add_workspace(project_b.clone(), window, cx)+ })+ .unwrap();+ cx.run_until_parked();+ assert_eq!(cx.window_title().as_deref(), Some("root2"));++ // Switching back must update the title again, even though workspace A's+ // own computed title hasn't changed since it was last active. This is+ // the regression: the per-workspace title cache was stale relative to+ // the shared window title.+ multi_workspace_handle+ .update(cx, |mw, window, cx| {+ mw.activate(workspace_a.clone(), None, window, cx);+ })+ .unwrap();+ cx.run_until_parked();+ assert_eq!(cx.window_title().as_deref(), Some("root1"));+ }++ #[gpui::test]+ async fn test_background_workspace_does_not_clobber_window_title(cx: &mut TestAppContext) {+ init_test(cx);+ let fs = FakeFs::new(cx.executor());+ fs.insert_tree(path!("/root1"), json!({ "a.txt": "" }))+ .await;+ fs.insert_tree(path!("/root2"), json!({ "b.txt": "" }))+ .await;+ fs.insert_tree(path!("/root3"), json!({ "c.txt": "" }))+ .await;++ let project_a = Project::test(fs.clone(), [path!("/root1").as_ref()], cx).await;+ let project_b = Project::test(fs.clone(), [path!("/root2").as_ref()], cx).await;++ let multi_workspace_handle =+ cx.add_window(|window, cx| MultiWorkspace::test_new(project_a.clone(), window, cx));++ let cx = &mut VisualTestContext::from_window(multi_workspace_handle.into(), cx);+ cx.run_until_parked();+ assert_eq!(cx.window_title().as_deref(), Some("root1"));++ // Switch to workspace B; workspace A becomes a background workspace whose+ // event subscriptions are still live.+ multi_workspace_handle+ .update(cx, |mw, window, cx| {+ mw.test_add_workspace(project_b.clone(), window, cx)+ })+ .unwrap();+ cx.run_until_parked();+ assert_eq!(cx.window_title().as_deref(), Some("root2"));++ // A title-affecting change in the background workspace A must not touch+ // the shared window's title, which belongs to the active workspace B.+ project_a+ .update(cx, |project, cx| {+ project.find_or_create_worktree(path!("/root3"), true, cx)+ })+ .await+ .unwrap();+ cx.run_until_parked();+ assert_eq!(cx.window_title().as_deref(), Some("root2"));+ }+fn pane_items_paths(pane: &Entity<Pane>, cx: &App) -> Vec<String> {pane.read(cx).items()