From 69fd438370fb7d92ed7b2ccfc940b471b437494c Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Sat, 8 Feb 2025 17:30:27 -0600 Subject: [PATCH] gtk: make previous_tab, next_tab, last_tab, and goto_tab performable --- src/apprt/gtk/App.zig | 14 ++++++++------ src/apprt/gtk/Surface.zig | 2 +- src/apprt/gtk/Window.zig | 29 ++++++++++++++++------------- src/apprt/gtk/notebook.zig | 23 ++++++++++++++--------- 4 files changed, 39 insertions(+), 29 deletions(-) diff --git a/src/apprt/gtk/App.zig b/src/apprt/gtk/App.zig index e7974495a..41bc755c2 100644 --- a/src/apprt/gtk/App.zig +++ b/src/apprt/gtk/App.zig @@ -526,7 +526,7 @@ pub fn performAction( .new_tab => try self.newTab(target), .close_tab => try self.closeTab(target), - .goto_tab => self.gotoTab(target, value), + .goto_tab => return self.gotoTab(target, value), .move_tab => self.moveTab(target, value), .new_split => try self.newSplit(target, value), .resize_split => self.resizeSplit(target, value), @@ -600,24 +600,26 @@ fn closeTab(_: *App, target: apprt.Target) !void { } } -fn gotoTab(_: *App, target: apprt.Target, tab: apprt.action.GotoTab) void { +fn gotoTab(_: *App, target: apprt.Target, tab: apprt.action.GotoTab) bool { switch (target) { - .app => {}, + .app => { + return false; + }, .surface => |v| { const window = v.rt_surface.container.window() orelse { log.info( "gotoTab invalid for container={s}", .{@tagName(v.rt_surface.container)}, ); - return; + return false; }; - switch (tab) { + return switch (tab) { .previous => window.gotoPreviousTab(v.rt_surface), .next => window.gotoNextTab(v.rt_surface), .last => window.gotoLastTab(), else => window.gotoTab(@intCast(@intFromEnum(tab))), - } + }; }, } } diff --git a/src/apprt/gtk/Surface.zig b/src/apprt/gtk/Surface.zig index b9f8949fb..37ffb26cb 100644 --- a/src/apprt/gtk/Surface.zig +++ b/src/apprt/gtk/Surface.zig @@ -2124,7 +2124,7 @@ pub fn present(self: *Surface) void { if (self.container.window()) |window| { if (self.container.tab()) |tab| { if (window.notebook.getTabPosition(tab)) |position| - window.notebook.gotoNthTab(position); + _ = window.notebook.gotoNthTab(position); } c.gtk_window_present(window.window); } diff --git a/src/apprt/gtk/Window.zig b/src/apprt/gtk/Window.zig index bb49165b9..3daeffe76 100644 --- a/src/apprt/gtk/Window.zig +++ b/src/apprt/gtk/Window.zig @@ -506,23 +506,25 @@ pub fn closeTab(self: *Window, tab: *Tab) void { } /// Go to the previous tab for a surface. -pub fn gotoPreviousTab(self: *Window, surface: *Surface) void { +pub fn gotoPreviousTab(self: *Window, surface: *Surface) bool { const tab = surface.container.tab() orelse { log.info("surface is not attached to a tab bar, cannot navigate", .{}); - return; + return false; }; - self.notebook.gotoPreviousTab(tab); + if (!self.notebook.gotoPreviousTab(tab)) return false; self.focusCurrentTab(); + return true; } /// Go to the next tab for a surface. -pub fn gotoNextTab(self: *Window, surface: *Surface) void { +pub fn gotoNextTab(self: *Window, surface: *Surface) bool { const tab = surface.container.tab() orelse { log.info("surface is not attached to a tab bar, cannot navigate", .{}); - return; + return false; }; - self.notebook.gotoNextTab(tab); + if (!self.notebook.gotoNextTab(tab)) return false; self.focusCurrentTab(); + return true; } /// Move the current tab for a surface. @@ -535,19 +537,20 @@ pub fn moveTab(self: *Window, surface: *Surface, position: c_int) void { } /// Go to the last tab for a surface. -pub fn gotoLastTab(self: *Window) void { +pub fn gotoLastTab(self: *Window) bool { const max = self.notebook.nPages(); - self.gotoTab(@intCast(max)); + return self.gotoTab(@intCast(max)); } /// Go to the specific tab index. -pub fn gotoTab(self: *Window, n: usize) void { - if (n == 0) return; +pub fn gotoTab(self: *Window, n: usize) bool { + if (n == 0) return false; const max = self.notebook.nPages(); - if (max == 0) return; - const page_idx = std.math.cast(c_int, n - 1) orelse return; - self.notebook.gotoNthTab(@min(page_idx, max - 1)); + if (max == 0) return false; + const page_idx = std.math.cast(c_int, n - 1) orelse return false; + if (!self.notebook.gotoNthTab(@min(page_idx, max - 1))) return false; self.focusCurrentTab(); + return true; } /// Toggle tab overview (if present) diff --git a/src/apprt/gtk/notebook.zig b/src/apprt/gtk/notebook.zig index 4676c2529..548f2acaf 100644 --- a/src/apprt/gtk/notebook.zig +++ b/src/apprt/gtk/notebook.zig @@ -59,11 +59,14 @@ pub const Notebook = union(enum) { }; } - pub fn gotoNthTab(self: *Notebook, position: c_int) void { + pub fn gotoNthTab(self: *Notebook, position: c_int) bool { + const current_page_ = self.currentPage(); + if (current_page_) |current_page| if (current_page == position) return false; switch (self.*) { .adw => |*adw| adw.gotoNthTab(position), .gtk => |*gtk| gtk.gotoNthTab(position), } + return true; } pub fn getTabPosition(self: *Notebook, tab: *Tab) ?c_int { @@ -73,8 +76,8 @@ pub const Notebook = union(enum) { }; } - pub fn gotoPreviousTab(self: *Notebook, tab: *Tab) void { - const page_idx = self.getTabPosition(tab) orelse return; + pub fn gotoPreviousTab(self: *Notebook, tab: *Tab) bool { + const page_idx = self.getTabPosition(tab) orelse return false; // The next index is the previous or we wrap around. const next_idx = if (page_idx > 0) page_idx - 1 else next_idx: { @@ -83,19 +86,21 @@ pub const Notebook = union(enum) { }; // Do nothing if we have one tab - if (next_idx == page_idx) return; + if (next_idx == page_idx) return false; - self.gotoNthTab(next_idx); + return self.gotoNthTab(next_idx); } - pub fn gotoNextTab(self: *Notebook, tab: *Tab) void { - const page_idx = self.getTabPosition(tab) orelse return; + pub fn gotoNextTab(self: *Notebook, tab: *Tab) bool { + const page_idx = self.getTabPosition(tab) orelse return false; const max = self.nPages() -| 1; const next_idx = if (page_idx < max) page_idx + 1 else 0; - if (next_idx == page_idx) return; - self.gotoNthTab(next_idx); + // Do nothing if we have one tab + if (next_idx == page_idx) return false; + + return self.gotoNthTab(next_idx); } pub fn moveTab(self: *Notebook, tab: *Tab, position: c_int) void {