From 8a5d4cd19628ffced0e9cfca025224438782bb54 Mon Sep 17 00:00:00 2001 From: Friedrich Stoltzfus Date: Thu, 12 Jun 2025 12:47:03 -0400 Subject: [PATCH] macOS: update zig and c structs for quick terminal size Applying the feedback given by @pluiedev to use an enum to specify the type of quick terminal size configuration given (pixels or percentage). Updated the Swift code to work with the enum as well. --- include/ghostty.h | 17 +++++-- .../QuickTerminal/QuickTerminalSize.swift | 44 +++++++++-------- src/config/Config.zig | 49 +++++++++++-------- 3 files changed, 66 insertions(+), 44 deletions(-) diff --git a/include/ghostty.h b/include/ghostty.h index a2f2c068e..d24d0ee28 100644 --- a/include/ghostty.h +++ b/include/ghostty.h @@ -446,11 +446,20 @@ typedef struct { } ghostty_config_palette_s; // config.QuickTerminalSize +typedef enum { + GHOSTTY_QUICK_TERMINAL_SIZE_NONE, + GHOSTTY_QUICK_TERMINAL_SIZE_PERCENTAGE, + GHOSTTY_QUICK_TERMINAL_SIZE_PIXELS, +} ghostty_quick_terminal_size_e; + typedef struct { - uint8_t primary_type; // 0 = none, 1 = percentage, 2 = pixels - float primary_value; - uint8_t secondary_type; // 0 = none, 1 = percentage, 2 = pixels - float secondary_value; + ghostty_quick_terminal_size_e type; + uint32_t value; +} ghostty_quick_terminal_size_u; + +typedef struct { + ghostty_quick_terminal_size_u primary; + ghostty_quick_terminal_size_u secondary; } ghostty_config_quick_terminal_size_s; // apprt.Target.Key diff --git a/macos/Sources/Features/QuickTerminal/QuickTerminalSize.swift b/macos/Sources/Features/QuickTerminal/QuickTerminalSize.swift index c86f15b84..b2d39e8eb 100644 --- a/macos/Sources/Features/QuickTerminal/QuickTerminalSize.swift +++ b/macos/Sources/Features/QuickTerminal/QuickTerminalSize.swift @@ -1,35 +1,39 @@ -import Cocoa import GhosttyKit struct QuickTerminalSize { let primary: Size? let secondary: Size? - + init(primary: Size? = nil, secondary: Size? = nil) { self.primary = primary self.secondary = secondary } - + init(from cStruct: ghostty_config_quick_terminal_size_s) { - self.primary = cStruct.primary_type == 0 ? nil : Size(type: cStruct.primary_type, value: cStruct.primary_value) - self.secondary = cStruct.secondary_type == 0 ? nil : Size(type: cStruct.secondary_type, value: cStruct.secondary_value) + self.primary = Size(from: cStruct.primary) + self.secondary = Size(from: cStruct.secondary) } - + enum Size { case percentage(Float) case pixels(UInt32) - - init?(type: UInt8, value: Float) { - switch type { - case 1: - self = .percentage(value) - case 2: - self = .pixels(UInt32(value)) + + init?(from cStruct: ghostty_quick_terminal_size_u) { + switch cStruct.type { + case GHOSTTY_QUICK_TERMINAL_SIZE_NONE: + return nil + case GHOSTTY_QUICK_TERMINAL_SIZE_PERCENTAGE: + let floatValue = withUnsafePointer(to: cStruct.value) { ptr in + ptr.withMemoryRebound(to: Float.self, capacity: 1) { $0.pointee } + } + self = .percentage(floatValue) + case GHOSTTY_QUICK_TERMINAL_SIZE_PIXELS: + self = .pixels(cStruct.value) default: return nil } } - + func toPixels(parentDimension: CGFloat) -> CGFloat { switch self { case .percentage(let value): @@ -39,28 +43,28 @@ struct QuickTerminalSize { } } } - + struct Dimensions { let width: CGFloat let height: CGFloat } - + func calculate(position: QuickTerminalPosition, screenDimensions: CGSize) -> Dimensions { let dims = Dimensions(width: screenDimensions.width, height: screenDimensions.height) - + switch position { case .left, .right: return Dimensions( width: primary?.toPixels(parentDimension: dims.width) ?? 400, height: secondary?.toPixels(parentDimension: dims.height) ?? dims.height ) - + case .top, .bottom: return Dimensions( width: secondary?.toPixels(parentDimension: dims.width) ?? dims.width, height: primary?.toPixels(parentDimension: dims.height) ?? 400 ) - + case .center: if dims.width >= dims.height { // Landscape @@ -77,4 +81,4 @@ struct QuickTerminalSize { } } } -} \ No newline at end of file +} diff --git a/src/config/Config.zig b/src/config/Config.zig index 3169ba3e8..dcc39b3f6 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -6707,30 +6707,39 @@ pub const QuickTerminalSize = struct { /// C API structure for QuickTerminalSize pub const C = extern struct { - primary_type: u8, // 0 = none, 1 = percentage, 2 = pixels - primary_value: f32, - secondary_type: u8, // 0 = none, 1 = percentage, 2 = pixels - secondary_value: f32, + primary: CSize, + secondary: CSize, + }; + + pub const CSize = extern struct { + type: Type, + value: u32, + + pub const Type = enum(u8) { none, percentage, pixels }; + + fn none() CSize { + return .{ .type = .none, .value = 0 }; + } + + fn percentage(v: f32) CSize { + return .{ .type = .percentage, .value = @bitCast(v) }; + } + + fn pixels(v: u32) CSize { + return .{ .type = .pixels, .value = v }; + } }; pub fn cval(self: QuickTerminalSize) C { return .{ - .primary_type = if (self.primary) |p| switch (p) { - .percentage => 1, - .pixels => 2, - } else 0, - .primary_value = if (self.primary) |p| switch (p) { - .percentage => |v| v, - .pixels => |v| @floatFromInt(v), - } else 0, - .secondary_type = if (self.secondary) |s| switch (s) { - .percentage => 1, - .pixels => 2, - } else 0, - .secondary_value = if (self.secondary) |s| switch (s) { - .percentage => |v| v, - .pixels => |v| @floatFromInt(v), - } else 0, + .primary = if (self.primary) |p| switch (p) { + .percentage => |v| CSize.percentage(v), + .pixels => |v| CSize.pixels(v), + } else CSize.none(), + .secondary = if (self.secondary) |s| switch (s) { + .percentage => |v| CSize.percentage(v), + .pixels => |v| CSize.pixels(v), + } else CSize.none(), }; }