apprt/gtk: code aesthetic changes

This commit is contained in:
Mitchell Hashimoto
2023-12-01 08:08:06 -08:00
parent 89f4cf11c7
commit c7eeda1f00
2 changed files with 16 additions and 13 deletions

View File

@ -18,9 +18,10 @@ const log = std.log.scoped(.gtk);
pub const GHOSTTY_TAB = "ghostty_tab"; pub const GHOSTTY_TAB = "ghostty_tab";
/// The window that owns this tab.
window: *Window, window: *Window,
/// The tab label. /// The tab label. The tab label is the text that appears on the tab.
label_text: *c.GtkLabel, label_text: *c.GtkLabel,
/// We'll put our children into this box instead of packing them /// 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); 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); const box_widget = c.gtk_box_new(c.GTK_ORIENTATION_VERTICAL, 0);
c.gtk_widget_set_hexpand(box_widget, 1); c.gtk_widget_set_hexpand(box_widget, 1);
c.gtk_widget_set_vexpand(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); 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 // TODO: move this
/// Replace the surface element that this tab is showing. /// Replace the surface element that this tab is showing.
pub fn replaceElem(self: *Tab, elem: Surface.Container.Elem) void { pub fn replaceElem(self: *Tab, elem: Surface.Container.Elem) void {

View File

@ -35,6 +35,7 @@ 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), tabs: std.ArrayListUnmanaged(*Tab),
pub fn create(alloc: Allocator, app: *App) !*Window { pub fn create(alloc: Allocator, app: *App) !*Window {
@ -58,13 +59,9 @@ pub fn init(self: *Window, app: *App) !void {
.icon = undefined, .icon = undefined,
.window = undefined, .window = undefined,
.notebook = undefined, .notebook = undefined,
.tabs = undefined, .tabs = .{},
}; };
var tabs: std.ArrayListUnmanaged(*Tab) = .{};
errdefer tabs.deinit(app.core_app.alloc);
self.tabs = tabs;
// Create the window // Create the window
const window = c.gtk_application_window_new(app.app); const window = c.gtk_application_window_new(app.app);
const gtk_window: *c.GtkWindow = @ptrCast(window); const gtk_window: *c.GtkWindow = @ptrCast(window);
@ -192,17 +189,14 @@ 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| { for (self.tabs.items) |tab| tab.destroy(self.app.core_app.alloc);
tab.deinit(self.app.core_app.alloc);
self.app.core_app.alloc.destroy(tab);
}
self.tabs.deinit(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, parentSurface: ?*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, parentSurface); const tab = try Tab.create(alloc, self, parent);
try self.tabs.append(alloc, tab); 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