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

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