From dd39b29f30e078418e4ce366c35c33eff52a527f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 1 Dec 2023 08:52:08 -0800 Subject: [PATCH] apprt/gtk: we don't need to keep track of Tabs --- src/apprt/gtk/Tab.zig | 10 ++++++++++ src/apprt/gtk/Window.zig | 40 +--------------------------------------- 2 files changed, 11 insertions(+), 39 deletions(-) diff --git a/src/apprt/gtk/Tab.zig b/src/apprt/gtk/Tab.zig index c25f4ad55..e6a417792 100644 --- a/src/apprt/gtk/Tab.zig +++ b/src/apprt/gtk/Tab.zig @@ -134,6 +134,7 @@ pub fn init(self: *Tab, window: *Window, parent_: ?*CoreSurface) !void { // Attach all events _ = c.g_signal_connect_data(label_close, "clicked", c.G_CALLBACK(>kTabCloseClick), self, null, c.G_CONNECT_DEFAULT); + _ = c.g_signal_connect_data(box_widget, "destroy", c.G_CALLBACK(>kDestroy), self, null, c.G_CONNECT_DEFAULT); // Switch to the new tab c.gtk_notebook_set_current_page(window.notebook, page_idx); @@ -177,3 +178,12 @@ fn gtkTabCloseClick(_: *c.GtkButton, ud: ?*anyopaque) callconv(.C) void { const window = tab.window; window.closeTab(tab); } + +fn gtkDestroy(v: *c.GtkWidget, ud: ?*anyopaque) callconv(.C) void { + _ = v; + log.debug("tab box destroy", .{}); + + // When our box is destroyed, we want to destroy our tab, too. + const tab: *Tab = @ptrCast(@alignCast(ud)); + tab.destroy(tab.window.app.core_app.alloc); +} diff --git a/src/apprt/gtk/Window.zig b/src/apprt/gtk/Window.zig index 0f9d239da..1d02ba80c 100644 --- a/src/apprt/gtk/Window.zig +++ b/src/apprt/gtk/Window.zig @@ -35,9 +35,6 @@ notebook: *c.GtkNotebook, /// pointer to this because GTK can use it at any time. icon: icon.Icon, -/// The tab state for this window. -tabs: std.ArrayListUnmanaged(*Tab), - 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 @@ -59,7 +56,6 @@ pub fn init(self: *Window, app: *App) !void { .icon = undefined, .window = undefined, .notebook = undefined, - .tabs = .{}, }; // Create the window @@ -189,37 +185,18 @@ fn initActions(self: *Window) void { pub fn deinit(self: *Window) void { self.icon.deinit(self.app); - for (self.tabs.items) |tab| tab.destroy(self.app.core_app.alloc); - self.tabs.deinit(self.app.core_app.alloc); } /// Add a new tab to this window. pub fn newTab(self: *Window, parent: ?*CoreSurface) !void { const alloc = self.app.core_app.alloc; - const tab = try Tab.create(alloc, self, parent); - try self.tabs.append(alloc, tab); + _ = try Tab.create(alloc, self, parent); // TODO: When this is triggered through a GTK action, the new surface // redraws correctly. When it's triggered through keyboard shortcuts, it // does not (cursor doesn't blink) unless reactivated by refocusing. } -// addTab adds a tab to the windows list of tabs. -// This does *not* manage the underlying GtkNotebook pages. -pub fn addTab(self: *Window, tab: *Tab) !void { - tab.window = self; - try self.tabs.append(self.app.core_app.alloc, tab); -} - -pub fn removeTab(self: *Window, tab: *Tab) !void { - // Remove the tab from our stored tabs. - const tab_idx = for (self.tabs.items, 0..) |t, i| { - if (t == tab) break i; - } else null; - - if (tab_idx) |idx| _ = self.tabs.orderedRemove(idx) else return error.TabNotFound; -} - /// Close the tab for the given notebook page. This will automatically /// handle closing the window if there are no more tabs. pub fn closeTab(self: *Window, tab: *Tab) void { @@ -228,11 +205,6 @@ pub fn closeTab(self: *Window, tab: *Tab) void { // Find page and tab which we're closing const page_idx = getNotebookPageIndex(page); - // Remove the tab from our stored tabs. - self.removeTab(tab) catch |err| { - log.warn("tab {} not removable: {}", .{ page_idx, err }); - return; - }; // Deallocate the tab tab.deinit(self.app.core_app.alloc); self.app.core_app.alloc.destroy(tab); @@ -396,18 +368,8 @@ fn gtkNotebookCreateWindow( return null; }; - // Now remove the tab from the old window. - currentWindow.removeTab(tab) catch |err| { - log.warn("error removing tab error={}", .{err}); - return null; - }; - // And add it to the new window. tab.window = window; - window.addTab(tab) catch |err| { - log.warn("error adding tab to new window error={}", .{err}); - return null; - }; return window.notebook; }