mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-09-05 19:08:17 +00:00
apprt/gtk-ng: resizeSplit action
This commit is contained in:
@@ -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 => {},
|
||||
|
@@ -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 {
|
||||
|
@@ -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;
|
||||
|
Reference in New Issue
Block a user