Changes
5 changed files (+175/-126)
-
-
@@ -22,7 +22,7 @@ const optimize = b.standardOptimizeOption(.{});const exe = b.addExecutable(.{ .name = ",theme", .root_source_file = b.path("main.zig"), .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, });
-
-
programs/theme/main.zig (deleted)
-
@@ -1,125 +0,0 @@// Copyright 2025 Shota FUJI <pockawoooh@gmail.com> // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // SPDX-License-Identifier: Apache-2.0 //! Over-powered theme switcher. const std = @import("std"); const gio = @cImport({ @cInclude("gio/gio.h"); }); const gobject = @cImport({ @cInclude("glib-object.h"); }); const ExitCode = enum(u8) { ok = 0, generic_error = 1, incorrect_usage = 2, pub fn to_u8(self: @This()) u8 { return @intFromEnum(self); } }; const GnomeColorScheme = enum(c_int) { default = 0, @"prefer-dark" = 1, @"prefer-light" = 2, pub fn from_variant(variant: Variant) @This() { return switch (variant) { .system => .default, .dark => .@"prefer-dark", .light => .@"prefer-light", }; } }; const Variant = enum { system, dark, light, pub fn from_string(str: []const u8) ?@This() { inline for (@typeInfo(Variant).@"enum".fields) |field| { if (std.mem.eql(u8, field.name, str)) { return @enumFromInt(field.value); } } return null; } }; pub fn main() !u8 { var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena.deinit(); const allocator = arena.allocator(); var iter = try std.process.ArgIterator.initWithAllocator(allocator); defer iter.deinit(); // Skip program name. _ = iter.next(); const initial_arg = iter.next() orelse { std.log.err("Argument is required.", .{}); return ExitCode.incorrect_usage.to_u8(); }; const variant: Variant = Variant.from_string(initial_arg) orelse { std.log.err("Unknown variant \"{s}\".", .{initial_arg}); return ExitCode.incorrect_usage.to_u8(); }; if (iter.next()) |_| { std.log.err("Too many arguments.", .{}); return ExitCode.incorrect_usage.to_u8(); } gnome: { const gnome_color_scheme = GnomeColorScheme.from_variant(variant); const gsettings = gio.g_settings_new("org.gnome.desktop.interface"); defer gobject.g_object_unref(gsettings); const gsettings_wrote = gio.g_settings_set_enum( gsettings, "color-scheme", @intFromEnum(gnome_color_scheme), ); if (gsettings_wrote == 0) { std.log.warn( "Unable to set GNOME color scheme to {s}", .{@tagName(gnome_color_scheme)}, ); break :gnome; } // https://docs.gtk.org/gio/class.Settings.html#delay-apply-mode // > ...these writes may not complete by the time that g_settings_set() // > returns; see g_settings_sync()). gio.g_settings_sync(); std.log.info("Set GNOME color scheme to {s}", .{@tagName(gnome_color_scheme)}); } return ExitCode.ok.to_u8(); }
-
-
-
@@ -0,0 +1,73 @@// Copyright 2025 Shota FUJI <pockawoooh@gmail.com> // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // SPDX-License-Identifier: Apache-2.0 const std = @import("std"); const Variant = @import("./variant.zig").Variant; const gio = @cImport({ @cInclude("gio/gio.h"); }); const gobject = @cImport({ @cInclude("glib-object.h"); }); const GnomeColorScheme = enum(c_int) { default = 0, @"prefer-dark" = 1, @"prefer-light" = 2, pub fn from_variant(variant: Variant) @This() { return switch (variant) { .system => .default, .dark => .@"prefer-dark", .light => .@"prefer-light", }; } }; pub const ApplyError = error{ FailedToWriteGSettings, }; pub fn apply(variant: Variant) ApplyError!void { const gnome_color_scheme = GnomeColorScheme.from_variant(variant); const gsettings = gio.g_settings_new("org.gnome.desktop.interface"); defer gobject.g_object_unref(gsettings); const gsettings_wrote = gio.g_settings_set_enum( gsettings, "color-scheme", @intFromEnum(gnome_color_scheme), ); if (gsettings_wrote == 0) { std.log.warn( "Unable to set GNOME color scheme to {s}", .{@tagName(gnome_color_scheme)}, ); return ApplyError.FailedToWriteGSettings; } // https://docs.gtk.org/gio/class.Settings.html#delay-apply-mode // > ...these writes may not complete by the time that g_settings_set() // > returns; see g_settings_sync()). gio.g_settings_sync(); std.log.info("Set GNOME color scheme to {s}", .{@tagName(gnome_color_scheme)}); }
-
-
-
@@ -0,0 +1,64 @@// Copyright 2025 Shota FUJI <pockawoooh@gmail.com> // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // SPDX-License-Identifier: Apache-2.0 //! Over-powered theme switcher. const std = @import("std"); const gnome = @import("./gnome.zig"); const Variant = @import("./variant.zig").Variant; const ExitCode = enum(u8) { ok = 0, generic_error = 1, incorrect_usage = 2, pub fn to_u8(self: @This()) u8 { return @intFromEnum(self); } }; pub fn main() !u8 { var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena.deinit(); const allocator = arena.allocator(); var iter = try std.process.ArgIterator.initWithAllocator(allocator); defer iter.deinit(); // Skip program name. _ = iter.next(); const initial_arg = iter.next() orelse { std.log.err("Argument is required.", .{}); return ExitCode.incorrect_usage.to_u8(); }; const variant: Variant = Variant.from_string(initial_arg) catch { std.log.err("Unknown variant \"{s}\".", .{initial_arg}); return ExitCode.incorrect_usage.to_u8(); }; if (iter.next()) |_| { std.log.err("Too many arguments.", .{}); return ExitCode.incorrect_usage.to_u8(); } gnome.apply(variant) catch {}; return ExitCode.ok.to_u8(); }
-
-
-
@@ -0,0 +1,37 @@// Copyright 2025 Shota FUJI <pockawoooh@gmail.com> // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // SPDX-License-Identifier: Apache-2.0 const std = @import("std"); pub const Variant = enum { system, dark, light, pub const FromStringError = error{ UnknownVariant, }; pub fn from_string(str: []const u8) FromStringError!@This() { inline for (@typeInfo(Variant).@"enum".fields) |field| { if (std.mem.eql(u8, field.name, str)) { return @enumFromInt(field.value); } } return FromStringError.UnknownVariant; } };
-