Skip to content

Commit

Permalink
Fix some invalid msg_send! usage (#278)
Browse files Browse the repository at this point in the history
Co-authored-by: madsmtm <[email protected]>
  • Loading branch information
Ngo Iok Ui (Wu Yu Wei) and madsmtm authored Jan 13, 2022
1 parent 9d5da0c commit 8c215d8
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 43 deletions.
3 changes: 2 additions & 1 deletion src/platform_impl/ios/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
// window size/position.
macro_rules! assert_main_thread {
($($t:tt)*) => {
if !msg_send![class!(NSThread), isMainThread] {
let is_main_thread: ::objc::runtime::BOOL = msg_send!(class!(NSThread), isMainThread);
if is_main_thread == ::objc::runtime::NO {
panic!($($t)*);
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/ios/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl Inner {
let uiscreen = match monitor {
Some(Fullscreen::Exclusive(video_mode)) => {
let uiscreen = video_mode.video_mode.monitor.ui_screen() as id;
let () = msg_send![uiscreen, setCurrentMode: video_mode.video_mode.screen_mode];
let () = msg_send![uiscreen, setCurrentMode: video_mode.video_mode.screen_mode.0];
uiscreen
}
Some(Fullscreen::Borderless(monitor)) => monitor
Expand Down
21 changes: 11 additions & 10 deletions src/platform_impl/macos/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ use cocoa::{
base::{id, nil},
foundation::{NSAutoreleasePool, NSSize},
};
use objc::runtime::YES;

use objc::runtime::Object;
use objc::runtime::{Object, BOOL, NO, YES};

use crate::{
dpi::LogicalSize,
Expand Down Expand Up @@ -347,14 +345,16 @@ impl AppState {
}

pub fn queue_event(wrapper: EventWrapper) {
if !unsafe { msg_send![class!(NSThread), isMainThread] } {
let is_main_thread: BOOL = unsafe { msg_send!(class!(NSThread), isMainThread) };
if is_main_thread == NO {
panic!("Event queued from different thread: {:#?}", wrapper);
}
HANDLER.events().push_back(wrapper);
}

pub fn queue_events(mut wrappers: VecDeque<EventWrapper>) {
if !unsafe { msg_send![class!(NSThread), isMainThread] } {
let is_main_thread: BOOL = unsafe { msg_send!(class!(NSThread), isMainThread) };
if is_main_thread == NO {
panic!("Events queued from different thread: {:#?}", wrappers);
}
HANDLER.events().append(&mut wrappers);
Expand Down Expand Up @@ -388,8 +388,9 @@ impl AppState {

let dialog_open = if window_count > 1 {
let dialog: id = msg_send![windows, lastObject];
let is_main_window: bool = msg_send![dialog, isMainWindow];
msg_send![dialog, isVisible] && !is_main_window
let is_main_window: BOOL = msg_send![dialog, isMainWindow];
let is_visible: BOOL = msg_send![dialog, isVisible];
is_visible != NO && is_main_window == NO
} else {
false
};
Expand All @@ -404,9 +405,9 @@ impl AppState {
pool.drain();

if window_count > 0 {
let window: id = msg_send![windows, objectAtIndex:0];
let window_has_focus = msg_send![window, isKeyWindow];
if !dialog_open && window_has_focus && dialog_is_closing {
let window: id = msg_send![windows, firstObject];
let window_has_focus: BOOL = msg_send![window, isKeyWindow];
if !dialog_open && window_has_focus != NO && dialog_is_closing {
HANDLER.dialog_is_closing.store(false, Ordering::SeqCst);
}
if dialog_open {
Expand Down
21 changes: 11 additions & 10 deletions src/platform_impl/macos/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ use std::{
};

use cocoa::{
appkit::{NSApp, NSEventType::NSApplicationDefined},
base::{id, nil, YES},
foundation::{NSAutoreleasePool, NSPoint},
appkit::{NSApp, NSEventModifierFlags, NSEventSubtype, NSEventType::NSApplicationDefined},
base::{id, nil, BOOL, NO, YES},
foundation::{NSAutoreleasePool, NSInteger, NSPoint, NSTimeInterval},
};
use crossbeam_channel::{self as channel, Receiver, Sender};
use scopeguard::defer;
Expand Down Expand Up @@ -107,7 +107,8 @@ pub struct EventLoop<T: 'static> {
impl<T> EventLoop<T> {
pub fn new() -> Self {
let delegate = unsafe {
if !msg_send![class!(NSThread), isMainThread] {
let is_main_thread: BOOL = msg_send!(class!(NSThread), isMainThread);
if is_main_thread == NO {
panic!("On macOS, `EventLoop` must be created on the main thread!");
}

Expand Down Expand Up @@ -200,13 +201,13 @@ pub unsafe fn post_dummy_event(target: id) {
event_class,
otherEventWithType: NSApplicationDefined
location: NSPoint::new(0.0, 0.0)
modifierFlags: 0
timestamp: 0
windowNumber: 0
modifierFlags: NSEventModifierFlags::empty()
timestamp: 0 as NSTimeInterval
windowNumber: 0 as NSInteger
context: nil
subtype: 0
data1: 0
data2: 0
subtype: NSEventSubtype::NSWindowExposedEventType
data1: 0 as NSInteger
data2: 0 as NSInteger
];
let () = msg_send![target, postEvent: dummy_event atStart: YES];
}
Expand Down
1 change: 1 addition & 0 deletions src/platform_impl/macos/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ pub const kCGCursorWindowLevelKey: NSInteger = 19;
pub const kCGNumberOfWindowLevelKeys: NSInteger = 20;

#[derive(Debug, Clone, Copy)]
#[repr(isize)]
pub enum NSWindowLevel {
NSNormalWindowLevel = kCGBaseWindowLevelKey as _,
NSFloatingWindowLevel = kCGFloatingWindowLevelKey as _,
Expand Down
5 changes: 3 additions & 2 deletions src/platform_impl/macos/util/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use dispatch::Queue;
use menu::Menu;
use objc::{
rc::autoreleasepool,
runtime::{NO, YES},
runtime::{BOOL, NO, YES},
};

use crate::{
Expand Down Expand Up @@ -57,7 +57,8 @@ pub unsafe fn set_style_mask_async(ns_window: id, ns_view: id, mask: NSWindowSty
});
}
pub unsafe fn set_style_mask_sync(ns_window: id, ns_view: id, mask: NSWindowStyleMask) {
if msg_send![class!(NSThread), isMainThread] {
let is_main_thread: BOOL = msg_send!(class!(NSThread), isMainThread);
if is_main_thread != NO {
set_style_mask(ns_window, ns_view, mask);
} else {
let ns_window = MainThreadSafe(ns_window);
Expand Down
11 changes: 4 additions & 7 deletions src/platform_impl/macos/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl IdRef {
#[allow(dead_code)]
pub fn retain(inner: id) -> IdRef {
if inner != nil {
let () = unsafe { msg_send![inner, retain] };
let _: id = unsafe { msg_send![inner, retain] };
}
IdRef(inner)
}
Expand Down Expand Up @@ -84,10 +84,7 @@ impl Deref for IdRef {

impl Clone for IdRef {
fn clone(&self) -> IdRef {
if self.0 != nil {
let _: id = unsafe { msg_send![self.0, retain] };
}
IdRef(self.0)
IdRef::retain(self.0)
}
}

Expand Down Expand Up @@ -147,8 +144,8 @@ pub unsafe fn app_name() -> Option<id> {
}

pub unsafe fn superclass<'a>(this: &'a Object) -> &'a Class {
let superclass: id = msg_send![this, superclass];
&*(superclass as *const _)
let superclass: *const Class = msg_send![this, superclass];
&*superclass
}

pub unsafe fn create_input_context(view: id) -> IdRef {
Expand Down
16 changes: 8 additions & 8 deletions src/platform_impl/macos/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
boxed::Box,
collections::{HashSet, VecDeque},
os::raw::*,
slice, str,
ptr, slice, str,
sync::{Arc, Mutex, Weak},
};

Expand Down Expand Up @@ -349,7 +349,7 @@ extern "C" fn view_did_move_to_window(this: &Object, _sel: Sel) {
let tracking_rect: NSInteger = msg_send![this,
addTrackingRect:rect
owner:this
userData:nil
userData:ptr::null_mut::<c_void>()
assumeInside:NO
];
state.tracking_rect = Some(tracking_rect);
Expand All @@ -370,7 +370,7 @@ extern "C" fn frame_did_change(this: &Object, _sel: Sel, _event: id) {
let tracking_rect: NSInteger = msg_send![this,
addTrackingRect:rect
owner:this
userData:nil
userData:ptr::null_mut::<c_void>()
assumeInside:NO
];

Expand Down Expand Up @@ -462,8 +462,8 @@ extern "C" fn set_marked_text(
trace!("Triggered `setMarkedText`");
unsafe {
let marked_text_ref = clear_marked_text(this);
let has_attr = msg_send![string, isKindOfClass: class!(NSAttributedString)];
if has_attr {
let has_attr: BOOL = msg_send![string, isKindOfClass: class!(NSAttributedString)];
if has_attr != NO {
marked_text_ref.initWithAttributedString(string);
} else {
marked_text_ref.initWithString(string);
Expand Down Expand Up @@ -538,8 +538,8 @@ extern "C" fn insert_text(this: &Object, _sel: Sel, string: id, _replacement_ran
let state_ptr: *mut c_void = *this.get_ivar("taoState");
let state = &mut *(state_ptr as *mut ViewState);

let has_attr = msg_send![string, isKindOfClass: class!(NSAttributedString)];
let characters = if has_attr {
let has_attr: BOOL = msg_send![string, isKindOfClass: class!(NSAttributedString)];
let characters = if has_attr != NO {
// This is a *mut NSAttributedString
msg_send![string, string]
} else {
Expand Down Expand Up @@ -1006,7 +1006,7 @@ fn mouse_motion(this: &Object, event: id) {
|| view_point.x > view_rect.size.width
|| view_point.y > view_rect.size.height
{
let mouse_buttons_down: NSInteger = msg_send![class!(NSEvent), pressedMouseButtons];
let mouse_buttons_down: NSUInteger = msg_send![class!(NSEvent), pressedMouseButtons];
if mouse_buttons_down == 0 {
// Point is outside of the client area (view) and no buttons are pressed
return;
Expand Down
10 changes: 6 additions & 4 deletions src/platform_impl/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use raw_window_handle::{AppKitHandle, RawWindowHandle};
use std::{
collections::VecDeque,
convert::TryInto,
f64,
os::raw::c_void,
sync::{
Expand Down Expand Up @@ -38,7 +39,7 @@ use cocoa::{
NSWindowStyleMask,
},
base::{id, nil},
foundation::{NSAutoreleasePool, NSDictionary, NSPoint, NSRect, NSSize},
foundation::{NSAutoreleasePool, NSDictionary, NSPoint, NSRect, NSSize, NSUInteger},
};
use core_graphics::display::{CGDisplay, CGDisplayMode};
use objc::{
Expand Down Expand Up @@ -347,7 +348,8 @@ impl UnownedWindow {
pl_attribs: PlatformSpecificWindowBuilderAttributes,
) -> Result<(Arc<Self>, IdRef), RootOsError> {
unsafe {
if !msg_send![class!(NSThread), isMainThread] {
let is_main_thread: BOOL = msg_send!(class!(NSThread), isMainThread);
if is_main_thread == NO {
panic!("Windows can only be created on the main thread on macOS");
}
}
Expand Down Expand Up @@ -1089,9 +1091,9 @@ impl UnownedWindow {
let desc = NSScreen::deviceDescription(screen);
let key = util::ns_string_id_ref("NSScreenNumber");
let value = NSDictionary::valueForKey_(desc, *key);
let display_id = msg_send![value, unsignedIntegerValue];
let display_id: NSUInteger = msg_send![value, unsignedIntegerValue];
RootMonitorHandle {
inner: MonitorHandle::new(display_id),
inner: MonitorHandle::new(display_id.try_into().unwrap()),
}
}
}
Expand Down

0 comments on commit 8c215d8

Please sign in to comment.