gtk: refator Parent/Tab/Paned and how they interact

This commit is contained in:
Thorsten Ball
2023-10-26 06:47:48 +02:00
committed by Mitchell Hashimoto
parent 9ddf097a03
commit 790cd84203
3 changed files with 35 additions and 52 deletions

View File

@ -185,7 +185,7 @@ pub fn removeChildren(self: *Paned) void {
self.removeChildInPosition(.end); self.removeChildInPosition(.end);
} }
pub fn removeChildInPosition(self: *Paned, position: Position) void { fn removeChildInPosition(self: *Paned, position: Position) void {
switch (position) { switch (position) {
.start => { .start => {
assert(self.child1 != .none); 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); assert(self.child1 == .none);
const parent = Parent{ .paned = .{ self, .start } }; const widget = child.widget() orelse return;
self.child1 = child; c.gtk_paned_set_start_child(@ptrCast(self.paned), widget);
switch (child) { self.child1 = child;
.none => return, child.setParent(.{ .paned = .{ self, .start } });
.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));
},
}
} }
pub fn addChild2(self: *Paned, child: Child) void { fn addChild2(self: *Paned, child: Child) void {
assert(self.child2 == .none); assert(self.child2 == .none);
const parent = Parent{ .paned = .{ self, .end } }; const widget = child.widget() orelse return;
self.child2 = child; c.gtk_paned_set_end_child(@ptrCast(self.paned), widget);
switch (child) { self.child2 = child;
.none => return, child.setParent(.{ .paned = .{ self, .end } });
.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));
},
}
} }

View File

@ -144,34 +144,18 @@ pub fn init(self: *Tab, window: *Window, parent_: ?*CoreSurface) !void {
} }
pub fn removeChild(self: *Tab) void { pub fn removeChild(self: *Tab) void {
// Remove old child from box. const widget = self.child.widget() orelse return;
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,
};
c.gtk_box_remove(self.box, widget); c.gtk_box_remove(self.box, widget);
self.child = .none; self.child = .none;
} }
pub fn setChild(self: *Tab, newChild: Child) void { pub fn setChild(self: *Tab, child: Child) void {
const parent = Parent{ .tab = self }; const widget = child.widget() orelse return;
c.gtk_box_append(self.box, widget);
switch (newChild) { child.setParent(.{ .tab = self });
.surface => |surface| { self.child = child;
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;
} }
fn gtkTabCloseClick(_: *c.GtkButton, ud: ?*anyopaque) callconv(.C) void { fn gtkTabCloseClick(_: *c.GtkButton, ud: ?*anyopaque) callconv(.C) void {

View File

@ -1,6 +1,7 @@
const Surface = @import("Surface.zig"); const Surface = @import("Surface.zig");
const Paned = @import("Paned.zig"); const Paned = @import("Paned.zig");
const Tab = @import("Tab.zig"); const Tab = @import("Tab.zig");
const c = @import("c.zig");
pub const Position = enum { pub const Position = enum {
start, start,
@ -20,4 +21,20 @@ pub const Child = union(enum) {
none, none,
surface: *Surface, surface: *Surface,
paned: *Paned, 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),
};
}
}; };