config: change macos-background-style to be enums on background-blur

pull/8801/head
Mitchell Hashimoto 2025-12-15 10:29:21 -08:00
parent 42493de098
commit bb23071166
No known key found for this signature in database
GPG Key ID: 523D5DC389D273BC
4 changed files with 109 additions and 20 deletions

View File

@ -413,12 +413,12 @@ extension Ghostty {
return v; return v;
} }
var backgroundBlurRadius: Int { var backgroundBlur: BackgroundBlur {
guard let config = self.config else { return 1 } guard let config = self.config else { return .disabled }
var v: Int = 0 var v: Int16 = 0
let key = "background-blur" let key = "background-blur"
_ = ghostty_config_get(config, &v, key, UInt(key.lengthOfBytes(using: .utf8))) _ = ghostty_config_get(config, &v, key, UInt(key.lengthOfBytes(using: .utf8)))
return v; return BackgroundBlur(fromCValue: v)
} }
var unfocusedSplitOpacity: Double { var unfocusedSplitOpacity: Double {
@ -637,6 +637,50 @@ extension Ghostty.Config {
case download case download
} }
/// Background blur configuration that maps from the C API values.
/// Positive values represent blur radius, special negative values
/// represent macOS-specific glass effects.
enum BackgroundBlur: Equatable {
case disabled
case radius(Int)
case macosGlassRegular
case macosGlassClear
init(fromCValue value: Int16) {
switch value {
case 0:
self = .disabled
case -1:
self = .macosGlassRegular
case -2:
self = .macosGlassClear
default:
self = .radius(Int(value))
}
}
var isEnabled: Bool {
switch self {
case .disabled:
return false
default:
return true
}
}
/// Returns the blur radius if applicable, nil for glass effects.
var radius: Int? {
switch self {
case .disabled:
return nil
case .radius(let r):
return r
case .macosGlassRegular, .macosGlassClear:
return nil
}
}
}
struct BellFeatures: OptionSet { struct BellFeatures: OptionSet {
let rawValue: CUnsignedInt let rawValue: CUnsignedInt

View File

@ -8338,6 +8338,8 @@ pub const AutoUpdate = enum {
pub const BackgroundBlur = union(enum) { pub const BackgroundBlur = union(enum) {
false, false,
true, true,
@"macos-glass-regular",
@"macos-glass-clear",
radius: u8, radius: u8,
pub fn parseCLI(self: *BackgroundBlur, input: ?[]const u8) !void { pub fn parseCLI(self: *BackgroundBlur, input: ?[]const u8) !void {
@ -8347,14 +8349,35 @@ pub const BackgroundBlur = union(enum) {
return; return;
}; };
self.* = if (cli.args.parseBool(input_)) |b| // Try to parse normal bools
if (b) .true else .false if (cli.args.parseBool(input_)) |b| {
else |_| self.* = if (b) .true else .false;
.{ .radius = std.fmt.parseInt( return;
u8, } else |_| {}
input_,
0, // Try to parse enums
) catch return error.InvalidValue }; if (std.meta.stringToEnum(
std.meta.Tag(BackgroundBlur),
input_,
)) |v| switch (v) {
inline else => |tag| tag: {
// We can only parse void types
const info = std.meta.fieldInfo(BackgroundBlur, tag);
if (info.type != void) break :tag;
self.* = @unionInit(
BackgroundBlur,
@tagName(tag),
{},
);
return;
},
};
self.* = .{ .radius = std.fmt.parseInt(
u8,
input_,
0,
) catch return error.InvalidValue };
} }
pub fn enabled(self: BackgroundBlur) bool { pub fn enabled(self: BackgroundBlur) bool {
@ -8365,11 +8388,16 @@ pub const BackgroundBlur = union(enum) {
}; };
} }
pub fn cval(self: BackgroundBlur) u8 { pub fn cval(self: BackgroundBlur) i16 {
return switch (self) { return switch (self) {
.false => 0, .false => 0,
.true => 20, .true => 20,
.radius => |v| v, .radius => |v| v,
// I hate sentinel values like this but this is only for
// our macOS application currently. We can switch to a proper
// tagged union if we ever need to.
.@"macos-glass-regular" => -1,
.@"macos-glass-clear" => -2,
}; };
} }
@ -8381,6 +8409,8 @@ pub const BackgroundBlur = union(enum) {
.false => try formatter.formatEntry(bool, false), .false => try formatter.formatEntry(bool, false),
.true => try formatter.formatEntry(bool, true), .true => try formatter.formatEntry(bool, true),
.radius => |v| try formatter.formatEntry(u8, v), .radius => |v| try formatter.formatEntry(u8, v),
.@"macos-glass-regular" => try formatter.formatEntry([]const u8, "macos-glass-regular"),
.@"macos-glass-clear" => try formatter.formatEntry([]const u8, "macos-glass-clear"),
} }
} }
@ -8400,6 +8430,12 @@ pub const BackgroundBlur = union(enum) {
try v.parseCLI("42"); try v.parseCLI("42");
try testing.expectEqual(42, v.radius); try testing.expectEqual(42, v.radius);
try v.parseCLI("macos-glass-regular");
try testing.expectEqual(.@"macos-glass-regular", v);
try v.parseCLI("macos-glass-clear");
try testing.expectEqual(.@"macos-glass-clear", v);
try testing.expectError(error.InvalidValue, v.parseCLI("")); try testing.expectError(error.InvalidValue, v.parseCLI(""));
try testing.expectError(error.InvalidValue, v.parseCLI("aaaa")); try testing.expectError(error.InvalidValue, v.parseCLI("aaaa"));
try testing.expectError(error.InvalidValue, v.parseCLI("420")); try testing.expectError(error.InvalidValue, v.parseCLI("420"));

View File

@ -193,20 +193,32 @@ test "c_get: background-blur" {
{ {
c.@"background-blur" = .false; c.@"background-blur" = .false;
var cval: u8 = undefined; var cval: i16 = undefined;
try testing.expect(get(&c, .@"background-blur", @ptrCast(&cval))); try testing.expect(get(&c, .@"background-blur", @ptrCast(&cval)));
try testing.expectEqual(0, cval); try testing.expectEqual(0, cval);
} }
{ {
c.@"background-blur" = .true; c.@"background-blur" = .true;
var cval: u8 = undefined; var cval: i16 = undefined;
try testing.expect(get(&c, .@"background-blur", @ptrCast(&cval))); try testing.expect(get(&c, .@"background-blur", @ptrCast(&cval)));
try testing.expectEqual(20, cval); try testing.expectEqual(20, cval);
} }
{ {
c.@"background-blur" = .{ .radius = 42 }; c.@"background-blur" = .{ .radius = 42 };
var cval: u8 = undefined; var cval: i16 = undefined;
try testing.expect(get(&c, .@"background-blur", @ptrCast(&cval))); try testing.expect(get(&c, .@"background-blur", @ptrCast(&cval)));
try testing.expectEqual(42, cval); try testing.expectEqual(42, cval);
} }
{
c.@"background-blur" = .@"macos-glass-regular";
var cval: i16 = undefined;
try testing.expect(get(&c, .@"background-blur", @ptrCast(&cval)));
try testing.expectEqual(-1, cval);
}
{
c.@"background-blur" = .@"macos-glass-clear";
var cval: i16 = undefined;
try testing.expect(get(&c, .@"background-blur", @ptrCast(&cval)));
try testing.expectEqual(-2, cval);
}
} }

View File

@ -728,10 +728,7 @@ pub fn Renderer(comptime GraphicsAPI: type) type {
options.config.background.r, options.config.background.r,
options.config.background.g, options.config.background.g,
options.config.background.b, options.config.background.b,
if (shouldDisableBackground(options.config)) @intFromFloat(@round(options.config.background_opacity * 255.0)),
0
else
@intFromFloat(@round(options.config.background_opacity * 255.0)),
}, },
.bools = .{ .bools = .{
.cursor_wide = false, .cursor_wide = false,