From 2d7a81c0dbb76c050fcff5a51f71f0ec074cd820 Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Thu, 19 Oct 2023 16:15:57 +0200 Subject: [PATCH] gtk: cleanup code in *Paned --- src/apprt/gtk/Paned.zig | 38 +++++++++++-------------------------- src/apprt/gtk/Tab.zig | 28 +++++---------------------- src/apprt/gtk/Window.zig | 41 ++++++++++++++++++++-------------------- 3 files changed, 37 insertions(+), 70 deletions(-) diff --git a/src/apprt/gtk/Paned.zig b/src/apprt/gtk/Paned.zig index 8cf3d79ce..7d377031e 100644 --- a/src/apprt/gtk/Paned.zig +++ b/src/apprt/gtk/Paned.zig @@ -44,8 +44,8 @@ pub fn init(self: *Paned, window: *Window, label_text: *c.GtkWidget) !void { .window = window, .label_text = label_text, .paned = undefined, - .child1 = .empty, - .child2 = .empty, + .child1 = .none, + .child2 = .none, .parent = undefined, }; @@ -53,14 +53,6 @@ pub fn init(self: *Paned, window: *Window, label_text: *c.GtkWidget) !void { const gtk_paned: *c.GtkPaned = @ptrCast(paned); errdefer c.gtk_widget_destroy(paned); self.paned = gtk_paned; - - // const surface = try self.newSurface(self.window.actionSurface()); - // // We know that both panels are currently empty, so we maximize the 1st - // c.gtk_paned_set_position(self.paned, 100); - // const child_widget: *c.GtkWidget = @ptrCast(surface.gl_area); - // const child = Child{ .surface = surface }; - // c.gtk_paned_pack1(self.paned, child_widget, 1, 1); - // self.child1 = child; } pub fn newSurface(self: *Paned, tab: *Tab, parent_: ?*CoreSurface) !*Surface { @@ -87,7 +79,7 @@ pub fn newSurface(self: *Paned, tab: *Tab, parent_: ?*CoreSurface) !*Surface { .tab = tab, .parent = .{ .paned = .{ self, - Position.end, + .end, } }, .gl_area = @ptrCast(gl_area), .title_label = @ptrCast(self.label_text), @@ -97,36 +89,28 @@ pub fn newSurface(self: *Paned, tab: *Tab, parent_: ?*CoreSurface) !*Surface { } pub fn removeChildren(self: *Paned) void { + assert(self.child1 != .none); + assert(self.child2 != .none); + self.child1 = .none; + self.child2 = .none; c.gtk_paned_set_start_child(@ptrCast(self.paned), null); c.gtk_paned_set_end_child(@ptrCast(self.paned), null); } pub fn addChild1Surface(self: *Paned, surface: *Surface) void { - assert(self.child1.is_empty()); + assert(self.child1 == .none); self.child1 = Tab.Child{ .surface = surface }; - surface.setParent(Parent{ .paned = .{ self, Position.start } }); + surface.setParent(Parent{ .paned = .{ self, .start } }); c.gtk_paned_set_start_child(@ptrCast(self.paned), @ptrCast(surface.gl_area)); } pub fn addChild2Surface(self: *Paned, surface: *Surface) void { - assert(self.child2.is_empty()); + assert(self.child2 == .none); self.child2 = Tab.Child{ .surface = surface }; - surface.setParent(Parent{ .paned = .{ self, Position.end } }); + surface.setParent(Parent{ .paned = .{ self, .end } }); c.gtk_paned_set_end_child(@ptrCast(self.paned), @ptrCast(surface.gl_area)); } -pub fn removeChild1(self: *Paned) void { - assert(!self.child1.is_empty()); - self.child1 = .empty; - c.gtk_paned_set_start_child(@ptrCast(self.paned), null); -} - -pub fn removeChild2(self: *Paned) void { - assert(!self.child2.is_empty()); - self.child2 = .empty; - c.gtk_paned_set_end_child(@ptrCast(self.paned), null); -} - pub fn splitStartPosition(self: *Paned, orientation: c.GtkOrientation) !void { _ = orientation; _ = self; diff --git a/src/apprt/gtk/Tab.zig b/src/apprt/gtk/Tab.zig index 3855a60d5..165eef38a 100644 --- a/src/apprt/gtk/Tab.zig +++ b/src/apprt/gtk/Tab.zig @@ -18,17 +18,7 @@ pub const GHOSTTY_TAB = "ghostty_tab"; pub const Child = union(enum) { surface: *Surface, paned: *Paned, - - empty, - - const Self = @This(); - - pub fn is_empty(self: Self) bool { - switch (self) { - Child.empty => return true, - else => return false, - } - } + none, }; window: *Window, @@ -163,10 +153,10 @@ pub fn removeChild(self: *Tab) void { const widget = switch (self.child) { .surface => |surface| @as(*c.GtkWidget, @ptrCast(surface.gl_area)), .paned => |paned| @as(*c.GtkWidget, @ptrCast(@alignCast(paned.paned))), - .empty => return, + .none => return, }; c.gtk_box_remove(self.box, widget); - self.child = .empty; + self.child = .none; } pub fn setChild(self: *Tab, newChild: Child) void { @@ -183,23 +173,15 @@ pub fn setChild(self: *Tab, newChild: Child) void { const widget = @as(*c.GtkWidget, @ptrCast(@alignCast(paned.paned))); c.gtk_box_append(self.box, widget); }, - .empty => return, + .none => return, } self.child = newChild; } -pub fn setChildSurface(self: *Tab, surface: *Surface, gl_area: *c.GtkWidget) !void { - c.gtk_box_append(self.box, gl_area); - - const parent = Parent{ .tab = self }; - surface.setParent(parent); - - self.child = .{ .surface = surface }; -} - fn gtkTabCloseClick(_: *c.GtkButton, ud: ?*anyopaque) callconv(.C) void { const tab: *Tab = @ptrCast(@alignCast(ud)); _ = tab; + // TODO: Fix tab closing logic log.info("tab close click\n", .{}); } diff --git a/src/apprt/gtk/Window.zig b/src/apprt/gtk/Window.zig index c6a91bd90..5b4fab1af 100644 --- a/src/apprt/gtk/Window.zig +++ b/src/apprt/gtk/Window.zig @@ -195,13 +195,26 @@ pub fn deinit(self: *Window) void { /// Add a new tab to this window. pub fn newTab(self: *Window, parentSurface: ?*CoreSurface) !void { - const tab = try Tab.create(self.app.core_app.alloc, self, parentSurface); - try self.tabs.append(self.app.core_app.alloc, tab); + const alloc = self.app.core_app.alloc; + const tab = try Tab.create(alloc, self, parentSurface); + try self.tabs.append(alloc, tab); - log.info("\n\n\nnewTab. New tabs len={}\n", .{self.tabs.items.len}); // 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). + // does not (cursor doesn't blink) unless reactivated by refocusing. +} + +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; + + // TODO: Shrink capacity? + if (tab_idx) |idx| _ = self.tabs.orderedRemove(idx) else return error.TabNotFound; + + // Deallocate the tab + self.app.core_app.alloc.destroy(tab); } /// Close the tab for the given notebook page. This will automatically @@ -215,20 +228,10 @@ fn closeTab(self: *Window, page: *c.GtkNotebookPage) 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; - // TODO: Shrink capacity? - if (tab_idx) |idx| { - _ = self.tabs.orderedRemove(idx); - } else { - log.info("tab of page {} not found in managed tabs list\n", .{page_idx}); + self.removeTab(tab) catch |err| { + log.warn("tab {} not removable: {}", .{ page_idx, err }); return; - } - // Deallocate the tab - self.app.core_app.alloc.destroy(tab); - - log.info("\n\n\ncloseTab. New tabs len={}\n", .{self.tabs.items.len}); + }; // Now remove the page c.gtk_notebook_remove_page(self.notebook, page_idx); @@ -293,8 +296,7 @@ pub fn closeSurface(self: *Window, surface: *Surface) void { defer c.g_object_unref(sibling_object); // Remove children and kill Paned. - paned.removeChild1(); - paned.removeChild2(); + paned.removeChildren(); defer alloc.destroy(paned); // Remove children from Paned we were part of. @@ -478,7 +480,6 @@ fn gtkCloseRequest(v: *c.GtkWindow, ud: ?*anyopaque) callconv(.C) bool { return true; } - log.debug("WE ARE HERE", .{}); // Setup our basic message const alert = c.gtk_message_dialog_new( self.window,