From 8963c3b299cf25cb8d42e641b4508f853e8cc631 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 20 Sep 2023 21:30:57 -0700 Subject: [PATCH] config: window-theme, enum support for get --- src/config/Config.zig | 15 +++++++++++++++ src/config/c_get.zig | 26 ++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/config/Config.zig b/src/config/Config.zig index 629392436..79b9ea39d 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -244,6 +244,14 @@ keybind: Keybinds = .{}, /// borders. @"window-decoration": bool = true, +/// The theme to use for the windows. The default is "system" which +/// means that whatever the system theme is will be used. This can +/// also be set to "light" or "dark" to force a specific theme regardless +/// of the system settings. +/// +/// This is currently only supported on macOS. +@"window-theme": WindowTheme = .system, + /// Whether to allow programs running in the terminal to read/write to /// the system clipboard (OSC 52, for googling). The default is to /// disallow clipboard reading but allow writing. @@ -1507,3 +1515,10 @@ pub const OSCColorReportFormat = enum { @"8-bit", @"16-bit", }; + +/// The default window theme. +pub const WindowTheme = enum { + system, + light, + dark, +}; diff --git a/src/config/c_get.zig b/src/config/c_get.zig index 05fa5fe7d..504e98a87 100644 --- a/src/config/c_get.zig +++ b/src/config/c_get.zig @@ -19,7 +19,7 @@ pub fn get(config: *const Config, k: Key, ptr_raw: *anyopaque) bool { const value = fieldByKey(config, tag); switch (@TypeOf(value)) { ?[:0]const u8 => { - const ptr: *[*c]const u8 = @ptrCast(@alignCast(ptr_raw)); + const ptr: *?[*:0]const u8 = @ptrCast(@alignCast(ptr_raw)); ptr.* = if (value) |slice| @ptrCast(slice.ptr) else null; }, @@ -38,7 +38,14 @@ pub fn get(config: *const Config, k: Key, ptr_raw: *anyopaque) bool { ptr.* = @floatCast(value); }, - else => return false, + else => |T| switch (@typeInfo(T)) { + .Enum => { + const ptr: *[*:0]const u8 = @ptrCast(@alignCast(ptr_raw)); + ptr.* = @tagName(value); + }, + + else => return false, + }, } return true; @@ -74,3 +81,18 @@ test "u8" { try testing.expect(get(&c, .@"font-size", &cval)); try testing.expectEqual(@as(c_uint, 24), cval); } + +test "enum" { + const testing = std.testing; + const alloc = testing.allocator; + + var c = try Config.default(alloc); + defer c.deinit(); + c.@"window-theme" = .dark; + + var cval: [*:0]u8 = undefined; + try testing.expect(get(&c, .@"window-theme", @ptrCast(&cval))); + + const str = std.mem.sliceTo(cval, 0); + try testing.expectEqualStrings("dark", str); +}