diff --git a/src/apprt/gtk/Tab.zig b/src/apprt/gtk/Tab.zig index 96acfea0a..c25f4ad55 100644 --- a/src/apprt/gtk/Tab.zig +++ b/src/apprt/gtk/Tab.zig @@ -18,9 +18,10 @@ const log = std.log.scoped(.gtk); pub const GHOSTTY_TAB = "ghostty_tab"; +/// The window that owns this tab. window: *Window, -/// The tab label. +/// The tab label. The tab label is the text that appears on the tab. label_text: *c.GtkLabel, /// We'll put our children into this box instead of packing them @@ -86,7 +87,9 @@ pub fn init(self: *Tab, window: *Window, parent_: ?*CoreSurface) !void { c.gtk_widget_set_size_request(label_text_widget, 100, 1); } - // Create a Box in which we'll later keep either Surface or Split + // Create a Box in which we'll later keep either Surface or Split. + // Using a box makes it easier to maintain the tab contents because + // we never need to change the root widget of the notebook page (tab). const box_widget = c.gtk_box_new(c.GTK_ORIENTATION_VERTICAL, 0); c.gtk_widget_set_hexpand(box_widget, 1); c.gtk_widget_set_vexpand(box_widget, 1); @@ -145,6 +148,12 @@ pub fn deinit(self: *Tab, alloc: Allocator) void { self.elem.deinit(alloc); } +/// Deinit and deallocate the tab. +pub fn destroy(self: *Tab, alloc: Allocator) void { + self.deinit(alloc); + alloc.destroy(self); +} + // TODO: move this /// Replace the surface element that this tab is showing. pub fn replaceElem(self: *Tab, elem: Surface.Container.Elem) void { diff --git a/src/apprt/gtk/Window.zig b/src/apprt/gtk/Window.zig index fbe1f64f2..0f9d239da 100644 --- a/src/apprt/gtk/Window.zig +++ b/src/apprt/gtk/Window.zig @@ -35,6 +35,7 @@ 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 { @@ -58,13 +59,9 @@ pub fn init(self: *Window, app: *App) !void { .icon = undefined, .window = undefined, .notebook = undefined, - .tabs = undefined, + .tabs = .{}, }; - var tabs: std.ArrayListUnmanaged(*Tab) = .{}; - errdefer tabs.deinit(app.core_app.alloc); - self.tabs = tabs; - // Create the window const window = c.gtk_application_window_new(app.app); const gtk_window: *c.GtkWindow = @ptrCast(window); @@ -192,17 +189,14 @@ fn initActions(self: *Window) void { pub fn deinit(self: *Window) void { self.icon.deinit(self.app); - for (self.tabs.items) |tab| { - tab.deinit(self.app.core_app.alloc); - self.app.core_app.alloc.destroy(tab); - } + 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, parentSurface: ?*CoreSurface) !void { +pub fn newTab(self: *Window, parent: ?*CoreSurface) !void { const alloc = self.app.core_app.alloc; - const tab = try Tab.create(alloc, self, parentSurface); + const tab = try Tab.create(alloc, self, parent); try self.tabs.append(alloc, tab); // TODO: When this is triggered through a GTK action, the new surface