diff --git a/src/apprt/gtk/Paned.zig b/src/apprt/gtk/Paned.zig index 1f10b1cc4..5bd9a1159 100644 --- a/src/apprt/gtk/Paned.zig +++ b/src/apprt/gtk/Paned.zig @@ -70,10 +70,9 @@ pub fn init(self: *Paned, window: *Window, sibling: *Surface, direction: input.S const gtk_paned: *c.GtkPaned = @ptrCast(paned); self.paned = gtk_paned; - const new_surface = try self.newSurface(sibling.tab, &sibling.core_surface); - // This sets .parent on each surface + const surface = try self.newSurface(sibling.tab, &sibling.core_surface); self.addChild1(.{ .surface = sibling }); - self.addChild2(.{ .surface = new_surface }); + self.addChild2(.{ .surface = 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; } +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 { // Keep position of divider const parent_paned_position_before = c.gtk_paned_get_position(self.paned); diff --git a/src/apprt/gtk/Surface.zig b/src/apprt/gtk/Surface.zig index ba7b99275..b84ddc898 100644 --- a/src/apprt/gtk/Surface.zig +++ b/src/apprt/gtk/Surface.zig @@ -361,30 +361,10 @@ pub fn newSplit(self: *Surface, direction: input.SplitDirection) !void { switch (self.parent) { .none => return, .paned => |parent_paned_tuple| { - // Keep explicit reference to our gl_area before we remove ourselves. - const sibling_object: *c.GObject = @ptrCast(self.gl_area); - _ = c.g_object_ref(sibling_object); - defer c.g_object_unref(sibling_object); + const paned = parent_paned_tuple[0]; + const position = parent_paned_tuple[1]; - const parent_paned = parent_paned_tuple[0]; - 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); + try paned.splitSurfaceInPosition(position, direction); }, .tab => |tab| { tab.removeChild();