Skip to content

Commit

Permalink
feat: add dark mode (#21)
Browse files Browse the repository at this point in the history
* feat: add dark mode

* add docs
  • Loading branch information
jiacai2050 committed Nov 9, 2023
1 parent e78ed59 commit ae27bd3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
7 changes: 4 additions & 3 deletions README.org
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#+DATE: 2023-10-21T12:09:48+0800
#+LASTMOD: 2023-10-22T22:40:34+0800
#+LASTMOD: 2023-11-09T22:26:31+0800
#+TYPE: docs

* Zigcli
Expand All @@ -21,9 +21,10 @@ This package provides:
- =loc=, lines of code.
- =tree=, list contents of directories in a tree-like format.
- =yes=, output a string repeatedly until killed.
- =pidof=, like [[https://man7.org/linux/man-pages/man1/pidof.1.html][pidof]], but for macOS.
- =night-shift=, control [[https://support.apple.com/guide/mac-help/use-night-shift-mchl97bc676d/mac][Night Shift]] in cli, build for macOS.
- =repeat=, repeat a command until it succeeds.
- =pidof=, like [[https://man7.org/linux/man-pages/man1/pidof.1.html][pidof]], but for macOS.
- =night-shift=, control [[https://support.apple.com/guide/mac-help/use-night-shift-mchl97bc676d/mac][Night Shift]] in macOS.
- =dark-mode=, Get dark mode status in macOS.

* Install
** Programs
Expand Down
6 changes: 5 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ fn buildBinaries(
"pidof",
"yes",
"night-shift",
"dark-mode",
"repeat",
}) |name| {
try buildBinary(b, .{ .bin = name }, optimize, target, is_ci, all_tests);
Expand Down Expand Up @@ -168,7 +169,7 @@ fn makeCompileStep(
) ?*Build.CompileStep {
const name = comptime source.name();
const path = comptime source.path();
if (std.mem.eql(u8, name, "night-shift") or std.mem.eql(u8, name, "pidof")) {
if (std.mem.eql(u8, name, "night-shift") or std.mem.eql(u8, name, "dark-mode") or std.mem.eql(u8, name, "pidof")) {
if (target.getOsTag() != .macos) {
return null;
}
Expand Down Expand Up @@ -196,6 +197,9 @@ fn makeCompileStep(
exe.linkSystemLibrary("objc");
exe.addFrameworkPath(.{ .path = "/System/Library/PrivateFrameworks" });
exe.linkFramework("CoreBrightness");
} else if (std.mem.eql(u8, name, "dark-mode")) {
exe.linkSystemLibrary("objc");
exe.linkFramework("AppKit");
}
return exe;
}
2 changes: 2 additions & 0 deletions docs/content/docs/programs/dark-mode.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Dark mode
Get dark mode status for macOS.
31 changes: 31 additions & 0 deletions src/bin/dark-mode.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//! Dark mode status, built for macOS.
//!
const std = @import("std");
const c = @cImport({
@cInclude("objc/objc.h");
@cInclude("objc/message.h");
});

// https://developer.apple.com/documentation/appkit/nsappearancenamedarkaqua
const darkValue = "NSAppearanceNameDarkAqua";

pub fn main() !void {
const clazz = c.objc_getClass("NSAppearance");

const appearanceName = blk: {
const call: *fn (c.id, c.SEL) callconv(.C) c.id = @constCast(@ptrCast(&c.objc_msgSend));
const appearance = call(@alignCast(@ptrCast(clazz.?)), c.sel_registerName("currentDrawingAppearance"));
break :blk call(appearance, c.sel_registerName("name"));
};

const appearanceValue = blk: {
const call: *fn (c.id, c.SEL) callconv(.C) [*c]const u8 = @constCast(@ptrCast(&c.objc_msgSend));
break :blk call(appearanceName, c.sel_registerName("UTF8String"));
};

if (std.mem.eql(u8, darkValue, std.mem.span(appearanceValue.?))) {
std.debug.print("on", .{});
} else {
std.debug.print("off", .{});
}
}

0 comments on commit ae27bd3

Please sign in to comment.