"goto_tab" key binding to jump to a specific tab, defaults to Super+N

The apprt surface must implement `gotoTab` to make this work. This is
only implemented in GTK for now.
This commit is contained in:
Mitchell Hashimoto
2023-02-25 10:48:38 -08:00
parent 7a0411d65a
commit 6c6a3d6a5d
4 changed files with 37 additions and 0 deletions

View File

@ -927,6 +927,12 @@ pub fn keyCallback(
} else log.warn("runtime doesn't implement gotoNextTab", .{});
},
.goto_tab => |n| {
if (@hasDecl(apprt.Surface, "gotoTab")) {
self.rt_surface.gotoTab(n);
} else log.warn("runtime doesn't implement gotoTab", .{});
},
.close_window => {
_ = self.app_mailbox.push(.{ .close = self }, .{ .instant = {} });
},

View File

@ -329,6 +329,17 @@ const Window = struct {
}
}
/// Go to the specific tab index.
fn gotoTab(self: *Window, n: usize) void {
if (n == 0) return;
const max = c.gtk_notebook_get_n_pages(self.notebook);
const page_idx = std.math.cast(c_int, n - 1) orelse return;
if (page_idx < max) {
c.gtk_notebook_set_current_page(self.notebook, page_idx);
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);
@ -591,6 +602,10 @@ pub const Surface = struct {
self.window.gotoNextTab(self);
}
pub fn gotoTab(self: *Surface, n: usize) void {
self.window.gotoTab(n);
}
pub fn setShouldClose(self: *Surface) void {
_ = self;
}

View File

@ -288,6 +288,19 @@ pub const Config = struct {
.{ .key = .right_bracket, .mods = .{ .super = true, .shift = true } },
.{ .next_tab = {} },
);
{
// Cmd+N for goto tab N
const start = @enumToInt(inputpkg.Key.one);
const end = @enumToInt(inputpkg.Key.nine);
var i: usize = start;
while (i <= end) : (i += 1) {
try result.keybind.set.put(
alloc,
.{ .key = @intToEnum(inputpkg.Key, i), .mods = .{ .super = true } },
.{ .goto_tab = (i - start) + 1 },
);
}
}
if (comptime builtin.target.isDarwin()) {
try result.keybind.set.put(
alloc,

View File

@ -161,6 +161,9 @@ pub const Action = union(enum) {
/// Go to the next tab
next_tab: void,
/// Go to the tab with the specific number, 1-indexed.
goto_tab: usize,
/// Close the current window or tab
close_window: void,