From f36a35ecc9e632e30a5f08e9c8dfd58ef2e3d88c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 27 Mar 2023 10:10:06 -0700 Subject: [PATCH] core: quit flag is reset after tick --- include/ghostty.h | 2 +- macos/Sources/Ghostty/AppState.swift | 8 +++++++- src/App.zig | 13 ++++++------- src/apprt/embedded.zig | 5 +++-- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/include/ghostty.h b/include/ghostty.h index 6de3ae0a4..77259202a 100644 --- a/include/ghostty.h +++ b/include/ghostty.h @@ -251,7 +251,7 @@ void ghostty_config_finalize(ghostty_config_t); ghostty_app_t ghostty_app_new(const ghostty_runtime_config_s *, ghostty_config_t); void ghostty_app_free(ghostty_app_t); -int ghostty_app_tick(ghostty_app_t); +bool ghostty_app_tick(ghostty_app_t); void *ghostty_app_userdata(ghostty_app_t); ghostty_surface_t ghostty_surface_new(ghostty_app_t, ghostty_surface_config_s*); diff --git a/macos/Sources/Ghostty/AppState.swift b/macos/Sources/Ghostty/AppState.swift index 4463cce5c..4a76e9a9d 100644 --- a/macos/Sources/Ghostty/AppState.swift +++ b/macos/Sources/Ghostty/AppState.swift @@ -106,7 +106,13 @@ extension Ghostty { func appTick() { guard let app = self.app else { return } - ghostty_app_tick(app) + + // Tick our app, which lets us know if we want to quit + let exit = ghostty_app_tick(app) + if (!exit) { return } + + // We want to quit, start that process + NSApplication.shared.terminate(nil) } /// Request that the given surface is closed. This will trigger the full normal surface close event diff --git a/src/App.zig b/src/App.zig index b5635c0da..b39938392 100644 --- a/src/App.zig +++ b/src/App.zig @@ -81,8 +81,12 @@ pub fn tick(self: *App, rt_app: *apprt.App) !bool { i += 1; } - // Drain our mailbox only if we're not quitting. - if (!self.quit) try self.drainMailbox(rt_app); + // Drain our mailbox + try self.drainMailbox(rt_app); + + // No matter what, we reset the quit flag after a tick. If the apprt + // doesn't want to quit, then we can't force it to. + defer self.quit = false; // We quit if our quit flag is on or if we have closed all surfaces. return self.quit or self.surfaces.items.len == 0; @@ -175,11 +179,6 @@ fn newWindow(self: *App, rt_app: *apprt.App, msg: Message.NewWindow) !void { fn setQuit(self: *App) !void { if (self.quit) return; self.quit = true; - - // Mark that all our surfaces should close - for (self.surfaces.items) |surface| { - surface.setShouldClose(); - } } /// Handle a window message diff --git a/src/apprt/embedded.zig b/src/apprt/embedded.zig index 8fa2d0354..c492f0037 100644 --- a/src/apprt/embedded.zig +++ b/src/apprt/embedded.zig @@ -390,9 +390,10 @@ pub const CAPI = struct { /// Tick the event loop. This should be called whenever the "wakeup" /// callback is invoked for the runtime. - export fn ghostty_app_tick(v: *App) void { - _ = v.core_app.tick(v) catch |err| { + export fn ghostty_app_tick(v: *App) bool { + return v.core_app.tick(v) catch |err| err: { log.err("error app tick err={}", .{err}); + break :err false; }; }