gtk: move logic of splitting surface in a tab to Tab

This commit is contained in:
Thorsten Ball
2023-10-28 08:21:17 +02:00
committed by Mitchell Hashimoto
parent 633a3e2e85
commit 8c58bf222d
2 changed files with 28 additions and 8 deletions

View File

@ -356,7 +356,7 @@ pub fn getTitleLabel(self: *Surface) ?*c.GtkWidget {
} }
pub fn newSplit(self: *Surface, direction: input.SplitDirection) !void { pub fn newSplit(self: *Surface, direction: input.SplitDirection) !void {
log.debug("new split, direction: {}", .{direction}); log.debug("splitting surface, direction: {}", .{direction});
switch (self.parent) { switch (self.parent) {
.none => return, .none => return,
@ -367,13 +367,7 @@ pub fn newSplit(self: *Surface, direction: input.SplitDirection) !void {
try paned.splitSurfaceInPosition(position, direction); try paned.splitSurfaceInPosition(position, direction);
}, },
.tab => |tab| { .tab => |tab| {
tab.removeChild(); try tab.splitSurface(direction);
const paned = try Paned.create(self.app.core_app.alloc, self.window, self, direction);
tab.setChild(.{ .paned = paned });
// Focus on new surface
paned.focusSurfaceInPosition(.end);
}, },
} }
} }

View File

@ -4,7 +4,9 @@ const std = @import("std");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const assert = std.debug.assert; const assert = std.debug.assert;
const font = @import("../../font/main.zig"); const font = @import("../../font/main.zig");
const input = @import("../../input.zig");
const CoreSurface = @import("../../Surface.zig"); const CoreSurface = @import("../../Surface.zig");
const Paned = @import("Paned.zig"); const Paned = @import("Paned.zig");
const Parent = @import("relation.zig").Parent; const Parent = @import("relation.zig").Parent;
const Child = @import("relation.zig").Child; const Child = @import("relation.zig").Child;
@ -160,6 +162,28 @@ pub fn newSurface(self: *Tab, parent_: ?*CoreSurface) !*Surface {
return surface; return surface;
} }
/// Splits the current child surface into a Paned in given direction. Child of
/// Tab must be a Surface.
pub fn splitSurface(self: *Tab, direction: input.SplitDirection) !void {
assert(self.child == .surface);
const surface = switch (self.child) {
.surface => |s| s,
else => unreachable,
};
self.removeChild();
// Create a Paned with two Surfaces.
const paned = try Paned.create(self.window.app.core_app.alloc, self.window, surface, direction);
// Add Paned to the Tab.
self.setChild(.{ .paned = paned });
// Focus on new surface
paned.focusSurfaceInPosition(.end);
}
/// Remove the current child from the Tab. Noop if no child set.
pub fn removeChild(self: *Tab) void { pub fn removeChild(self: *Tab) void {
const widget = self.child.widget() orelse return; const widget = self.child.widget() orelse return;
c.gtk_box_remove(self.box, widget); c.gtk_box_remove(self.box, widget);
@ -167,6 +191,7 @@ pub fn removeChild(self: *Tab) void {
self.child = .none; self.child = .none;
} }
/// Sets child to given child and sets parent on child.
pub fn setChild(self: *Tab, child: Child) void { pub fn setChild(self: *Tab, child: Child) void {
const widget = child.widget() orelse return; const widget = child.widget() orelse return;
c.gtk_box_append(self.box, widget); c.gtk_box_append(self.box, widget);
@ -175,6 +200,7 @@ pub fn setChild(self: *Tab, child: Child) void {
self.child = child; self.child = child;
} }
/// Deinits tab by deiniting child if child is Paned.
pub fn deinit(self: *Tab) void { pub fn deinit(self: *Tab) void {
switch (self.child) { switch (self.child) {
.none, .surface => return, .none, .surface => return,