apprt: make gotoTab handle all tab movements

This commit is contained in:
Mitchell Hashimoto
2024-08-26 20:09:45 -07:00
parent 02c6fb5a8c
commit 3d1ee3daa8
4 changed files with 24 additions and 53 deletions

View File

@ -3411,9 +3411,9 @@ pub fn performBindingAction(self: *Surface, action: input.Binding.Action) !bool
} }
} }
if (@hasDecl(apprt.Surface, "gotoPreviousTab")) { if (@hasDecl(apprt.Surface, "gotoTab")) {
self.rt_surface.gotoPreviousTab(); self.rt_surface.gotoTab(.previous);
} else log.warn("runtime doesn't implement gotoPreviousTab", .{}); } else log.warn("runtime doesn't implement gotoTab", .{});
}, },
.next_tab => { .next_tab => {
@ -3424,9 +3424,9 @@ pub fn performBindingAction(self: *Surface, action: input.Binding.Action) !bool
} }
} }
if (@hasDecl(apprt.Surface, "gotoNextTab")) { if (@hasDecl(apprt.Surface, "gotoTab")) {
self.rt_surface.gotoNextTab(); self.rt_surface.gotoTab(.next);
} else log.warn("runtime doesn't implement gotoNextTab", .{}); } else log.warn("runtime doesn't implement gotoTab", .{});
}, },
.last_tab => { .last_tab => {
@ -3437,14 +3437,14 @@ pub fn performBindingAction(self: *Surface, action: input.Binding.Action) !bool
} }
} }
if (@hasDecl(apprt.Surface, "gotoLastTab")) { if (@hasDecl(apprt.Surface, "gotoTab")) {
self.rt_surface.gotoLastTab(); self.rt_surface.gotoTab(.last);
} else log.warn("runtime doesn't implement gotoLastTab", .{}); } else log.warn("runtime doesn't implement gotoTab", .{});
}, },
.goto_tab => |n| { .goto_tab => |n| {
if (@hasDecl(apprt.Surface, "gotoTab")) { if (@hasDecl(apprt.Surface, "gotoTab")) {
self.rt_surface.gotoTab(n); self.rt_surface.gotoTab(@enumFromInt(n));
} else log.warn("runtime doesn't implement gotoTab", .{}); } else log.warn("runtime doesn't implement gotoTab", .{});
}, },

View File

@ -28,6 +28,7 @@ pub const ClipboardRequestType = structs.ClipboardRequestType;
pub const ColorScheme = structs.ColorScheme; pub const ColorScheme = structs.ColorScheme;
pub const CursorPos = structs.CursorPos; pub const CursorPos = structs.CursorPos;
pub const DesktopNotification = structs.DesktopNotification; pub const DesktopNotification = structs.DesktopNotification;
pub const GotoTab = structs.GotoTab;
pub const IMEPos = structs.IMEPos; pub const IMEPos = structs.IMEPos;
pub const Selection = structs.Selection; pub const Selection = structs.Selection;
pub const SplitDirection = structs.SplitDirection; pub const SplitDirection = structs.SplitDirection;

View File

@ -108,7 +108,7 @@ pub const App = struct {
toggle_split_zoom: ?*const fn (SurfaceUD) callconv(.C) void = null, toggle_split_zoom: ?*const fn (SurfaceUD) callconv(.C) void = null,
/// Goto tab /// 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 for current window.
toggle_fullscreen: ?*const fn (SurfaceUD, configpkg.NonNativeFullscreen) callconv(.C) void = null, 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, 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, core_app: *CoreApp,
config: *const Config, config: *const Config,
opts: Options, 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 { const func = self.app.opts.goto_tab orelse {
log.info("runtime embedder does not goto_tab", .{}); log.info("runtime embedder does not goto_tab", .{});
return; return;
}; };
const idx = std.math.cast(i32, n) orelse { func(self.userdata, tab);
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);
} }
pub fn toggleFullscreen(self: *Surface, nonNativeFullscreen: configpkg.NonNativeFullscreen) void { pub fn toggleFullscreen(self: *Surface, nonNativeFullscreen: configpkg.NonNativeFullscreen) void {

View File

@ -62,6 +62,16 @@ pub const DesktopNotification = struct {
body: []const u8, 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 // 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. // runtime. The small size cost doesn't make a difference in our union.
pub const SplitDirection = enum(c_int) { pub const SplitDirection = enum(c_int) {