Skip to content

Commit

Permalink
Directly read preferences instead of using the defaults CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackHoleFox committed Jan 5, 2023
1 parent 362af8a commit 71ba3ef
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ version = "4.0.2"
optional = true
features = ["wrap_help", "cargo"]

[target.'cfg(target_os = "macos")'.dependencies]
plist = "1.3"

[dev-dependencies]
assert_cmd = "2.0.5"
serial_test = "0.6.0"
Expand Down
20 changes: 15 additions & 5 deletions src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,11 +401,21 @@ fn asset_from_cache<T: serde::de::DeserializeOwned>(

#[cfg(target_os = "macos")]
fn macos_dark_mode_active() -> bool {
let mut defaults_cmd = std::process::Command::new("defaults");
defaults_cmd.args(&["read", "-globalDomain", "AppleInterfaceStyle"]);
match defaults_cmd.output() {
Ok(output) => output.stdout == b"Dark\n",
Err(_) => true,
const PREFERNECES_FILE: &str = "Library/Preferences/.GlobalPreferences.plist";
const STYLE_KEY: &str = "AppleInterfaceStyle";

let preferences_file = dirs_next::home_dir()
.map(|home| home.join(PREFERNECES_FILE))
.expect("Could not get home directory");

match plist::Value::from_file(preferences_file).map(|file| file.into_dictionary()) {
Ok(Some(preferences)) => match preferences.get(STYLE_KEY).and_then(|val| val.as_string()) {
Some(value) => value == "Dark",
// If the key does not exist, then light theme is currently in use.
None => false,
},
// Unreachable, in theory. All macOS users have a home directory and preferences file setup.
Ok(None) | Err(_) => true,
}
}

Expand Down

0 comments on commit 71ba3ef

Please sign in to comment.