Skip to content

Commit

Permalink
fix(macos): guard theme APIs to not crash when running on 10.13 or ol…
Browse files Browse the repository at this point in the history
…der (#429)
  • Loading branch information
lucasfernog authored Jun 20, 2022
1 parent aae6bec commit ba9c557
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changes/theme-macos-guard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": patch
---

The `theme` function now `Theme::Light` on macOS older than 10.14 and the initial theme setter has no effect instead of crashing the application.
13 changes: 10 additions & 3 deletions src/platform_impl/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ pub(super) fn get_ns_theme() -> Theme {
appearances.push(NSString::alloc(nil).init_str("NSAppearanceNameDarkAqua"));
let app_class = class!(NSApplication);
let app: id = msg_send![app_class, sharedApplication];
let has_theme: BOOL = msg_send![app, respondsToSelector: sel!(effectiveAppearance)];
if has_theme == NO {
return Theme::Light;
}
let appearance: id = msg_send![app, effectiveAppearance];
let name: id = msg_send![
appearance,
Expand All @@ -293,11 +297,14 @@ pub(super) fn set_ns_theme(theme: Theme) {
Theme::Light => "NSAppearanceNameAqua",
};
unsafe {
let name = NSString::alloc(nil).init_str(name);
let appearance: id = msg_send![class!(NSAppearance), appearanceNamed: name];
let app_class = class!(NSApplication);
let app: id = msg_send![app_class, sharedApplication];
let _: () = msg_send![app, setAppearance: appearance];
let has_theme: BOOL = msg_send![app, respondsToSelector: sel!(effectiveAppearance)];
if has_theme == YES {
let name = NSString::alloc(nil).init_str(name);
let appearance: id = msg_send![class!(NSAppearance), appearanceNamed: name];
let _: () = msg_send![app, setAppearance: appearance];
}
}
}

Expand Down

0 comments on commit ba9c557

Please sign in to comment.