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.
This commit is contained in:
Friedrich Stoltzfus
2025-06-12 12:47:03 -04:00
committed by Mitchell Hashimoto
parent 1dab2d456e
commit 8a5d4cd196
3 changed files with 66 additions and 44 deletions

View File

@ -446,11 +446,20 @@ typedef struct {
} ghostty_config_palette_s; } ghostty_config_palette_s;
// config.QuickTerminalSize // 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 { typedef struct {
uint8_t primary_type; // 0 = none, 1 = percentage, 2 = pixels ghostty_quick_terminal_size_e type;
float primary_value; uint32_t value;
uint8_t secondary_type; // 0 = none, 1 = percentage, 2 = pixels } ghostty_quick_terminal_size_u;
float secondary_value;
typedef struct {
ghostty_quick_terminal_size_u primary;
ghostty_quick_terminal_size_u secondary;
} ghostty_config_quick_terminal_size_s; } ghostty_config_quick_terminal_size_s;
// apprt.Target.Key // apprt.Target.Key

View File

@ -1,35 +1,39 @@
import Cocoa
import GhosttyKit import GhosttyKit
struct QuickTerminalSize { struct QuickTerminalSize {
let primary: Size? let primary: Size?
let secondary: Size? let secondary: Size?
init(primary: Size? = nil, secondary: Size? = nil) { init(primary: Size? = nil, secondary: Size? = nil) {
self.primary = primary self.primary = primary
self.secondary = secondary self.secondary = secondary
} }
init(from cStruct: ghostty_config_quick_terminal_size_s) { 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.primary = Size(from: cStruct.primary)
self.secondary = cStruct.secondary_type == 0 ? nil : Size(type: cStruct.secondary_type, value: cStruct.secondary_value) self.secondary = Size(from: cStruct.secondary)
} }
enum Size { enum Size {
case percentage(Float) case percentage(Float)
case pixels(UInt32) case pixels(UInt32)
init?(type: UInt8, value: Float) { init?(from cStruct: ghostty_quick_terminal_size_u) {
switch type { switch cStruct.type {
case 1: case GHOSTTY_QUICK_TERMINAL_SIZE_NONE:
self = .percentage(value) return nil
case 2: case GHOSTTY_QUICK_TERMINAL_SIZE_PERCENTAGE:
self = .pixels(UInt32(value)) 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: default:
return nil return nil
} }
} }
func toPixels(parentDimension: CGFloat) -> CGFloat { func toPixels(parentDimension: CGFloat) -> CGFloat {
switch self { switch self {
case .percentage(let value): case .percentage(let value):
@ -39,28 +43,28 @@ struct QuickTerminalSize {
} }
} }
} }
struct Dimensions { struct Dimensions {
let width: CGFloat let width: CGFloat
let height: CGFloat let height: CGFloat
} }
func calculate(position: QuickTerminalPosition, screenDimensions: CGSize) -> Dimensions { func calculate(position: QuickTerminalPosition, screenDimensions: CGSize) -> Dimensions {
let dims = Dimensions(width: screenDimensions.width, height: screenDimensions.height) let dims = Dimensions(width: screenDimensions.width, height: screenDimensions.height)
switch position { switch position {
case .left, .right: case .left, .right:
return Dimensions( return Dimensions(
width: primary?.toPixels(parentDimension: dims.width) ?? 400, width: primary?.toPixels(parentDimension: dims.width) ?? 400,
height: secondary?.toPixels(parentDimension: dims.height) ?? dims.height height: secondary?.toPixels(parentDimension: dims.height) ?? dims.height
) )
case .top, .bottom: case .top, .bottom:
return Dimensions( return Dimensions(
width: secondary?.toPixels(parentDimension: dims.width) ?? dims.width, width: secondary?.toPixels(parentDimension: dims.width) ?? dims.width,
height: primary?.toPixels(parentDimension: dims.height) ?? 400 height: primary?.toPixels(parentDimension: dims.height) ?? 400
) )
case .center: case .center:
if dims.width >= dims.height { if dims.width >= dims.height {
// Landscape // Landscape
@ -77,4 +81,4 @@ struct QuickTerminalSize {
} }
} }
} }
} }

View File

@ -6707,30 +6707,39 @@ pub const QuickTerminalSize = struct {
/// C API structure for QuickTerminalSize /// C API structure for QuickTerminalSize
pub const C = extern struct { pub const C = extern struct {
primary_type: u8, // 0 = none, 1 = percentage, 2 = pixels primary: CSize,
primary_value: f32, secondary: CSize,
secondary_type: u8, // 0 = none, 1 = percentage, 2 = pixels };
secondary_value: f32,
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 { pub fn cval(self: QuickTerminalSize) C {
return .{ return .{
.primary_type = if (self.primary) |p| switch (p) { .primary = if (self.primary) |p| switch (p) {
.percentage => 1, .percentage => |v| CSize.percentage(v),
.pixels => 2, .pixels => |v| CSize.pixels(v),
} else 0, } else CSize.none(),
.primary_value = if (self.primary) |p| switch (p) { .secondary = if (self.secondary) |s| switch (s) {
.percentage => |v| v, .percentage => |v| CSize.percentage(v),
.pixels => |v| @floatFromInt(v), .pixels => |v| CSize.pixels(v),
} else 0, } else CSize.none(),
.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,
}; };
} }