From 3d1ee3daa8dea8f5f332d9db52b7cc72171cc9cf Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 26 Aug 2024 20:09:45 -0700 Subject: [PATCH] apprt: make gotoTab handle all tab movements --- src/Surface.zig | 20 +++++++++--------- src/apprt.zig | 1 + src/apprt/embedded.zig | 46 +++--------------------------------------- src/apprt/structs.zig | 10 +++++++++ 4 files changed, 24 insertions(+), 53 deletions(-) diff --git a/src/Surface.zig b/src/Surface.zig index 08db61c10..11c3190a5 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -3411,9 +3411,9 @@ pub fn performBindingAction(self: *Surface, action: input.Binding.Action) !bool } } - if (@hasDecl(apprt.Surface, "gotoPreviousTab")) { - self.rt_surface.gotoPreviousTab(); - } else log.warn("runtime doesn't implement gotoPreviousTab", .{}); + if (@hasDecl(apprt.Surface, "gotoTab")) { + self.rt_surface.gotoTab(.previous); + } else log.warn("runtime doesn't implement gotoTab", .{}); }, .next_tab => { @@ -3424,9 +3424,9 @@ pub fn performBindingAction(self: *Surface, action: input.Binding.Action) !bool } } - if (@hasDecl(apprt.Surface, "gotoNextTab")) { - self.rt_surface.gotoNextTab(); - } else log.warn("runtime doesn't implement gotoNextTab", .{}); + if (@hasDecl(apprt.Surface, "gotoTab")) { + self.rt_surface.gotoTab(.next); + } else log.warn("runtime doesn't implement gotoTab", .{}); }, .last_tab => { @@ -3437,14 +3437,14 @@ pub fn performBindingAction(self: *Surface, action: input.Binding.Action) !bool } } - if (@hasDecl(apprt.Surface, "gotoLastTab")) { - self.rt_surface.gotoLastTab(); - } else log.warn("runtime doesn't implement gotoLastTab", .{}); + if (@hasDecl(apprt.Surface, "gotoTab")) { + self.rt_surface.gotoTab(.last); + } else log.warn("runtime doesn't implement gotoTab", .{}); }, .goto_tab => |n| { if (@hasDecl(apprt.Surface, "gotoTab")) { - self.rt_surface.gotoTab(n); + self.rt_surface.gotoTab(@enumFromInt(n)); } else log.warn("runtime doesn't implement gotoTab", .{}); }, diff --git a/src/apprt.zig b/src/apprt.zig index 767fc57e6..491f1b8b5 100644 --- a/src/apprt.zig +++ b/src/apprt.zig @@ -28,6 +28,7 @@ pub const ClipboardRequestType = structs.ClipboardRequestType; pub const ColorScheme = structs.ColorScheme; pub const CursorPos = structs.CursorPos; pub const DesktopNotification = structs.DesktopNotification; +pub const GotoTab = structs.GotoTab; pub const IMEPos = structs.IMEPos; pub const Selection = structs.Selection; pub const SplitDirection = structs.SplitDirection; diff --git a/src/apprt/embedded.zig b/src/apprt/embedded.zig index fff14967d..52b42fe2a 100644 --- a/src/apprt/embedded.zig +++ b/src/apprt/embedded.zig @@ -108,7 +108,7 @@ pub const App = struct { toggle_split_zoom: ?*const fn (SurfaceUD) callconv(.C) void = null, /// Goto tab - goto_tab: ?*const fn (SurfaceUD, GotoTab) callconv(.C) void = null, + goto_tab: ?*const fn (SurfaceUD, apprt.GotoTab) callconv(.C) void = null, /// Toggle fullscreen for current window. toggle_fullscreen: ?*const fn (SurfaceUD, configpkg.NonNativeFullscreen) callconv(.C) void = null, @@ -135,14 +135,6 @@ pub const App = struct { mouse_over_link: ?*const fn (SurfaceUD, ?[*]const u8, usize) void = null, }; - /// Special values for the goto_tab callback. - const GotoTab = enum(i32) { - previous = -1, - next = -2, - last = -3, - _, - }; - core_app: *CoreApp, config: *const Config, opts: Options, @@ -995,45 +987,13 @@ pub const Surface = struct { }; } - pub fn gotoTab(self: *Surface, n: usize) void { + pub fn gotoTab(self: *Surface, tab: apprt.GotoTab) void { const func = self.app.opts.goto_tab orelse { log.info("runtime embedder does not goto_tab", .{}); return; }; - const idx = std.math.cast(i32, n) orelse { - log.warn("cannot cast tab index to i32 n={}", .{n}); - return; - }; - - func(self.userdata, @enumFromInt(idx)); - } - - pub fn gotoLastTab(self: *Surface) void { - const func = self.app.opts.goto_tab orelse { - log.info("runtime embedder does not goto_tab", .{}); - return; - }; - - func(self.userdata, .last); - } - - pub fn gotoPreviousTab(self: *Surface) void { - const func = self.app.opts.goto_tab orelse { - log.info("runtime embedder does not goto_tab", .{}); - return; - }; - - func(self.userdata, .previous); - } - - pub fn gotoNextTab(self: *Surface) void { - const func = self.app.opts.goto_tab orelse { - log.info("runtime embedder does not goto_tab", .{}); - return; - }; - - func(self.userdata, .next); + func(self.userdata, tab); } pub fn toggleFullscreen(self: *Surface, nonNativeFullscreen: configpkg.NonNativeFullscreen) void { diff --git a/src/apprt/structs.zig b/src/apprt/structs.zig index 1982cc497..1e14b1b7c 100644 --- a/src/apprt/structs.zig +++ b/src/apprt/structs.zig @@ -62,6 +62,16 @@ pub const DesktopNotification = struct { body: []const u8, }; +/// The tab to jump to. This is non-exhaustive so that integer values represent +/// the index (zero-based) of the tab to jump to. Negative values are special +/// values. +pub const GotoTab = enum(c_int) { + previous = -1, + next = -2, + last = -3, + _, +}; + // This is made extern (c_int) to make interop easier with our embedded // runtime. The small size cost doesn't make a difference in our union. pub const SplitDirection = enum(c_int) {