diff --git a/src/apprt/gtk/Paned.zig b/src/apprt/gtk/Paned.zig index 5bd9a1159..29d9a0cd5 100644 --- a/src/apprt/gtk/Paned.zig +++ b/src/apprt/gtk/Paned.zig @@ -185,7 +185,7 @@ pub fn removeChildren(self: *Paned) void { self.removeChildInPosition(.end); } -pub fn removeChildInPosition(self: *Paned, position: Position) void { +fn removeChildInPosition(self: *Paned, position: Position) void { switch (position) { .start => { assert(self.child1 != .none); @@ -200,40 +200,22 @@ pub fn removeChildInPosition(self: *Paned, position: Position) void { } } -pub fn addChild1(self: *Paned, child: Child) void { +fn addChild1(self: *Paned, child: Child) void { assert(self.child1 == .none); - const parent = Parent{ .paned = .{ self, .start } }; - self.child1 = child; + const widget = child.widget() orelse return; + c.gtk_paned_set_start_child(@ptrCast(self.paned), widget); - switch (child) { - .none => return, - .paned => |paned| { - paned.setParent(parent); - c.gtk_paned_set_start_child(@ptrCast(self.paned), @ptrCast(@alignCast(paned.paned))); - }, - .surface => |surface| { - surface.setParent(parent); - c.gtk_paned_set_start_child(@ptrCast(self.paned), @ptrCast(surface.gl_area)); - }, - } + self.child1 = child; + child.setParent(.{ .paned = .{ self, .start } }); } -pub fn addChild2(self: *Paned, child: Child) void { +fn addChild2(self: *Paned, child: Child) void { assert(self.child2 == .none); - const parent = Parent{ .paned = .{ self, .end } }; - self.child2 = child; + const widget = child.widget() orelse return; + c.gtk_paned_set_end_child(@ptrCast(self.paned), widget); - switch (child) { - .none => return, - .paned => |paned| { - paned.setParent(parent); - c.gtk_paned_set_end_child(@ptrCast(self.paned), @ptrCast(@alignCast(paned.paned))); - }, - .surface => |surface| { - surface.setParent(parent); - c.gtk_paned_set_end_child(@ptrCast(self.paned), @ptrCast(surface.gl_area)); - }, - } + self.child2 = child; + child.setParent(.{ .paned = .{ self, .end } }); } diff --git a/src/apprt/gtk/Tab.zig b/src/apprt/gtk/Tab.zig index e076ebee2..c77819805 100644 --- a/src/apprt/gtk/Tab.zig +++ b/src/apprt/gtk/Tab.zig @@ -144,34 +144,18 @@ pub fn init(self: *Tab, window: *Window, parent_: ?*CoreSurface) !void { } pub fn removeChild(self: *Tab) void { - // Remove old child from box. - const widget = switch (self.child) { - .surface => |surface| @as(*c.GtkWidget, @ptrCast(surface.gl_area)), - .paned => |paned| @as(*c.GtkWidget, @ptrCast(@alignCast(paned.paned))), - .none => return, - }; + const widget = self.child.widget() orelse return; c.gtk_box_remove(self.box, widget); + self.child = .none; } -pub fn setChild(self: *Tab, newChild: Child) void { - const parent = Parent{ .tab = self }; +pub fn setChild(self: *Tab, child: Child) void { + const widget = child.widget() orelse return; + c.gtk_box_append(self.box, widget); - switch (newChild) { - .surface => |surface| { - surface.setParent(parent); - const widget = @as(*c.GtkWidget, @ptrCast(surface.gl_area)); - c.gtk_box_append(self.box, widget); - }, - .paned => |paned| { - paned.parent = parent; - const widget = @as(*c.GtkWidget, @ptrCast(@alignCast(paned.paned))); - c.gtk_box_append(self.box, widget); - }, - .none => return, - } - - self.child = newChild; + child.setParent(.{ .tab = self }); + self.child = child; } fn gtkTabCloseClick(_: *c.GtkButton, ud: ?*anyopaque) callconv(.C) void { diff --git a/src/apprt/gtk/relation.zig b/src/apprt/gtk/relation.zig index 8d0e8b5cf..b14b0b86e 100644 --- a/src/apprt/gtk/relation.zig +++ b/src/apprt/gtk/relation.zig @@ -1,6 +1,7 @@ const Surface = @import("Surface.zig"); const Paned = @import("Paned.zig"); const Tab = @import("Tab.zig"); +const c = @import("c.zig"); pub const Position = enum { start, @@ -20,4 +21,20 @@ pub const Child = union(enum) { none, surface: *Surface, paned: *Paned, + + pub fn setParent(self: Child, parent: Parent) void { + switch (self) { + .none => return, + .surface => |surface| surface.setParent(parent), + .paned => |paned| paned.setParent(parent), + } + } + + pub fn widget(self: Child) ?*c.GtkWidget { + return switch (self) { + .none => null, + .paned => |paned| @ptrCast(@alignCast(paned.paned)), + .surface => |surface| @ptrCast(surface.gl_area), + }; + } };