gtk: make previous_tab, next_tab, last_tab, and goto_tab performable

This commit is contained in:
Jeffrey C. Ollie
2025-02-08 17:30:27 -06:00
committed by Mitchell Hashimoto
parent 57e7565b7f
commit 69fd438370
4 changed files with 39 additions and 29 deletions

View File

@ -526,7 +526,7 @@ pub fn performAction(
.new_tab => try self.newTab(target), .new_tab => try self.newTab(target),
.close_tab => try self.closeTab(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), .move_tab => self.moveTab(target, value),
.new_split => try self.newSplit(target, value), .new_split => try self.newSplit(target, value),
.resize_split => self.resizeSplit(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) { switch (target) {
.app => {}, .app => {
return false;
},
.surface => |v| { .surface => |v| {
const window = v.rt_surface.container.window() orelse { const window = v.rt_surface.container.window() orelse {
log.info( log.info(
"gotoTab invalid for container={s}", "gotoTab invalid for container={s}",
.{@tagName(v.rt_surface.container)}, .{@tagName(v.rt_surface.container)},
); );
return; return false;
}; };
switch (tab) { return switch (tab) {
.previous => window.gotoPreviousTab(v.rt_surface), .previous => window.gotoPreviousTab(v.rt_surface),
.next => window.gotoNextTab(v.rt_surface), .next => window.gotoNextTab(v.rt_surface),
.last => window.gotoLastTab(), .last => window.gotoLastTab(),
else => window.gotoTab(@intCast(@intFromEnum(tab))), else => window.gotoTab(@intCast(@intFromEnum(tab))),
} };
}, },
} }
} }

View File

@ -2124,7 +2124,7 @@ pub fn present(self: *Surface) void {
if (self.container.window()) |window| { if (self.container.window()) |window| {
if (self.container.tab()) |tab| { if (self.container.tab()) |tab| {
if (window.notebook.getTabPosition(tab)) |position| if (window.notebook.getTabPosition(tab)) |position|
window.notebook.gotoNthTab(position); _ = window.notebook.gotoNthTab(position);
} }
c.gtk_window_present(window.window); c.gtk_window_present(window.window);
} }

View File

@ -506,23 +506,25 @@ pub fn closeTab(self: *Window, tab: *Tab) void {
} }
/// Go to the previous tab for a surface. /// 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 { const tab = surface.container.tab() orelse {
log.info("surface is not attached to a tab bar, cannot navigate", .{}); 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(); self.focusCurrentTab();
return true;
} }
/// Go to the next tab for a surface. /// 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 { const tab = surface.container.tab() orelse {
log.info("surface is not attached to a tab bar, cannot navigate", .{}); 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(); self.focusCurrentTab();
return true;
} }
/// Move the current tab for a surface. /// 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. /// 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(); const max = self.notebook.nPages();
self.gotoTab(@intCast(max)); return self.gotoTab(@intCast(max));
} }
/// Go to the specific tab index. /// Go to the specific tab index.
pub fn gotoTab(self: *Window, n: usize) void { pub fn gotoTab(self: *Window, n: usize) bool {
if (n == 0) return; if (n == 0) return false;
const max = self.notebook.nPages(); const max = self.notebook.nPages();
if (max == 0) return; if (max == 0) return false;
const page_idx = std.math.cast(c_int, n - 1) orelse return; const page_idx = std.math.cast(c_int, n - 1) orelse return false;
self.notebook.gotoNthTab(@min(page_idx, max - 1)); if (!self.notebook.gotoNthTab(@min(page_idx, max - 1))) return false;
self.focusCurrentTab(); self.focusCurrentTab();
return true;
} }
/// Toggle tab overview (if present) /// Toggle tab overview (if present)

View File

@ -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.*) { switch (self.*) {
.adw => |*adw| adw.gotoNthTab(position), .adw => |*adw| adw.gotoNthTab(position),
.gtk => |*gtk| gtk.gotoNthTab(position), .gtk => |*gtk| gtk.gotoNthTab(position),
} }
return true;
} }
pub fn getTabPosition(self: *Notebook, tab: *Tab) ?c_int { 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 { pub fn gotoPreviousTab(self: *Notebook, tab: *Tab) bool {
const page_idx = self.getTabPosition(tab) orelse return; const page_idx = self.getTabPosition(tab) orelse return false;
// The next index is the previous or we wrap around. // The next index is the previous or we wrap around.
const next_idx = if (page_idx > 0) page_idx - 1 else next_idx: { 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 // 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 { pub fn gotoNextTab(self: *Notebook, tab: *Tab) bool {
const page_idx = self.getTabPosition(tab) orelse return; const page_idx = self.getTabPosition(tab) orelse return false;
const max = self.nPages() -| 1; const max = self.nPages() -| 1;
const next_idx = if (page_idx < max) page_idx + 1 else 0; 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 { pub fn moveTab(self: *Notebook, tab: *Tab, position: c_int) void {