Fix several shutdown related bugs

This commit is contained in:
Mikayla 2023-11-14 11:42:58 -08:00
parent 5c8db996ee
commit 3419aaf17e
No known key found for this signature in database
5 changed files with 19 additions and 9 deletions

View file

@ -234,10 +234,10 @@ impl AppContext {
app_version: platform.app_version().ok(), app_version: platform.app_version().ok(),
}; };
Rc::new_cyclic(|this| AppCell { let app = Rc::new_cyclic(|this| AppCell {
app: RefCell::new(AppContext { app: RefCell::new(AppContext {
this: this.clone(), this: this.clone(),
platform, platform: platform.clone(),
app_metadata, app_metadata,
text_system, text_system,
flushing_effects: false, flushing_effects: false,
@ -269,12 +269,21 @@ impl AppContext {
layout_id_buffer: Default::default(), layout_id_buffer: Default::default(),
propagate_event: true, propagate_event: true,
}), }),
}) });
platform.on_quit(Box::new({
let cx = app.clone();
move || {
cx.borrow_mut().shutdown();
}
}));
app
} }
/// Quit the application gracefully. Handlers registered with `ModelContext::on_app_quit` /// Quit the application gracefully. Handlers registered with `ModelContext::on_app_quit`
/// will be given 100ms to complete before exiting. /// will be given 100ms to complete before exiting.
pub fn quit(&mut self) { pub fn shutdown(&mut self) {
let mut futures = Vec::new(); let mut futures = Vec::new();
for observer in self.quit_observers.remove(&()) { for observer in self.quit_observers.remove(&()) {
@ -292,8 +301,10 @@ impl AppContext {
{ {
log::error!("timed out waiting on app_will_quit"); log::error!("timed out waiting on app_will_quit");
} }
}
self.globals_by_type.clear(); pub fn quit(&mut self) {
self.platform.quit();
} }
pub fn app_metadata(&self) -> AppMetadata { pub fn app_metadata(&self) -> AppMetadata {

View file

@ -26,7 +26,7 @@ impl EntityId {
impl Display for EntityId { impl Display for EntityId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self) write!(f, "{}", self.as_u64())
} }
} }

View file

@ -96,7 +96,7 @@ impl TestAppContext {
} }
pub fn quit(&self) { pub fn quit(&self) {
self.app.borrow_mut().quit(); self.app.borrow_mut().shutdown();
} }
pub fn refresh(&mut self) -> Result<()> { pub fn refresh(&mut self) -> Result<()> {

View file

@ -167,7 +167,7 @@ fn main() {
panic!("unexpected message"); panic!("unexpected message");
} }
cx.update(|cx| cx.quit()).ok(); cx.update(|cx| cx.shutdown()).ok();
}) })
.detach(); .detach();
}); });

View file

@ -502,7 +502,6 @@ 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);