apprt/gtk: we don't need to keep track of Tabs

This commit is contained in:
Mitchell Hashimoto
2023-12-01 08:52:08 -08:00
parent c7eeda1f00
commit dd39b29f30
2 changed files with 11 additions and 39 deletions

View File

@ -134,6 +134,7 @@ pub fn init(self: *Tab, window: *Window, parent_: ?*CoreSurface) !void {
// Attach all events // Attach all events
_ = c.g_signal_connect_data(label_close, "clicked", c.G_CALLBACK(&gtkTabCloseClick), self, null, c.G_CONNECT_DEFAULT); _ = c.g_signal_connect_data(label_close, "clicked", c.G_CALLBACK(&gtkTabCloseClick), self, null, c.G_CONNECT_DEFAULT);
_ = c.g_signal_connect_data(box_widget, "destroy", c.G_CALLBACK(&gtkDestroy), self, null, c.G_CONNECT_DEFAULT);
// Switch to the new tab // Switch to the new tab
c.gtk_notebook_set_current_page(window.notebook, page_idx); 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; const window = tab.window;
window.closeTab(tab); 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);
}

View File

@ -35,9 +35,6 @@ notebook: *c.GtkNotebook,
/// pointer to this because GTK can use it at any time. /// pointer to this because GTK can use it at any time.
icon: icon.Icon, icon: icon.Icon,
/// The tab state for this window.
tabs: std.ArrayListUnmanaged(*Tab),
pub fn create(alloc: Allocator, app: *App) !*Window { pub fn create(alloc: Allocator, app: *App) !*Window {
// Allocate a fixed pointer for our window. We try to minimize // Allocate a fixed pointer for our window. We try to minimize
// allocations but windows and other GUI requirements are so minimal // allocations but windows and other GUI requirements are so minimal
@ -59,7 +56,6 @@ pub fn init(self: *Window, app: *App) !void {
.icon = undefined, .icon = undefined,
.window = undefined, .window = undefined,
.notebook = undefined, .notebook = undefined,
.tabs = .{},
}; };
// Create the window // Create the window
@ -189,37 +185,18 @@ fn initActions(self: *Window) void {
pub fn deinit(self: *Window) void { pub fn deinit(self: *Window) void {
self.icon.deinit(self.app); 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. /// Add a new tab to this window.
pub fn newTab(self: *Window, parent: ?*CoreSurface) !void { pub fn newTab(self: *Window, parent: ?*CoreSurface) !void {
const alloc = self.app.core_app.alloc; const alloc = self.app.core_app.alloc;
const tab = try Tab.create(alloc, self, parent); _ = try Tab.create(alloc, self, parent);
try self.tabs.append(alloc, tab);
// TODO: When this is triggered through a GTK action, the new surface // TODO: When this is triggered through a GTK action, the new surface
// redraws correctly. When it's triggered through keyboard shortcuts, it // redraws correctly. When it's triggered through keyboard shortcuts, it
// does not (cursor doesn't blink) unless reactivated by refocusing. // 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 /// Close the tab for the given notebook page. This will automatically
/// handle closing the window if there are no more tabs. /// handle closing the window if there are no more tabs.
pub fn closeTab(self: *Window, tab: *Tab) void { 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 // Find page and tab which we're closing
const page_idx = getNotebookPageIndex(page); 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 // Deallocate the tab
tab.deinit(self.app.core_app.alloc); tab.deinit(self.app.core_app.alloc);
self.app.core_app.alloc.destroy(tab); self.app.core_app.alloc.destroy(tab);
@ -396,18 +368,8 @@ fn gtkNotebookCreateWindow(
return null; 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. // And add it to the new window.
tab.window = window; tab.window = window;
window.addTab(tab) catch |err| {
log.warn("error adding tab to new window error={}", .{err});
return null;
};
return window.notebook; return window.notebook;
} }