From 6c6a3d6a5d360427a8c837f0392f1b552a688faf Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 25 Feb 2023 10:48:38 -0800 Subject: [PATCH] "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. --- src/Surface.zig | 6 ++++++ src/apprt/gtk.zig | 15 +++++++++++++++ src/config.zig | 13 +++++++++++++ src/input/Binding.zig | 3 +++ 4 files changed, 37 insertions(+) diff --git a/src/Surface.zig b/src/Surface.zig index 39094766f..19a4c36f6 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -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 = {} }); }, diff --git a/src/apprt/gtk.zig b/src/apprt/gtk.zig index 51dfb010d..9864764d5 100644 --- a/src/apprt/gtk.zig +++ b/src/apprt/gtk.zig @@ -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; } diff --git a/src/config.zig b/src/config.zig index 280a71d70..324bc713c 100644 --- a/src/config.zig +++ b/src/config.zig @@ -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, diff --git a/src/input/Binding.zig b/src/input/Binding.zig index ba2c8a098..394e62c8c 100644 --- a/src/input/Binding.zig +++ b/src/input/Binding.zig @@ -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,