From 5327daac0497ef1f41dc129d8fa3b4c5c78fc97c Mon Sep 17 00:00:00 2001 From: Paul Berg Date: Mon, 10 Jun 2024 10:06:34 +0200 Subject: [PATCH] adw: implement next/previous --- src/apprt/gtk/Window.zig | 37 +++------------------------ src/apprt/gtk/notebook.zig | 52 +++++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 34 deletions(-) diff --git a/src/apprt/gtk/Window.zig b/src/apprt/gtk/Window.zig index c269b51bf..034abf25e 100644 --- a/src/apprt/gtk/Window.zig +++ b/src/apprt/gtk/Window.zig @@ -211,21 +211,7 @@ pub fn gotoPreviousTab(self: *Window, surface: *Surface) void { log.info("surface is not attached to a tab bar, cannot navigate", .{}); return; }; - - const notebook: *c.GtkNotebook = self.notebook.gtk_notebook; - const page = c.gtk_notebook_get_page(notebook, @ptrCast(tab.box)) orelse return; - const page_idx = Notebook.getNotebookPageIndex(page); - - // The next index is the previous or we wrap around. - const next_idx = if (page_idx > 0) page_idx - 1 else next_idx: { - const max = c.gtk_notebook_get_n_pages(notebook); - break :next_idx max -| 1; - }; - - // Do nothing if we have one tab - if (next_idx == page_idx) return; - - c.gtk_notebook_set_current_page(notebook, next_idx); + self.notebook.gotoPreviousTab(tab); self.focusCurrentTab(); } @@ -235,15 +221,7 @@ pub fn gotoNextTab(self: *Window, surface: *Surface) void { log.info("surface is not attached to a tab bar, cannot navigate", .{}); return; }; - - const notebook: *c.GtkNotebook = self.notebook.gtk_notebook; - const page = c.gtk_notebook_get_page(notebook, @ptrCast(tab.box)) orelse return; - const page_idx = Notebook.getNotebookPageIndex(page); - const max = c.gtk_notebook_get_n_pages(notebook) -| 1; - const next_idx = if (page_idx < max) page_idx + 1 else 0; - if (next_idx == page_idx) return; - - c.gtk_notebook_set_current_page(notebook, next_idx); + self.notebook.gotoNextTab(tab); self.focusCurrentTab(); } @@ -257,11 +235,10 @@ pub fn gotoLastTab(self: *Window) void { /// Go to the specific tab index. pub fn gotoTab(self: *Window, n: usize) void { if (n == 0) return; - const notebook: *c.GtkNotebook = self.notebook.gtk_notebook; - const max = c.gtk_notebook_get_n_pages(notebook); + const max = self.notebook.nPages(); const page_idx = std.math.cast(c_int, n - 1) orelse return; if (page_idx < max) { - c.gtk_notebook_set_current_page(notebook, page_idx); + self.notebook.gotoNthTab(page_idx); self.focusCurrentTab(); } } @@ -523,12 +500,6 @@ fn gtkActionReset( /// Returns the surface to use for an action. fn actionSurface(self: *Window) ?*CoreSurface { const tab = self.notebook.currentTab() orelse return null; - // const notebook: *c.GtkNotebook = self.notebook.gtk_notebook; - // const page_idx = c.gtk_notebook_get_current_page(notebook); - // const page = c.gtk_notebook_get_nth_page(notebook, page_idx); - // const tab: *Tab = @ptrCast(@alignCast( - // c.g_object_get_data(@ptrCast(page), Tab.GHOSTTY_TAB) orelse return null, - // )); return &tab.focus_child.core_surface; } diff --git a/src/apprt/gtk/notebook.zig b/src/apprt/gtk/notebook.zig index 8e43c343b..5e528a3c0 100644 --- a/src/apprt/gtk/notebook.zig +++ b/src/apprt/gtk/notebook.zig @@ -24,7 +24,7 @@ pub const Notebook = union(enum) { const tab_view = c.adw_tab_view_new(); const tab_bar = c.adw_tab_bar_new(); c.gtk_box_append(@ptrCast(box), @ptrCast(@alignCast(tab_bar))); - c.adw_tab_bar_set_view(tab_bar, tab_view.?); + c.adw_tab_bar_set_view(tab_bar, tab_view); if (window.app.config.@"gtk-wide-tabs") c.adw_tab_bar_set_expand_tabs(tab_bar, @intCast(1)); @@ -115,6 +115,56 @@ pub const Notebook = union(enum) { )); } + pub fn gotoNthTab(self: Notebook, position: c_int) void { + switch (self) { + .adw_tab_view => |tab_view| { + if (!build_options.libadwaita) unreachable; + const page_to_select = c.adw_tab_view_get_nth_page(tab_view, position); + c.adw_tab_view_set_selected_page(tab_view, page_to_select); + }, + .gtk_notebook => |notebook| c.gtk_notebook_set_current_page(notebook, position), + } + } + + pub fn getTabPosition(self: Notebook, tab: *Tab) ?c_int { + return switch (self) { + .adw_tab_view => |tab_view| page_idx: { + if (!build_options.libadwaita) unreachable; + const page = c.adw_tab_view_get_page(tab_view, @ptrCast(tab.box)) orelse return null; + break :page_idx c.adw_tab_view_get_page_position(tab_view, page); + }, + .gtk_notebook => |notebook| page_idx: { + const page = c.gtk_notebook_get_page(notebook, @ptrCast(tab.box)) orelse return null; + break :page_idx getNotebookPageIndex(page); + }, + }; + } + + pub fn gotoPreviousTab(self: Notebook, tab: *Tab) void { + const page_idx = self.getTabPosition(tab) orelse return; + + // The next index is the previous or we wrap around. + const next_idx = if (page_idx > 0) page_idx - 1 else next_idx: { + const max = self.nPages(); + break :next_idx max -| 1; + }; + + // Do nothing if we have one tab + if (next_idx == page_idx) return; + + self.gotoNthTab(next_idx); + } + + pub fn gotoNextTab(self: Notebook, tab: *Tab) void { + const page_idx = self.getTabPosition(tab) orelse return; + + 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); + } + pub fn setTabLabel(self: Notebook, tab: *Tab, title: [:0]const u8) void { switch (self) { .adw_tab_view => |tab_view| {