mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-16 08:46:08 +03:00
gtk: previous/next tab bindings
This commit is contained in:
@ -917,6 +917,9 @@ pub fn keyCallback(
|
|||||||
}, .{ .instant = {} });
|
}, .{ .instant = {} });
|
||||||
},
|
},
|
||||||
|
|
||||||
|
.previous_tab => self.rt_surface.gotoPreviousTab(),
|
||||||
|
.next_tab => self.rt_surface.gotoNextTab(),
|
||||||
|
|
||||||
.close_window => {
|
.close_window => {
|
||||||
_ = self.app_mailbox.push(.{ .close = self }, .{ .instant = {} });
|
_ = self.app_mailbox.push(.{ .close = self }, .{ .instant = {} });
|
||||||
},
|
},
|
||||||
|
@ -291,18 +291,8 @@ const Window = struct {
|
|||||||
/// Close the tab for the given notebook page. This will automatically
|
/// Close the tab for the given notebook page. This will automatically
|
||||||
/// handle closing the window if there are no more tabs.
|
/// handle closing the window if there are no more tabs.
|
||||||
fn closeTab(self: *Window, page: *c.GtkNotebookPage) void {
|
fn closeTab(self: *Window, page: *c.GtkNotebookPage) void {
|
||||||
// Get the page index from the page
|
|
||||||
var value: c.GValue = std.mem.zeroes(c.GValue);
|
|
||||||
defer c.g_value_unset(&value);
|
|
||||||
_ = c.g_value_init(&value, c.G_TYPE_INT);
|
|
||||||
c.g_object_get_property(
|
|
||||||
@ptrCast(*c.GObject, @alignCast(@alignOf(c.GObject), page)),
|
|
||||||
"position",
|
|
||||||
&value,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Remove the page
|
// Remove the page
|
||||||
const page_idx = c.g_value_get_int(&value);
|
const page_idx = getNotebookPageIndex(page);
|
||||||
c.gtk_notebook_remove_page(self.notebook, page_idx);
|
c.gtk_notebook_remove_page(self.notebook, page_idx);
|
||||||
|
|
||||||
const remaining = c.gtk_notebook_get_n_pages(self.notebook);
|
const remaining = c.gtk_notebook_get_n_pages(self.notebook);
|
||||||
@ -323,6 +313,34 @@ const Window = struct {
|
|||||||
self.closeTab(getNotebookPage(@ptrCast(*c.GObject, surface.gl_area)) orelse return);
|
self.closeTab(getNotebookPage(@ptrCast(*c.GObject, surface.gl_area)) orelse return);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Go to the previous tab for a surface.
|
||||||
|
fn gotoPreviousTab(self: *Window, surface: *Surface) void {
|
||||||
|
const page = getNotebookPage(@ptrCast(*c.GObject, surface.gl_area)) orelse return;
|
||||||
|
const page_idx = getNotebookPageIndex(page);
|
||||||
|
if (page_idx > 0) {
|
||||||
|
c.gtk_notebook_set_current_page(self.notebook, page_idx - 1);
|
||||||
|
self.focusCurrentTab();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Go to the next tab for a surface.
|
||||||
|
fn gotoNextTab(self: *Window, surface: *Surface) void {
|
||||||
|
const page = getNotebookPage(@ptrCast(*c.GObject, surface.gl_area)) orelse return;
|
||||||
|
const page_idx = getNotebookPageIndex(page);
|
||||||
|
const max = c.gtk_notebook_get_n_pages(self.notebook) -| 1;
|
||||||
|
if (page_idx < max) {
|
||||||
|
c.gtk_notebook_set_current_page(self.notebook, page_idx + 1);
|
||||||
|
self.focusCurrentTab();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Grabs focus on the currently selected tab.
|
||||||
|
fn focusCurrentTab(self: *Window) void {
|
||||||
|
const page_idx = c.gtk_notebook_get_current_page(self.notebook);
|
||||||
|
const widget = c.gtk_notebook_get_nth_page(self.notebook, page_idx);
|
||||||
|
_ = c.gtk_widget_grab_focus(widget);
|
||||||
|
}
|
||||||
|
|
||||||
fn gtkTabAddClick(_: *c.GtkButton, ud: ?*anyopaque) callconv(.C) void {
|
fn gtkTabAddClick(_: *c.GtkButton, ud: ?*anyopaque) callconv(.C) void {
|
||||||
const self = userdataSelf(ud.?);
|
const self = userdataSelf(ud.?);
|
||||||
self.newTab() catch |err| {
|
self.newTab() catch |err| {
|
||||||
@ -356,6 +374,19 @@ const Window = struct {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn getNotebookPageIndex(page: *c.GtkNotebookPage) c_int {
|
||||||
|
var value: c.GValue = std.mem.zeroes(c.GValue);
|
||||||
|
defer c.g_value_unset(&value);
|
||||||
|
_ = c.g_value_init(&value, c.G_TYPE_INT);
|
||||||
|
c.g_object_get_property(
|
||||||
|
@ptrCast(*c.GObject, @alignCast(@alignOf(c.GObject), page)),
|
||||||
|
"position",
|
||||||
|
&value,
|
||||||
|
);
|
||||||
|
|
||||||
|
return c.g_value_get_int(&value);
|
||||||
|
}
|
||||||
|
|
||||||
fn userdataSelf(ud: *anyopaque) *Window {
|
fn userdataSelf(ud: *anyopaque) *Window {
|
||||||
return @ptrCast(*Window, @alignCast(@alignOf(Window), ud));
|
return @ptrCast(*Window, @alignCast(@alignOf(Window), ud));
|
||||||
}
|
}
|
||||||
@ -553,6 +584,14 @@ pub const Surface = struct {
|
|||||||
self.window.closeSurface(self);
|
self.window.closeSurface(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn gotoPreviousTab(self: *Surface) void {
|
||||||
|
self.window.gotoPreviousTab(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn gotoNextTab(self: *Surface) void {
|
||||||
|
self.window.gotoNextTab(self);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn setShouldClose(self: *Surface) void {
|
pub fn setShouldClose(self: *Surface) void {
|
||||||
_ = self;
|
_ = self;
|
||||||
}
|
}
|
||||||
|
@ -278,6 +278,16 @@ pub const Config = struct {
|
|||||||
.{ .key = .t, .mods = .{ .super = true } },
|
.{ .key = .t, .mods = .{ .super = true } },
|
||||||
.{ .new_tab = {} },
|
.{ .new_tab = {} },
|
||||||
);
|
);
|
||||||
|
try result.keybind.set.put(
|
||||||
|
alloc,
|
||||||
|
.{ .key = .left_bracket, .mods = .{ .super = true, .shift = true } },
|
||||||
|
.{ .previous_tab = {} },
|
||||||
|
);
|
||||||
|
try result.keybind.set.put(
|
||||||
|
alloc,
|
||||||
|
.{ .key = .right_bracket, .mods = .{ .super = true, .shift = true } },
|
||||||
|
.{ .next_tab = {} },
|
||||||
|
);
|
||||||
if (comptime builtin.target.isDarwin()) {
|
if (comptime builtin.target.isDarwin()) {
|
||||||
try result.keybind.set.put(
|
try result.keybind.set.put(
|
||||||
alloc,
|
alloc,
|
||||||
|
@ -155,6 +155,12 @@ pub const Action = union(enum) {
|
|||||||
/// Open a new tab
|
/// Open a new tab
|
||||||
new_tab: void,
|
new_tab: void,
|
||||||
|
|
||||||
|
/// Go to the previous tab
|
||||||
|
previous_tab: void,
|
||||||
|
|
||||||
|
/// Go to the next tab
|
||||||
|
next_tab: void,
|
||||||
|
|
||||||
/// Close the current window or tab
|
/// Close the current window or tab
|
||||||
close_window: void,
|
close_window: void,
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user