From c9b01fdc6c0a3daf5db6eb44434ba4756446928b Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 6 Nov 2022 14:10:28 -0800 Subject: [PATCH] support app quitting to close all windows --- src/App.zig | 11 ++++++++--- src/Window.zig | 9 ++++++++- src/config.zig | 8 ++++++++ src/input/Binding.zig | 3 +++ 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/App.zig b/src/App.zig index 744615f6c..00187bdcd 100644 --- a/src/App.zig +++ b/src/App.zig @@ -74,7 +74,8 @@ pub fn wakeup(self: App) void { /// Run the main event loop for the application. This blocks until the /// application quits or every window is closed. pub fn run(self: *App) !void { - while (self.windows.items.len > 0) { + var quit: bool = false; + while (!quit and self.windows.items.len > 0) { // Block for any glfw events. try glfw.waitEvents(); @@ -100,12 +101,12 @@ pub fn run(self: *App) !void { } // Drain our mailbox - try self.drainMailbox(); + try self.drainMailbox(&quit); } } /// Drain the mailbox. -fn drainMailbox(self: *App) !void { +fn drainMailbox(self: *App, quit: *bool) !void { var drain = self.mailbox.drain(); defer drain.deinit(); @@ -113,6 +114,7 @@ fn drainMailbox(self: *App) !void { log.debug("mailbox message={}", .{message}); switch (message) { .new_window => try self.newWindow(), + .quit => quit.* = true, } } } @@ -134,4 +136,7 @@ fn newWindow(self: *App) !void { pub const Message = union(enum) { /// Create a new terminal window. new_window: void, + + /// Quit + quit: void, }; diff --git a/src/Window.zig b/src/Window.zig index 1f5e48c27..2dad88b67 100644 --- a/src/Window.zig +++ b/src/Window.zig @@ -342,7 +342,7 @@ pub fn create(alloc: Allocator, app: *App, config: *const Config) !*Window { .grid_size = grid_size, .config = config, - .imgui_ctx = if (!DevMode.enabled) void else try imgui.Context.create(), + .imgui_ctx = if (!DevMode.enabled) {} else try imgui.Context.create(), }; errdefer if (DevMode.enabled) self.imgui_ctx.destroy(); @@ -766,6 +766,13 @@ fn keyCallback( }, .close_window => win.window.setShouldClose(true), + + .quit => { + _ = win.app.mailbox.push(.{ + .quit = {}, + }, .{ .instant = {} }); + win.app.wakeup(); + }, } // Bindings always result in us ignoring the char if printable diff --git a/src/config.zig b/src/config.zig index 179a5b95d..9249254df 100644 --- a/src/config.zig +++ b/src/config.zig @@ -168,6 +168,14 @@ pub const Config = struct { .{ .close_window = {} }, ); + if (builtin.os.tag == .macos) { + try result.keybind.set.put( + alloc, + .{ .key = .q, .mods = .{ .super = true } }, + .{ .close_window = {} }, + ); + } + return result; } diff --git a/src/input/Binding.zig b/src/input/Binding.zig index 63a32678e..81cc18fdc 100644 --- a/src/input/Binding.zig +++ b/src/input/Binding.zig @@ -139,6 +139,9 @@ pub const Action = union(enum) { /// Close the current window close_window: void, + + /// Quit ghostty + quit: void, }; /// Trigger is the associated key state that can trigger an action.