From e6df8453c6fb69b3f23d8b66a711153a339425ba Mon Sep 17 00:00:00 2001 From: Ivan Duran Date: Sun, 10 Nov 2024 17:28:49 +0300 Subject: [PATCH] Change Percentage to u16 --- include/ghostty.h | 6 ++-- .../Terminal/BaseTerminalController.swift | 4 +-- macos/Sources/Ghostty/Ghostty.App.swift | 2 +- .../Ghostty/Ghostty.TerminalSplit.swift | 4 +-- .../Sources/Ghostty/SurfaceView_AppKit.swift | 4 +-- src/apprt/action.zig | 2 +- src/apprt/embedded.zig | 2 +- src/apprt/gtk/App.zig | 8 ++--- src/apprt/gtk/Split.zig | 9 +++--- src/apprt/gtk/Window.zig | 8 ++--- src/config/Config.zig | 8 ++--- src/input/Binding.zig | 32 +++++++------------ 12 files changed, 41 insertions(+), 48 deletions(-) diff --git a/include/ghostty.h b/include/ghostty.h index 2b28c6cc7..650f650b0 100644 --- a/include/ghostty.h +++ b/include/ghostty.h @@ -358,7 +358,7 @@ typedef enum { // apprt.action.SplitDirection typedef struct { - float percent; + uint16_t percent; ghostty_action_split_direction_e direction; } ghostty_action_split_s; @@ -699,9 +699,9 @@ void ghostty_surface_mouse_scroll(ghostty_surface_t, void ghostty_surface_mouse_pressure(ghostty_surface_t, uint32_t, double); void ghostty_surface_ime_point(ghostty_surface_t, double*, double*); 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, - float); + uint16_t); void ghostty_surface_split_focus(ghostty_surface_t, ghostty_action_goto_split_e); void ghostty_surface_split_resize(ghostty_surface_t, diff --git a/macos/Sources/Features/Terminal/BaseTerminalController.swift b/macos/Sources/Features/Terminal/BaseTerminalController.swift index ca03e17f7..02ad02528 100644 --- a/macos/Sources/Features/Terminal/BaseTerminalController.swift +++ b/macos/Sources/Features/Terminal/BaseTerminalController.swift @@ -476,12 +476,12 @@ class BaseTerminalController: NSWindowController, @IBAction func splitRight(_ sender: Any) { 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) { 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) { diff --git a/macos/Sources/Ghostty/Ghostty.App.swift b/macos/Sources/Ghostty/Ghostty.App.swift index 7019e9504..885212c69 100644 --- a/macos/Sources/Ghostty/Ghostty.App.swift +++ b/macos/Sources/Ghostty/Ghostty.App.swift @@ -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) } diff --git a/macos/Sources/Ghostty/Ghostty.TerminalSplit.swift b/macos/Sources/Ghostty/Ghostty.TerminalSplit.swift index 3c55e789d..b1dc5ae3b 100644 --- a/macos/Sources/Ghostty/Ghostty.TerminalSplit.swift +++ b/macos/Sources/Ghostty/Ghostty.TerminalSplit.swift @@ -221,7 +221,7 @@ extension Ghostty { guard let directionAny = notification.userInfo?["direction"] else { return } guard let direction = directionAny as? ghostty_action_split_direction_e 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 swap: Bool switch (direction) { @@ -246,7 +246,7 @@ extension Ghostty { let container = SplitNode.Container(from: leaf, direction: splitDirection, baseConfig: config) // 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. node = .split(container) diff --git a/macos/Sources/Ghostty/SurfaceView_AppKit.swift b/macos/Sources/Ghostty/SurfaceView_AppKit.swift index 40dea9784..d5227d00d 100644 --- a/macos/Sources/Ghostty/SurfaceView_AppKit.swift +++ b/macos/Sources/Ghostty/SurfaceView_AppKit.swift @@ -963,12 +963,12 @@ extension Ghostty { @IBAction func splitRight(_ sender: Any) { 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) { 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) { diff --git a/src/apprt/action.zig b/src/apprt/action.zig index e96da8081..f4d9cfb20 100644 --- a/src/apprt/action.zig +++ b/src/apprt/action.zig @@ -289,7 +289,7 @@ pub const Action = union(Key) { /// The percentage and direction to create the split pub const SplitDirection = extern struct { - percent: f32, + percent: u16, direction: Direction, // This is made extern (c_int) to make interop easier with our embedded diff --git a/src/apprt/embedded.zig b/src/apprt/embedded.zig index ce6e6491f..7f722f08a 100644 --- a/src/apprt/embedded.zig +++ b/src/apprt/embedded.zig @@ -1634,7 +1634,7 @@ pub const CAPI = struct { } /// 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{ .direction = direction, .percent = percent, diff --git a/src/apprt/gtk/App.zig b/src/apprt/gtk/App.zig index d6fc3324e..b531a7d41 100644 --- a/src/apprt/gtk/App.zig +++ b/src/apprt/gtk/App.zig @@ -871,10 +871,10 @@ fn syncActionAccelerators(self: *App) !void { try self.syncActionAccelerator("win.close", .{ .close_surface = {} }); try self.syncActionAccelerator("win.new_window", .{ .new_window = {} }); try self.syncActionAccelerator("win.new_tab", .{ .new_tab = {} }); - try self.syncActionAccelerator("win.split_right", .{ .new_split = .{ .right, 0.5 } }); - try self.syncActionAccelerator("win.split_down", .{ .new_split = .{ .down, 0.5 } }); - try self.syncActionAccelerator("win.split_left", .{ .new_split = .{ .left, 0.5 } }); - try self.syncActionAccelerator("win.split_up", .{ .new_split = .{ .up, 0.5 } }); + try self.syncActionAccelerator("win.split_right", .{ .new_split = .{ .right, 50 } }); + try self.syncActionAccelerator("win.split_down", .{ .new_split = .{ .down, 50 } }); + try self.syncActionAccelerator("win.split_left", .{ .new_split = .{ .left, 50 } }); + try self.syncActionAccelerator("win.split_up", .{ .new_split = .{ .up, 50 } }); try self.syncActionAccelerator("win.copy", .{ .copy_to_clipboard = {} }); try self.syncActionAccelerator("win.paste", .{ .paste_from_clipboard = {} }); try self.syncActionAccelerator("win.reset", .{ .reset = {} }); diff --git a/src/apprt/gtk/Split.zig b/src/apprt/gtk/Split.zig index 91a4ff45e..92e3989b9 100644 --- a/src/apprt/gtk/Split.zig +++ b/src/apprt/gtk/Split.zig @@ -126,9 +126,10 @@ pub fn init( // added to the paned. self.updateChildren(); - // Skip resize logic if percent is 0.5 (this is the default behavior) - if (new_split.percent != 0.5) { + // Skip resize logic if percent is 50 (this is the default behavior) + if (new_split.percent != 50) { const allocation = sibling.size; + const split_percentage: f32 = @as(f32, @floatFromInt(new_split.percent)) / 100; const total_surface_size: f32 = switch (self.orientation) { .horizontal => @floatFromInt(allocation.width), .vertical => @floatFromInt(allocation.height), @@ -136,8 +137,8 @@ pub fn init( // percentage to apply based on direction const pct = switch (new_split.direction) { - .right, .down => 1 - new_split.percent, - .left, .up => new_split.percent, + .right, .down => 1 - split_percentage, + .left, .up => split_percentage, }; const divider_position = @as(c_int, @intFromFloat(total_surface_size * pct)); diff --git a/src/apprt/gtk/Window.zig b/src/apprt/gtk/Window.zig index 971e3cc8f..17901d467 100644 --- a/src/apprt/gtk/Window.zig +++ b/src/apprt/gtk/Window.zig @@ -807,7 +807,7 @@ fn gtkActionSplitRight( ) callconv(.C) void { const self: *Window = @ptrCast(@alignCast(ud 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}); return; }; @@ -820,7 +820,7 @@ fn gtkActionSplitDown( ) callconv(.C) void { const self: *Window = @ptrCast(@alignCast(ud 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}); return; }; @@ -833,7 +833,7 @@ fn gtkActionSplitLeft( ) callconv(.C) void { const self: *Window = @ptrCast(@alignCast(ud 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}); return; }; @@ -846,7 +846,7 @@ fn gtkActionSplitUp( ) callconv(.C) void { const self: *Window = @ptrCast(@alignCast(ud 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}); return; }; diff --git a/src/config/Config.zig b/src/config/Config.zig index 90e7de7bd..5f39cb524 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -1950,12 +1950,12 @@ pub fn default(alloc_gpa: Allocator) Allocator.Error!Config { try result.keybind.set.put( alloc, .{ .key = .{ .translated = .o }, .mods = .{ .ctrl = true, .shift = true } }, - .{ .new_split = .{ .right, 0.5 } }, + .{ .new_split = .{ .right, 50 } }, ); try result.keybind.set.put( alloc, .{ .key = .{ .translated = .e }, .mods = .{ .ctrl = true, .shift = true } }, - .{ .new_split = .{ .down, 0.5 } }, + .{ .new_split = .{ .down, 50 } }, ); try result.keybind.set.put( alloc, @@ -2213,12 +2213,12 @@ pub fn default(alloc_gpa: Allocator) Allocator.Error!Config { try result.keybind.set.put( alloc, .{ .key = .{ .translated = .d }, .mods = .{ .super = true } }, - .{ .new_split = .{ .right, 0.5 } }, + .{ .new_split = .{ .right, 50 } }, ); try result.keybind.set.put( alloc, .{ .key = .{ .translated = .d }, .mods = .{ .super = true, .shift = true } }, - .{ .new_split = .{ .down, 0.5 } }, + .{ .new_split = .{ .down, 50 } }, ); try result.keybind.set.put( alloc, diff --git a/src/input/Binding.zig b/src/input/Binding.zig index 7094a5a61..21dbb0fbf 100644 --- a/src/input/Binding.zig +++ b/src/input/Binding.zig @@ -453,7 +453,7 @@ pub const Action = union(enum) { auto, // splits along the larger direction }; - pub const Percentage = f32; + pub const Percentage = u16; pub const SplitParameter = struct { SplitDirection, @@ -499,23 +499,23 @@ pub const Action = union(enum) { } 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) { Action.Percentage => blk: { if (value.len < 2) return Error.InvalidFormat; if (value[value.len - 1] != '%') return Error.InvalidFormat; const percent = value[0 .. value.len - 1]; - const parsed_percent = std.fmt.parseFloat(f32, percent) catch return Error.InvalidFormat; - const clamped_percent: f32 = @min(@max(parsed_percent / 100.0, 0.0), 1.0); + const parsed_percent = std.fmt.parseInt(u16, percent, 10) catch return Error.InvalidFormat; + const clamped_percent: u16 = @min(parsed_percent, 100); 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( comptime field: std.builtin.Type.UnionField, param: []const u8, @@ -825,13 +825,6 @@ pub const Action = union(enum) { @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. else => std.hash.autoHashStrat( hasher, @@ -1714,7 +1707,7 @@ test "parse: action with enum" { const binding = try parseSingle("a=new_split:right,50%"); try testing.expect(binding.action == .new_split); 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 % @@ -1725,10 +1718,9 @@ test "parse: action with enum" { // clamping value { - var binding = try parseSingle("a=new_split:right,150%"); - try testing.expectEqual(@as(f32, 1.0), binding.action.new_split[1]); - binding = try parseSingle("a=new_split:right,-50%"); - try testing.expectEqual(@as(f32, 0.0), binding.action.new_split[1]); + const binding = try parseSingle("a=new_split:right,150%"); + try testing.expectEqual(@as(u16, 100), binding.action.new_split[1]); + try testing.expectError(Error.InvalidFormat, parseSingle("a=new_split:right,-50%")); } }