From 559ce87b4eec8df766b435f5873fd2a85e1e2d28 Mon Sep 17 00:00:00 2001 From: apricotbucket28 <71973804+apricotbucket28@users.noreply.github.com> Date: Mon, 5 Aug 2024 04:54:02 -0300 Subject: [PATCH] linux: Save opened workspace when closing last window (#15754) Fixes https://github.com/zed-industries/zed/issues/15642 (also fixes a bug where replacing a workspace in the same window would reopen the old project after closing with `CTRL + Q`) Release Notes: - Linux: Fixed last workspace not being restored on startup ([#15642](https://github.com/zed-industries/zed/issues/15642)). - Fixed a bug where a closed workspace could be reopened on startup. --- crates/collab/src/tests/editor_tests.rs | 6 ++-- crates/recent_projects/src/recent_projects.rs | 7 ++-- crates/workspace/src/workspace.rs | 34 ++++++++++++++----- crates/zed/src/zed.rs | 3 +- 4 files changed, 36 insertions(+), 14 deletions(-) diff --git a/crates/collab/src/tests/editor_tests.rs b/crates/collab/src/tests/editor_tests.rs index cf6220d107..5b8b514f23 100644 --- a/crates/collab/src/tests/editor_tests.rs +++ b/crates/collab/src/tests/editor_tests.rs @@ -43,7 +43,7 @@ use std::{ }, }; use text::Point; -use workspace::Workspace; +use workspace::{CloseIntent, Workspace}; #[gpui::test(iterations = 10)] async fn test_host_disconnect( @@ -134,7 +134,9 @@ async fn test_host_disconnect( // Ensure client B is not prompted to save edits when closing window after disconnecting. let can_close = workspace_b - .update(cx_b, |workspace, cx| workspace.prepare_to_close(true, cx)) + .update(cx_b, |workspace, cx| { + workspace.prepare_to_close(CloseIntent::Quit, cx) + }) .unwrap() .await .unwrap(); diff --git a/crates/recent_projects/src/recent_projects.rs b/crates/recent_projects/src/recent_projects.rs index dd49d8fb69..7b75c280d6 100644 --- a/crates/recent_projects/src/recent_projects.rs +++ b/crates/recent_projects/src/recent_projects.rs @@ -32,7 +32,8 @@ use ui::{ }; use util::{paths::PathExt, ResultExt}; use workspace::{ - AppState, ModalView, SerializedWorkspaceLocation, Workspace, WorkspaceId, WORKSPACE_DB, + AppState, CloseIntent, ModalView, SerializedWorkspaceLocation, Workspace, WorkspaceId, + WORKSPACE_DB, }; #[derive(PartialEq, Clone, Deserialize, Default)] @@ -311,7 +312,7 @@ impl PickerDelegate for RecentProjectsDelegate { cx.spawn(move |workspace, mut cx| async move { let continue_replacing = workspace .update(&mut cx, |workspace, cx| { - workspace.prepare_to_close(true, cx) + workspace.prepare_to_close(CloseIntent::ReplaceWindow, cx) })? .await?; if continue_replacing { @@ -570,7 +571,7 @@ fn open_dev_server_project( cx.spawn(move |workspace, mut cx| async move { let continue_replacing = workspace .update(&mut cx, |workspace, cx| { - workspace.prepare_to_close(true, cx) + workspace.prepare_to_close(CloseIntent::ReplaceWindow, cx) })? .await?; if continue_replacing { diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index db12249871..37f75e7fda 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -224,6 +224,16 @@ impl_actions!( ] ); +#[derive(PartialEq, Eq, Debug)] +pub enum CloseIntent { + /// Quit the program entirely. + Quit, + /// Close a window. + CloseWindow, + /// Replace the workspace in an existing window. + ReplaceWindow, +} + #[derive(Clone)] pub struct Toast { id: NotificationId, @@ -1612,8 +1622,8 @@ impl Workspace { } pub fn close_window(&mut self, _: &CloseWindow, cx: &mut ViewContext) { + let prepare = self.prepare_to_close(CloseIntent::CloseWindow, cx); let window = cx.window_handle(); - let prepare = self.prepare_to_close(false, cx); cx.spawn(|_, mut cx| async move { if prepare.await? { window.update(&mut cx, |_, cx| { @@ -1627,12 +1637,17 @@ impl Workspace { pub fn prepare_to_close( &mut self, - quitting: bool, + close_intent: CloseIntent, cx: &mut ViewContext, ) -> Task> { let active_call = self.active_call().cloned(); let window = cx.window_handle(); + // On Linux and Windows, closing the last window should restore the last workspace. + let save_last_workspace = cfg!(not(target_os = "macos")) + && close_intent != CloseIntent::ReplaceWindow + && cx.windows().len() == 1; + cx.spawn(|this, mut cx| async move { let workspace_count = (*cx).update(|cx| { cx.windows() @@ -1642,7 +1657,7 @@ impl Workspace { })?; if let Some(active_call) = active_call { - if !quitting + if close_intent != CloseIntent::Quit && workspace_count == 1 && active_call.read_with(&cx, |call, _| call.room().is_some())? { @@ -1674,7 +1689,10 @@ impl Workspace { // If we're not quitting, but closing, we remove the workspace from // the current session. - if !quitting && save_result.as_ref().map_or(false, |&res| res) { + if close_intent != CloseIntent::Quit + && !save_last_workspace + && save_result.as_ref().map_or(false, |&res| res) + { this.update(&mut cx, |this, cx| this.remove_from_session(cx))? .await; } @@ -5572,7 +5590,7 @@ pub fn reload(reload: &Reload, cx: &mut AppContext) { // If the user cancels any save prompt, then keep the app open. for window in workspace_windows { if let Ok(should_close) = window.update(&mut cx, |workspace, cx| { - workspace.prepare_to_close(true, cx) + workspace.prepare_to_close(CloseIntent::Quit, cx) }) { if !should_close.await? { return Ok(()); @@ -5776,7 +5794,7 @@ mod tests { workspace.update(cx, |w, cx| { w.add_item_to_active_pane(Box::new(item1.clone()), None, true, cx) }); - let task = workspace.update(cx, |w, cx| w.prepare_to_close(false, cx)); + let task = workspace.update(cx, |w, cx| w.prepare_to_close(CloseIntent::CloseWindow, cx)); assert!(task.await.unwrap()); // When there are dirty untitled items, prompt to save each one. If the user @@ -5791,7 +5809,7 @@ mod tests { w.add_item_to_active_pane(Box::new(item2.clone()), None, true, cx); w.add_item_to_active_pane(Box::new(item3.clone()), None, true, cx); }); - let task = workspace.update(cx, |w, cx| w.prepare_to_close(false, cx)); + let task = workspace.update(cx, |w, cx| w.prepare_to_close(CloseIntent::CloseWindow, cx)); cx.executor().run_until_parked(); cx.simulate_prompt_answer(2); // cancel save all cx.executor().run_until_parked(); @@ -5832,7 +5850,7 @@ mod tests { w.add_item_to_active_pane(Box::new(item1.clone()), None, true, cx); w.add_item_to_active_pane(Box::new(item2.clone()), None, true, cx); }); - let task = workspace.update(cx, |w, cx| w.prepare_to_close(false, cx)); + let task = workspace.update(cx, |w, cx| w.prepare_to_close(CloseIntent::CloseWindow, cx)); assert!(task.await.unwrap()); } diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs index e1ffaf8b95..e727f0e170 100644 --- a/crates/zed/src/zed.rs +++ b/crates/zed/src/zed.rs @@ -35,6 +35,7 @@ use std::{borrow::Cow, ops::Deref, path::Path, sync::Arc}; use task::static_source::{StaticSource, TrackedFile}; use theme::ActiveTheme; use workspace::notifications::NotificationId; +use workspace::CloseIntent; use paths::{local_settings_file_relative_path, local_tasks_file_relative_path}; use terminal_view::terminal_panel::{self, TerminalPanel}; @@ -621,7 +622,7 @@ fn quit(_: &Quit, cx: &mut AppContext) { for window in workspace_windows { if let Some(should_close) = window .update(&mut cx, |workspace, cx| { - workspace.prepare_to_close(true, cx) + workspace.prepare_to_close(CloseIntent::Quit, cx) }) .log_err() {