BOOL handling differs on M1 vs x64.

The M1 ruined me. Fixes building and running on Intel-based Macs.
This commit is contained in:
Ryan McGrath 2021-02-08 11:54:35 -08:00
parent 724b40e5a8
commit 9511a5a82c
8 changed files with 56 additions and 94 deletions

View file

@ -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.

View file

@ -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.

View file

@ -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;

View file

@ -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`.

View file

@ -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`.

View file

@ -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<T: AppDelegate>(this: &Object, _: Sel, _: id) {
/// Fires when the Application Delegate receives a
/// `applicationShouldHandleReopen:hasVisibleWindows:` notification.
extern fn should_handle_reopen<T: AppDelegate>(this: &Object, _: Sel, _: id, has_visible_windows: BOOL) -> BOOL {
match app::<T>(this).should_handle_reopen(match has_visible_windows {
YES => true,
NO => false
}) {
match app::<T>(this).should_handle_reopen(to_bool(has_visible_windows)) {
true => YES,
false => NO
}
@ -266,10 +263,7 @@ extern fn print_files<T: AppDelegate>(this: &Object, _: Sel, _: id, files: id, s
let settings = PrintSettings::with_inner(settings);
app::<T>(this).print_files(files, settings, match show_print_panels {
YES => true,
NO => false
}).into()
app::<T>(this).print_files(files, settings, to_bool(show_print_panels)).into()
}
/// Called when the application's occlusion state has changed.

View file

@ -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<T> Window<T> {
/// 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<T> Window<T> {
/// 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<T> Window<T> {
/// 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.

View file

@ -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
}
}