Skip to content

Commit

Permalink
Replace BOOL with Bool when declaring delegates
Browse files Browse the repository at this point in the history
This makes cacao compile on Aarch64 again
  • Loading branch information
madsmtm committed Aug 1, 2023
1 parent 2c402a3 commit ee57388
Show file tree
Hide file tree
Showing 16 changed files with 132 additions and 216 deletions.
6 changes: 3 additions & 3 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -401,12 +401,12 @@ to the Rust `ViewDelegate` implementation.
The methods we're setting up can range from simple to complex - take `isFlipped`:

``` rust
extern "C" fn is_flipped(_: &Object, _: Sel) -> BOOL {
return YES;
extern "C" fn is_flipped(_: &Object, _: Sel) -> Bool {
return Bool::YES;
}
```

Here, we just want to tell `NSView` to use top,left as the origin point, so we need to respond `YES` in this subclass method.
Here, we just want to tell `NSView` to use top,left as the origin point, so we need to respond `Bool::YES` in this subclass method.

``` rust
extern "C" fn dragging_entered<T: ViewDelegate>(this: &mut Object, _: Sel, info: id) -> NSUInteger {
Expand Down
82 changes: 27 additions & 55 deletions src/appkit/app/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use std::ffi::c_void;

use block::Block;
use objc::runtime::{Class, Object, Sel};
use objc::runtime::{Bool, Class, Object, Sel};
use objc::{msg_send, sel};
use url::Url;

Expand All @@ -14,7 +14,7 @@ use crate::appkit::printing::PrintSettings;
#[cfg(feature = "cloudkit")]
use crate::cloudkit::share::CKShareMetaData;
use crate::error::Error;
use crate::foundation::{id, load_or_register_class, nil, to_bool, NSArray, NSString, NSUInteger, BOOL, NO, YES};
use crate::foundation::{id, load_or_register_class, nil, NSArray, NSString, NSUInteger};
use crate::user_activity::UserActivity;

/// A handy method for grabbing our `AppDelegate` from the pointer. This is different from our
Expand Down Expand Up @@ -99,11 +99,8 @@ extern "C" fn did_update<T: AppDelegate>(this: &Object, _: Sel, _: id) {

/// Fires when the Application Delegate receives a
/// `applicationShouldHandleReopen:hasVisibleWindows:` notification.
extern "C" fn should_handle_reopen<T: AppDelegate>(this: &Object, _: Sel, _: id, has_visible_windows: BOOL) -> BOOL {
match app::<T>(this).should_handle_reopen(to_bool(has_visible_windows)) {
true => YES,
false => NO
}
extern "C" fn should_handle_reopen<T: AppDelegate>(this: &Object, _: Sel, _: id, has_visible_windows: Bool) -> Bool {
Bool::new(app::<T>(this).should_handle_reopen(has_visible_windows.as_bool()))
}

/// Fires when the application delegate receives a `applicationDockMenu:` request.
Expand All @@ -128,29 +125,23 @@ extern "C" fn did_change_screen_parameters<T: AppDelegate>(this: &Object, _: Sel

/// Fires when the application receives a `application:willContinueUserActivityWithType:`
/// notification.
extern "C" fn will_continue_user_activity_with_type<T: AppDelegate>(this: &Object, _: Sel, _: id, activity_type: id) -> BOOL {
extern "C" fn will_continue_user_activity_with_type<T: AppDelegate>(this: &Object, _: Sel, _: id, activity_type: id) -> Bool {
let activity = NSString::retain(activity_type);

match app::<T>(this).will_continue_user_activity(activity.to_str()) {
true => YES,
false => NO
}
Bool::new(app::<T>(this).will_continue_user_activity(activity.to_str()))
}

/// Fires when the application receives a `application:continueUserActivity:restorationHandler:` notification.
extern "C" fn continue_user_activity<T: AppDelegate>(this: &Object, _: Sel, _: id, activity: id, handler: id) -> BOOL {
extern "C" fn continue_user_activity<T: AppDelegate>(this: &Object, _: Sel, _: id, activity: id, handler: id) -> Bool {
// @TODO: This needs to support restorable objects, but it involves a larger question about how
// much `NSObject` retainping we want to do here. For now, pass the handler for whenever it's
// useful.
let activity = UserActivity::with_inner(activity);

match app::<T>(this).continue_user_activity(activity, || unsafe {
Bool::new(app::<T>(this).continue_user_activity(activity, || unsafe {
let handler = handler as *const Block<(id,), ()>;
(*handler).call((nil,));
}) {
true => YES,
false => NO
}
}))
}

/// Fires when the application receives a
Expand Down Expand Up @@ -199,57 +190,39 @@ extern "C" fn open_urls<T: AppDelegate>(this: &Object, _: Sel, _: id, file_urls:
}

/// Fires when the application receives an `application:openFileWithoutUI:` message.
extern "C" fn open_file_without_ui<T: AppDelegate>(this: &Object, _: Sel, _: id, file: id) -> BOOL {
extern "C" fn open_file_without_ui<T: AppDelegate>(this: &Object, _: Sel, _: id, file: id) -> Bool {
let filename = NSString::retain(file);

match app::<T>(this).open_file_without_ui(filename.to_str()) {
true => YES,
false => NO
}
Bool::new(app::<T>(this).open_file_without_ui(filename.to_str()))
}

/// Fired when the application receives an `applicationShouldOpenUntitledFile:` message.
extern "C" fn should_open_untitled_file<T: AppDelegate>(this: &Object, _: Sel, _: id) -> BOOL {
match app::<T>(this).should_open_untitled_file() {
true => YES,
false => NO
}
extern "C" fn should_open_untitled_file<T: AppDelegate>(this: &Object, _: Sel, _: id) -> Bool {
Bool::new(app::<T>(this).should_open_untitled_file())
}

/// Fired when the application receives an `applicationShouldTerminateAfterLastWindowClosed:` message.
extern "C" fn should_terminate_after_last_window_closed<T: AppDelegate>(this: &Object, _: Sel, _: id) -> BOOL {
match app::<T>(this).should_terminate_after_last_window_closed() {
true => YES,
false => NO
}
extern "C" fn should_terminate_after_last_window_closed<T: AppDelegate>(this: &Object, _: Sel, _: id) -> Bool {
Bool::new(app::<T>(this).should_terminate_after_last_window_closed())
}

/// Fired when the application receives an `applicationOpenUntitledFile:` message.
extern "C" fn open_untitled_file<T: AppDelegate>(this: &Object, _: Sel, _: id) -> BOOL {
match app::<T>(this).open_untitled_file() {
true => YES,
false => NO
}
extern "C" fn open_untitled_file<T: AppDelegate>(this: &Object, _: Sel, _: id) -> Bool {
Bool::new(app::<T>(this).open_untitled_file())
}

/// Fired when the application receives an `application:openTempFile:` message.
extern "C" fn open_temp_file<T: AppDelegate>(this: &Object, _: Sel, _: id, filename: id) -> BOOL {
extern "C" fn open_temp_file<T: AppDelegate>(this: &Object, _: Sel, _: id, filename: id) -> Bool {
let filename = NSString::retain(filename);

match app::<T>(this).open_temp_file(filename.to_str()) {
true => YES,
false => NO
}
Bool::new(app::<T>(this).open_temp_file(filename.to_str()))
}

/// Fired when the application receives an `application:printFile:` message.
extern "C" fn print_file<T: AppDelegate>(this: &Object, _: Sel, _: id, file: id) -> BOOL {
extern "C" fn print_file<T: AppDelegate>(this: &Object, _: Sel, _: id, file: id) -> Bool {
let filename = NSString::retain(file);

match app::<T>(this).print_file(filename.to_str()) {
true => YES,
false => NO
}
Bool::new(app::<T>(this).print_file(filename.to_str()))
}

/// Fired when the application receives an `application:printFiles:withSettings:showPrintPanels:`
Expand All @@ -260,7 +233,7 @@ extern "C" fn print_files<T: AppDelegate>(
_: id,
files: id,
settings: id,
show_print_panels: BOOL
show_print_panels: Bool
) -> NSUInteger {
let files = NSArray::retain(files)
.iter()
Expand All @@ -269,7 +242,9 @@ extern "C" fn print_files<T: AppDelegate>(

let settings = PrintSettings::with_inner(settings);

app::<T>(this).print_files(files, settings, to_bool(show_print_panels)).into()
app::<T>(this)
.print_files(files, settings, show_print_panels.as_bool())
.into()
}

/// Called when the application's occlusion state has changed.
Expand All @@ -280,13 +255,10 @@ extern "C" fn did_change_occlusion_state<T: AppDelegate>(this: &Object, _: Sel,
/// Called when the application receives an `application:delegateHandlesKey:` message.
/// Note: this may not fire in sandboxed applications. Apple's documentation is unclear on the
/// matter.
extern "C" fn delegate_handles_key<T: AppDelegate>(this: &Object, _: Sel, _: id, key: id) -> BOOL {
extern "C" fn delegate_handles_key<T: AppDelegate>(this: &Object, _: Sel, _: id, key: id) -> Bool {
let key = NSString::retain(key);

match app::<T>(this).delegate_handles_key(key.to_str()) {
true => YES,
false => NO
}
Bool::new(app::<T>(this).delegate_handles_key(key.to_str()))
}

/// Registers an `NSObject` application delegate, and configures it for the various callbacks and
Expand Down
6 changes: 3 additions & 3 deletions src/appkit/toolbar/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
use std::sync::Once;

use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel};
use objc::runtime::{Bool, Class, Object, Sel};
use objc::{class, msg_send, sel};

use crate::appkit::toolbar::{ToolbarDelegate, TOOLBAR_PTR};
use crate::foundation::{id, load_or_register_class, NSArray, NSString, BOOL};
use crate::foundation::{id, load_or_register_class, NSArray, NSString};
use crate::utils::load;

/// Retrieves and passes the allowed item identifiers for this toolbar.
Expand Down Expand Up @@ -54,7 +54,7 @@ extern "C" fn selectable_item_identifiers<T: ToolbarDelegate>(this: &Object, _:

/// Loads the controller, grabs whatever item is for this identifier, and returns what the
/// Objective-C runtime needs.
extern "C" fn item_for_identifier<T: ToolbarDelegate>(this: &Object, _: Sel, _: id, identifier: id, _: BOOL) -> id {
extern "C" fn item_for_identifier<T: ToolbarDelegate>(this: &Object, _: Sel, _: id, identifier: id, _: Bool) -> id {
let toolbar = load::<T>(this, TOOLBAR_PTR);
let identifier = NSString::from_retained(identifier);

Expand Down
11 changes: 4 additions & 7 deletions src/appkit/window/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@ use std::sync::Once;
use core_graphics::base::CGFloat;

use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel};
use objc::runtime::{Bool, Class, Object, Sel};
use objc::{class, sel};

use crate::appkit::window::{WindowDelegate, WINDOW_DELEGATE_PTR};
use crate::foundation::{id, load_or_register_class, NSUInteger, BOOL, NO, YES};
use crate::foundation::{id, load_or_register_class, NSUInteger};
use crate::utils::{load, CGSize};

/// Called when an `NSWindowDelegate` receives a `windowWillClose:` event.
/// Good place to clean up memory and what not.
extern "C" fn should_close<T: WindowDelegate>(this: &Object, _: Sel, _: id) -> BOOL {
extern "C" fn should_close<T: WindowDelegate>(this: &Object, _: Sel, _: id) -> Bool {
let window = load::<T>(this, WINDOW_DELEGATE_PTR);

match window.should_close() {
true => YES,
false => NO
}
Bool::new(window.should_close())
}

/// Called when an `NSWindowDelegate` receives a `windowWillClose:` event.
Expand Down
9 changes: 3 additions & 6 deletions src/image/image.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use objc::rc::{Id, Shared};
use objc::runtime::{Class, Object};
use objc::runtime::{Bool, Class, Object};

use objc::{class, msg_send, msg_send_id, sel};

Expand All @@ -12,7 +12,7 @@ use core_graphics::{
};

use super::icons::*;
use crate::foundation::{id, NSData, NSString, NO, NSURL, YES};
use crate::foundation::{id, NSData, NSString, NSURL};
use crate::utils::os;

/// Specifies resizing behavior for image drawing.
Expand Down Expand Up @@ -280,10 +280,7 @@ impl Image {

let _: () = msg_send![class!(NSGraphicsContext), restoreGraphicsState];

match result {
true => YES,
false => NO
}
Bool::new(result)
});
let block = block.copy();

Expand Down
18 changes: 6 additions & 12 deletions src/input/appkit.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use objc::rc::{Id, Owned};
use objc::runtime::{Class, Object, Sel, BOOL};
use objc::runtime::{Bool, Class, Object, Sel};
use objc::{msg_send, sel};

use crate::foundation::{id, load_or_register_class, NSString, NO, YES};
use crate::foundation::{id, load_or_register_class, NSString};
use crate::input::{TextFieldDelegate, TEXTFIELD_DELEGATE_PTR};
use crate::utils::load;

Expand All @@ -25,23 +25,17 @@ extern "C" fn text_did_change<T: TextFieldDelegate>(this: &Object, _: Sel, _info
view.text_did_change(s.to_str());
}

extern "C" fn text_should_begin_editing<T: TextFieldDelegate>(this: &Object, _: Sel, _info: id) -> BOOL {
extern "C" fn text_should_begin_editing<T: TextFieldDelegate>(this: &Object, _: Sel, _info: id) -> Bool {
let view = load::<T>(this, TEXTFIELD_DELEGATE_PTR);
let s = NSString::retain(unsafe { msg_send![this, stringValue] });

match view.text_should_begin_editing(s.to_str()) {
true => YES,
false => NO
}
Bool::new(view.text_should_begin_editing(s.to_str()))
}

extern "C" fn text_should_end_editing<T: TextFieldDelegate>(this: &Object, _: Sel, _info: id) -> BOOL {
extern "C" fn text_should_end_editing<T: TextFieldDelegate>(this: &Object, _: Sel, _info: id) -> Bool {
let view = load::<T>(this, TEXTFIELD_DELEGATE_PTR);
let s = NSString::retain(unsafe { msg_send![this, stringValue] });
match view.text_should_end_editing(s.to_str()) {
true => YES,
false => NO
}
Bool::new(view.text_should_end_editing(s.to_str()))
}

/// Injects an `NSTextField` subclass. This is used for the default views that don't use delegates - we
Expand Down
18 changes: 6 additions & 12 deletions src/input/uikit.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::sync::Once;

use objc::declare::ClassDecl;
use objc::runtime::{Class, Object, Sel, BOOL};
use objc::runtime::{Bool, Class, Object, Sel};
use objc::{class, msg_send, sel};

use crate::foundation::{id, load_or_register_class, NSString, NSUInteger, NO, YES};
use crate::foundation::{id, load_or_register_class, NSString, NSUInteger};
use crate::input::{TextFieldDelegate, TEXTFIELD_DELEGATE_PTR};
use crate::utils::load;

Expand All @@ -27,23 +27,17 @@ extern "C" fn text_did_change<T: TextFieldDelegate>(this: &Object, _: Sel, _info
view.text_did_change(s.to_str());
}

extern "C" fn text_should_begin_editing<T: TextFieldDelegate>(this: &Object, _: Sel, _info: id) -> BOOL {
extern "C" fn text_should_begin_editing<T: TextFieldDelegate>(this: &Object, _: Sel, _info: id) -> Bool {
let view = load::<T>(this, TEXTFIELD_DELEGATE_PTR);
let s = NSString::retain(unsafe { msg_send![this, text] });

match view.text_should_begin_editing(s.to_str()) {
true => YES,
false => NO
}
Bool::new(view.text_should_begin_editing(s.to_str()))
}

extern "C" fn text_should_end_editing<T: TextFieldDelegate>(this: &Object, _: Sel, _info: id) -> BOOL {
extern "C" fn text_should_end_editing<T: TextFieldDelegate>(this: &Object, _: Sel, _info: id) -> Bool {
let view = load::<T>(this, TEXTFIELD_DELEGATE_PTR);
let s = NSString::retain(unsafe { msg_send![this, text] });
match view.text_should_end_editing(s.to_str()) {
true => YES,
false => NO
}
Bool::new(view.text_should_end_editing(s.to_str()))
}

/// Injects an `UITextField` subclass. This is used for the default views that don't use delegates - we
Expand Down
Loading

0 comments on commit ee57388

Please sign in to comment.