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.
This commit is contained in:
apricotbucket28 2024-08-05 04:54:02 -03:00 committed by GitHub
parent 0ec29d6866
commit 559ce87b4e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 36 additions and 14 deletions

View File

@ -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();

View File

@ -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 {

View File

@ -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<Self>) {
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<Self>,
) -> Task<Result<bool>> {
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());
}

View File

@ -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()
{