From ba9c5571f408bdca8584b1b44cc1b95a927d8e34 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Mon, 20 Jun 2022 03:46:44 -0700 Subject: [PATCH] fix(macos): guard theme APIs to not crash when running on 10.13 or older (#429) --- .changes/theme-macos-guard.md | 5 +++++ src/platform_impl/macos/window.rs | 13 ++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 .changes/theme-macos-guard.md diff --git a/.changes/theme-macos-guard.md b/.changes/theme-macos-guard.md new file mode 100644 index 000000000..b83175bcc --- /dev/null +++ b/.changes/theme-macos-guard.md @@ -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. diff --git a/src/platform_impl/macos/window.rs b/src/platform_impl/macos/window.rs index 8183a3826..a7f6812ef 100644 --- a/src/platform_impl/macos/window.rs +++ b/src/platform_impl/macos/window.rs @@ -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, @@ -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]; + } } }