Initial transition to objc2 (#2452)

* Use objc2

* Use objc2's NSInteger/NSUInteger/NSRange
This commit is contained in:
Mads Marquart 2022-09-02 15:48:02 +02:00 committed by GitHub
parent e0018d0710
commit 112965b4ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 394 additions and 480 deletions

View file

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