Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use objc2-foundation and objc2-app-kit #37

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ xdg = "2.4.1"
winreg = "0.52.0"

[target.'cfg(target_os = "macos")'.dependencies]
objc = "0.2"
objc2 = "0.5.1"
objc2-foundation = { version = "0.2.0", features = [
"NSArray",
"NSObject",
"NSString",
] }
objc2-app-kit = { version = "0.2.0", features = [
"NSAppearance",
"NSApplication",
"NSResponder",
] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys = { version = "0.3", features = ["MediaQueryList", "Window"] }
63 changes: 27 additions & 36 deletions src/platforms/macos/detect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,43 @@
// Written with help from Ryan McGrath (https://rymc.io/).

use crate::Mode;
use objc::runtime::Object;
use objc::{class, msg_send, sel, sel_impl};

extern "C" {
static NSAppearanceNameAqua: *const Object;
static NSAppearanceNameAccessibilityHighContrastAqua: *const Object;
static NSAppearanceNameDarkAqua: *const Object;
static NSAppearanceNameAccessibilityHighContrastDarkAqua: *const Object;
}

use objc2::sel;
use objc2_app_kit::{
NSAppearance, NSAppearanceNameAccessibilityHighContrastAqua,
NSAppearanceNameAccessibilityHighContrastDarkAqua, NSAppearanceNameAqua,
NSAppearanceNameDarkAqua, NSApplication,
};
use objc2_foundation::{MainThreadMarker, NSArray, NSCopying, NSObjectProtocol};

fn is_dark_mode_enabled() -> bool {
// SAFETY: TODO, only perform this function on the main thread.
let mtm = unsafe { MainThreadMarker::new_unchecked() };

unsafe {
let mut appearance: *const Object = msg_send![class!(NSAppearance), currentAppearance];
if appearance.is_null() {
appearance = msg_send![class!(NSApp), effectiveAppearance];
}
#[allow(deprecated)]
let appearance = NSAppearance::currentAppearance()
.unwrap_or_else(|| NSApplication::sharedApplication(mtm).effectiveAppearance());

let objects = [
NSAppearanceNameAqua,
NSAppearanceNameAccessibilityHighContrastAqua,
NSAppearanceNameDarkAqua,
NSAppearanceNameAccessibilityHighContrastDarkAqua,
];
let names: *const Object = msg_send![
class!(NSArray),
arrayWithObjects:objects.as_ptr()
count:objects.len()
];
let names = NSArray::from_id_slice(&[
NSAppearanceNameAqua.copy(),
NSAppearanceNameAccessibilityHighContrastAqua.copy(),
NSAppearanceNameDarkAqua.copy(),
NSAppearanceNameAccessibilityHighContrastDarkAqua.copy(),
]);

// `bestMatchFromAppearancesWithNames` is only available in macOS 10.14+.
// Gracefully handle earlier versions.
let responds_to_selector: objc::runtime::BOOL = msg_send![
appearance,
respondsToSelector: sel!(bestMatchFromAppearancesWithNames:)
];
if responds_to_selector == objc::runtime::NO {
if !appearance.respondsToSelector(sel!(bestMatchFromAppearancesWithNames:)) {
return false;
}

let style: *const Object = msg_send![
appearance,
bestMatchFromAppearancesWithNames:&*names
];

style == NSAppearanceNameDarkAqua
|| style == NSAppearanceNameAccessibilityHighContrastDarkAqua
if let Some(style) = appearance.bestMatchFromAppearancesWithNames(&names) {
*style == *NSAppearanceNameDarkAqua
|| *style == *NSAppearanceNameAccessibilityHighContrastDarkAqua
} else {
false
}
}
}

Expand Down
Loading