diff --git a/src/defaults/mod.rs b/src/defaults/mod.rs index 6dbedb5..73b284c 100644 --- a/src/defaults/mod.rs +++ b/src/defaults/mod.rs @@ -38,7 +38,7 @@ use objc::{class, msg_send, sel, sel_impl}; use objc::runtime::Object; use objc_id::Id; -use crate::foundation::{id, nil, YES, NO, BOOL, NSData, NSString, NSDictionary, NSNumber}; +use crate::foundation::{id, nil, to_bool, YES, NO, BOOL, NSData, NSString, NSDictionary, NSNumber}; mod value; pub use value::Value; @@ -244,10 +244,7 @@ impl UserDefaults { msg_send![&*self.0, objectIsForcedForKey:key.into_inner()] }; - match result { - YES => true, - NO => false - } + to_bool(result) } /// Blocks for any asynchronous updates to the defaults database and returns. diff --git a/src/foundation/data.rs b/src/foundation/data.rs index 6c04c46..2c85c8f 100644 --- a/src/foundation/data.rs +++ b/src/foundation/data.rs @@ -17,7 +17,7 @@ use objc::{class, msg_send, sel, sel_impl}; use objc::runtime::Object; use objc_id::Id; -use crate::foundation::{id, BOOL, YES, NO, NSUInteger}; +use crate::foundation::{id, to_bool, BOOL, YES, NO, NSUInteger}; /// Wrapper for a retained `NSData` object. /// @@ -70,10 +70,7 @@ impl NSData { msg_send![obj, isKindOfClass:class!(NSData)] }; - match result { - YES => true, - NO => false - } + to_bool(result) } /// Returns the length of the underlying `NSData` bytes. diff --git a/src/foundation/mod.rs b/src/foundation/mod.rs index 976895a..0dc3b7c 100644 --- a/src/foundation/mod.rs +++ b/src/foundation/mod.rs @@ -42,6 +42,20 @@ pub use number::NSNumber; mod string; pub use string::NSString; +/// Bool mapping types differ between ARM and x64. There's a number of places that we need to check +/// against BOOL results throughout the framework, and this just simplifies some mismatches. +#[inline(always)] +pub fn to_bool(result: BOOL) -> bool { + match result { + YES => true, + NO => false, + + //#[cfg(target_arch = "aarch64")] + #[cfg(not(target_arch = "aarch64"))] + _ => { std::unreachable!(); } + } +} + /// More or less maps over to Objective-C's `id` type, which... can really be anything. #[allow(non_camel_case_types)] pub type id = *mut runtime::Object; diff --git a/src/foundation/number.rs b/src/foundation/number.rs index a40b77c..c7bc3bd 100644 --- a/src/foundation/number.rs +++ b/src/foundation/number.rs @@ -5,7 +5,7 @@ use objc::{class, msg_send, sel, sel_impl}; use objc::runtime::Object; use objc_id::Id; -use crate::foundation::{id, BOOL, YES, NO, NSInteger}; +use crate::foundation::{id, to_bool, BOOL, YES, NO, NSInteger}; /// Wrapper for a retained `NSNumber` object. /// @@ -90,10 +90,7 @@ impl NSNumber { msg_send![&*self.0, boolValue] }; - match result { - YES => true, - NO => false - } + to_bool(result) } /// A helper method for determining if a given `NSObject` is an `NSNumber`. @@ -102,10 +99,7 @@ impl NSNumber { msg_send![obj, isKindOfClass:class!(NSNumber)] }; - match result { - YES => true, - NO => false - } + to_bool(result) } /// Consumes and returns the underlying `NSNumber`. diff --git a/src/foundation/string.rs b/src/foundation/string.rs index e036318..af04c19 100644 --- a/src/foundation/string.rs +++ b/src/foundation/string.rs @@ -5,7 +5,7 @@ use objc::{class, msg_send, sel, sel_impl}; use objc::runtime::Object; use objc_id::Id; -use crate::foundation::{id, BOOL, YES, NO}; +use crate::foundation::{id, to_bool, BOOL, YES, NO}; const UTF8_ENCODING: usize = 4; @@ -38,11 +38,7 @@ impl NSString { /// Utility method for checking whether an `NSObject` is an `NSString`. pub fn is(obj: id) -> bool { let result: BOOL = unsafe { msg_send![obj, isKindOfClass:class!(NSString)] }; - - match result { - YES => true, - NO => false - } + to_bool(result) } /// Helper method for returning the UTF8 bytes for this `NSString`. diff --git a/src/macos/app/delegate.rs b/src/macos/app/delegate.rs index 5b09221..01b66b8 100644 --- a/src/macos/app/delegate.rs +++ b/src/macos/app/delegate.rs @@ -14,7 +14,7 @@ use objc::runtime::{Class, Object, Sel}; use url::Url; use crate::error::Error; -use crate::foundation::{id, nil, BOOL, YES, NO, NSUInteger, NSArray, NSString}; +use crate::foundation::{id, nil, to_bool, BOOL, YES, NO, NSUInteger, NSArray, NSString}; use crate::macos::app::{APP_PTR, AppDelegate}; use crate::macos::printing::PrintSettings; use crate::user_activity::UserActivity; @@ -105,10 +105,7 @@ extern fn did_update(this: &Object, _: Sel, _: id) { /// Fires when the Application Delegate receives a /// `applicationShouldHandleReopen:hasVisibleWindows:` notification. extern fn should_handle_reopen(this: &Object, _: Sel, _: id, has_visible_windows: BOOL) -> BOOL { - match app::(this).should_handle_reopen(match has_visible_windows { - YES => true, - NO => false - }) { + match app::(this).should_handle_reopen(to_bool(has_visible_windows)) { true => YES, false => NO } @@ -266,10 +263,7 @@ extern fn print_files(this: &Object, _: Sel, _: id, files: id, s let settings = PrintSettings::with_inner(settings); - app::(this).print_files(files, settings, match show_print_panels { - YES => true, - NO => false - }).into() + app::(this).print_files(files, settings, to_bool(show_print_panels)).into() } /// Called when the application's occlusion state has changed. diff --git a/src/macos/window/mod.rs b/src/macos/window/mod.rs index 28e917a..c27534e 100644 --- a/src/macos/window/mod.rs +++ b/src/macos/window/mod.rs @@ -18,7 +18,7 @@ use objc::runtime::Object; use objc_id::ShareId; use crate::color::Color; -use crate::foundation::{id, nil, YES, NO, NSString, NSInteger, NSUInteger}; +use crate::foundation::{id, nil, to_bool, YES, NO, NSString, NSInteger, NSUInteger}; use crate::layout::traits::Layout; use crate::macos::toolbar::{Toolbar, ToolbarDelegate}; use crate::utils::{os, Controller}; @@ -322,22 +322,16 @@ impl Window { /// Returns whether this window is opaque or not. pub fn is_opaque(&self) -> bool { - unsafe { - match msg_send![&*self.objc, isOpaque] { - YES => true, - NO => false - } - } + to_bool(unsafe { + msg_send![&*self.objc, isOpaque] + }) } /// Returns whether this window is miniaturized or not. pub fn is_miniaturized(&self) -> bool { - unsafe { - match msg_send![&*self.objc, isMiniaturized] { - YES => true, - NO => false - } - } + to_bool(unsafe { + msg_send![&*self.objc, isMiniaturized] + }) } /// Miniaturize this window. @@ -371,42 +365,30 @@ impl Window { /// space. For nonvisible windows, it indicates whether ordering the window onscreen would cause it to /// be on the active space._ pub fn is_on_active_space(&self) -> bool { - unsafe { - match msg_send![&*self.objc, isOnActiveSpace] { - YES => true, - NO => false - } - } + to_bool(unsafe { + msg_send![&*self.objc, isOnActiveSpace] + }) } /// Returns whether this window is visible or not. pub fn is_visible(&self) -> bool { - unsafe { - match msg_send![&*self.objc, isVisible] { - YES => true, - NO => false - } - } + to_bool(unsafe { + msg_send![&*self.objc, isVisible] + }) } /// Returns whether this window is the key or not. pub fn is_key(&self) -> bool { - unsafe { - match msg_send![&*self.objc, isKeyWindow] { - YES => true, - NO => false - } - } + to_bool(unsafe { + msg_send![&*self.objc, isKeyWindow] + }) } /// Returns whether this window can become the key window. pub fn can_become_key(&self) -> bool { - unsafe { - match msg_send![&*self.objc, canBecomeKeyWindow] { - YES => true, - NO => false - } - } + to_bool(unsafe { + msg_send![&*self.objc, canBecomeKeyWindow] + }) } /// Make this window the key window. @@ -426,22 +408,16 @@ impl Window { /// Returns if this is the main window or not. pub fn is_main_window(&self) -> bool { - unsafe { - match msg_send![&*self.objc, isMainWindow] { - YES => true, - NO => false - } - } + to_bool(unsafe { + msg_send![&*self.objc, isMainWindow] + }) } /// Returns if this can become the main window. pub fn can_become_main_window(&self) -> bool { - unsafe { - match msg_send![&*self.objc, canBecomeMainWindow] { - YES => true, - NO => false - } - } + to_bool(unsafe { + msg_send![&*self.objc, canBecomeMainWindow] + }) } /// Set whether this window should be excluded from the top-level "Windows" menu. diff --git a/src/utils.rs b/src/utils.rs index b90ab5e..703c8c6 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -23,9 +23,12 @@ pub mod os { /// In rare cases we need to check whether something is a specific version of macOS. This is a /// runtime check thhat returns a boolean indicating whether the current version is a minimum target. #[inline(always)] - pub fn is_minimum_version(major: u64) -> bool { + pub fn is_minimum_version(minimum_major: u64) -> bool { + //println!("Looking for: {}", major); + //println!("VERSION: {}", OS_VERSION.version()); + match OS_VERSION.version() { - Version::Semantic(maj, _, _) => major >= *maj, + Version::Semantic(os_major, _, _) => { *os_major >= minimum_major }, _ => false } } @@ -140,12 +143,3 @@ pub fn activate_cocoa_multithreading() { let _: () = msg_send![thread, start]; } } - -/// Utility method for turning an Objective-C `BOOL` into a Rust `bool`. -#[inline] -pub fn as_bool(value: BOOL) -> bool { - match value { - YES => true, - NO => false - } -}