Skip to content

Commit

Permalink
Replace BOOL usage with normal bool, which is possible with objc2
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Sep 12, 2023
1 parent af44a5a commit d071b68
Show file tree
Hide file tree
Showing 45 changed files with 173 additions and 345 deletions.
7 changes: 5 additions & 2 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,13 @@ impl View {
/// so on. It returns a generic `View<T>`, which the caller can then customize as needed.
pub(crate) fn init<T>(view: id) -> View<T> {
unsafe {
let _: () = msg_send![view, setTranslatesAutoresizingMaskIntoConstraints:NO];
let _: () = msg_send![
view,
setTranslatesAutoresizingMaskIntoConstraints: false,
];

#[cfg(target_os = "macos")]
let _: () = msg_send![view, setWantsLayer:YES];
let _: () = msg_send![view, setWantsLayer: true];
}

View {
Expand Down
7 changes: 2 additions & 5 deletions src/appkit/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use objc::runtime::Object;
use objc::{class, msg_send, msg_send_id, sel};

use crate::appkit::menu::Menu;
use crate::foundation::{id, nil, AutoReleasePool, NSUInteger, NO, YES};
use crate::foundation::{id, nil, AutoReleasePool, NSUInteger};
use crate::invoker::TargetActionHandler;
use crate::notification_center::Dispatcher;
use crate::utils::activate_cocoa_multithreading;
Expand Down Expand Up @@ -246,10 +246,7 @@ impl App {
/// from your trait implementation of `should_terminate()`.
pub fn reply_to_termination_request(should_terminate: bool) {
shared_application(|app| unsafe {
let _: () = msg_send![app, replyToApplicationShouldTerminate:match should_terminate {
true => YES,
false => NO
}];
let _: () = msg_send![app, replyToApplicationShouldTerminate: should_terminate];
});
}

Expand Down
7 changes: 2 additions & 5 deletions src/appkit/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use objc::{class, msg_send, sel};

use crate::foundation::{id, NO, YES};
use crate::foundation::id;

/// Represents a type of cursor that you can associate with mouse movement.
/// @TODO: Loading?
Expand Down Expand Up @@ -163,10 +163,7 @@ impl Cursor {
/// Trying to invert this with `unhide` will result in undefined system behavior.
pub fn set_hidden_until_mouse_moves(status: bool) {
unsafe {
let _: () = msg_send![class!(NSCursor), setHiddenUntilMouseMoves:match status {
true => YES,
false => NO
}];
let _: () = msg_send![class!(NSCursor), setHiddenUntilMouseMoves: status];
}
}
}
18 changes: 6 additions & 12 deletions src/appkit/segmentedcontrol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use std::rc::Rc;

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

use crate::color::Color;
use crate::control::Control;
use crate::foundation::{id, nil, NSArray, NSString, NSUInteger, BOOL, NO, YES};
use crate::foundation::{id, nil, NSArray, NSString, NSUInteger};
use crate::image::Image;
use crate::invoker::TargetActionHandler;
use crate::keys::Key;
Expand Down Expand Up @@ -120,10 +120,10 @@ impl SegmentedControl {
action:nil
];

let _: () = msg_send![control, setWantsLayer: YES];
let _: () = msg_send![control, setWantsLayer: true];

#[cfg(feature = "autolayout")]
let _: () = msg_send![control, setTranslatesAutoresizingMaskIntoConstraints: NO];
let _: () = msg_send![control, setTranslatesAutoresizingMaskIntoConstraints: false];

control
};
Expand Down Expand Up @@ -253,10 +253,7 @@ impl SegmentedControl {
#[cfg(feature = "appkit")]
pub fn set_bordered(&self, is_bordered: bool) {
self.objc.with_mut(|obj| unsafe {
let _: () = msg_send![obj, setBordered:match is_bordered {
true => YES,
false => NO
}];
let _: () = msg_send![obj, setBordered: is_bordered];
});
}

Expand Down Expand Up @@ -284,10 +281,7 @@ impl SegmentedControl {
/// Toggles the highlighted status of the button.
pub fn set_highlighted(&self, highlight: bool) {
self.objc.with_mut(|obj| unsafe {
let _: () = msg_send![obj, highlight:match highlight {
true => YES,
false => NO
}];
let _: () = msg_send![obj, highlight: highlight];
});
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/appkit/toolbar/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use objc::{class, msg_send, msg_send_id, sel};

use crate::appkit::segmentedcontrol::SegmentedControl;
use crate::button::{BezelStyle, Button};
use crate::foundation::{id, NSString, NO, YES};
use crate::foundation::{id, NSString};
use crate::image::Image;
use crate::invoker::TargetActionHandler;

Expand Down Expand Up @@ -110,10 +110,7 @@ impl ToolbarItem {

pub fn set_bordered(&self, bordered: bool) {
unsafe {
let _: () = msg_send![&*self.objc, setBordered:match bordered {
true => YES,
false => NO
}];
let _: () = msg_send![&*self.objc, setBordered: bordered];
}
}
}
12 changes: 3 additions & 9 deletions src/appkit/toolbar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use objc::rc::{Id, Owned, Shared};
use objc::runtime::Object;
use objc::{class, msg_send, msg_send_id, sel};

use crate::foundation::{id, nil, NSString, NSUInteger, NO, YES};
use crate::foundation::{id, nil, NSString, NSUInteger};

mod class;
use class::register_toolbar_class;
Expand Down Expand Up @@ -88,10 +88,7 @@ impl<T> Toolbar<T> {
/// contents.
pub fn set_shows_baseline_separator(&self, shows: bool) {
unsafe {
let _: () = msg_send![&*self.objc, setShowsBaselineSeparator:match shows {
true => YES,
false => NO
}];
let _: () = msg_send![&*self.objc, setShowsBaselineSeparator: shows];
}
}

Expand All @@ -116,10 +113,7 @@ impl<T> Toolbar<T> {
/// Set whether the toolbar is visible or not.
pub fn set_visible(&self, visibility: bool) {
unsafe {
let _: () = msg_send![&*self.objc, setVisible:match visibility {
true => YES,
false => NO
}];
let _: () = msg_send![&*self.objc, setVisible: visibility];
}
}

Expand Down
66 changes: 24 additions & 42 deletions src/appkit/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use objc::{class, msg_send, msg_send_id, sel};

use crate::appkit::toolbar::{Toolbar, ToolbarDelegate};
use crate::color::Color;
use crate::foundation::{id, nil, to_bool, NSInteger, NSString, NSUInteger, NO, YES};
use crate::foundation::{id, nil, NSInteger, NSString, NSUInteger};
use crate::layout::Layout;
use crate::objc_access::ObjcAccess;
use crate::utils::{os, Controller};
Expand Down Expand Up @@ -66,9 +66,9 @@ impl Window {
/// after we initialize the backing `NSWindow`.
pub fn new(config: WindowConfig) -> Window {
let objc = unsafe {
// This behavior might make sense to keep as default (YES), but I think the majority of
// This behavior might make sense to keep as default (true), but I think the majority of
// apps that would use this toolkit wouldn't be tab-oriented...
let _: () = msg_send![class!(NSWindow), setAllowsAutomaticWindowTabbing: NO];
let _: () = msg_send![class!(NSWindow), setAllowsAutomaticWindowTabbing: false];

// Other types of backing (Retained/NonRetained) are archaic, dating back to the
// NeXTSTEP era, and are outright deprecated... so we don't allow setting them.
Expand All @@ -79,19 +79,16 @@ impl Window {
initWithContentRect: dimensions,
styleMask: config.style,
backing: buffered,
defer: match config.defer {
true => YES,
false => NO
},
defer: config.defer,
];

// This is very important! NSWindow is an old class and has some behavior that we need
// to disable, like... this. If we don't set this, we'll segfault entirely because the
// Objective-C runtime gets out of sync by releasing the window out from underneath of
// us.
let _: () = msg_send![&*window, setReleasedWhenClosed: NO];
let _: () = msg_send![&*window, setReleasedWhenClosed: false];

let _: () = msg_send![&*window, setRestorable: NO];
let _: () = msg_send![&*window, setRestorable: false];

// This doesn't exist prior to Big Sur, but is important to support for Big Sur.
//
Expand Down Expand Up @@ -131,9 +128,9 @@ where
let mut delegate = Box::new(delegate);

let objc: Id<Object, Shared> = unsafe {
// This behavior might make sense to keep as default (YES), but I think the majority of
// This behavior might make sense to keep as default (true), but I think the majority of
// apps that would use this toolkit wouldn't be tab-oriented...
let _: () = msg_send![class!(NSWindow), setAllowsAutomaticWindowTabbing: NO];
let _: () = msg_send![class!(NSWindow), setAllowsAutomaticWindowTabbing: false];

// Other types of backing (Retained/NonRetained) are archaic, dating back to the
// NeXTSTEP era, and are outright deprecated... so we don't allow setting them.
Expand All @@ -144,10 +141,7 @@ where
initWithContentRect: dimensions,
styleMask: config.style,
backing: buffered,
defer: match config.defer {
true => YES,
false => NO
},
defer: config.defer,
];

let delegate_ptr: *const T = &*delegate;
Expand All @@ -157,12 +151,12 @@ where
// to disable, like... this. If we don't set this, we'll segfault entirely because the
// Objective-C runtime gets out of sync by releasing the window out from underneath of
// us.
let _: () = msg_send![&*window, setReleasedWhenClosed: NO];
let _: () = msg_send![&*window, setReleasedWhenClosed: false];

// We set the window to be its own delegate - this is cleaned up inside `Drop`.
let _: () = msg_send![&*window, setDelegate: &*window];

let _: () = msg_send![&*window, setRestorable: NO];
let _: () = msg_send![&*window, setRestorable: false];

// This doesn't exist prior to Big Sur, but is important to support for Big Sur.
//
Expand Down Expand Up @@ -225,20 +219,14 @@ impl<T> Window<T> {
/// Used for configuring whether the window is movable via the background.
pub fn set_movable_by_background(&self, movable: bool) {
unsafe {
let _: () = msg_send![&*self.objc, setMovableByWindowBackground:match movable {
true => YES,
false => NO
}];
let _: () = msg_send![&*self.objc, setMovableByWindowBackground: movable];
}
}

/// Used for setting whether this titlebar appears transparent.
pub fn set_titlebar_appears_transparent(&self, transparent: bool) {
unsafe {
let _: () = msg_send![&*self.objc, setTitlebarAppearsTransparent:match transparent {
true => YES,
false => NO
}];
let _: () = msg_send![&*self.objc, setTitlebarAppearsTransparent: transparent];
}
}

Expand Down Expand Up @@ -314,10 +302,7 @@ impl<T> Window<T> {
/// window.
pub fn set_shows_toolbar_button(&self, shows: bool) {
unsafe {
let _: () = msg_send![&*self.objc, setShowsToolbarButton:match shows {
true => YES,
false => NO
}];
let _: () = msg_send![&*self.objc, setShowsToolbarButton: shows];
}
}

Expand Down Expand Up @@ -377,12 +362,12 @@ impl<T> Window<T> {

/// Returns whether this window is opaque or not.
pub fn is_opaque(&self) -> bool {
to_bool(unsafe { msg_send![&*self.objc, isOpaque] })
unsafe { msg_send![&*self.objc, isOpaque] }
}

/// Returns whether this window is miniaturized or not.
pub fn is_miniaturized(&self) -> bool {
to_bool(unsafe { msg_send![&*self.objc, isMiniaturized] })
unsafe { msg_send![&*self.objc, isMiniaturized] }
}

/// Miniaturize this window.
Expand Down Expand Up @@ -411,27 +396,27 @@ impl<T> Window<T> {
///
/// From Apple's documentation:
///
/// _The value of this property is YES if the window is on the currently active space; otherwise, NO.
/// _The value of this property is `true` if the window is on the currently active space; otherwise, `false`.
/// For visible windows, this property indicates whether the window is currently visible on the active
/// 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 {
to_bool(unsafe { msg_send![&*self.objc, isOnActiveSpace] })
unsafe { msg_send![&*self.objc, isOnActiveSpace] }
}

/// Returns whether this window is visible or not.
pub fn is_visible(&self) -> bool {
to_bool(unsafe { msg_send![&*self.objc, isVisible] })
unsafe { msg_send![&*self.objc, isVisible] }
}

/// Returns whether this window is the key or not.
pub fn is_key(&self) -> bool {
to_bool(unsafe { msg_send![&*self.objc, isKeyWindow] })
unsafe { msg_send![&*self.objc, isKeyWindow] }
}

/// Returns whether this window can become the key window.
pub fn can_become_key(&self) -> bool {
to_bool(unsafe { msg_send![&*self.objc, canBecomeKeyWindow] })
unsafe { msg_send![&*self.objc, canBecomeKeyWindow] }
}

/// Make this window the key window.
Expand All @@ -451,21 +436,18 @@ impl<T> Window<T> {

/// Returns if this is the main window or not.
pub fn is_main_window(&self) -> bool {
to_bool(unsafe { msg_send![&*self.objc, isMainWindow] })
unsafe { msg_send![&*self.objc, isMainWindow] }
}

/// Returns if this can become the main window.
pub fn can_become_main_window(&self) -> bool {
to_bool(unsafe { msg_send![&*self.objc, canBecomeMainWindow] })
unsafe { msg_send![&*self.objc, canBecomeMainWindow] }
}

/// Set whether this window should be excluded from the top-level "Windows" menu.
pub fn set_excluded_from_windows_menu(&self, excluded: bool) {
unsafe {
let _: () = msg_send![&*self.objc, setExcludedFromWindowsMenu:match excluded {
true => YES,
false => NO
}];
let _: () = msg_send![&*self.objc, setExcludedFromWindowsMenu: excluded];
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use objc::ffi;
use objc::runtime::{Class, Imp, Object, Sel};
use objc::{class, msg_send, sel, Encode, EncodeArguments, Encoding, Message};

use crate::foundation::{id, nil, BOOL, YES, NSString};
use crate::foundation::{id, nil, NSString};

/// Types that can be used as the implementation of an Objective-C method.
pub trait MethodImplementation {
Expand Down Expand Up @@ -56,8 +56,8 @@ extern "C" fn get_bundle_id(this: &Object, s: Sel, v: id) -> id {
unsafe {
let bundle = class!(NSBundle);
let main_bundle: id = msg_send![bundle, mainBundle];
let e: BOOL = msg_send![this, isEqual:main_bundle];
if e == YES {
let e = msg_send![this, isEqual:main_bundle];
if e {
let url: id = msg_send![main_bundle, bundleURL];
let x: id = msg_send![url, absoluteString];
println!("Got here? {:?}", x);
Expand Down
Loading

0 comments on commit d071b68

Please sign in to comment.