Fix seg fault when using the WindowContext::on_window_should_close() API

This commit is contained in:
Mikayla 2024-01-10 11:23:18 -08:00
parent 2a09c6aad5
commit 7ef88397c9
No known key found for this signature in database
5 changed files with 13 additions and 6 deletions

View File

@ -111,7 +111,6 @@ fn notification_window_options(
let screen_bounds = screen.bounds();
let size: Size<GlobalPixels> = window_size.into();
// todo!() use content bounds instead of screen.bounds and get rid of magics in point's 2nd argument.
let bounds = gpui::Bounds::<GlobalPixels> {
origin: screen_bounds.upper_right()
- point(

View File

@ -1043,7 +1043,7 @@ mod tests {
#[gpui::test(iterations = 100)]
async fn test_random_wraps(cx: &mut gpui::TestAppContext, mut rng: StdRng) {
// todo!() this test is flaky
// todo this test is flaky
init_test(cx);
cx.background_executor.set_block_on_ticks(0..=50);

View File

@ -269,6 +269,7 @@ unsafe fn build_window_class(name: &'static str, superclass: &Class) -> *const C
sel!(windowShouldClose:),
window_should_close as extern "C" fn(&Object, Sel, id) -> BOOL,
);
decl.add_method(sel!(close), close_window as extern "C" fn(&Object, Sel));
decl.add_method(
@ -685,9 +686,6 @@ 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();
}
})

View File

@ -1904,7 +1904,16 @@ impl<'a> WindowContext<'a> {
let mut this = self.to_async();
self.window
.platform_window
.on_should_close(Box::new(move || this.update(|_, cx| f(cx)).unwrap_or(true)))
.on_should_close(Box::new(move || {
this.update(|_, cx| {
// Ensure that the window is removed from the app if it's been closed.
if f(cx) {
cx.remove_window();
}
false
})
.unwrap_or(true)
}))
}
}

View File

@ -148,6 +148,7 @@ pub fn initialize_workspace(app_state: Arc<AppState>, cx: &mut AppContext) {
cx.on_window_should_close(move |cx| {
handle
.update(cx, |workspace, cx| {
// We'll handle closing asynchoronously
workspace.close_window(&Default::default(), cx);
false
})