Change Percentage to u16

This commit is contained in:
Ivan Duran
2024-11-10 17:28:49 +03:00
parent ab88e159e6
commit e6df8453c6
12 changed files with 41 additions and 48 deletions

View File

@ -358,7 +358,7 @@ typedef enum {
// apprt.action.SplitDirection // apprt.action.SplitDirection
typedef struct { typedef struct {
float percent; uint16_t percent;
ghostty_action_split_direction_e direction; ghostty_action_split_direction_e direction;
} ghostty_action_split_s; } ghostty_action_split_s;
@ -701,7 +701,7 @@ void ghostty_surface_ime_point(ghostty_surface_t, double*, double*);
void ghostty_surface_request_close(ghostty_surface_t); void ghostty_surface_request_close(ghostty_surface_t);
void ghostty_surface_split(ghostty_surface_t, void ghostty_surface_split(ghostty_surface_t,
ghostty_action_split_direction_e, ghostty_action_split_direction_e,
float); uint16_t);
void ghostty_surface_split_focus(ghostty_surface_t, void ghostty_surface_split_focus(ghostty_surface_t,
ghostty_action_goto_split_e); ghostty_action_goto_split_e);
void ghostty_surface_split_resize(ghostty_surface_t, void ghostty_surface_split_resize(ghostty_surface_t,

View File

@ -476,12 +476,12 @@ class BaseTerminalController: NSWindowController,
@IBAction func splitRight(_ sender: Any) { @IBAction func splitRight(_ sender: Any) {
guard let surface = focusedSurface?.surface else { return } guard let surface = focusedSurface?.surface else { return }
ghostty.split(surface: surface, direction: GHOSTTY_SPLIT_DIRECTION_RIGHT, percent: 0.5) ghostty.split(surface: surface, direction: GHOSTTY_SPLIT_DIRECTION_RIGHT, percent: 50)
} }
@IBAction func splitDown(_ sender: Any) { @IBAction func splitDown(_ sender: Any) {
guard let surface = focusedSurface?.surface else { return } guard let surface = focusedSurface?.surface else { return }
ghostty.split(surface: surface, direction: GHOSTTY_SPLIT_DIRECTION_DOWN, percent: 0.5) ghostty.split(surface: surface, direction: GHOSTTY_SPLIT_DIRECTION_DOWN, percent: 50)
} }
@IBAction func splitZoom(_ sender: Any) { @IBAction func splitZoom(_ sender: Any) {

View File

@ -170,7 +170,7 @@ extension Ghostty {
} }
} }
func split(surface: ghostty_surface_t, direction: ghostty_action_split_direction_e, percent: Float) { func split(surface: ghostty_surface_t, direction: ghostty_action_split_direction_e, percent: UInt16) {
ghostty_surface_split(surface, direction, percent) ghostty_surface_split(surface, direction, percent)
} }

View File

@ -221,7 +221,7 @@ extension Ghostty {
guard let directionAny = notification.userInfo?["direction"] else { return } guard let directionAny = notification.userInfo?["direction"] else { return }
guard let direction = directionAny as? ghostty_action_split_direction_e else { return } guard let direction = directionAny as? ghostty_action_split_direction_e else { return }
guard let percentAny = notification.userInfo?["percent"] else { return } guard let percentAny = notification.userInfo?["percent"] else { return }
guard let percent = percentAny as? Float else { return } guard let percent = percentAny as? UInt16 else { return }
let splitDirection: SplitViewDirection let splitDirection: SplitViewDirection
let swap: Bool let swap: Bool
switch (direction) { switch (direction) {
@ -246,7 +246,7 @@ extension Ghostty {
let container = SplitNode.Container(from: leaf, direction: splitDirection, baseConfig: config) let container = SplitNode.Container(from: leaf, direction: splitDirection, baseConfig: config)
// Set the split ratio // Set the split ratio
container.split = 1.0 - CGFloat(percent) container.split = 1.0 - (CGFloat(percent) / 100.0)
// Change the parent node. This will trigger the parent to relayout our views. // Change the parent node. This will trigger the parent to relayout our views.
node = .split(container) node = .split(container)

View File

@ -963,12 +963,12 @@ extension Ghostty {
@IBAction func splitRight(_ sender: Any) { @IBAction func splitRight(_ sender: Any) {
guard let surface = self.surface else { return } guard let surface = self.surface else { return }
ghostty_surface_split(surface, GHOSTTY_SPLIT_DIRECTION_RIGHT, 0.5) ghostty_surface_split(surface, GHOSTTY_SPLIT_DIRECTION_RIGHT, 50)
} }
@IBAction func splitDown(_ sender: Any) { @IBAction func splitDown(_ sender: Any) {
guard let surface = self.surface else { return } guard let surface = self.surface else { return }
ghostty_surface_split(surface, GHOSTTY_SPLIT_DIRECTION_DOWN, 0.5) ghostty_surface_split(surface, GHOSTTY_SPLIT_DIRECTION_DOWN, 50)
} }
@objc func resetTerminal(_ sender: Any) { @objc func resetTerminal(_ sender: Any) {

View File

@ -289,7 +289,7 @@ pub const Action = union(Key) {
/// The percentage and direction to create the split /// The percentage and direction to create the split
pub const SplitDirection = extern struct { pub const SplitDirection = extern struct {
percent: f32, percent: u16,
direction: Direction, direction: Direction,
// This is made extern (c_int) to make interop easier with our embedded // This is made extern (c_int) to make interop easier with our embedded

View File

@ -1634,7 +1634,7 @@ pub const CAPI = struct {
} }
/// Request that the surface split in the given direction. /// Request that the surface split in the given direction.
export fn ghostty_surface_split(ptr: *Surface, direction: apprt.action.SplitDirection.Direction, percent: f32) void { export fn ghostty_surface_split(ptr: *Surface, direction: apprt.action.SplitDirection.Direction, percent: u16) void {
const split_direction = apprt.action.SplitDirection{ const split_direction = apprt.action.SplitDirection{
.direction = direction, .direction = direction,
.percent = percent, .percent = percent,

View File

@ -871,10 +871,10 @@ fn syncActionAccelerators(self: *App) !void {
try self.syncActionAccelerator("win.close", .{ .close_surface = {} }); try self.syncActionAccelerator("win.close", .{ .close_surface = {} });
try self.syncActionAccelerator("win.new_window", .{ .new_window = {} }); try self.syncActionAccelerator("win.new_window", .{ .new_window = {} });
try self.syncActionAccelerator("win.new_tab", .{ .new_tab = {} }); try self.syncActionAccelerator("win.new_tab", .{ .new_tab = {} });
try self.syncActionAccelerator("win.split_right", .{ .new_split = .{ .right, 0.5 } }); try self.syncActionAccelerator("win.split_right", .{ .new_split = .{ .right, 50 } });
try self.syncActionAccelerator("win.split_down", .{ .new_split = .{ .down, 0.5 } }); try self.syncActionAccelerator("win.split_down", .{ .new_split = .{ .down, 50 } });
try self.syncActionAccelerator("win.split_left", .{ .new_split = .{ .left, 0.5 } }); try self.syncActionAccelerator("win.split_left", .{ .new_split = .{ .left, 50 } });
try self.syncActionAccelerator("win.split_up", .{ .new_split = .{ .up, 0.5 } }); try self.syncActionAccelerator("win.split_up", .{ .new_split = .{ .up, 50 } });
try self.syncActionAccelerator("win.copy", .{ .copy_to_clipboard = {} }); try self.syncActionAccelerator("win.copy", .{ .copy_to_clipboard = {} });
try self.syncActionAccelerator("win.paste", .{ .paste_from_clipboard = {} }); try self.syncActionAccelerator("win.paste", .{ .paste_from_clipboard = {} });
try self.syncActionAccelerator("win.reset", .{ .reset = {} }); try self.syncActionAccelerator("win.reset", .{ .reset = {} });

View File

@ -126,9 +126,10 @@ pub fn init(
// added to the paned. // added to the paned.
self.updateChildren(); self.updateChildren();
// Skip resize logic if percent is 0.5 (this is the default behavior) // Skip resize logic if percent is 50 (this is the default behavior)
if (new_split.percent != 0.5) { if (new_split.percent != 50) {
const allocation = sibling.size; const allocation = sibling.size;
const split_percentage: f32 = @as(f32, @floatFromInt(new_split.percent)) / 100;
const total_surface_size: f32 = switch (self.orientation) { const total_surface_size: f32 = switch (self.orientation) {
.horizontal => @floatFromInt(allocation.width), .horizontal => @floatFromInt(allocation.width),
.vertical => @floatFromInt(allocation.height), .vertical => @floatFromInt(allocation.height),
@ -136,8 +137,8 @@ pub fn init(
// percentage to apply based on direction // percentage to apply based on direction
const pct = switch (new_split.direction) { const pct = switch (new_split.direction) {
.right, .down => 1 - new_split.percent, .right, .down => 1 - split_percentage,
.left, .up => new_split.percent, .left, .up => split_percentage,
}; };
const divider_position = @as(c_int, @intFromFloat(total_surface_size * pct)); const divider_position = @as(c_int, @intFromFloat(total_surface_size * pct));

View File

@ -807,7 +807,7 @@ fn gtkActionSplitRight(
) callconv(.C) void { ) callconv(.C) void {
const self: *Window = @ptrCast(@alignCast(ud orelse return)); const self: *Window = @ptrCast(@alignCast(ud orelse return));
const surface = self.actionSurface() orelse return; const surface = self.actionSurface() orelse return;
_ = surface.performBindingAction(.{ .new_split = .{ .right, 0.5 } }) catch |err| { _ = surface.performBindingAction(.{ .new_split = .{ .right, 50 } }) catch |err| {
log.warn("error performing binding action error={}", .{err}); log.warn("error performing binding action error={}", .{err});
return; return;
}; };
@ -820,7 +820,7 @@ fn gtkActionSplitDown(
) callconv(.C) void { ) callconv(.C) void {
const self: *Window = @ptrCast(@alignCast(ud orelse return)); const self: *Window = @ptrCast(@alignCast(ud orelse return));
const surface = self.actionSurface() orelse return; const surface = self.actionSurface() orelse return;
_ = surface.performBindingAction(.{ .new_split = .{ .down, 0.5 } }) catch |err| { _ = surface.performBindingAction(.{ .new_split = .{ .down, 50 } }) catch |err| {
log.warn("error performing binding action error={}", .{err}); log.warn("error performing binding action error={}", .{err});
return; return;
}; };
@ -833,7 +833,7 @@ fn gtkActionSplitLeft(
) callconv(.C) void { ) callconv(.C) void {
const self: *Window = @ptrCast(@alignCast(ud orelse return)); const self: *Window = @ptrCast(@alignCast(ud orelse return));
const surface = self.actionSurface() orelse return; const surface = self.actionSurface() orelse return;
_ = surface.performBindingAction(.{ .new_split = .{ .left, 0.5 } }) catch |err| { _ = surface.performBindingAction(.{ .new_split = .{ .left, 50 } }) catch |err| {
log.warn("error performing binding action error={}", .{err}); log.warn("error performing binding action error={}", .{err});
return; return;
}; };
@ -846,7 +846,7 @@ fn gtkActionSplitUp(
) callconv(.C) void { ) callconv(.C) void {
const self: *Window = @ptrCast(@alignCast(ud orelse return)); const self: *Window = @ptrCast(@alignCast(ud orelse return));
const surface = self.actionSurface() orelse return; const surface = self.actionSurface() orelse return;
_ = surface.performBindingAction(.{ .new_split = .{ .up, 0.5 } }) catch |err| { _ = surface.performBindingAction(.{ .new_split = .{ .up, 50 } }) catch |err| {
log.warn("error performing binding action error={}", .{err}); log.warn("error performing binding action error={}", .{err});
return; return;
}; };

View File

@ -1950,12 +1950,12 @@ pub fn default(alloc_gpa: Allocator) Allocator.Error!Config {
try result.keybind.set.put( try result.keybind.set.put(
alloc, alloc,
.{ .key = .{ .translated = .o }, .mods = .{ .ctrl = true, .shift = true } }, .{ .key = .{ .translated = .o }, .mods = .{ .ctrl = true, .shift = true } },
.{ .new_split = .{ .right, 0.5 } }, .{ .new_split = .{ .right, 50 } },
); );
try result.keybind.set.put( try result.keybind.set.put(
alloc, alloc,
.{ .key = .{ .translated = .e }, .mods = .{ .ctrl = true, .shift = true } }, .{ .key = .{ .translated = .e }, .mods = .{ .ctrl = true, .shift = true } },
.{ .new_split = .{ .down, 0.5 } }, .{ .new_split = .{ .down, 50 } },
); );
try result.keybind.set.put( try result.keybind.set.put(
alloc, alloc,
@ -2213,12 +2213,12 @@ pub fn default(alloc_gpa: Allocator) Allocator.Error!Config {
try result.keybind.set.put( try result.keybind.set.put(
alloc, alloc,
.{ .key = .{ .translated = .d }, .mods = .{ .super = true } }, .{ .key = .{ .translated = .d }, .mods = .{ .super = true } },
.{ .new_split = .{ .right, 0.5 } }, .{ .new_split = .{ .right, 50 } },
); );
try result.keybind.set.put( try result.keybind.set.put(
alloc, alloc,
.{ .key = .{ .translated = .d }, .mods = .{ .super = true, .shift = true } }, .{ .key = .{ .translated = .d }, .mods = .{ .super = true, .shift = true } },
.{ .new_split = .{ .down, 0.5 } }, .{ .new_split = .{ .down, 50 } },
); );
try result.keybind.set.put( try result.keybind.set.put(
alloc, alloc,

View File

@ -453,7 +453,7 @@ pub const Action = union(enum) {
auto, // splits along the larger direction auto, // splits along the larger direction
}; };
pub const Percentage = f32; pub const Percentage = u16;
pub const SplitParameter = struct { pub const SplitParameter = struct {
SplitDirection, SplitDirection,
@ -499,23 +499,23 @@ pub const Action = union(enum) {
} }
fn parseInt(comptime T: type, value: []const u8) !T { fn parseInt(comptime T: type, value: []const u8) !T {
return std.fmt.parseInt(T, value, 10) catch return Error.InvalidFormat;
}
fn parseFloat(comptime T: type, value: []const u8) !T {
return switch (T) { return switch (T) {
Action.Percentage => blk: { Action.Percentage => blk: {
if (value.len < 2) return Error.InvalidFormat; if (value.len < 2) return Error.InvalidFormat;
if (value[value.len - 1] != '%') return Error.InvalidFormat; if (value[value.len - 1] != '%') return Error.InvalidFormat;
const percent = value[0 .. value.len - 1]; const percent = value[0 .. value.len - 1];
const parsed_percent = std.fmt.parseFloat(f32, percent) catch return Error.InvalidFormat; const parsed_percent = std.fmt.parseInt(u16, percent, 10) catch return Error.InvalidFormat;
const clamped_percent: f32 = @min(@max(parsed_percent / 100.0, 0.0), 1.0); const clamped_percent: u16 = @min(parsed_percent, 100);
break :blk clamped_percent; break :blk clamped_percent;
}, },
else => std.fmt.parseFloat(T, value) catch return Error.InvalidFormat, else => std.fmt.parseInt(T, value, 10) catch return Error.InvalidFormat,
}; };
} }
fn parseFloat(comptime T: type, value: []const u8) !T {
return std.fmt.parseFloat(T, value) catch return Error.InvalidFormat;
}
fn parseParameter( fn parseParameter(
comptime field: std.builtin.Type.UnionField, comptime field: std.builtin.Type.UnionField,
param: []const u8, param: []const u8,
@ -825,13 +825,6 @@ pub const Action = union(enum) {
@as(u64, @bitCast(field)), @as(u64, @bitCast(field)),
), ),
// Handle SplitParameter specifically
Action.SplitParameter => {
std.hash.autoHash(hasher, field[0]);
const bits = @as(u32, @bitCast(field[1]));
std.hash.autoHash(hasher, bits);
},
// Everything else automatically handle. // Everything else automatically handle.
else => std.hash.autoHashStrat( else => std.hash.autoHashStrat(
hasher, hasher,
@ -1714,7 +1707,7 @@ test "parse: action with enum" {
const binding = try parseSingle("a=new_split:right,50%"); const binding = try parseSingle("a=new_split:right,50%");
try testing.expect(binding.action == .new_split); try testing.expect(binding.action == .new_split);
try testing.expectEqual(Action.SplitDirection.right, binding.action.new_split[0]); try testing.expectEqual(Action.SplitDirection.right, binding.action.new_split[0]);
try testing.expectEqual(@as(f32, 0.5), binding.action.new_split[1]); try testing.expectEqual(@as(u16, 50), binding.action.new_split[1]);
} }
// missing unit % // missing unit %
@ -1725,10 +1718,9 @@ test "parse: action with enum" {
// clamping value // clamping value
{ {
var binding = try parseSingle("a=new_split:right,150%"); const binding = try parseSingle("a=new_split:right,150%");
try testing.expectEqual(@as(f32, 1.0), binding.action.new_split[1]); try testing.expectEqual(@as(u16, 100), binding.action.new_split[1]);
binding = try parseSingle("a=new_split:right,-50%"); try testing.expectError(Error.InvalidFormat, parseSingle("a=new_split:right,-50%"));
try testing.expectEqual(@as(f32, 0.0), binding.action.new_split[1]);
} }
} }