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.
pull/8402/head
Friedrich Stoltzfus 2025-06-12 12:47:03 -04:00 committed by Mitchell Hashimoto
parent 63cd424678
commit 17f7f204e1
3 changed files with 66 additions and 44 deletions

View File

@ -451,11 +451,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

View File

@ -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 {
}
}
}
}
}

View File

@ -7200,30 +7200,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(),
};
}