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);
}
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 } });
}

View File

@ -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 {

View File

@ -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),
};
}
};