Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some invalid msg_send! usage #2138

Merged
merged 2 commits into from Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/platform_impl/ios/mod.rs
Expand Up @@ -62,7 +62,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
3 changes: 2 additions & 1 deletion src/platform_impl/ios/window.rs
Expand Up @@ -204,7 +204,8 @@ 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
19 changes: 11 additions & 8 deletions src/platform_impl/macos/app_state.rs
Expand Up @@ -19,7 +19,7 @@ use cocoa::{
};
use objc::{
rc::autoreleasepool,
runtime::{Object, YES},
runtime::{Object, BOOL, NO, YES},
};

use crate::{
Expand Down Expand Up @@ -356,14 +356,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 @@ -400,8 +402,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 @@ -419,9 +422,9 @@ impl AppState {
});

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
Expand Up @@ -12,9 +12,9 @@ use std::{
};

use cocoa::{
appkit::{NSApp, NSEventType::NSApplicationDefined},
base::{id, nil, YES},
foundation::NSPoint,
appkit::{NSApp, NSEventModifierFlags, NSEventSubtype, NSEventType::NSApplicationDefined},
base::{id, nil, BOOL, NO, YES},
foundation::{NSInteger, NSPoint, NSTimeInterval},
};
use objc::rc::autoreleasepool;

Expand Down Expand Up @@ -117,7 +117,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 @@ -208,13 +209,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
Expand Up @@ -107,6 +107,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
Expand Up @@ -10,7 +10,7 @@ use cocoa::{
};
use dispatch::Queue;
use objc::rc::autoreleasepool;
use objc::runtime::NO;
use objc::runtime::{BOOL, NO};

use crate::{
dpi::LogicalSize,
Expand Down Expand Up @@ -51,7 +51,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: 3 additions & 8 deletions src/platform_impl/macos/util/mod.rs
Expand Up @@ -40,10 +40,9 @@ impl IdRef {
IdRef(inner)
}

#[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 @@ -76,10 +75,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 @@ -118,8 +114,7 @@ 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 _)
msg_send![this, superclass]
}
madsmtm marked this conversation as resolved.
Show resolved Hide resolved

pub unsafe fn create_input_context(view: id) -> IdRef {
Expand Down
24 changes: 13 additions & 11 deletions src/platform_impl/macos/view.rs
Expand Up @@ -2,7 +2,7 @@ use std::{
boxed::Box,
collections::VecDeque,
os::raw::*,
slice, str,
ptr, slice, str,
sync::{Arc, Mutex, Weak},
};

Expand Down Expand Up @@ -314,7 +314,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 @@ -335,7 +335,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 @@ -426,8 +426,8 @@ extern "C" fn set_marked_text(
let marked_text_ref: &mut id = this.get_mut_ivar("markedText");
let _: () = msg_send![(*marked_text_ref), release];
let marked_text = NSMutableAttributedString::alloc(nil);
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.initWithAttributedString(string);
} else {
marked_text.initWithString(string);
Expand All @@ -442,7 +442,9 @@ extern "C" fn unmark_text(this: &Object, _sel: Sel) {
unsafe {
let marked_text: id = *this.get_ivar("markedText");
let mutable_string = marked_text.mutableString();
let _: () = msg_send![mutable_string, setString:""];
let s: id = msg_send![class!(NSString), new];
let _: () = msg_send![mutable_string, setString: s];
let _: () = msg_send![s, release];
ArturKovacs marked this conversation as resolved.
Show resolved Hide resolved
let input_context: id = msg_send![this, inputContext];
let _: () = msg_send![input_context, discardMarkedText];
}
Expand Down Expand Up @@ -502,8 +504,8 @@ extern "C" fn insert_text(this: &Object, _sel: Sel, string: id, _replacement_ran
let state_ptr: *mut c_void = *this.get_ivar("winitState");
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 @@ -650,7 +652,7 @@ extern "C" fn key_down(this: &Object, _sel: Sel, event: id) {
let scancode = get_scancode(event) as u32;
let virtual_keycode = retrieve_keycode(event);

let is_repeat = msg_send![event, isARepeat];
let is_repeat: BOOL = msg_send![event, isARepeat];

update_potentially_stale_modifiers(state, event);

Expand All @@ -672,7 +674,7 @@ extern "C" fn key_down(this: &Object, _sel: Sel, event: id) {
let pass_along = {
AppState::queue_event(EventWrapper::StaticEvent(window_event));
// Emit `ReceivedCharacter` for key repeats
if is_repeat {
if is_repeat != NO {
for character in characters.chars().filter(|c| !is_corporate_character(*c)) {
AppState::queue_event(EventWrapper::StaticEvent(Event::WindowEvent {
window_id,
Expand Down Expand Up @@ -914,7 +916,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
@@ -1,6 +1,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::{
NSRequestUserAttentionType, NSScreen, NSView, NSWindow, NSWindowButton, NSWindowStyleMask,
},
base::{id, nil},
foundation::{NSDictionary, NSPoint, NSRect, NSSize},
foundation::{NSDictionary, NSPoint, NSRect, NSSize, NSUInteger},
};
use core_graphics::display::{CGDisplay, CGDisplayMode};
use objc::{
Expand Down Expand Up @@ -332,7 +333,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 @@ -1034,9 +1036,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