support app quitting to close all windows

This commit is contained in:
Mitchell Hashimoto
2022-11-06 14:10:28 -08:00
parent ac6f960b92
commit c9b01fdc6c
4 changed files with 27 additions and 4 deletions

View File

@ -74,7 +74,8 @@ pub fn wakeup(self: App) void {
/// Run the main event loop for the application. This blocks until the /// Run the main event loop for the application. This blocks until the
/// application quits or every window is closed. /// application quits or every window is closed.
pub fn run(self: *App) !void { 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. // Block for any glfw events.
try glfw.waitEvents(); try glfw.waitEvents();
@ -100,12 +101,12 @@ pub fn run(self: *App) !void {
} }
// Drain our mailbox // Drain our mailbox
try self.drainMailbox(); try self.drainMailbox(&quit);
} }
} }
/// Drain the mailbox. /// Drain the mailbox.
fn drainMailbox(self: *App) !void { fn drainMailbox(self: *App, quit: *bool) !void {
var drain = self.mailbox.drain(); var drain = self.mailbox.drain();
defer drain.deinit(); defer drain.deinit();
@ -113,6 +114,7 @@ fn drainMailbox(self: *App) !void {
log.debug("mailbox message={}", .{message}); log.debug("mailbox message={}", .{message});
switch (message) { switch (message) {
.new_window => try self.newWindow(), .new_window => try self.newWindow(),
.quit => quit.* = true,
} }
} }
} }
@ -134,4 +136,7 @@ fn newWindow(self: *App) !void {
pub const Message = union(enum) { pub const Message = union(enum) {
/// Create a new terminal window. /// Create a new terminal window.
new_window: void, new_window: void,
/// Quit
quit: void,
}; };

View File

@ -342,7 +342,7 @@ pub fn create(alloc: Allocator, app: *App, config: *const Config) !*Window {
.grid_size = grid_size, .grid_size = grid_size,
.config = config, .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(); errdefer if (DevMode.enabled) self.imgui_ctx.destroy();
@ -766,6 +766,13 @@ fn keyCallback(
}, },
.close_window => win.window.setShouldClose(true), .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 // Bindings always result in us ignoring the char if printable

View File

@ -168,6 +168,14 @@ pub const Config = struct {
.{ .close_window = {} }, .{ .close_window = {} },
); );
if (builtin.os.tag == .macos) {
try result.keybind.set.put(
alloc,
.{ .key = .q, .mods = .{ .super = true } },
.{ .close_window = {} },
);
}
return result; return result;
} }

View File

@ -139,6 +139,9 @@ pub const Action = union(enum) {
/// Close the current window /// Close the current window
close_window: void, close_window: void,
/// Quit ghostty
quit: void,
}; };
/// Trigger is the associated key state that can trigger an action. /// Trigger is the associated key state that can trigger an action.