Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Restore the focused workspace after quitting (#58392)

Lukas Wirth8b5db3582cde
diff --git a/crates/workspace/src/persistence.rs b/crates/workspace/src/persistence.rs
index 66af132b47c..1295ce60e52 100644
--- a/crates/workspace/src/persistence.rs
+++ b/crates/workspace/src/persistence.rs
@@ -356,6 +356,13 @@ pub fn read_serialized_multi_workspaces(
let active_workspace = state
.active_workspace_id
.and_then(|id| group.iter().position(|ws| ws.workspace_id == id))
+ // If the persisted active workspace can't be matched (e.g. its
+ // pointer was lost or its row was pruned), fall back to the
+ // first workspace that actually has paths rather than blindly
+ // taking index 0, so a stray scratch/empty workspace isn't
+ // restored as the focused window. Only if none have paths do we
+ // fall back to the first entry.
+ .or_else(|| group.iter().position(|ws| !ws.paths.is_empty()))
.or(Some(0))
.and_then(|index| group.into_iter().nth(index))?;
Some(model::SerializedMultiWorkspace {
diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs
index 7bc26f4679d..55644986147 100644
--- a/crates/zed/src/zed.rs
+++ b/crates/zed/src/zed.rs
@@ -1631,13 +1631,16 @@ fn quit(_: &Quit, cx: &mut App) {
// If the user cancels any save prompt, then keep the app open.
for window in &workspace_windows {
let window = *window;
- let workspaces = window
+ let active_and_workspaces = window
.update(cx, |multi_workspace, _, _cx| {
- multi_workspace.workspaces().cloned().collect::<Vec<_>>()
+ (
+ multi_workspace.workspace().clone(),
+ multi_workspace.workspaces().cloned().collect::<Vec<_>>(),
+ )
})
.log_err();
- let Some(workspaces) = workspaces else {
+ let Some((originally_active, workspaces)) = active_and_workspaces else {
continue;
};
@@ -1653,10 +1656,34 @@ fn quit(_: &Quit, cx: &mut App) {
.log_err()
{
if !should_close.await? {
+ // Activating each workspace above to surface its save
+ // prompts changed which workspace is active. Restore the
+ // user's focused workspace before bailing so the window
+ // is left as they had it.
+ window
+ .update(cx, |multi_workspace, window, cx| {
+ multi_workspace.activate(
+ originally_active.clone(),
+ None,
+ window,
+ cx,
+ );
+ })
+ .log_err();
return Ok(());
}
}
}
+
+ // The loop above activated each workspace in turn, overwriting the
+ // persisted active workspace. Re-activate the workspace the user
+ // actually had focused so it is the one serialized (and restored on
+ // next launch) as active, rather than whichever happened to be last.
+ window
+ .update(cx, |multi_workspace, window, cx| {
+ multi_workspace.activate(originally_active, None, window, cx);
+ })
+ .log_err();
}
// Flush all pending workspace serialization before quitting so that
// session_id/window_id are up-to-date in the database.
@@ -6459,6 +6486,145 @@ mod tests {
.unwrap();
}
+ #[gpui::test]
+ async fn test_quit_preserves_focused_workspace_for_restore(cx: &mut TestAppContext) {
+ use session::Session;
+ use workspace::{OpenMode, Workspace};
+
+ let app_state = init_test(cx);
+ cx.update(init);
+
+ let dir1 = path!("/dir1");
+ let dir2 = path!("/dir2");
+
+ let fs = app_state.fs.clone();
+ let fake_fs = fs.as_fake();
+ fake_fs.insert_tree(dir1, json!({})).await;
+ fake_fs.insert_tree(dir2, json!({})).await;
+
+ let session_id = cx.read(|cx| app_state.session.read(cx).id().to_owned());
+
+ // Window with two retained workspaces: dir1 added first, dir2 second.
+ let workspace::OpenResult { window, .. } = cx
+ .update(|cx| {
+ Workspace::new_local(
+ vec![dir1.into()],
+ app_state.clone(),
+ None,
+ None,
+ None,
+ OpenMode::Activate,
+ cx,
+ )
+ })
+ .await
+ .expect("failed to open first workspace");
+
+ window
+ .update(cx, |multi_workspace, _, cx| {
+ multi_workspace.open_sidebar(cx);
+ })
+ .unwrap();
+
+ window
+ .update(cx, |multi_workspace, window, cx| {
+ multi_workspace.open_project(vec![dir2.into()], OpenMode::Activate, window, cx)
+ })
+ .unwrap()
+ .await
+ .expect("failed to open second workspace");
+ cx.run_until_parked();
+
+ // Focus dir1 (the first workspace). dir2 was activated last when it was
+ // opened and is iterated last by the quit-time close-prompt loop, so
+ // without the fix the persisted active workspace gets clobbered to dir2.
+ window
+ .update(cx, |multi_workspace, window, cx| {
+ let workspace = multi_workspace.workspaces().next().unwrap().clone();
+ multi_workspace.activate(workspace, None, window, cx);
+ })
+ .unwrap();
+ cx.run_until_parked();
+
+ window
+ .read_with(cx, |mw, cx| {
+ assert!(
+ mw.workspace()
+ .read(cx)
+ .root_paths(cx)
+ .iter()
+ .any(|p| p.as_ref() == Path::new(dir1)),
+ "dir1 should be the focused workspace before quitting"
+ );
+ })
+ .unwrap();
+
+ // Quit. With no dirty items there are no save prompts, so the quit flow
+ // runs the prepare_to_close loop (which activates every workspace in
+ // turn to surface prompts) and then flushes serialization. cx.quit() is
+ // a no-op in tests, so the window stays around for inspection.
+ cx.dispatch_action(*window, Quit);
+ cx.run_until_parked();
+
+ // The fix re-activates the originally-focused workspace after the loop,
+ // so the window must still be focused on dir1, not dir2.
+ window
+ .read_with(cx, |mw, cx| {
+ let active = mw.workspace().read(cx).root_paths(cx);
+ assert!(
+ active.iter().any(|p| p.as_ref() == Path::new(dir1)),
+ "quitting must not change which workspace is focused"
+ );
+ assert!(
+ !active.iter().any(|p| p.as_ref() == Path::new(dir2)),
+ "dir2 must not become the focused workspace after quitting"
+ );
+ })
+ .unwrap();
+
+ // Simulate a fresh launch and verify dir1 is restored as the active
+ // workspace rather than dir2 (or an empty window).
+ window
+ .update(cx, |_, window, _| window.remove_window())
+ .unwrap();
+ cx.run_until_parked();
+
+ cx.update(|cx| {
+ app_state.session.update(cx, |app_session, _cx| {
+ app_session
+ .replace_session_for_test(Session::test_with_old_session(session_id.clone()));
+ });
+ });
+
+ let mut async_cx = cx.to_async();
+ crate::restore_or_create_workspace(app_state.clone(), &mut async_cx)
+ .await
+ .expect("failed to restore workspaces");
+ cx.run_until_parked();
+
+ let restored_windows: Vec<WindowHandle<MultiWorkspace>> = cx.read(|cx| {
+ cx.windows()
+ .into_iter()
+ .filter_map(|window| window.downcast::<MultiWorkspace>())
+ .collect()
+ });
+ assert_eq!(restored_windows.len(), 1);
+
+ restored_windows[0]
+ .read_with(cx, |mw, cx| {
+ let active = mw.workspace().read(cx).root_paths(cx);
+ assert!(
+ active.iter().any(|p| p.as_ref() == Path::new(dir1)),
+ "the focused workspace (dir1) must be restored as active"
+ );
+ assert!(
+ !active.iter().any(|p| p.as_ref() == Path::new(dir2)),
+ "dir2 must not be restored as the active workspace"
+ );
+ })
+ .unwrap();
+ }
+
#[gpui::test]
async fn test_restored_project_groups_survive_workspace_key_change(cx: &mut TestAppContext) {
use session::Session;