Initial transition to objc2 (#2452)
* Use objc2 * Use objc2's NSInteger/NSUInteger/NSRange
This commit is contained in:
parent
e0018d0710
commit
112965b4ff
23 changed files with 394 additions and 480 deletions
|
|
@ -5,7 +5,7 @@ use cocoa::{
|
|||
base::id,
|
||||
};
|
||||
use objc::{
|
||||
declare::ClassDecl,
|
||||
declare::ClassBuilder,
|
||||
runtime::{Class, Object, Sel},
|
||||
};
|
||||
use once_cell::sync::Lazy;
|
||||
|
|
@ -19,12 +19,9 @@ unsafe impl Sync for AppClass {}
|
|||
|
||||
pub static APP_CLASS: Lazy<AppClass> = Lazy::new(|| unsafe {
|
||||
let superclass = class!(NSApplication);
|
||||
let mut decl = ClassDecl::new("WinitApp", superclass).unwrap();
|
||||
let mut decl = ClassBuilder::new("WinitApp", superclass).unwrap();
|
||||
|
||||
decl.add_method(
|
||||
sel!(sendEvent:),
|
||||
send_event as extern "C" fn(&Object, Sel, id),
|
||||
);
|
||||
decl.add_method(sel!(sendEvent:), send_event as extern "C" fn(_, _, _));
|
||||
|
||||
AppClass(decl.register())
|
||||
});
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ use std::{
|
|||
|
||||
use cocoa::base::id;
|
||||
use objc::{
|
||||
declare::ClassDecl,
|
||||
declare::ClassBuilder,
|
||||
runtime::{Class, Object, Sel},
|
||||
};
|
||||
use once_cell::sync::Lazy;
|
||||
|
|
@ -25,18 +25,18 @@ unsafe impl Sync for AppDelegateClass {}
|
|||
|
||||
pub static APP_DELEGATE_CLASS: Lazy<AppDelegateClass> = Lazy::new(|| unsafe {
|
||||
let superclass = class!(NSResponder);
|
||||
let mut decl = ClassDecl::new("WinitAppDelegate", superclass).unwrap();
|
||||
let mut decl = ClassBuilder::new("WinitAppDelegate", superclass).unwrap();
|
||||
|
||||
decl.add_class_method(sel!(new), new as extern "C" fn(&Class, Sel) -> id);
|
||||
decl.add_method(sel!(dealloc), dealloc as extern "C" fn(&Object, Sel));
|
||||
decl.add_class_method(sel!(new), new as extern "C" fn(_, _) -> _);
|
||||
decl.add_method(sel!(dealloc), dealloc as extern "C" fn(_, _));
|
||||
|
||||
decl.add_method(
|
||||
sel!(applicationDidFinishLaunching:),
|
||||
did_finish_launching as extern "C" fn(&Object, Sel, id),
|
||||
did_finish_launching as extern "C" fn(_, _, _),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(applicationWillTerminate:),
|
||||
will_terminate as extern "C" fn(&Object, Sel, id),
|
||||
will_terminate as extern "C" fn(_, _, _),
|
||||
);
|
||||
|
||||
decl.add_ivar::<*mut c_void>(AUX_DELEGATE_STATE_NAME);
|
||||
|
|
@ -46,7 +46,7 @@ pub static APP_DELEGATE_CLASS: Lazy<AppDelegateClass> = Lazy::new(|| unsafe {
|
|||
|
||||
/// Safety: Assumes that Object is an instance of APP_DELEGATE_CLASS
|
||||
pub unsafe fn get_aux_state_mut(this: &Object) -> RefMut<'_, AuxDelegateState> {
|
||||
let ptr: *mut c_void = *this.get_ivar(AUX_DELEGATE_STATE_NAME);
|
||||
let ptr: *mut c_void = *this.ivar(AUX_DELEGATE_STATE_NAME);
|
||||
// Watch out that this needs to be the correct type
|
||||
(*(ptr as *mut RefCell<AuxDelegateState>)).borrow_mut()
|
||||
}
|
||||
|
|
@ -69,7 +69,7 @@ extern "C" fn new(class: &Class, _: Sel) -> id {
|
|||
|
||||
extern "C" fn dealloc(this: &Object, _: Sel) {
|
||||
unsafe {
|
||||
let state_ptr: *mut c_void = *(this.get_ivar(AUX_DELEGATE_STATE_NAME));
|
||||
let state_ptr: *mut c_void = *(this.ivar(AUX_DELEGATE_STATE_NAME));
|
||||
// As soon as the box is constructed it is immediately dropped, releasing the underlying
|
||||
// memory
|
||||
drop(Box::from_raw(state_ptr as *mut RefCell<AuxDelegateState>));
|
||||
|
|
|
|||
|
|
@ -18,8 +18,9 @@ use cocoa::{
|
|||
foundation::NSSize,
|
||||
};
|
||||
use objc::{
|
||||
foundation::is_main_thread,
|
||||
rc::autoreleasepool,
|
||||
runtime::{Object, BOOL, NO, YES},
|
||||
runtime::{Bool, Object},
|
||||
};
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
|
|
@ -288,7 +289,7 @@ impl AppState {
|
|||
let ns_app = NSApp();
|
||||
window_activation_hack(ns_app);
|
||||
// TODO: Consider allowing the user to specify they don't want their application activated
|
||||
ns_app.activateIgnoringOtherApps_(YES);
|
||||
ns_app.activateIgnoringOtherApps_(Bool::YES.as_raw());
|
||||
};
|
||||
HANDLER.set_ready();
|
||||
HANDLER.waker().start();
|
||||
|
|
@ -361,16 +362,14 @@ impl AppState {
|
|||
}
|
||||
|
||||
pub fn queue_event(wrapper: EventWrapper) {
|
||||
let is_main_thread: BOOL = unsafe { msg_send!(class!(NSThread), isMainThread) };
|
||||
if is_main_thread == NO {
|
||||
if !is_main_thread() {
|
||||
panic!("Event queued from different thread: {:#?}", wrapper);
|
||||
}
|
||||
HANDLER.events().push_back(wrapper);
|
||||
}
|
||||
|
||||
pub fn queue_events(mut wrappers: VecDeque<EventWrapper>) {
|
||||
let is_main_thread: BOOL = unsafe { msg_send!(class!(NSThread), isMainThread) };
|
||||
if is_main_thread == NO {
|
||||
if !is_main_thread() {
|
||||
panic!("Events queued from different thread: {:#?}", wrappers);
|
||||
}
|
||||
HANDLER.events().append(&mut wrappers);
|
||||
|
|
@ -403,7 +402,7 @@ impl AppState {
|
|||
unsafe {
|
||||
let app: id = NSApp();
|
||||
|
||||
autoreleasepool(|| {
|
||||
autoreleasepool(|_| {
|
||||
let _: () = msg_send![app, stop: nil];
|
||||
// To stop event loop immediately, we need to post some event here.
|
||||
post_dummy_event(app);
|
||||
|
|
@ -443,7 +442,7 @@ unsafe fn window_activation_hack(ns_app: id) {
|
|||
// And call `makeKeyAndOrderFront` if it was called on the window in `UnownedWindow::new`
|
||||
// This way we preserve the user's desired initial visiblity status
|
||||
// TODO: Also filter on the type/"level" of the window, and maybe other things?
|
||||
if ns_window.isVisible() == YES {
|
||||
if Bool::from_raw(ns_window.isVisible()).as_bool() {
|
||||
trace!("Activating visible window");
|
||||
ns_window.makeKeyAndOrderFront_(nil);
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -13,9 +13,10 @@ use std::{
|
|||
|
||||
use cocoa::{
|
||||
appkit::{NSApp, NSEventModifierFlags, NSEventSubtype, NSEventType::NSApplicationDefined},
|
||||
base::{id, nil, BOOL, NO, YES},
|
||||
foundation::{NSInteger, NSPoint, NSTimeInterval},
|
||||
base::{id, nil},
|
||||
foundation::{NSPoint, NSTimeInterval},
|
||||
};
|
||||
use objc::foundation::is_main_thread;
|
||||
use objc::rc::autoreleasepool;
|
||||
use raw_window_handle::{AppKitDisplayHandle, RawDisplayHandle};
|
||||
|
||||
|
|
@ -144,8 +145,7 @@ impl Default for PlatformSpecificEventLoopAttributes {
|
|||
impl<T> EventLoop<T> {
|
||||
pub(crate) fn new(attributes: &PlatformSpecificEventLoopAttributes) -> Self {
|
||||
let delegate = unsafe {
|
||||
let is_main_thread: BOOL = msg_send!(class!(NSThread), isMainThread);
|
||||
if is_main_thread == NO {
|
||||
if !is_main_thread() {
|
||||
panic!("On macOS, `EventLoop` must be created on the main thread!");
|
||||
}
|
||||
|
||||
|
|
@ -161,7 +161,7 @@ impl<T> EventLoop<T> {
|
|||
aux_state.activation_policy = attributes.activation_policy;
|
||||
aux_state.default_menu = attributes.default_menu;
|
||||
|
||||
autoreleasepool(|| {
|
||||
autoreleasepool(|_| {
|
||||
let _: () = msg_send![app, setDelegate:*delegate];
|
||||
});
|
||||
|
||||
|
|
@ -209,7 +209,7 @@ impl<T> EventLoop<T> {
|
|||
|
||||
self._callback = Some(Rc::clone(&callback));
|
||||
|
||||
let exit_code = autoreleasepool(|| unsafe {
|
||||
let exit_code = autoreleasepool(|_| unsafe {
|
||||
let app = NSApp();
|
||||
assert_ne!(app, nil);
|
||||
|
||||
|
|
@ -246,13 +246,13 @@ pub unsafe fn post_dummy_event(target: id) {
|
|||
location: NSPoint::new(0.0, 0.0)
|
||||
modifierFlags: NSEventModifierFlags::empty()
|
||||
timestamp: 0 as NSTimeInterval
|
||||
windowNumber: 0 as NSInteger
|
||||
windowNumber: 0isize
|
||||
context: nil
|
||||
subtype: NSEventSubtype::NSWindowExposedEventType
|
||||
data1: 0 as NSInteger
|
||||
data2: 0 as NSInteger
|
||||
data1: 0isize
|
||||
data2: 0isize
|
||||
];
|
||||
let _: () = msg_send![target, postEvent: dummy_event atStart: YES];
|
||||
let _: () = msg_send![target, postEvent: dummy_event, atStart: true];
|
||||
}
|
||||
|
||||
/// Catches panics that happen inside `f` and when a panic
|
||||
|
|
|
|||
|
|
@ -4,10 +4,7 @@
|
|||
|
||||
use std::ffi::c_void;
|
||||
|
||||
use cocoa::{
|
||||
base::id,
|
||||
foundation::{NSInteger, NSUInteger},
|
||||
};
|
||||
use cocoa::base::id;
|
||||
use core_foundation::{
|
||||
array::CFArrayRef, dictionary::CFDictionaryRef, string::CFStringRef, uuid::CFUUIDRef,
|
||||
};
|
||||
|
|
@ -15,34 +12,10 @@ use core_graphics::{
|
|||
base::CGError,
|
||||
display::{CGDirectDisplayID, CGDisplayConfigRef},
|
||||
};
|
||||
use objc::foundation::{NSInteger, NSUInteger};
|
||||
|
||||
pub const NSNotFound: NSInteger = NSInteger::max_value();
|
||||
|
||||
#[repr(C)]
|
||||
pub struct NSRange {
|
||||
pub location: NSUInteger,
|
||||
pub length: NSUInteger,
|
||||
}
|
||||
|
||||
impl NSRange {
|
||||
#[inline]
|
||||
pub fn new(location: NSUInteger, length: NSUInteger) -> NSRange {
|
||||
NSRange { location, length }
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl objc::Encode for NSRange {
|
||||
fn encode() -> objc::Encoding {
|
||||
let encoding = format!(
|
||||
// TODO: Verify that this is correct
|
||||
"{{NSRange={}{}}}",
|
||||
NSUInteger::encode().as_str(),
|
||||
NSUInteger::encode().as_str(),
|
||||
);
|
||||
unsafe { objc::Encoding::from_str(&encoding) }
|
||||
}
|
||||
}
|
||||
|
||||
pub trait NSMutableAttributedString: Sized {
|
||||
unsafe fn alloc(_: Self) -> id {
|
||||
msg_send![class!(NSMutableAttributedString), alloc]
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ struct KeyEquivalent<'a> {
|
|||
}
|
||||
|
||||
pub fn initialize() {
|
||||
autoreleasepool(|| unsafe {
|
||||
autoreleasepool(|_| unsafe {
|
||||
let menubar = IdRef::new(NSMenu::new(nil));
|
||||
let app_menu_item = IdRef::new(NSMenuItem::new(nil));
|
||||
menubar.addItem_(*app_menu_item);
|
||||
|
|
|
|||
|
|
@ -75,7 +75,7 @@ impl Window {
|
|||
attributes: WindowAttributes,
|
||||
pl_attribs: PlatformSpecificWindowBuilderAttributes,
|
||||
) -> Result<Self, RootOsError> {
|
||||
let (window, _delegate) = autoreleasepool(|| UnownedWindow::new(attributes, pl_attribs))?;
|
||||
let (window, _delegate) = autoreleasepool(|_| UnownedWindow::new(attributes, pl_attribs))?;
|
||||
Ok(Window { window, _delegate })
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ use crate::{
|
|||
use cocoa::{
|
||||
appkit::NSScreen,
|
||||
base::{id, nil},
|
||||
foundation::NSUInteger,
|
||||
};
|
||||
use core_foundation::{
|
||||
array::{CFArrayGetCount, CFArrayGetValueAtIndex},
|
||||
|
|
@ -16,6 +15,7 @@ use core_foundation::{
|
|||
string::CFString,
|
||||
};
|
||||
use core_graphics::display::{CGDirectDisplayID, CGDisplay, CGDisplayBounds};
|
||||
use objc::foundation::NSUInteger;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct VideoMode {
|
||||
|
|
|
|||
|
|
@ -9,8 +9,9 @@ use cocoa::{
|
|||
foundation::{NSPoint, NSSize, NSString},
|
||||
};
|
||||
use dispatch::Queue;
|
||||
use objc::foundation::is_main_thread;
|
||||
use objc::rc::autoreleasepool;
|
||||
use objc::runtime::{BOOL, NO, YES};
|
||||
use objc::runtime::Bool;
|
||||
|
||||
use crate::{
|
||||
dpi::LogicalSize,
|
||||
|
|
@ -55,8 +56,7 @@ 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) {
|
||||
let is_main_thread: BOOL = msg_send!(class!(NSThread), isMainThread);
|
||||
if is_main_thread != NO {
|
||||
if is_main_thread() {
|
||||
set_style_mask(ns_window, ns_view, mask);
|
||||
} else {
|
||||
let ns_window = MainThreadSafe(ns_window);
|
||||
|
|
@ -97,7 +97,7 @@ pub unsafe fn set_level_async(ns_window: id, level: ffi::NSWindowLevel) {
|
|||
pub unsafe fn set_ignore_mouse_events(ns_window: id, ignore: bool) {
|
||||
let ns_window = MainThreadSafe(ns_window);
|
||||
Queue::main().exec_async(move || {
|
||||
ns_window.setIgnoresMouseEvents_(if ignore { YES } else { NO });
|
||||
ns_window.setIgnoresMouseEvents_(Bool::from(ignore).as_raw());
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -186,7 +186,7 @@ pub unsafe fn set_maximized_async(
|
|||
} else {
|
||||
shared_state_lock.saved_standard_frame()
|
||||
};
|
||||
ns_window.setFrame_display_(new_rect, NO);
|
||||
ns_window.setFrame_display_(new_rect, Bool::NO.as_raw());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
@ -229,7 +229,7 @@ pub unsafe fn set_title_async(ns_window: id, title: String) {
|
|||
pub unsafe fn close_async(ns_window: IdRef) {
|
||||
let ns_window = MainThreadSafe(ns_window);
|
||||
Queue::main().exec_async(move || {
|
||||
autoreleasepool(move || {
|
||||
autoreleasepool(move |_| {
|
||||
ns_window.close();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ use cocoa::{
|
|||
base::{id, nil},
|
||||
foundation::{NSDictionary, NSPoint, NSString},
|
||||
};
|
||||
use objc::{runtime::Sel, runtime::NO};
|
||||
use objc::runtime::Sel;
|
||||
use std::cell::RefCell;
|
||||
|
||||
use crate::window::CursorIcon;
|
||||
|
|
@ -147,10 +147,11 @@ pub unsafe fn invisible_cursor() -> id {
|
|||
CURSOR_OBJECT.with(|cursor_obj| {
|
||||
if *cursor_obj.borrow() == nil {
|
||||
// Create a cursor from `CURSOR_BYTES`
|
||||
let cursor_data: id = msg_send![class!(NSData),
|
||||
dataWithBytesNoCopy:CURSOR_BYTES as *const [u8]
|
||||
length:CURSOR_BYTES.len()
|
||||
freeWhenDone:NO
|
||||
let cursor_data: id = msg_send![
|
||||
class!(NSData),
|
||||
dataWithBytesNoCopy: CURSOR_BYTES.as_ptr(),
|
||||
length: CURSOR_BYTES.len(),
|
||||
freeWhenDone: false,
|
||||
];
|
||||
|
||||
let ns_image: id = msg_send![class!(NSImage), alloc];
|
||||
|
|
|
|||
|
|
@ -9,10 +9,11 @@ use std::os::raw::c_uchar;
|
|||
use cocoa::{
|
||||
appkit::{CGFloat, NSApp, NSWindowStyleMask},
|
||||
base::{id, nil},
|
||||
foundation::{NSPoint, NSRect, NSString, NSUInteger},
|
||||
foundation::{NSPoint, NSRect, NSString},
|
||||
};
|
||||
use core_graphics::display::CGDisplay;
|
||||
use objc::runtime::{Class, Object, BOOL, NO};
|
||||
use objc::foundation::{NSRange, NSUInteger};
|
||||
use objc::runtime::{Class, Object};
|
||||
|
||||
use crate::dpi::LogicalPosition;
|
||||
use crate::platform_impl::platform::ffi;
|
||||
|
|
@ -28,7 +29,7 @@ where
|
|||
bitset & flag == flag
|
||||
}
|
||||
|
||||
pub const EMPTY_RANGE: ffi::NSRange = ffi::NSRange {
|
||||
pub const EMPTY_RANGE: NSRange = NSRange {
|
||||
location: ffi::NSNotFound as NSUInteger,
|
||||
length: 0,
|
||||
};
|
||||
|
|
@ -172,8 +173,8 @@ pub unsafe fn toggle_style_mask(window: id, view: id, mask: NSWindowStyleMask, o
|
|||
///
|
||||
/// Safety: Assumes that `string` is an instance of `NSAttributedString` or `NSString`
|
||||
pub unsafe fn id_to_string_lossy(string: id) -> String {
|
||||
let has_attr: BOOL = msg_send![string, isKindOfClass: class!(NSAttributedString)];
|
||||
let characters = if has_attr != NO {
|
||||
let has_attr = msg_send![string, isKindOfClass: class!(NSAttributedString)];
|
||||
let characters = if has_attr {
|
||||
// This is a *mut NSAttributedString
|
||||
msg_send![string, string]
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -12,11 +12,12 @@ use std::{
|
|||
use cocoa::{
|
||||
appkit::{NSApp, NSEvent, NSEventModifierFlags, NSEventPhase, NSView, NSWindow},
|
||||
base::{id, nil},
|
||||
foundation::{NSInteger, NSPoint, NSRect, NSSize, NSString, NSUInteger},
|
||||
foundation::{NSPoint, NSRect, NSSize, NSString},
|
||||
};
|
||||
use objc::{
|
||||
declare::ClassDecl,
|
||||
runtime::{Class, Object, Protocol, Sel, BOOL, NO, YES},
|
||||
declare::ClassBuilder,
|
||||
foundation::{NSInteger, NSRange, NSUInteger},
|
||||
runtime::{Bool, Class, Object, Protocol, Sel},
|
||||
};
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
|
|
@ -124,7 +125,7 @@ pub fn new_view(ns_window: id) -> (IdRef, Weak<Mutex<CursorState>>) {
|
|||
}
|
||||
|
||||
pub unsafe fn set_ime_position(ns_view: id, position: LogicalPosition<f64>) {
|
||||
let state_ptr: *mut c_void = *(*ns_view).get_mut_ivar("winitState");
|
||||
let state_ptr: *mut c_void = *(*ns_view).ivar_mut("winitState");
|
||||
let state = &mut *(state_ptr as *mut ViewState);
|
||||
state.ime_position = position;
|
||||
let input_context: id = msg_send![ns_view, inputContext];
|
||||
|
|
@ -132,7 +133,7 @@ pub unsafe fn set_ime_position(ns_view: id, position: LogicalPosition<f64>) {
|
|||
}
|
||||
|
||||
pub unsafe fn set_ime_allowed(ns_view: id, ime_allowed: bool) {
|
||||
let state_ptr: *mut c_void = *(*ns_view).get_mut_ivar("winitState");
|
||||
let state_ptr: *mut c_void = *(*ns_view).ivar_mut("winitState");
|
||||
let state = &mut *(state_ptr as *mut ViewState);
|
||||
if state.ime_allowed == ime_allowed {
|
||||
return;
|
||||
|
|
@ -141,7 +142,7 @@ pub unsafe fn set_ime_allowed(ns_view: id, ime_allowed: bool) {
|
|||
if state.ime_allowed {
|
||||
return;
|
||||
}
|
||||
let marked_text_ref: &mut id = (*ns_view).get_mut_ivar("markedText");
|
||||
let marked_text_ref: &mut id = (*ns_view).ivar_mut("markedText");
|
||||
|
||||
// Clear markedText
|
||||
let _: () = msg_send![*marked_text_ref, release];
|
||||
|
|
@ -164,170 +165,135 @@ unsafe impl Sync for ViewClass {}
|
|||
|
||||
static VIEW_CLASS: Lazy<ViewClass> = Lazy::new(|| unsafe {
|
||||
let superclass = class!(NSView);
|
||||
let mut decl = ClassDecl::new("WinitView", superclass).unwrap();
|
||||
decl.add_method(sel!(dealloc), dealloc as extern "C" fn(&Object, Sel));
|
||||
let mut decl = ClassBuilder::new("WinitView", superclass).unwrap();
|
||||
decl.add_method(sel!(dealloc), dealloc as extern "C" fn(_, _));
|
||||
decl.add_method(
|
||||
sel!(initWithWinit:),
|
||||
init_with_winit as extern "C" fn(&Object, Sel, *mut c_void) -> id,
|
||||
init_with_winit as extern "C" fn(_, _, _) -> _,
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(viewDidMoveToWindow),
|
||||
view_did_move_to_window as extern "C" fn(&Object, Sel),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(drawRect:),
|
||||
draw_rect as extern "C" fn(&Object, Sel, NSRect),
|
||||
view_did_move_to_window as extern "C" fn(_, _),
|
||||
);
|
||||
decl.add_method(sel!(drawRect:), draw_rect as extern "C" fn(_, _, _));
|
||||
decl.add_method(
|
||||
sel!(acceptsFirstResponder),
|
||||
accepts_first_responder as extern "C" fn(&Object, Sel) -> BOOL,
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(touchBar),
|
||||
touch_bar as extern "C" fn(&Object, Sel) -> BOOL,
|
||||
accepts_first_responder as extern "C" fn(_, _) -> _,
|
||||
);
|
||||
decl.add_method(sel!(touchBar), touch_bar as extern "C" fn(_, _) -> _);
|
||||
decl.add_method(
|
||||
sel!(resetCursorRects),
|
||||
reset_cursor_rects as extern "C" fn(&Object, Sel),
|
||||
reset_cursor_rects as extern "C" fn(_, _),
|
||||
);
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// NSTextInputClient
|
||||
decl.add_method(
|
||||
sel!(hasMarkedText),
|
||||
has_marked_text as extern "C" fn(&Object, Sel) -> BOOL,
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(markedRange),
|
||||
marked_range as extern "C" fn(&Object, Sel) -> NSRange,
|
||||
has_marked_text as extern "C" fn(_, _) -> _,
|
||||
);
|
||||
decl.add_method(sel!(markedRange), marked_range as extern "C" fn(_, _) -> _);
|
||||
decl.add_method(
|
||||
sel!(selectedRange),
|
||||
selected_range as extern "C" fn(&Object, Sel) -> NSRange,
|
||||
selected_range as extern "C" fn(_, _) -> _,
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(setMarkedText:selectedRange:replacementRange:),
|
||||
set_marked_text as extern "C" fn(&mut Object, Sel, id, NSRange, NSRange),
|
||||
set_marked_text as extern "C" fn(_, _, _, _, _),
|
||||
);
|
||||
decl.add_method(sel!(unmarkText), unmark_text as extern "C" fn(&Object, Sel));
|
||||
decl.add_method(sel!(unmarkText), unmark_text as extern "C" fn(_, _));
|
||||
decl.add_method(
|
||||
sel!(validAttributesForMarkedText),
|
||||
valid_attributes_for_marked_text as extern "C" fn(&Object, Sel) -> id,
|
||||
valid_attributes_for_marked_text as extern "C" fn(_, _) -> _,
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(attributedSubstringForProposedRange:actualRange:),
|
||||
attributed_substring_for_proposed_range
|
||||
as extern "C" fn(&Object, Sel, NSRange, *mut c_void) -> id,
|
||||
attributed_substring_for_proposed_range as extern "C" fn(_, _, _, _) -> _,
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(insertText:replacementRange:),
|
||||
insert_text as extern "C" fn(&Object, Sel, id, NSRange),
|
||||
insert_text as extern "C" fn(_, _, _, _),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(characterIndexForPoint:),
|
||||
character_index_for_point as extern "C" fn(&Object, Sel, NSPoint) -> NSUInteger,
|
||||
character_index_for_point as extern "C" fn(_, _, _) -> _,
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(firstRectForCharacterRange:actualRange:),
|
||||
first_rect_for_character_range
|
||||
as extern "C" fn(&Object, Sel, NSRange, *mut c_void) -> NSRect,
|
||||
first_rect_for_character_range as extern "C" fn(_, _, _, _) -> _,
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(doCommandBySelector:),
|
||||
do_command_by_selector as extern "C" fn(&Object, Sel, Sel),
|
||||
do_command_by_selector as extern "C" fn(_, _, _),
|
||||
);
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
decl.add_method(sel!(keyDown:), key_down as extern "C" fn(&Object, Sel, id));
|
||||
decl.add_method(sel!(keyUp:), key_up as extern "C" fn(&Object, Sel, id));
|
||||
decl.add_method(
|
||||
sel!(flagsChanged:),
|
||||
flags_changed as extern "C" fn(&Object, Sel, id),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(insertTab:),
|
||||
insert_tab as extern "C" fn(&Object, Sel, id),
|
||||
);
|
||||
decl.add_method(sel!(keyDown:), key_down as extern "C" fn(_, _, _));
|
||||
decl.add_method(sel!(keyUp:), key_up as extern "C" fn(_, _, _));
|
||||
decl.add_method(sel!(flagsChanged:), flags_changed as extern "C" fn(_, _, _));
|
||||
decl.add_method(sel!(insertTab:), insert_tab as extern "C" fn(_, _, _));
|
||||
decl.add_method(
|
||||
sel!(insertBackTab:),
|
||||
insert_back_tab as extern "C" fn(&Object, Sel, id),
|
||||
insert_back_tab as extern "C" fn(_, _, _),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(mouseDown:),
|
||||
mouse_down as extern "C" fn(&Object, Sel, id),
|
||||
);
|
||||
decl.add_method(sel!(mouseUp:), mouse_up as extern "C" fn(&Object, Sel, id));
|
||||
decl.add_method(sel!(mouseDown:), mouse_down as extern "C" fn(_, _, _));
|
||||
decl.add_method(sel!(mouseUp:), mouse_up as extern "C" fn(_, _, _));
|
||||
decl.add_method(
|
||||
sel!(rightMouseDown:),
|
||||
right_mouse_down as extern "C" fn(&Object, Sel, id),
|
||||
right_mouse_down as extern "C" fn(_, _, _),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(rightMouseUp:),
|
||||
right_mouse_up as extern "C" fn(&Object, Sel, id),
|
||||
right_mouse_up as extern "C" fn(_, _, _),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(otherMouseDown:),
|
||||
other_mouse_down as extern "C" fn(&Object, Sel, id),
|
||||
other_mouse_down as extern "C" fn(_, _, _),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(otherMouseUp:),
|
||||
other_mouse_up as extern "C" fn(&Object, Sel, id),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(mouseMoved:),
|
||||
mouse_moved as extern "C" fn(&Object, Sel, id),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(mouseDragged:),
|
||||
mouse_dragged as extern "C" fn(&Object, Sel, id),
|
||||
other_mouse_up as extern "C" fn(_, _, _),
|
||||
);
|
||||
decl.add_method(sel!(mouseMoved:), mouse_moved as extern "C" fn(_, _, _));
|
||||
decl.add_method(sel!(mouseDragged:), mouse_dragged as extern "C" fn(_, _, _));
|
||||
decl.add_method(
|
||||
sel!(rightMouseDragged:),
|
||||
right_mouse_dragged as extern "C" fn(&Object, Sel, id),
|
||||
right_mouse_dragged as extern "C" fn(_, _, _),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(otherMouseDragged:),
|
||||
other_mouse_dragged as extern "C" fn(&Object, Sel, id),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(mouseEntered:),
|
||||
mouse_entered as extern "C" fn(&Object, Sel, id),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(mouseExited:),
|
||||
mouse_exited as extern "C" fn(&Object, Sel, id),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(scrollWheel:),
|
||||
scroll_wheel as extern "C" fn(&Object, Sel, id),
|
||||
other_mouse_dragged as extern "C" fn(_, _, _),
|
||||
);
|
||||
decl.add_method(sel!(mouseEntered:), mouse_entered as extern "C" fn(_, _, _));
|
||||
decl.add_method(sel!(mouseExited:), mouse_exited as extern "C" fn(_, _, _));
|
||||
decl.add_method(sel!(scrollWheel:), scroll_wheel as extern "C" fn(_, _, _));
|
||||
decl.add_method(
|
||||
sel!(magnifyWithEvent:),
|
||||
magnify_with_event as extern "C" fn(&Object, Sel, id),
|
||||
magnify_with_event as extern "C" fn(_, _, _),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(rotateWithEvent:),
|
||||
rotate_with_event as extern "C" fn(&Object, Sel, id),
|
||||
rotate_with_event as extern "C" fn(_, _, _),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(pressureChangeWithEvent:),
|
||||
pressure_change_with_event as extern "C" fn(&Object, Sel, id),
|
||||
pressure_change_with_event as extern "C" fn(_, _, _),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(_wantsKeyDownForEvent:),
|
||||
wants_key_down_for_event as extern "C" fn(&Object, Sel, id) -> BOOL,
|
||||
wants_key_down_for_event as extern "C" fn(_, _, _) -> _,
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(cancelOperation:),
|
||||
cancel_operation as extern "C" fn(&Object, Sel, id),
|
||||
cancel_operation as extern "C" fn(_, _, _),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(frameDidChange:),
|
||||
frame_did_change as extern "C" fn(&Object, Sel, id),
|
||||
frame_did_change as extern "C" fn(_, _, _),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(acceptsFirstMouse:),
|
||||
accepts_first_mouse as extern "C" fn(&Object, Sel, id) -> BOOL,
|
||||
accepts_first_mouse as extern "C" fn(_, _, _) -> _,
|
||||
);
|
||||
decl.add_ivar::<*mut c_void>("winitState");
|
||||
decl.add_ivar::<id>("markedText");
|
||||
|
|
@ -338,9 +304,9 @@ static VIEW_CLASS: Lazy<ViewClass> = Lazy::new(|| unsafe {
|
|||
|
||||
extern "C" fn dealloc(this: &Object, _sel: Sel) {
|
||||
unsafe {
|
||||
let marked_text: id = *this.get_ivar("markedText");
|
||||
let marked_text: id = *this.ivar("markedText");
|
||||
let _: () = msg_send![marked_text, release];
|
||||
let state: *mut c_void = *this.get_ivar("winitState");
|
||||
let state: *mut c_void = *this.ivar("winitState");
|
||||
drop(Box::from_raw(state as *mut ViewState));
|
||||
}
|
||||
}
|
||||
|
|
@ -353,7 +319,7 @@ extern "C" fn init_with_winit(this: &Object, _sel: Sel, state: *mut c_void) -> i
|
|||
let marked_text =
|
||||
<id as NSMutableAttributedString>::init(NSMutableAttributedString::alloc(nil));
|
||||
(*this).set_ivar("markedText", marked_text);
|
||||
let _: () = msg_send![this, setPostsFrameChangedNotifications: YES];
|
||||
let _: () = msg_send![this, setPostsFrameChangedNotifications: true];
|
||||
|
||||
let notification_center: &Object =
|
||||
msg_send![class!(NSNotificationCenter), defaultCenter];
|
||||
|
|
@ -364,7 +330,7 @@ extern "C" fn init_with_winit(this: &Object, _sel: Sel, state: *mut c_void) -> i
|
|||
notification_center,
|
||||
addObserver: this
|
||||
selector: sel!(frameDidChange:)
|
||||
name: frame_did_change_notification_name
|
||||
name: *frame_did_change_notification_name
|
||||
object: this
|
||||
];
|
||||
|
||||
|
|
@ -378,7 +344,7 @@ extern "C" fn init_with_winit(this: &Object, _sel: Sel, state: *mut c_void) -> i
|
|||
extern "C" fn view_did_move_to_window(this: &Object, _sel: Sel) {
|
||||
trace_scope!("viewDidMoveToWindow");
|
||||
unsafe {
|
||||
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
||||
let state_ptr: *mut c_void = *this.ivar("winitState");
|
||||
let state = &mut *(state_ptr as *mut ViewState);
|
||||
|
||||
if let Some(tracking_rect) = state.tracking_rect.take() {
|
||||
|
|
@ -386,11 +352,12 @@ extern "C" fn view_did_move_to_window(this: &Object, _sel: Sel) {
|
|||
}
|
||||
|
||||
let rect: NSRect = msg_send![this, visibleRect];
|
||||
let tracking_rect: NSInteger = msg_send![this,
|
||||
addTrackingRect:rect
|
||||
owner:this
|
||||
userData:ptr::null_mut::<c_void>()
|
||||
assumeInside:NO
|
||||
let tracking_rect: NSInteger = msg_send![
|
||||
this,
|
||||
addTrackingRect: rect,
|
||||
owner: this,
|
||||
userData: ptr::null_mut::<c_void>(),
|
||||
assumeInside: false
|
||||
];
|
||||
state.tracking_rect = Some(tracking_rect);
|
||||
}
|
||||
|
|
@ -399,7 +366,7 @@ extern "C" fn view_did_move_to_window(this: &Object, _sel: Sel) {
|
|||
extern "C" fn frame_did_change(this: &Object, _sel: Sel, _event: id) {
|
||||
trace_scope!("frameDidChange:");
|
||||
unsafe {
|
||||
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
||||
let state_ptr: *mut c_void = *this.ivar("winitState");
|
||||
let state = &mut *(state_ptr as *mut ViewState);
|
||||
|
||||
if let Some(tracking_rect) = state.tracking_rect.take() {
|
||||
|
|
@ -407,11 +374,12 @@ extern "C" fn frame_did_change(this: &Object, _sel: Sel, _event: id) {
|
|||
}
|
||||
|
||||
let rect: NSRect = msg_send![this, visibleRect];
|
||||
let tracking_rect: NSInteger = msg_send![this,
|
||||
addTrackingRect:rect
|
||||
owner:this
|
||||
userData:ptr::null_mut::<c_void>()
|
||||
assumeInside:NO
|
||||
let tracking_rect: NSInteger = msg_send![
|
||||
this,
|
||||
addTrackingRect: rect,
|
||||
owner: this,
|
||||
userData: ptr::null_mut::<c_void>(),
|
||||
assumeInside: false,
|
||||
];
|
||||
state.tracking_rect = Some(tracking_rect);
|
||||
|
||||
|
|
@ -430,7 +398,7 @@ extern "C" fn frame_did_change(this: &Object, _sel: Sel, _event: id) {
|
|||
extern "C" fn draw_rect(this: &Object, _sel: Sel, rect: NSRect) {
|
||||
trace_scope!("drawRect:");
|
||||
unsafe {
|
||||
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
||||
let state_ptr: *mut c_void = *this.ivar("winitState");
|
||||
let state = &mut *(state_ptr as *mut ViewState);
|
||||
|
||||
AppState::handle_redraw(WindowId(get_window_id(state.ns_window)));
|
||||
|
|
@ -440,23 +408,23 @@ extern "C" fn draw_rect(this: &Object, _sel: Sel, rect: NSRect) {
|
|||
}
|
||||
}
|
||||
|
||||
extern "C" fn accepts_first_responder(_this: &Object, _sel: Sel) -> BOOL {
|
||||
extern "C" fn accepts_first_responder(_this: &Object, _sel: Sel) -> Bool {
|
||||
trace_scope!("acceptsFirstResponder");
|
||||
YES
|
||||
Bool::YES
|
||||
}
|
||||
|
||||
// This is necessary to prevent a beefy terminal error on MacBook Pros:
|
||||
// IMKInputSession [0x7fc573576ff0 presentFunctionRowItemTextInputViewWithEndpoint:completionHandler:] : [self textInputContext]=0x7fc573558e10 *NO* NSRemoteViewController to client, NSError=Error Domain=NSCocoaErrorDomain Code=4099 "The connection from pid 0 was invalidated from this process." UserInfo={NSDebugDescription=The connection from pid 0 was invalidated from this process.}, com.apple.inputmethod.EmojiFunctionRowItem
|
||||
// TODO: Add an API extension for using `NSTouchBar`
|
||||
extern "C" fn touch_bar(_this: &Object, _sel: Sel) -> BOOL {
|
||||
extern "C" fn touch_bar(_this: &Object, _sel: Sel) -> Bool {
|
||||
trace_scope!("touchBar");
|
||||
NO
|
||||
Bool::NO
|
||||
}
|
||||
|
||||
extern "C" fn reset_cursor_rects(this: &Object, _sel: Sel) {
|
||||
trace_scope!("resetCursorRects");
|
||||
unsafe {
|
||||
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
||||
let state_ptr: *mut c_void = *this.ivar("winitState");
|
||||
let state = &mut *(state_ptr as *mut ViewState);
|
||||
|
||||
let bounds: NSRect = msg_send![this, bounds];
|
||||
|
|
@ -473,18 +441,18 @@ extern "C" fn reset_cursor_rects(this: &Object, _sel: Sel) {
|
|||
}
|
||||
}
|
||||
|
||||
extern "C" fn has_marked_text(this: &Object, _sel: Sel) -> BOOL {
|
||||
extern "C" fn has_marked_text(this: &Object, _sel: Sel) -> Bool {
|
||||
trace_scope!("hasMarkedText");
|
||||
unsafe {
|
||||
let marked_text: id = *this.get_ivar("markedText");
|
||||
(marked_text.length() > 0) as BOOL
|
||||
let marked_text: id = *this.ivar("markedText");
|
||||
Bool::new(marked_text.length() > 0)
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" fn marked_range(this: &Object, _sel: Sel) -> NSRange {
|
||||
trace_scope!("markedRange");
|
||||
unsafe {
|
||||
let marked_text: id = *this.get_ivar("markedText");
|
||||
let marked_text: id = *this.ivar("markedText");
|
||||
let length = marked_text.length();
|
||||
if length > 0 {
|
||||
NSRange::new(0, length)
|
||||
|
|
@ -516,13 +484,13 @@ extern "C" fn set_marked_text(
|
|||
trace_scope!("setMarkedText:selectedRange:replacementRange:");
|
||||
unsafe {
|
||||
// Get pre-edit text
|
||||
let marked_text_ref: &mut id = this.get_mut_ivar("markedText");
|
||||
let marked_text_ref: &mut id = this.ivar_mut("markedText");
|
||||
|
||||
// Update markedText
|
||||
let _: () = msg_send![(*marked_text_ref), release];
|
||||
let _: () = msg_send![*marked_text_ref, release];
|
||||
let marked_text = NSMutableAttributedString::alloc(nil);
|
||||
let has_attr: BOOL = msg_send![string, isKindOfClass: class!(NSAttributedString)];
|
||||
if has_attr != NO {
|
||||
let has_attr = msg_send![string, isKindOfClass: class!(NSAttributedString)];
|
||||
if has_attr {
|
||||
marked_text.initWithAttributedString(string);
|
||||
} else {
|
||||
marked_text.initWithString(string);
|
||||
|
|
@ -530,7 +498,7 @@ extern "C" fn set_marked_text(
|
|||
*marked_text_ref = marked_text;
|
||||
|
||||
// Update ViewState with new marked text
|
||||
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
||||
let state_ptr: *mut c_void = *this.ivar("winitState");
|
||||
let state = &mut *(state_ptr as *mut ViewState);
|
||||
let preedit_string = id_to_string_lossy(string);
|
||||
|
||||
|
|
@ -568,7 +536,7 @@ extern "C" fn set_marked_text(
|
|||
extern "C" fn unmark_text(this: &Object, _sel: Sel) {
|
||||
trace_scope!("unmarkText");
|
||||
unsafe {
|
||||
let marked_text: id = *this.get_ivar("markedText");
|
||||
let marked_text: id = *this.ivar("markedText");
|
||||
let mutable_string = marked_text.mutableString();
|
||||
let s: id = msg_send![class!(NSString), new];
|
||||
let _: () = msg_send![mutable_string, setString: s];
|
||||
|
|
@ -576,7 +544,7 @@ extern "C" fn unmark_text(this: &Object, _sel: Sel) {
|
|||
let input_context: id = msg_send![this, inputContext];
|
||||
let _: () = msg_send![input_context, discardMarkedText];
|
||||
|
||||
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
||||
let state_ptr: *mut c_void = *this.ivar("winitState");
|
||||
let state = &mut *(state_ptr as *mut ViewState);
|
||||
AppState::queue_event(EventWrapper::StaticEvent(Event::WindowEvent {
|
||||
window_id: WindowId(get_window_id(state.ns_window)),
|
||||
|
|
@ -619,7 +587,7 @@ extern "C" fn first_rect_for_character_range(
|
|||
) -> NSRect {
|
||||
trace_scope!("firstRectForCharacterRange:actualRange:");
|
||||
unsafe {
|
||||
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
||||
let state_ptr: *mut c_void = *this.ivar("winitState");
|
||||
let state = &mut *(state_ptr as *mut ViewState);
|
||||
let content_rect =
|
||||
NSWindow::contentRectForFrameRect_(state.ns_window, NSWindow::frame(state.ns_window));
|
||||
|
|
@ -638,7 +606,7 @@ extern "C" fn first_rect_for_character_range(
|
|||
extern "C" fn insert_text(this: &Object, _sel: Sel, string: id, _replacement_range: NSRange) {
|
||||
trace_scope!("insertText:replacementRange:");
|
||||
unsafe {
|
||||
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
||||
let state_ptr: *mut c_void = *this.ivar("winitState");
|
||||
let state = &mut *(state_ptr as *mut ViewState);
|
||||
|
||||
let string = id_to_string_lossy(string);
|
||||
|
|
@ -663,7 +631,7 @@ extern "C" fn do_command_by_selector(this: &Object, _sel: Sel, _command: Sel) {
|
|||
// Basically, we're sent this message whenever a keyboard event that doesn't generate a "human
|
||||
// readable" character happens, i.e. newlines, tabs, and Ctrl+C.
|
||||
unsafe {
|
||||
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
||||
let state_ptr: *mut c_void = *this.ivar("winitState");
|
||||
let state = &mut *(state_ptr as *mut ViewState);
|
||||
|
||||
// We shouldn't forward any character from just commited text, since we'll end up sending
|
||||
|
|
@ -675,8 +643,8 @@ extern "C" fn do_command_by_selector(this: &Object, _sel: Sel, _command: Sel) {
|
|||
|
||||
state.forward_key_to_app = true;
|
||||
|
||||
let has_marked_text: BOOL = msg_send![this, hasMarkedText];
|
||||
if has_marked_text == NO && state.ime_state == ImeState::Preedit {
|
||||
let has_marked_text = msg_send![this, hasMarkedText];
|
||||
if has_marked_text && state.ime_state == ImeState::Preedit {
|
||||
// Leave preedit so that we also report the keyup for this key
|
||||
state.ime_state = ImeState::Enabled;
|
||||
}
|
||||
|
|
@ -754,7 +722,7 @@ fn update_potentially_stale_modifiers(state: &mut ViewState, event: id) {
|
|||
extern "C" fn key_down(this: &Object, _sel: Sel, event: id) {
|
||||
trace_scope!("keyDown:");
|
||||
unsafe {
|
||||
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
||||
let state_ptr: *mut c_void = *this.ivar("winitState");
|
||||
let state = &mut *(state_ptr as *mut ViewState);
|
||||
let window_id = WindowId(get_window_id(state.ns_window));
|
||||
|
||||
|
|
@ -837,7 +805,7 @@ extern "C" fn key_down(this: &Object, _sel: Sel, event: id) {
|
|||
extern "C" fn key_up(this: &Object, _sel: Sel, event: id) {
|
||||
trace_scope!("keyUp:");
|
||||
unsafe {
|
||||
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
||||
let state_ptr: *mut c_void = *this.ivar("winitState");
|
||||
let state = &mut *(state_ptr as *mut ViewState);
|
||||
|
||||
let scancode = get_scancode(event) as u32;
|
||||
|
|
@ -870,7 +838,7 @@ extern "C" fn key_up(this: &Object, _sel: Sel, event: id) {
|
|||
extern "C" fn flags_changed(this: &Object, _sel: Sel, event: id) {
|
||||
trace_scope!("flagsChanged:");
|
||||
unsafe {
|
||||
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
||||
let state_ptr: *mut c_void = *this.ivar("winitState");
|
||||
let state = &mut *(state_ptr as *mut ViewState);
|
||||
|
||||
let mut events = VecDeque::with_capacity(4);
|
||||
|
|
@ -956,7 +924,7 @@ extern "C" fn insert_back_tab(this: &Object, _sel: Sel, _sender: id) {
|
|||
extern "C" fn cancel_operation(this: &Object, _sel: Sel, _sender: id) {
|
||||
trace_scope!("cancelOperation:");
|
||||
unsafe {
|
||||
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
||||
let state_ptr: *mut c_void = *this.ivar("winitState");
|
||||
let state = &mut *(state_ptr as *mut ViewState);
|
||||
|
||||
let scancode = 0x2f;
|
||||
|
|
@ -988,7 +956,7 @@ extern "C" fn cancel_operation(this: &Object, _sel: Sel, _sender: id) {
|
|||
|
||||
fn mouse_click(this: &Object, event: id, button: MouseButton, button_state: ElementState) {
|
||||
unsafe {
|
||||
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
||||
let state_ptr: *mut c_void = *this.ivar("winitState");
|
||||
let state = &mut *(state_ptr as *mut ViewState);
|
||||
|
||||
update_potentially_stale_modifiers(state, event);
|
||||
|
|
@ -1045,7 +1013,7 @@ extern "C" fn other_mouse_up(this: &Object, _sel: Sel, event: id) {
|
|||
|
||||
fn mouse_motion(this: &Object, event: id) {
|
||||
unsafe {
|
||||
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
||||
let state_ptr: *mut c_void = *this.ivar("winitState");
|
||||
let state = &mut *(state_ptr as *mut ViewState);
|
||||
|
||||
// We have to do this to have access to the `NSView` trait...
|
||||
|
|
@ -1107,7 +1075,7 @@ extern "C" fn other_mouse_dragged(this: &Object, _sel: Sel, event: id) {
|
|||
extern "C" fn mouse_entered(this: &Object, _sel: Sel, _event: id) {
|
||||
trace_scope!("mouseEntered:");
|
||||
unsafe {
|
||||
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
||||
let state_ptr: *mut c_void = *this.ivar("winitState");
|
||||
let state = &mut *(state_ptr as *mut ViewState);
|
||||
|
||||
let enter_event = Event::WindowEvent {
|
||||
|
|
@ -1124,7 +1092,7 @@ extern "C" fn mouse_entered(this: &Object, _sel: Sel, _event: id) {
|
|||
extern "C" fn mouse_exited(this: &Object, _sel: Sel, _event: id) {
|
||||
trace_scope!("mouseExited:");
|
||||
unsafe {
|
||||
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
||||
let state_ptr: *mut c_void = *this.ivar("winitState");
|
||||
let state = &mut *(state_ptr as *mut ViewState);
|
||||
|
||||
let window_event = Event::WindowEvent {
|
||||
|
|
@ -1144,12 +1112,12 @@ extern "C" fn scroll_wheel(this: &Object, _sel: Sel, event: id) {
|
|||
mouse_motion(this, event);
|
||||
|
||||
unsafe {
|
||||
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
||||
let state_ptr: *mut c_void = *this.ivar("winitState");
|
||||
let state = &mut *(state_ptr as *mut ViewState);
|
||||
|
||||
let delta = {
|
||||
let (x, y) = (event.scrollingDeltaX(), event.scrollingDeltaY());
|
||||
if event.hasPreciseScrollingDeltas() == YES {
|
||||
if Bool::from_raw(event.hasPreciseScrollingDeltas()).as_bool() {
|
||||
let delta = LogicalPosition::new(x, y).to_physical(state.get_scale_factor());
|
||||
MouseScrollDelta::PixelDelta(delta)
|
||||
} else {
|
||||
|
|
@ -1184,7 +1152,7 @@ extern "C" fn scroll_wheel(this: &Object, _sel: Sel, event: id) {
|
|||
event: DeviceEvent::MouseWheel { delta },
|
||||
};
|
||||
|
||||
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
||||
let state_ptr: *mut c_void = *this.ivar("winitState");
|
||||
let state = &mut *(state_ptr as *mut ViewState);
|
||||
|
||||
update_potentially_stale_modifiers(state, event);
|
||||
|
|
@ -1208,7 +1176,7 @@ extern "C" fn magnify_with_event(this: &Object, _sel: Sel, event: id) {
|
|||
trace_scope!("magnifyWithEvent:");
|
||||
|
||||
unsafe {
|
||||
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
||||
let state_ptr: *mut c_void = *this.ivar("winitState");
|
||||
let state = &mut *(state_ptr as *mut ViewState);
|
||||
|
||||
let delta = event.magnification();
|
||||
|
|
@ -1237,7 +1205,7 @@ extern "C" fn rotate_with_event(this: &Object, _sel: Sel, event: id) {
|
|||
trace_scope!("rotateWithEvent:");
|
||||
|
||||
unsafe {
|
||||
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
||||
let state_ptr: *mut c_void = *this.ivar("winitState");
|
||||
let state = &mut *(state_ptr as *mut ViewState);
|
||||
|
||||
let delta = event.rotation();
|
||||
|
|
@ -1268,7 +1236,7 @@ extern "C" fn pressure_change_with_event(this: &Object, _sel: Sel, event: id) {
|
|||
mouse_motion(this, event);
|
||||
|
||||
unsafe {
|
||||
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
||||
let state_ptr: *mut c_void = *this.ivar("winitState");
|
||||
let state = &mut *(state_ptr as *mut ViewState);
|
||||
|
||||
let pressure = event.pressure();
|
||||
|
|
@ -1290,12 +1258,12 @@ extern "C" fn pressure_change_with_event(this: &Object, _sel: Sel, event: id) {
|
|||
// Allows us to receive Ctrl-Tab and Ctrl-Esc.
|
||||
// Note that this *doesn't* help with any missing Cmd inputs.
|
||||
// https://github.com/chromium/chromium/blob/a86a8a6bcfa438fa3ac2eba6f02b3ad1f8e0756f/ui/views/cocoa/bridged_content_view.mm#L816
|
||||
extern "C" fn wants_key_down_for_event(_this: &Object, _sel: Sel, _event: id) -> BOOL {
|
||||
extern "C" fn wants_key_down_for_event(_this: &Object, _sel: Sel, _event: id) -> Bool {
|
||||
trace_scope!("_wantsKeyDownForEvent:");
|
||||
YES
|
||||
Bool::YES
|
||||
}
|
||||
|
||||
extern "C" fn accepts_first_mouse(_this: &Object, _sel: Sel, _event: id) -> BOOL {
|
||||
extern "C" fn accepts_first_mouse(_this: &Object, _sel: Sel, _event: id) -> Bool {
|
||||
trace_scope!("acceptsFirstMouse:");
|
||||
YES
|
||||
Bool::YES
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,13 +42,14 @@ use cocoa::{
|
|||
NSRequestUserAttentionType, NSScreen, NSView, NSWindow, NSWindowButton, NSWindowStyleMask,
|
||||
},
|
||||
base::{id, nil},
|
||||
foundation::{NSDictionary, NSPoint, NSRect, NSSize, NSUInteger},
|
||||
foundation::{NSDictionary, NSPoint, NSRect, NSSize},
|
||||
};
|
||||
use core_graphics::display::{CGDisplay, CGDisplayMode};
|
||||
use objc::{
|
||||
declare::ClassDecl,
|
||||
declare::ClassBuilder,
|
||||
foundation::{is_main_thread, NSUInteger},
|
||||
rc::autoreleasepool,
|
||||
runtime::{Class, Object, Sel, BOOL, NO, YES},
|
||||
runtime::{Bool, Class, Object, Sel},
|
||||
};
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
|
|
@ -119,9 +120,9 @@ unsafe fn create_view(
|
|||
// macos 10.14 and `true` after 10.15, we should set it to `YES` or `NO` to avoid
|
||||
// always the default system value in favour of the user's code
|
||||
if !pl_attribs.disallow_hidpi {
|
||||
ns_view.setWantsBestResolutionOpenGLSurface_(YES);
|
||||
ns_view.setWantsBestResolutionOpenGLSurface_(Bool::YES.as_raw());
|
||||
} else {
|
||||
ns_view.setWantsBestResolutionOpenGLSurface_(NO);
|
||||
ns_view.setWantsBestResolutionOpenGLSurface_(Bool::NO.as_raw());
|
||||
}
|
||||
|
||||
// On Mojave, views automatically become layer-backed shortly after being added to
|
||||
|
|
@ -130,7 +131,7 @@ unsafe fn create_view(
|
|||
// explicitly make the view layer-backed up front so that AppKit doesn't do it
|
||||
// itself and break the association with its context.
|
||||
if f64::floor(appkit::NSAppKitVersionNumber) > appkit::NSAppKitVersionNumber10_12 {
|
||||
ns_view.setWantsLayer(YES);
|
||||
ns_view.setWantsLayer(Bool::YES.as_raw());
|
||||
}
|
||||
|
||||
(ns_view, cursor_state)
|
||||
|
|
@ -141,7 +142,7 @@ fn create_window(
|
|||
attrs: &WindowAttributes,
|
||||
pl_attrs: &PlatformSpecificWindowBuilderAttributes,
|
||||
) -> Option<IdRef> {
|
||||
autoreleasepool(|| unsafe {
|
||||
autoreleasepool(|_| unsafe {
|
||||
let screen = match attrs.fullscreen {
|
||||
Some(Fullscreen::Borderless(Some(RootMonitorHandle { inner: ref monitor })))
|
||||
| Some(Fullscreen::Exclusive(RootVideoMode {
|
||||
|
|
@ -208,17 +209,17 @@ fn create_window(
|
|||
frame,
|
||||
masks,
|
||||
appkit::NSBackingStoreBuffered,
|
||||
NO,
|
||||
Bool::NO.as_raw(),
|
||||
));
|
||||
|
||||
ns_window.non_nil().map(|ns_window| {
|
||||
let title = util::ns_string_id_ref(&attrs.title);
|
||||
ns_window.setReleasedWhenClosed_(NO);
|
||||
ns_window.setReleasedWhenClosed_(Bool::NO.as_raw());
|
||||
ns_window.setTitle_(*title);
|
||||
ns_window.setAcceptsMouseMovedEvents_(YES);
|
||||
ns_window.setAcceptsMouseMovedEvents_(Bool::YES.as_raw());
|
||||
|
||||
if pl_attrs.titlebar_transparent {
|
||||
ns_window.setTitlebarAppearsTransparent_(YES);
|
||||
ns_window.setTitlebarAppearsTransparent_(Bool::YES.as_raw());
|
||||
}
|
||||
if pl_attrs.title_hidden {
|
||||
ns_window.setTitleVisibility_(appkit::NSWindowTitleVisibility::NSWindowTitleHidden);
|
||||
|
|
@ -231,18 +232,15 @@ fn create_window(
|
|||
NSWindowButton::NSWindowZoomButton,
|
||||
] {
|
||||
let button = ns_window.standardWindowButton_(*titlebar_button);
|
||||
let _: () = msg_send![button, setHidden: YES];
|
||||
let _: () = msg_send![button, setHidden: true];
|
||||
}
|
||||
}
|
||||
if pl_attrs.movable_by_window_background {
|
||||
ns_window.setMovableByWindowBackground_(YES);
|
||||
ns_window.setMovableByWindowBackground_(Bool::YES.as_raw());
|
||||
}
|
||||
|
||||
if attrs.always_on_top {
|
||||
let _: () = msg_send![
|
||||
*ns_window,
|
||||
setLevel: ffi::NSWindowLevel::NSFloatingWindowLevel
|
||||
];
|
||||
let _: () = msg_send![*ns_window, setLevel: ffi::kCGFloatingWindowLevelKey];
|
||||
}
|
||||
|
||||
if let Some(increments) = pl_attrs.resize_increments {
|
||||
|
|
@ -254,7 +252,7 @@ fn create_window(
|
|||
}
|
||||
|
||||
if !pl_attrs.has_shadow {
|
||||
ns_window.setHasShadow_(NO);
|
||||
ns_window.setHasShadow_(Bool::NO.as_raw());
|
||||
}
|
||||
if attrs.position.is_none() {
|
||||
ns_window.center();
|
||||
|
|
@ -270,25 +268,25 @@ unsafe impl Sync for WindowClass {}
|
|||
|
||||
static WINDOW_CLASS: Lazy<WindowClass> = Lazy::new(|| unsafe {
|
||||
let window_superclass = class!(NSWindow);
|
||||
let mut decl = ClassDecl::new("WinitWindow", window_superclass).unwrap();
|
||||
let mut decl = ClassBuilder::new("WinitWindow", window_superclass).unwrap();
|
||||
|
||||
pub extern "C" fn can_become_main_window(_: &Object, _: Sel) -> BOOL {
|
||||
pub extern "C" fn can_become_main_window(_: &Object, _: Sel) -> Bool {
|
||||
trace_scope!("canBecomeMainWindow");
|
||||
YES
|
||||
Bool::YES
|
||||
}
|
||||
|
||||
pub extern "C" fn can_become_key_window(_: &Object, _: Sel) -> BOOL {
|
||||
pub extern "C" fn can_become_key_window(_: &Object, _: Sel) -> Bool {
|
||||
trace_scope!("canBecomeKeyWindow");
|
||||
YES
|
||||
Bool::YES
|
||||
}
|
||||
|
||||
decl.add_method(
|
||||
sel!(canBecomeMainWindow),
|
||||
can_become_main_window as extern "C" fn(&Object, Sel) -> BOOL,
|
||||
can_become_main_window as extern "C" fn(_, _) -> _,
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(canBecomeKeyWindow),
|
||||
can_become_key_window as extern "C" fn(&Object, Sel) -> BOOL,
|
||||
can_become_key_window as extern "C" fn(_, _) -> _,
|
||||
);
|
||||
WindowClass(decl.register())
|
||||
});
|
||||
|
|
@ -396,11 +394,8 @@ impl UnownedWindow {
|
|||
mut win_attribs: WindowAttributes,
|
||||
pl_attribs: PlatformSpecificWindowBuilderAttributes,
|
||||
) -> Result<(Arc<Self>, IdRef), RootOsError> {
|
||||
unsafe {
|
||||
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");
|
||||
}
|
||||
if !is_main_thread() {
|
||||
panic!("Windows can only be created on the main thread on macOS");
|
||||
}
|
||||
trace!("Creating new window");
|
||||
|
||||
|
|
@ -420,7 +415,7 @@ impl UnownedWindow {
|
|||
|
||||
unsafe {
|
||||
if win_attribs.transparent {
|
||||
ns_window.setOpaque_(NO);
|
||||
ns_window.setOpaque_(Bool::NO.as_raw());
|
||||
ns_window.setBackgroundColor_(NSColor::clearColor(nil));
|
||||
}
|
||||
|
||||
|
|
@ -519,8 +514,8 @@ impl UnownedWindow {
|
|||
|
||||
#[inline]
|
||||
pub fn is_visible(&self) -> Option<bool> {
|
||||
let is_visible: BOOL = unsafe { msg_send![*self.ns_window, isVisible] };
|
||||
Some(is_visible == YES)
|
||||
let is_visible = unsafe { msg_send![*self.ns_window, isVisible] };
|
||||
Some(is_visible)
|
||||
}
|
||||
|
||||
pub fn request_redraw(&self) {
|
||||
|
|
@ -625,8 +620,7 @@ impl UnownedWindow {
|
|||
|
||||
#[inline]
|
||||
pub fn is_resizable(&self) -> bool {
|
||||
let is_resizable: BOOL = unsafe { msg_send![*self.ns_window, isResizable] };
|
||||
is_resizable == YES
|
||||
unsafe { msg_send![*self.ns_window, isResizable] }
|
||||
}
|
||||
|
||||
pub fn set_cursor_icon(&self, cursor: CursorIcon) {
|
||||
|
|
@ -726,14 +720,14 @@ impl UnownedWindow {
|
|||
self.set_style_mask_sync(required);
|
||||
}
|
||||
|
||||
let is_zoomed: BOOL = unsafe { msg_send![*self.ns_window, isZoomed] };
|
||||
let is_zoomed = unsafe { msg_send![*self.ns_window, isZoomed] };
|
||||
|
||||
// Roll back temp styles
|
||||
if needs_temp_mask {
|
||||
self.set_style_mask_async(curr_mask);
|
||||
}
|
||||
|
||||
is_zoomed != NO
|
||||
is_zoomed
|
||||
}
|
||||
|
||||
fn saved_style(&self, shared_state: &mut SharedState) -> NSWindowStyleMask {
|
||||
|
|
@ -767,8 +761,7 @@ impl UnownedWindow {
|
|||
|
||||
#[inline]
|
||||
pub fn set_minimized(&self, minimized: bool) {
|
||||
let is_minimized: BOOL = unsafe { msg_send![*self.ns_window, isMiniaturized] };
|
||||
let is_minimized: bool = is_minimized == YES;
|
||||
let is_minimized: bool = unsafe { msg_send![*self.ns_window, isMiniaturized] };
|
||||
if is_minimized == minimized {
|
||||
return;
|
||||
}
|
||||
|
|
@ -998,10 +991,7 @@ impl UnownedWindow {
|
|||
|
||||
// Restore the normal window level following the Borderless fullscreen
|
||||
// `CGShieldingWindowLevel() + 1` hack.
|
||||
let _: () = msg_send![
|
||||
*self.ns_window,
|
||||
setLevel: ffi::NSWindowLevel::NSNormalWindowLevel
|
||||
];
|
||||
let _: () = msg_send![*self.ns_window, setLevel: ffi::kCGBaseWindowLevelKey];
|
||||
},
|
||||
_ => {}
|
||||
};
|
||||
|
|
@ -1088,14 +1078,12 @@ impl UnownedWindow {
|
|||
|
||||
#[inline]
|
||||
pub fn focus_window(&self) {
|
||||
let is_minimized: BOOL = unsafe { msg_send![*self.ns_window, isMiniaturized] };
|
||||
let is_minimized = is_minimized == YES;
|
||||
let is_visible: BOOL = unsafe { msg_send![*self.ns_window, isVisible] };
|
||||
let is_visible = is_visible == YES;
|
||||
let is_minimized: bool = unsafe { msg_send![*self.ns_window, isMiniaturized] };
|
||||
let is_visible: bool = unsafe { msg_send![*self.ns_window, isVisible] };
|
||||
|
||||
if !is_minimized && is_visible {
|
||||
unsafe {
|
||||
NSApp().activateIgnoringOtherApps_(YES);
|
||||
NSApp().activateIgnoringOtherApps_(Bool::YES.as_raw());
|
||||
util::make_key_and_order_front_async(*self.ns_window);
|
||||
}
|
||||
}
|
||||
|
|
@ -1223,7 +1211,7 @@ impl WindowExtMacOS for UnownedWindow {
|
|||
// Set the window frame to the screen frame size
|
||||
let screen = self.ns_window.screen();
|
||||
let screen_frame = NSScreen::frame(screen);
|
||||
NSWindow::setFrame_display_(*self.ns_window, screen_frame, YES);
|
||||
NSWindow::setFrame_display_(*self.ns_window, screen_frame, Bool::YES.as_raw());
|
||||
|
||||
// Fullscreen windows can't be resized, minimized, or moved
|
||||
util::toggle_style_mask(
|
||||
|
|
@ -1238,7 +1226,7 @@ impl WindowExtMacOS for UnownedWindow {
|
|||
NSWindowStyleMask::NSResizableWindowMask,
|
||||
false,
|
||||
);
|
||||
NSWindow::setMovable_(*self.ns_window, NO);
|
||||
NSWindow::setMovable_(*self.ns_window, Bool::NO.as_raw());
|
||||
|
||||
true
|
||||
} else {
|
||||
|
|
@ -1251,8 +1239,8 @@ impl WindowExtMacOS for UnownedWindow {
|
|||
}
|
||||
|
||||
let frame = shared_state_lock.saved_standard_frame();
|
||||
NSWindow::setFrame_display_(*self.ns_window, frame, YES);
|
||||
NSWindow::setMovable_(*self.ns_window, YES);
|
||||
NSWindow::setFrame_display_(*self.ns_window, frame, Bool::YES.as_raw());
|
||||
NSWindow::setMovable_(*self.ns_window, Bool::YES.as_raw());
|
||||
|
||||
true
|
||||
}
|
||||
|
|
@ -1261,15 +1249,12 @@ impl WindowExtMacOS for UnownedWindow {
|
|||
|
||||
#[inline]
|
||||
fn has_shadow(&self) -> bool {
|
||||
unsafe { self.ns_window.hasShadow() == YES }
|
||||
unsafe { Bool::from_raw(self.ns_window.hasShadow()).as_bool() }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_has_shadow(&self, has_shadow: bool) {
|
||||
unsafe {
|
||||
self.ns_window
|
||||
.setHasShadow_(if has_shadow { YES } else { NO })
|
||||
}
|
||||
unsafe { self.ns_window.setHasShadow_(Bool::new(has_shadow).as_raw()) }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1297,14 +1282,14 @@ unsafe fn set_min_inner_size<V: NSWindow + Copy>(window: V, mut min_size: Logica
|
|||
// If necessary, resize the window to match constraint
|
||||
if current_rect.size.width < min_size.width {
|
||||
current_rect.size.width = min_size.width;
|
||||
window.setFrame_display_(current_rect, NO)
|
||||
window.setFrame_display_(current_rect, Bool::NO.as_raw())
|
||||
}
|
||||
if current_rect.size.height < min_size.height {
|
||||
// The origin point of a rectangle is at its bottom left in Cocoa.
|
||||
// To ensure the window's top-left point remains the same:
|
||||
current_rect.origin.y += current_rect.size.height - min_size.height;
|
||||
current_rect.size.height = min_size.height;
|
||||
window.setFrame_display_(current_rect, NO)
|
||||
window.setFrame_display_(current_rect, Bool::NO.as_raw())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1322,13 +1307,13 @@ unsafe fn set_max_inner_size<V: NSWindow + Copy>(window: V, mut max_size: Logica
|
|||
// If necessary, resize the window to match constraint
|
||||
if current_rect.size.width > max_size.width {
|
||||
current_rect.size.width = max_size.width;
|
||||
window.setFrame_display_(current_rect, NO)
|
||||
window.setFrame_display_(current_rect, Bool::NO.as_raw())
|
||||
}
|
||||
if current_rect.size.height > max_size.height {
|
||||
// The origin point of a rectangle is at its bottom left in Cocoa.
|
||||
// To ensure the window's top-left point remains the same:
|
||||
current_rect.origin.y += current_rect.size.height - max_size.height;
|
||||
current_rect.size.height = max_size.height;
|
||||
window.setFrame_display_(current_rect, NO)
|
||||
window.setFrame_display_(current_rect, Bool::NO.as_raw())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,12 +7,12 @@ use std::{
|
|||
use cocoa::{
|
||||
appkit::{self, NSApplicationPresentationOptions, NSView, NSWindow, NSWindowOcclusionState},
|
||||
base::{id, nil},
|
||||
foundation::NSUInteger,
|
||||
};
|
||||
use objc::{
|
||||
declare::ClassDecl,
|
||||
declare::ClassBuilder,
|
||||
foundation::NSUInteger,
|
||||
rc::autoreleasepool,
|
||||
runtime::{Class, Object, Sel, BOOL, NO, YES},
|
||||
runtime::{Bool, Class, Object, Sel},
|
||||
};
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
|
|
@ -137,92 +137,91 @@ unsafe impl Sync for WindowDelegateClass {}
|
|||
|
||||
static WINDOW_DELEGATE_CLASS: Lazy<WindowDelegateClass> = Lazy::new(|| unsafe {
|
||||
let superclass = class!(NSResponder);
|
||||
let mut decl = ClassDecl::new("WinitWindowDelegate", superclass).unwrap();
|
||||
let mut decl = ClassBuilder::new("WinitWindowDelegate", superclass).unwrap();
|
||||
|
||||
decl.add_method(sel!(dealloc), dealloc as extern "C" fn(&Object, Sel));
|
||||
decl.add_method(sel!(dealloc), dealloc as extern "C" fn(_, _));
|
||||
decl.add_method(
|
||||
sel!(initWithWinit:),
|
||||
init_with_winit as extern "C" fn(&Object, Sel, *mut c_void) -> id,
|
||||
init_with_winit as extern "C" fn(_, _, _) -> _,
|
||||
);
|
||||
|
||||
decl.add_method(
|
||||
sel!(windowShouldClose:),
|
||||
window_should_close as extern "C" fn(&Object, Sel, id) -> BOOL,
|
||||
window_should_close as extern "C" fn(_, _, _) -> _,
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(windowWillClose:),
|
||||
window_will_close as extern "C" fn(&Object, Sel, id),
|
||||
window_will_close as extern "C" fn(_, _, _),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(windowDidResize:),
|
||||
window_did_resize as extern "C" fn(&Object, Sel, id),
|
||||
window_did_resize as extern "C" fn(_, _, _),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(windowDidMove:),
|
||||
window_did_move as extern "C" fn(&Object, Sel, id),
|
||||
window_did_move as extern "C" fn(_, _, _),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(windowDidChangeBackingProperties:),
|
||||
window_did_change_backing_properties as extern "C" fn(&Object, Sel, id),
|
||||
window_did_change_backing_properties as extern "C" fn(_, _, _),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(windowDidBecomeKey:),
|
||||
window_did_become_key as extern "C" fn(&Object, Sel, id),
|
||||
window_did_become_key as extern "C" fn(_, _, _),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(windowDidResignKey:),
|
||||
window_did_resign_key as extern "C" fn(&Object, Sel, id),
|
||||
window_did_resign_key as extern "C" fn(_, _, _),
|
||||
);
|
||||
|
||||
decl.add_method(
|
||||
sel!(draggingEntered:),
|
||||
dragging_entered as extern "C" fn(&Object, Sel, id) -> BOOL,
|
||||
dragging_entered as extern "C" fn(_, _, _) -> _,
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(prepareForDragOperation:),
|
||||
prepare_for_drag_operation as extern "C" fn(&Object, Sel, id) -> BOOL,
|
||||
prepare_for_drag_operation as extern "C" fn(_, _, _) -> _,
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(performDragOperation:),
|
||||
perform_drag_operation as extern "C" fn(&Object, Sel, id) -> BOOL,
|
||||
perform_drag_operation as extern "C" fn(_, _, _) -> _,
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(concludeDragOperation:),
|
||||
conclude_drag_operation as extern "C" fn(&Object, Sel, id),
|
||||
conclude_drag_operation as extern "C" fn(_, _, _),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(draggingExited:),
|
||||
dragging_exited as extern "C" fn(&Object, Sel, id),
|
||||
dragging_exited as extern "C" fn(_, _, _),
|
||||
);
|
||||
|
||||
decl.add_method(
|
||||
sel!(window:willUseFullScreenPresentationOptions:),
|
||||
window_will_use_fullscreen_presentation_options
|
||||
as extern "C" fn(&Object, Sel, id, NSUInteger) -> NSUInteger,
|
||||
window_will_use_fullscreen_presentation_options as extern "C" fn(_, _, _, _) -> _,
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(windowDidEnterFullScreen:),
|
||||
window_did_enter_fullscreen as extern "C" fn(&Object, Sel, id),
|
||||
window_did_enter_fullscreen as extern "C" fn(_, _, _),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(windowWillEnterFullScreen:),
|
||||
window_will_enter_fullscreen as extern "C" fn(&Object, Sel, id),
|
||||
window_will_enter_fullscreen as extern "C" fn(_, _, _),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(windowDidExitFullScreen:),
|
||||
window_did_exit_fullscreen as extern "C" fn(&Object, Sel, id),
|
||||
window_did_exit_fullscreen as extern "C" fn(_, _, _),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(windowWillExitFullScreen:),
|
||||
window_will_exit_fullscreen as extern "C" fn(&Object, Sel, id),
|
||||
window_will_exit_fullscreen as extern "C" fn(_, _, _),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(windowDidFailToEnterFullScreen:),
|
||||
window_did_fail_to_enter_fullscreen as extern "C" fn(&Object, Sel, id),
|
||||
window_did_fail_to_enter_fullscreen as extern "C" fn(_, _, _),
|
||||
);
|
||||
decl.add_method(
|
||||
sel!(windowDidChangeOcclusionState:),
|
||||
window_did_change_occlusion_state as extern "C" fn(&Object, Sel, id),
|
||||
window_did_change_occlusion_state as extern "C" fn(_, _, _),
|
||||
);
|
||||
|
||||
decl.add_ivar::<*mut c_void>("winitState");
|
||||
|
|
@ -233,7 +232,7 @@ static WINDOW_DELEGATE_CLASS: Lazy<WindowDelegateClass> = Lazy::new(|| unsafe {
|
|||
// boilerplate and wouldn't really clarify anything...
|
||||
fn with_state<F: FnOnce(&mut WindowDelegateState) -> T, T>(this: &Object, callback: F) {
|
||||
let state_ptr = unsafe {
|
||||
let state_ptr: *mut c_void = *this.get_ivar("winitState");
|
||||
let state_ptr: *mut c_void = *this.ivar("winitState");
|
||||
&mut *(state_ptr as *mut WindowDelegateState)
|
||||
};
|
||||
callback(state_ptr);
|
||||
|
|
@ -258,17 +257,17 @@ extern "C" fn init_with_winit(this: &Object, _sel: Sel, state: *mut c_void) -> i
|
|||
}
|
||||
}
|
||||
|
||||
extern "C" fn window_should_close(this: &Object, _: Sel, _: id) -> BOOL {
|
||||
extern "C" fn window_should_close(this: &Object, _: Sel, _: id) -> Bool {
|
||||
trace_scope!("windowShouldClose:");
|
||||
with_state(this, |state| state.emit_event(WindowEvent::CloseRequested));
|
||||
NO
|
||||
Bool::NO
|
||||
}
|
||||
|
||||
extern "C" fn window_will_close(this: &Object, _: Sel, _: id) {
|
||||
trace_scope!("windowWillClose:");
|
||||
with_state(this, |state| unsafe {
|
||||
// `setDelegate:` retains the previous value and then autoreleases it
|
||||
autoreleasepool(|| {
|
||||
autoreleasepool(|_| {
|
||||
// Since El Capitan, we need to be careful that delegate methods can't
|
||||
// be called after the window closes.
|
||||
let _: () = msg_send![*state.ns_window, setDelegate: nil];
|
||||
|
|
@ -325,7 +324,7 @@ extern "C" fn window_did_resign_key(this: &Object, _: Sel, _: id) {
|
|||
// to an id)
|
||||
let view_state: &mut ViewState = unsafe {
|
||||
let ns_view: &Object = (*state.ns_view).as_ref().expect("failed to deref");
|
||||
let state_ptr: *mut c_void = *ns_view.get_ivar("winitState");
|
||||
let state_ptr: *mut c_void = *ns_view.ivar("winitState");
|
||||
&mut *(state_ptr as *mut ViewState)
|
||||
};
|
||||
|
||||
|
|
@ -340,7 +339,7 @@ extern "C" fn window_did_resign_key(this: &Object, _: Sel, _: id) {
|
|||
}
|
||||
|
||||
/// Invoked when the dragged image enters destination bounds or frame
|
||||
extern "C" fn dragging_entered(this: &Object, _: Sel, sender: id) -> BOOL {
|
||||
extern "C" fn dragging_entered(this: &Object, _: Sel, sender: id) -> Bool {
|
||||
trace_scope!("draggingEntered:");
|
||||
|
||||
use cocoa::{appkit::NSPasteboard, foundation::NSFastEnumeration};
|
||||
|
|
@ -363,17 +362,17 @@ extern "C" fn dragging_entered(this: &Object, _: Sel, sender: id) -> BOOL {
|
|||
}
|
||||
}
|
||||
|
||||
YES
|
||||
Bool::YES
|
||||
}
|
||||
|
||||
/// Invoked when the image is released
|
||||
extern "C" fn prepare_for_drag_operation(_: &Object, _: Sel, _: id) -> BOOL {
|
||||
extern "C" fn prepare_for_drag_operation(_: &Object, _: Sel, _: id) -> Bool {
|
||||
trace_scope!("prepareForDragOperation:");
|
||||
YES
|
||||
Bool::YES
|
||||
}
|
||||
|
||||
/// Invoked after the released image has been removed from the screen
|
||||
extern "C" fn perform_drag_operation(this: &Object, _: Sel, sender: id) -> BOOL {
|
||||
extern "C" fn perform_drag_operation(this: &Object, _: Sel, sender: id) -> Bool {
|
||||
trace_scope!("performDragOperation:");
|
||||
|
||||
use cocoa::{appkit::NSPasteboard, foundation::NSFastEnumeration};
|
||||
|
|
@ -396,7 +395,7 @@ extern "C" fn perform_drag_operation(this: &Object, _: Sel, sender: id) -> BOOL
|
|||
}
|
||||
}
|
||||
|
||||
YES
|
||||
Bool::YES
|
||||
}
|
||||
|
||||
/// Invoked when the dragging operation is complete
|
||||
|
|
@ -478,7 +477,7 @@ extern "C" fn window_will_use_fullscreen_presentation_options(
|
|||
options = (NSApplicationPresentationOptions::NSApplicationPresentationFullScreen
|
||||
| NSApplicationPresentationOptions::NSApplicationPresentationHideDock
|
||||
| NSApplicationPresentationOptions::NSApplicationPresentationHideMenuBar)
|
||||
.bits();
|
||||
.bits() as NSUInteger;
|
||||
}
|
||||
})
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue