From 27f5a6d08e37cdd72d1cc0078336a13ccf6d2ba2 Mon Sep 17 00:00:00 2001 From: Cameron Dart <8763518+SkamDart@users.noreply.github.com> Date: Fri, 21 Jun 2024 03:56:50 +0000 Subject: [PATCH] gtk: last_active_tab --- src/apprt/gtk/Surface.zig | 12 ++++++++++++ src/apprt/gtk/Window.zig | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/apprt/gtk/Surface.zig b/src/apprt/gtk/Surface.zig index 1ee433db9..2d1c92be5 100644 --- a/src/apprt/gtk/Surface.zig +++ b/src/apprt/gtk/Surface.zig @@ -695,6 +695,18 @@ pub fn gotoTab(self: *Surface, n: usize) void { window.gotoTab(n); } +pub fn gotoLastActiveTab(self: *Surface) void { + const window = self.container.window() orelse { + log.info( + "gotoLastActiveTab invalid for container={s}", + .{@tagName(self.container)}, + ); + return; + }; + + window.gotoLastActiveTab(); +} + pub fn setShouldClose(self: *Surface) void { _ = self; } diff --git a/src/apprt/gtk/Window.zig b/src/apprt/gtk/Window.zig index 0cb73c407..d1b1b9f8f 100644 --- a/src/apprt/gtk/Window.zig +++ b/src/apprt/gtk/Window.zig @@ -30,6 +30,8 @@ window: *c.GtkWindow, /// The notebook (tab grouping) for this window. notebook: *c.GtkNotebook, +last_active_tab_index: c_int = 0, + pub fn create(alloc: Allocator, app: *App) !*Window { // Allocate a fixed pointer for our window. We try to minimize // allocations but windows and other GUI requirements are so minimal @@ -248,6 +250,9 @@ pub fn gotoPreviousTab(self: *Window, surface: *Surface) void { // Do nothing if we have one tab if (next_idx == page_idx) return; + // Cache the last active tab index for our last-active-tab hotkey. + self.last_active_tab_index = page_idx; + c.gtk_notebook_set_current_page(self.notebook, next_idx); self.focusCurrentTab(); } @@ -265,6 +270,9 @@ pub fn gotoNextTab(self: *Window, surface: *Surface) void { const next_idx = if (page_idx < max) page_idx + 1 else 0; if (next_idx == page_idx) return; + // Cache the last active tab index for our last-active-tab hotkey. + self.last_active_tab_index = page_idx; + c.gtk_notebook_set_current_page(self.notebook, next_idx); self.focusCurrentTab(); } @@ -278,6 +286,20 @@ pub fn gotoTab(self: *Window, n: usize) void { c.gtk_notebook_set_current_page(self.notebook, page_idx); self.focusCurrentTab(); } + + // Cache the last active tab index for our last-active-tab hotkey. + self.last_active_tab_index = page_idx; +} + +// Goto the more recently selected tab. +pub fn gotoLastActiveTab(self: *Window) void { + if (self.last_active_tab_index >= 0) { + const page_idx = c.gtk_notebook_get_current_page(self.notebook); + c.gtk_notebook_set_current_page(self.notebook, self.last_active_tab_index); + self.focusCurrentTab(); + log.debug("going to last active tab", .{}); + self.last_active_tab_index = page_idx; + } } /// Toggle fullscreen for this window.