Skip to content

Commit

Permalink
config: allow booleans for background-blur-radius
Browse files Browse the repository at this point in the history
  • Loading branch information
pluiedev committed Jan 5, 2025
1 parent d44f92d commit 47543b4
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/apprt/embedded.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1895,7 +1895,7 @@ pub const CAPI = struct {
_ = CGSSetWindowBackgroundBlurRadius(
CGSDefaultConnectionForThread(),
nswindow.msgSend(usize, objc.sel("windowNumber"), .{}),
@intCast(config.@"background-blur-radius"),
@intCast(config.@"background-blur-radius".cval()),
);
}

Expand Down
7 changes: 6 additions & 1 deletion src/apprt/gtk/Window.zig
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,12 @@ pub fn syncAppearance(self: *Window, config: *const configpkg.Config) !void {
}

if (self.wayland) |*wl| {
try wl.setBlur(config.@"background-blur-radius" > 0);
const blurred = switch (config.@"background-blur-radius") {
.false => false,
.true => true,
.value => |v| v > 0,
};
try wl.setBlur(blurred);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/cli/args.zig
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ fn parsePackedStruct(comptime T: type, v: []const u8) !T {
return result;
}

fn parseBool(v: []const u8) !bool {
pub fn parseBool(v: []const u8) !bool {
const t = &[_][]const u8{ "1", "t", "T", "true" };
const f = &[_][]const u8{ "0", "f", "F", "false" };

Expand Down
91 changes: 81 additions & 10 deletions src/config/Config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -562,29 +562,38 @@ palette: Palette = .{},
/// widgets to show through which isn't generally desirable.
@"background-opacity": f64 = 1.0,

/// A positive value enables blurring of the background when background-opacity
/// is less than 1.
/// Whether to blur the background when `background-opacity` is less than 1.
///
/// On macOS, the value is the blur radius to apply. A value of 20
/// is reasonable for a good looking blur. Higher values will cause strange
/// rendering issues as well as performance issues.
/// Valid values are:
///
/// * a nonnegative integer specifying the *blur intensity*
/// * `false`, equivalent to a blur intensity of 0
/// * `true`, equivalent to the default blur intensity of 20, which is
/// reasonable for a good looking blur. Higher blur intensities may
/// cause strange rendering and performance issues.
///
/// Supported on macOS and on some Linux desktop environments, including:
///
/// On KDE Plasma under Wayland, the exact value is _ignored_ — the reason is
/// that KWin, the window compositor powering Plasma, only has one global blur
/// setting and does not allow applications to have individual blur settings.
/// * KDE Plasma (Wayland only)
///
/// Warning: the exact blur intensity is _ignored_ under KDE Plasma, and setting
/// this setting to either `true` or any positive blur intensity value would
/// achieve the same effect. The reason is that KWin, the window compositor
/// powering Plasma, only has one global blur setting and does not allow
/// applications to specify individual blur settings.
///
/// To configure KWin's global blur setting, open System Settings and go to
/// "Apps & Windows" > "Window Management" > "Desktop Effects" and select the
/// "Blur" plugin. If disabled, enable it by ticking the checkbox to the left.
/// Then click on the "Configure" button and there will be two sliders that
/// allow you to set background blur and noise strengths for all apps,
/// allow you to set background blur and noise intensities for all apps,
/// including Ghostty.
///
/// All other Linux desktop environments are as of now unsupported. Users may
/// need to set environment-specific settings and/or install third-party plugins
/// in order to support background blur, as there isn't a unified interface for
/// doing so.
@"background-blur-radius": u8 = 0,
@"background-blur-radius": BackgroundBlur = .false,

/// The opacity level (opposite of transparency) of an unfocused split.
/// Unfocused splits by default are slightly faded out to make it easier to see
Expand Down Expand Up @@ -5460,6 +5469,68 @@ pub const AutoUpdate = enum {
download,
};

/// See background-blur-radius
pub const BackgroundBlur = union(enum) {
false,
true,
value: u8,

pub fn parseCLI(self: *BackgroundBlur, input: ?[]const u8) !void {
const input_ = input orelse {
// Emulate behavior for bools
self.* = .true;
return;
};

if (cli.args.parseBool(input_)) |b| {
self.* = if (b) .true else .false;
} else |_| {
const value = std.fmt.parseInt(u8, input_, 0) catch return error.InvalidValue;
self.* = .{ .value = value };
}
}

pub fn cval(self: BackgroundBlur) u8 {
return switch (self) {
.false => 0,
.true => 20,
.value => |v| v,
};
}

pub fn formatEntry(
self: BackgroundBlur,
formatter: anytype,
) !void {
switch (self) {
.false => try formatter.formatEntry(bool, false),
.true => try formatter.formatEntry(bool, true),
.value => |v| try formatter.formatEntry(u8, v),
}
}

test "parse BackgroundBlur" {
const testing = std.testing;
var v: BackgroundBlur = undefined;

try v.parseCLI(null);
try testing.expectEqual(.true, v);

try v.parseCLI("true");
try testing.expectEqual(.true, v);

try v.parseCLI("false");
try testing.expectEqual(.false, v);

try v.parseCLI("42");
try testing.expectEqual(42, v.value);

try testing.expectError(error.InvalidValue, v.parseCLI(""));
try testing.expectError(error.InvalidValue, v.parseCLI("aaaa"));
try testing.expectError(error.InvalidValue, v.parseCLI("420"));
}
};

/// See theme
pub const Theme = struct {
light: []const u8,
Expand Down
38 changes: 38 additions & 0 deletions src/config/c_get.zig
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ fn getValue(ptr_raw: *anyopaque, value: anytype) bool {
ptr.* = @intCast(@as(Backing, @bitCast(value)));
},

.Union => |_| {
if (@hasDecl(T, "cval")) {
const PtrT = @typeInfo(@TypeOf(T.cval)).Fn.return_type.?;
const ptr: *PtrT = @ptrCast(@alignCast(ptr_raw));
ptr.* = value.cval();
return true;
}

return false;
},

else => return false,
},
}
Expand Down Expand Up @@ -167,3 +178,30 @@ test "c_get: optional" {
try testing.expectEqual(0, cval.b);
}
}

test "c_get: background-blur" {
const testing = std.testing;
const alloc = testing.allocator;

var c = try Config.default(alloc);
defer c.deinit();

{
c.@"background-blur-radius" = .false;
var cval: u8 = undefined;
try testing.expect(get(&c, .@"background-blur-radius", @ptrCast(&cval)));
try testing.expectEqual(0, cval);
}
{
c.@"background-blur-radius" = .true;
var cval: u8 = undefined;
try testing.expect(get(&c, .@"background-blur-radius", @ptrCast(&cval)));
try testing.expectEqual(20, cval);
}
{
c.@"background-blur-radius" = .{ .value = 42 };
var cval: u8 = undefined;
try testing.expect(get(&c, .@"background-blur-radius", @ptrCast(&cval)));
try testing.expectEqual(42, cval);
}
}

0 comments on commit 47543b4

Please sign in to comment.