From 47b4d9942fb4351413a0f869938e96d9dd40cdae Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Mon, 27 Nov 2023 15:32:26 -0700 Subject: [PATCH] Fix panic on quit --- crates/gpui2/src/platform/mac/window.rs | 3 +++ crates/gpui2/src/window.rs | 7 +++++++ crates/zed2/src/zed2.rs | 17 +++++++++++------ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/crates/gpui2/src/platform/mac/window.rs b/crates/gpui2/src/platform/mac/window.rs index bb3a659a62..5b72c10851 100644 --- a/crates/gpui2/src/platform/mac/window.rs +++ b/crates/gpui2/src/platform/mac/window.rs @@ -683,6 +683,9 @@ impl Drop for MacWindow { this.executor .spawn(async move { unsafe { + // todo!() this panic()s when you click the red close button + // unless should_close returns false. + // (luckliy in zed it always returns false) window.close(); } }) diff --git a/crates/gpui2/src/window.rs b/crates/gpui2/src/window.rs index 5d33f0161c..20561c5443 100644 --- a/crates/gpui2/src/window.rs +++ b/crates/gpui2/src/window.rs @@ -1517,6 +1517,13 @@ impl<'a> WindowContext<'a> { .set_input_handler(Box::new(input_handler)); } } + + pub fn on_window_should_close(&mut self, f: impl Fn(&mut WindowContext) -> bool + 'static) { + let mut this = self.to_async(); + self.window + .platform_window + .on_should_close(Box::new(move || this.update(|_, cx| f(cx)).unwrap_or(true))) + } } impl Context for WindowContext<'_> { diff --git a/crates/zed2/src/zed2.rs b/crates/zed2/src/zed2.rs index 1286594138..50998a7fb8 100644 --- a/crates/zed2/src/zed2.rs +++ b/crates/zed2/src/zed2.rs @@ -166,12 +166,17 @@ pub fn initialize_workspace(app_state: Arc, cx: &mut AppContext) { // vim::observe_keystrokes(cx); - // cx.on_window_should_close(|workspace, cx| { - // if let Some(task) = workspace.close(&Default::default(), cx) { - // task.detach_and_log_err(cx); - // } - // false - // }); + let handle = cx.view().downgrade(); + cx.on_window_should_close(move |cx| { + handle + .update(cx, |workspace, cx| { + if let Some(task) = workspace.close(&Default::default(), cx) { + task.detach_and_log_err(cx); + } + false + }) + .unwrap_or(true) + }); cx.spawn(|workspace_handle, mut cx| async move { let project_panel = ProjectPanel::load(workspace_handle.clone(), cx.clone());