Fix quitting Zed when project was unshared

This commit is contained in:
Antonio Scandurra 2024-01-04 15:00:36 +01:00
parent f0afa3f9e3
commit f6af7ab27c
3 changed files with 31 additions and 35 deletions

View file

@ -1095,19 +1095,21 @@ impl Workspace {
} }
pub fn close_global(_: &CloseWindow, cx: &mut AppContext) { pub fn close_global(_: &CloseWindow, cx: &mut AppContext) {
cx.windows().iter().find(|window| { cx.defer(|cx| {
window cx.windows().iter().find(|window| {
.update(cx, |_, window| { window
if window.is_window_active() { .update(cx, |_, window| {
//This can only get called when the window's project connection has been lost if window.is_window_active() {
//so we don't need to prompt the user for anything and instead just close the window //This can only get called when the window's project connection has been lost
window.remove_window(); //so we don't need to prompt the user for anything and instead just close the window
true window.remove_window();
} else { true
false } else {
} false
}) }
.unwrap_or(false) })
.unwrap_or(false)
});
}); });
} }

View file

@ -144,6 +144,7 @@ fn main() {
cx.set_global(client.clone()); cx.set_global(client.clone());
zed::init(cx);
theme::init(theme::LoadThemes::All, cx); theme::init(theme::LoadThemes::All, cx);
project::Project::init(&client, cx); project::Project::init(&client, cx);
client::init(&client, cx); client::init(&client, cx);
@ -158,7 +159,6 @@ fn main() {
cx, cx,
); );
assistant::init(cx); assistant::init(cx);
// component_test::init(cx);
cx.spawn(|_| watch_languages(fs.clone(), languages.clone())) cx.spawn(|_| watch_languages(fs.clone(), languages.clone()))
.detach(); .detach();

View file

@ -64,6 +64,13 @@ actions!(
] ]
); );
pub fn init(cx: &mut AppContext) {
cx.on_action(|_: &Hide, cx| cx.hide());
cx.on_action(|_: &HideOthers, cx| cx.hide_other_apps());
cx.on_action(|_: &ShowAll, cx| cx.unhide_other_apps());
cx.on_action(quit);
}
pub fn build_window_options( pub fn build_window_options(
bounds: Option<WindowBounds>, bounds: Option<WindowBounds>,
display_uuid: Option<Uuid>, display_uuid: Option<Uuid>,
@ -130,7 +137,6 @@ pub fn initialize_workspace(app_state: Arc<AppState>, cx: &mut AppContext) {
status_bar.add_left_item(diagnostic_summary, cx); status_bar.add_left_item(diagnostic_summary, cx);
status_bar.add_left_item(activity_indicator, cx); status_bar.add_left_item(activity_indicator, cx);
status_bar.add_right_item(feedback_button, cx); status_bar.add_right_item(feedback_button, cx);
// status_bar.add_right_item(copilot, cx);
status_bar.add_right_item(copilot, cx); status_bar.add_right_item(copilot, cx);
status_bar.add_right_item(active_buffer_language, cx); status_bar.add_right_item(active_buffer_language, cx);
status_bar.add_right_item(vim_mode_indicator, cx); status_bar.add_right_item(vim_mode_indicator, cx);
@ -207,15 +213,6 @@ pub fn initialize_workspace(app_state: Arc<AppState>, cx: &mut AppContext) {
workspace workspace
.register_action(about) .register_action(about)
.register_action(|_, _: &Hide, cx| {
cx.hide();
})
.register_action(|_, _: &HideOthers, cx| {
cx.hide_other_apps();
})
.register_action(|_, _: &ShowAll, cx| {
cx.unhide_other_apps();
})
.register_action(|_, _: &Minimize, cx| { .register_action(|_, _: &Minimize, cx| {
cx.minimize_window(); cx.minimize_window();
}) })
@ -225,7 +222,6 @@ pub fn initialize_workspace(app_state: Arc<AppState>, cx: &mut AppContext) {
.register_action(|_, _: &ToggleFullScreen, cx| { .register_action(|_, _: &ToggleFullScreen, cx| {
cx.toggle_full_screen(); cx.toggle_full_screen();
}) })
.register_action(quit)
.register_action(|_, action: &OpenZedURL, cx| { .register_action(|_, action: &OpenZedURL, cx| {
cx.global::<Arc<OpenListener>>() cx.global::<Arc<OpenListener>>()
.open_urls(&[action.url.clone()]) .open_urls(&[action.url.clone()])
@ -451,10 +447,10 @@ fn about(_: &mut Workspace, _: &About, cx: &mut gpui::ViewContext<Workspace>) {
.detach(); .detach();
} }
fn quit(_: &mut Workspace, _: &Quit, cx: &mut gpui::ViewContext<Workspace>) { fn quit(_: &Quit, cx: &mut AppContext) {
let should_confirm = WorkspaceSettings::get_global(cx).confirm_quit; let should_confirm = WorkspaceSettings::get_global(cx).confirm_quit;
cx.spawn(|_, mut cx| async move { cx.spawn(|mut cx| async move {
let mut workspace_windows = cx.update(|_, cx| { let mut workspace_windows = cx.update(|cx| {
cx.windows() cx.windows()
.into_iter() .into_iter()
.filter_map(|window| window.downcast::<Workspace>()) .filter_map(|window| window.downcast::<Workspace>())
@ -463,14 +459,14 @@ fn quit(_: &mut Workspace, _: &Quit, cx: &mut gpui::ViewContext<Workspace>) {
// If multiple windows have unsaved changes, and need a save prompt, // If multiple windows have unsaved changes, and need a save prompt,
// prompt in the active window before switching to a different window. // prompt in the active window before switching to a different window.
cx.update(|_, cx| { cx.update(|cx| {
workspace_windows.sort_by_key(|window| window.is_active(&cx) == Some(false)); workspace_windows.sort_by_key(|window| window.is_active(&cx) == Some(false));
}) })
.log_err(); .log_err();
if let (true, Some(_)) = (should_confirm, workspace_windows.first().copied()) { if let (true, Some(workspace)) = (should_confirm, workspace_windows.first().copied()) {
let answer = cx let answer = workspace
.update(|_, cx| { .update(&mut cx, |_, cx| {
cx.prompt( cx.prompt(
PromptLevel::Info, PromptLevel::Info,
"Are you sure you want to quit?", "Are you sure you want to quit?",
@ -500,9 +496,7 @@ fn quit(_: &mut Workspace, _: &Quit, cx: &mut gpui::ViewContext<Workspace>) {
} }
} }
} }
cx.update(|_, cx| { cx.update(|cx| cx.quit())?;
cx.quit();
})?;
anyhow::Ok(()) anyhow::Ok(())
}) })
.detach_and_log_err(cx); .detach_and_log_err(cx);