gtk: Refactor how a new split is created

This commit is contained in:
Thorsten Ball
2023-10-26 06:31:27 +02:00
committed by Mitchell Hashimoto
parent de7cbb602f
commit 9ddf097a03
2 changed files with 41 additions and 26 deletions

View File

@ -70,10 +70,9 @@ pub fn init(self: *Paned, window: *Window, sibling: *Surface, direction: input.S
const gtk_paned: *c.GtkPaned = @ptrCast(paned); const gtk_paned: *c.GtkPaned = @ptrCast(paned);
self.paned = gtk_paned; self.paned = gtk_paned;
const new_surface = try self.newSurface(sibling.tab, &sibling.core_surface); const surface = try self.newSurface(sibling.tab, &sibling.core_surface);
// This sets .parent on each surface
self.addChild1(.{ .surface = sibling }); self.addChild1(.{ .surface = sibling });
self.addChild2(.{ .surface = new_surface }); self.addChild2(.{ .surface = surface });
} }
pub fn newSurface(self: *Paned, tab: *Tab, parent_: ?*CoreSurface) !*Surface { pub fn newSurface(self: *Paned, tab: *Tab, parent_: ?*CoreSurface) !*Surface {
@ -130,6 +129,42 @@ pub fn setParent(self: *Paned, parent: Parent) void {
self.parent = parent; self.parent = parent;
} }
pub fn splitSurfaceInPosition(self: *Paned, position: Position, direction: input.SplitDirection) !void {
const child = switch (position) {
.start => self.child1,
.end => self.child2,
};
const surface: *Surface = switch (child) {
.surface => |surface| surface,
else => return,
};
// Keep explicit reference to surface gl_area before we remove it.
const object: *c.GObject = @ptrCast(surface.gl_area);
_ = c.g_object_ref(object);
defer c.g_object_unref(object);
// Keep position of divider
const parent_paned_position_before = c.gtk_paned_get_position(self.paned);
// Now remove it
self.removeChildInPosition(position);
// Create new Paned
// NOTE: We cannot use `replaceChildInPosition` here because we need to
// first remove the surface before we create a new pane.
const paned = try Paned.create(self.window.app.core_app.alloc, self.window, surface, direction);
switch (position) {
.start => self.addChild1(.{ .paned = paned }),
.end => self.addChild2(.{ .paned = paned }),
}
// Restore position
c.gtk_paned_set_position(self.paned, parent_paned_position_before);
// Focus on new surface
paned.focusSurfaceInPosition(.end);
}
pub fn replaceChildInPosition(self: *Paned, child: Child, position: Position) void { pub fn replaceChildInPosition(self: *Paned, child: Child, position: Position) void {
// Keep position of divider // Keep position of divider
const parent_paned_position_before = c.gtk_paned_get_position(self.paned); const parent_paned_position_before = c.gtk_paned_get_position(self.paned);

View File

@ -361,30 +361,10 @@ pub fn newSplit(self: *Surface, direction: input.SplitDirection) !void {
switch (self.parent) { switch (self.parent) {
.none => return, .none => return,
.paned => |parent_paned_tuple| { .paned => |parent_paned_tuple| {
// Keep explicit reference to our gl_area before we remove ourselves. const paned = parent_paned_tuple[0];
const sibling_object: *c.GObject = @ptrCast(self.gl_area); const position = parent_paned_tuple[1];
_ = c.g_object_ref(sibling_object);
defer c.g_object_unref(sibling_object);
const parent_paned = parent_paned_tuple[0]; try paned.splitSurfaceInPosition(position, direction);
const parent_position = parent_paned_tuple[1];
// Keep position of divider
const parent_paned_position_before = c.gtk_paned_get_position(parent_paned.paned);
// Now remove ourselves from parent
parent_paned.removeChildInPosition(parent_position);
const paned = try Paned.create(self.app.core_app.alloc, self.window, self, direction);
// Add new split-paned
switch (parent_position) {
.start => parent_paned.addChild1(.{ .paned = paned }),
.end => parent_paned.addChild2(.{ .paned = paned }),
}
// Restore position
c.gtk_paned_set_position(parent_paned.paned, parent_paned_position_before);
// Focus on new surface
paned.focusSurfaceInPosition(.end);
}, },
.tab => |tab| { .tab => |tab| {
tab.removeChild(); tab.removeChild();