apprt/gtk: new window, tab

This commit is contained in:
Mitchell Hashimoto
2023-09-18 14:51:19 -07:00
parent 71ca254be8
commit 7996be25a8
2 changed files with 60 additions and 6 deletions

View File

@ -398,8 +398,22 @@ fn initActions(self: *App) void {
fn initMenu(self: *App) void { fn initMenu(self: *App) void {
const menu = c.g_menu_new(); const menu = c.g_menu_new();
errdefer c.g_object_unref(menu); errdefer c.g_object_unref(menu);
c.g_menu_append(menu, "Close", "win.close");
c.g_menu_append(menu, "Quit", "app.quit"); {
const section = c.g_menu_new();
defer c.g_object_unref(section);
c.g_menu_append_section(menu, null, @ptrCast(@alignCast(section)));
c.g_menu_append(section, "New Window", "win.new_window");
c.g_menu_append(section, "New Tab", "win.new_tab");
}
{
const section = c.g_menu_new();
defer c.g_object_unref(section);
c.g_menu_append_section(menu, null, @ptrCast(@alignCast(section)));
c.g_menu_append(section, "Close Window", "win.close");
c.g_menu_append(section, "Quit Ghostty", "app.quit");
}
// { // {
// const section = c.g_menu_new(); // const section = c.g_menu_new();

View File

@ -159,11 +159,25 @@ pub fn init(self: *Window, app: *App) !void {
/// menus and such. The menu is defined in App.zig but the action is defined /// menus and such. The menu is defined in App.zig but the action is defined
/// here. The string name binds them. /// here. The string name binds them.
fn initActions(self: *Window) void { fn initActions(self: *Window) void {
const action_close = c.g_simple_action_new("close", null); const actions = .{
defer c.g_object_unref(action_close); .{ "close", &gtkActionClose },
_ = c.g_signal_connect_data(action_close, "activate", c.G_CALLBACK(&gtkActionClose), self, null, c.G_CONNECT_DEFAULT); .{ "new_window", &gtkActionNewWindow },
.{ "new_tab", &gtkActionNewTab },
};
c.g_action_map_add_action(@ptrCast(self.window), @ptrCast(action_close)); inline for (actions) |entry| {
const action = c.g_simple_action_new(entry[0], null);
defer c.g_object_unref(action);
_ = c.g_signal_connect_data(
action,
"activate",
c.G_CALLBACK(entry[1]),
self,
null,
c.G_CONNECT_DEFAULT,
);
c.g_action_map_add_action(@ptrCast(self.window), @ptrCast(action));
}
} }
pub fn deinit(self: *Window) void { pub fn deinit(self: *Window) void {
@ -492,6 +506,32 @@ fn gtkActionClose(
}; };
} }
fn gtkActionNewWindow(
_: *c.GSimpleAction,
_: *c.GVariant,
ud: ?*anyopaque,
) callconv(.C) void {
const self: *Window = @ptrCast(@alignCast(ud orelse return));
const surface = self.app.core_app.focusedSurface() orelse return;
surface.performBindingAction(.{ .new_window = {} }) catch |err| {
log.warn("error performing binding action error={}", .{err});
return;
};
}
fn gtkActionNewTab(
_: *c.GSimpleAction,
_: *c.GVariant,
ud: ?*anyopaque,
) callconv(.C) void {
const self: *Window = @ptrCast(@alignCast(ud orelse return));
const surface = self.app.core_app.focusedSurface() orelse return;
surface.performBindingAction(.{ .new_tab = {} }) catch |err| {
log.warn("error performing binding action error={}", .{err});
return;
};
}
fn userdataSelf(ud: *anyopaque) *Window { fn userdataSelf(ud: *anyopaque) *Window {
return @ptrCast(@alignCast(ud)); return @ptrCast(@alignCast(ud));
} }