Fix some invalid msg_send! usage (#2138)

* Fix some invalid msg_send! usage

* Make the implementation of superclass clearer
This commit is contained in:
Mads Marquart 2022-01-10 21:39:17 +01:00 committed by GitHub
parent 8af222c1e3
commit 2a2abc4843
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 53 additions and 45 deletions

View file

@ -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)*);
}
};

View file

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

View file

@ -19,7 +19,7 @@ use cocoa::{
};
use objc::{
rc::autoreleasepool,
runtime::{Object, YES},
runtime::{Object, BOOL, NO, YES},
};
use crate::{
@ -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);
@ -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
};
@ -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 {

View file

@ -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;
@ -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!");
}
@ -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];
}

View file

@ -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 _,

View file

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

View file

@ -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)
}
@ -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)
}
}
@ -118,8 +114,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 {

View file

@ -2,7 +2,7 @@ use std::{
boxed::Box,
collections::VecDeque,
os::raw::*,
slice, str,
ptr, slice, str,
sync::{Arc, Mutex, Weak},
};
@ -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);
@ -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
];
@ -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);
@ -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];
let input_context: id = msg_send![this, inputContext];
let _: () = msg_send![input_context, discardMarkedText];
}
@ -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 {
@ -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);
@ -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,
@ -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;

View file

@ -1,6 +1,7 @@
use raw_window_handle::{AppKitHandle, RawWindowHandle};
use std::{
collections::VecDeque,
convert::TryInto,
f64,
os::raw::c_void,
sync::{
@ -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::{
@ -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");
}
}
@ -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()),
}
}
}