zed: Add ability to restore last session w/ multiple windows (#14965)

This adds the ability for Zed to restore multiple windows after
restarting. It's now the default behavior.

Release Notes:

- Added ability to restore all windows that were open when Zed was quit.
Previously only the last used workspace was restored. This is now the
default behavior. To get back the old behavior, add the following to
your settings: `{"restore_on_startup": "last_workspace"}` (Part of
[#4985](https://github.com/zed-industries/zed/issues/4985) and
[#4683](https://github.com/zed-industries/zed/issues/4683))

Demo:



https://github.com/user-attachments/assets/57a375ec-0c6a-4724-97c4-3fea8f18bc2d

---------

Co-authored-by: Nathan <nathan@zed.dev>
This commit is contained in:
Thorsten Ball 2024-07-23 19:44:02 +02:00 committed by GitHub
parent 53f828df7d
commit 17ef9a367f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 660 additions and 172 deletions

View file

@ -28,6 +28,7 @@ use assets::Assets;
use node_runtime::RealNodeRuntime;
use parking_lot::Mutex;
use release_channel::{AppCommitSha, AppVersion};
use session::Session;
use settings::{handle_settings_file_changes, watch_config_file, Settings, SettingsStore};
use simplelog::ConfigBuilder;
use smol::process::Command;
@ -307,10 +308,15 @@ fn main() {
.block(installation_id())
.ok()
.unzip();
let session_id = Uuid::new_v4().to_string();
let session = app.background_executor().block(Session::new());
let app_version = AppVersion::init(env!("CARGO_PKG_VERSION"));
reliability::init_panic_hook(installation_id.clone(), app_version, session_id.clone());
reliability::init_panic_hook(
installation_id.clone(),
app_version,
session.id().to_owned(),
);
let (open_listener, mut open_rx) = OpenListener::new();
@ -422,7 +428,7 @@ fn main() {
client::init(&client, cx);
language::init(cx);
let telemetry = client.telemetry();
telemetry.start(installation_id.clone(), session_id, cx);
telemetry.start(installation_id.clone(), session.id().to_owned(), cx);
telemetry.report_app_event(
match existing_installation_id_found {
Some(false) => "first open",
@ -438,6 +444,7 @@ fn main() {
build_window_options,
workspace_store,
node_runtime: node_runtime.clone(),
session,
});
AppState::set_global(Arc::downgrade(&app_state), cx);
@ -657,23 +664,18 @@ async fn restore_or_create_workspace(
app_state: Arc<AppState>,
cx: &mut AsyncAppContext,
) -> Result<()> {
let restore_behaviour = cx.update(|cx| WorkspaceSettings::get(None, cx).restore_on_startup)?;
let location = match restore_behaviour {
workspace::RestoreOnStartupBehaviour::LastWorkspace => {
workspace::last_opened_workspace_paths().await
if let Some(locations) = restorable_workspace_locations(cx, &app_state).await {
for location in locations {
cx.update(|cx| {
workspace::open_paths(
location.paths().as_ref(),
app_state.clone(),
workspace::OpenOptions::default(),
cx,
)
})?
.await?;
}
_ => None,
};
if let Some(location) = location {
cx.update(|cx| {
workspace::open_paths(
location.paths().as_ref(),
app_state,
workspace::OpenOptions::default(),
cx,
)
})?
.await?;
} else if matches!(KEY_VALUE_STORE.read_kvp(FIRST_OPEN), Ok(None)) {
cx.update(|cx| show_welcome_view(app_state, cx))?.await?;
} else {
@ -688,6 +690,42 @@ async fn restore_or_create_workspace(
Ok(())
}
pub(crate) async fn restorable_workspace_locations(
cx: &mut AsyncAppContext,
app_state: &Arc<AppState>,
) -> Option<Vec<workspace::LocalPaths>> {
let mut restore_behaviour = cx
.update(|cx| WorkspaceSettings::get(None, cx).restore_on_startup)
.ok()?;
let last_session_id = app_state.session.last_session_id();
if last_session_id.is_none()
&& matches!(
restore_behaviour,
workspace::RestoreOnStartupBehaviour::LastSession
)
{
restore_behaviour = workspace::RestoreOnStartupBehaviour::LastWorkspace;
}
match restore_behaviour {
workspace::RestoreOnStartupBehaviour::LastWorkspace => {
workspace::last_opened_workspace_paths()
.await
.map(|location| vec![location])
}
workspace::RestoreOnStartupBehaviour::LastSession => {
if let Some(last_session_id) = last_session_id {
workspace::last_session_workspace_locations(last_session_id)
.filter(|locations| !locations.is_empty())
} else {
None
}
}
_ => None,
}
}
fn init_paths() -> anyhow::Result<()> {
for path in [
paths::config_dir(),