apprt/gtk-ng: resizeSplit action

This commit is contained in:
Mitchell Hashimoto
2025-08-12 11:04:17 -07:00
parent 4afd3445c4
commit 93da59682f
3 changed files with 86 additions and 1 deletions

View File

@@ -597,6 +597,8 @@ pub const Application = extern struct {
.render => Action.render(target),
.resize_split => return Action.resizeSplit(target, value),
.ring_bell => Action.ringBell(target),
.set_title => Action.setTitle(target, value),
@@ -618,7 +620,6 @@ pub const Application = extern struct {
.prompt_title,
.inspector,
// TODO: splits
.resize_split,
.toggle_split_zoom,
=> {
log.warn("unimplemented action={}", .{action});
@@ -2003,6 +2004,43 @@ const Action = struct {
}
}
pub fn resizeSplit(
target: apprt.Target,
value: apprt.action.ResizeSplit,
) bool {
switch (target) {
.app => {
log.warn("resize_split to app is unexpected", .{});
return false;
},
.surface => |core| {
const surface = core.rt_surface.surface;
const tree = ext.getAncestor(
SplitTree,
surface.as(gtk.Widget),
) orelse {
log.warn("surface is not in a split tree, ignoring goto_split", .{});
return false;
};
return tree.resize(
switch (value.direction) {
.up => .up,
.down => .down,
.left => .left,
.right => .right,
},
value.amount,
) catch |err| switch (err) {
error.OutOfMemory => {
log.warn("unable to resize split, out of memory", .{});
return false;
},
};
},
}
}
pub fn ringBell(target: apprt.Target) void {
switch (target) {
.app => {},

View File

@@ -261,6 +261,51 @@ pub const SplitTree = extern struct {
self.setTree(&new_tree);
}
pub fn resize(
self: *Self,
direction: Surface.Tree.Split.Direction,
amount: u16,
) Allocator.Error!bool {
// Avoid useless work
if (amount == 0) return false;
const old_tree = self.getTree() orelse return false;
const active = self.getActiveSurfaceHandle() orelse return false;
// Get all our dimensions we're going to need to turn our
// amount into a percentage.
const priv = self.private();
const width = priv.tree_bin.as(gtk.Widget).getWidth();
const height = priv.tree_bin.as(gtk.Widget).getHeight();
if (width == 0 or height == 0) return false;
const width_f64: f64 = @floatFromInt(width);
const height_f64: f64 = @floatFromInt(height);
const amount_f64: f64 = @floatFromInt(amount);
// Get our ratio and use positive/neg for directions.
const ratio: f64 = switch (direction) {
.right => amount_f64 / width_f64,
.left => -(amount_f64 / width_f64),
.down => amount_f64 / height_f64,
.up => -(amount_f64 / height_f64),
};
const layout: Surface.Tree.Split.Layout = switch (direction) {
.left, .right => .horizontal,
.up, .down => .vertical,
};
var new_tree = try old_tree.resize(
Application.default().allocator(),
active,
layout,
@floatCast(ratio),
);
defer new_tree.deinit();
self.setTree(&new_tree);
return true;
}
/// Move focus from the currently focused surface to the given
/// direction. Returns true if focus switched to a new surface.
pub fn goto(self: *Self, to: Surface.Tree.Goto) bool {

View File

@@ -723,6 +723,8 @@ pub fn SplitTree(comptime V: type) type {
ratio: f16,
) Allocator.Error!Self {
assert(ratio >= 0 and ratio <= 1);
assert(!std.math.isNan(ratio));
assert(!std.math.isInf(ratio));
// Fast path empty trees.
if (self.isEmpty()) return .empty;