From e9507f8d8698c6750c0382b9376afa846953b683 Mon Sep 17 00:00:00 2001 From: Micah Johnston Date: Tue, 8 Dec 2020 19:22:26 -0600 Subject: [PATCH] store WindowHandler as trait object in cocoa backend --- src/macos/view.rs | 90 ++++++++++++++++++++++----------------------- src/macos/window.rs | 14 +++---- 2 files changed, 52 insertions(+), 52 deletions(-) diff --git a/src/macos/view.rs b/src/macos/view.rs index f6a3f83..35a9445 100644 --- a/src/macos/view.rs +++ b/src/macos/view.rs @@ -25,10 +25,10 @@ use super::window::{ }; -pub(super) unsafe fn create_view( +pub(super) unsafe fn create_view( window_options: &WindowOpenOptions, ) -> id { - let class = create_view_class::(); + let class = create_view_class(); let view: id = msg_send![class, alloc]; @@ -43,7 +43,7 @@ pub(super) unsafe fn create_view( } -unsafe fn create_view_class() -> &'static Class { +unsafe fn create_view_class() -> &'static Class { // Use unique class names so that there are no conflicts between different // instances. The class is deleted when the view is released. Previously, // the class was stored in a OnceCell after creation. This way, we didn't @@ -54,104 +54,104 @@ unsafe fn create_view_class() -> &'static Class { class.add_method( sel!(acceptsFirstResponder), - property_yes:: as extern "C" fn(&Object, Sel) -> BOOL + property_yes as extern "C" fn(&Object, Sel) -> BOOL ); class.add_method( sel!(isFlipped), - property_yes:: as extern "C" fn(&Object, Sel) -> BOOL + property_yes as extern "C" fn(&Object, Sel) -> BOOL ); class.add_method( sel!(preservesContentInLiveResize), - property_no:: as extern "C" fn(&Object, Sel) -> BOOL + property_no as extern "C" fn(&Object, Sel) -> BOOL ); class.add_method( sel!(acceptsFirstMouse:), - accepts_first_mouse:: as extern "C" fn(&Object, Sel, id) -> BOOL + accepts_first_mouse as extern "C" fn(&Object, Sel, id) -> BOOL ); class.add_method( sel!(triggerOnFrame:), - trigger_on_frame:: as extern "C" fn(&Object, Sel, id) + trigger_on_frame as extern "C" fn(&Object, Sel, id) ); class.add_method( sel!(release), - release:: as extern "C" fn(&Object, Sel) + release as extern "C" fn(&Object, Sel) ); class.add_method( sel!(viewWillMoveToWindow:), - view_will_move_to_window:: as extern "C" fn(&Object, Sel, id) + view_will_move_to_window as extern "C" fn(&Object, Sel, id) ); class.add_method( sel!(updateTrackingAreas:), - update_tracking_areas:: as extern "C" fn(&Object, Sel, id) + update_tracking_areas as extern "C" fn(&Object, Sel, id) ); class.add_method( sel!(mouseMoved:), - mouse_moved:: as extern "C" fn(&Object, Sel, id), + mouse_moved as extern "C" fn(&Object, Sel, id), ); class.add_method( sel!(mouseDragged:), - mouse_moved:: as extern "C" fn(&Object, Sel, id), + mouse_moved as extern "C" fn(&Object, Sel, id), ); class.add_method( sel!(rightMouseDragged:), - mouse_moved:: as extern "C" fn(&Object, Sel, id), + mouse_moved as extern "C" fn(&Object, Sel, id), ); class.add_method( sel!(otherMouseDragged:), - mouse_moved:: as extern "C" fn(&Object, Sel, id), + mouse_moved as extern "C" fn(&Object, Sel, id), ); class.add_method( sel!(mouseEntered:), - mouse_entered:: as extern "C" fn(&Object, Sel, id), + mouse_entered as extern "C" fn(&Object, Sel, id), ); class.add_method( sel!(mouseExited:), - mouse_exited:: as extern "C" fn(&Object, Sel, id), + mouse_exited as extern "C" fn(&Object, Sel, id), ); class.add_method( sel!(mouseDown:), - left_mouse_down:: as extern "C" fn(&Object, Sel, id), + left_mouse_down as extern "C" fn(&Object, Sel, id), ); class.add_method( sel!(mouseUp:), - left_mouse_up:: as extern "C" fn(&Object, Sel, id), + left_mouse_up as extern "C" fn(&Object, Sel, id), ); class.add_method( sel!(rightMouseDown:), - right_mouse_down:: as extern "C" fn(&Object, Sel, id), + right_mouse_down as extern "C" fn(&Object, Sel, id), ); class.add_method( sel!(rightMouseUp:), - right_mouse_up:: as extern "C" fn(&Object, Sel, id), + right_mouse_up as extern "C" fn(&Object, Sel, id), ); class.add_method( sel!(otherMouseDown:), - middle_mouse_down:: as extern "C" fn(&Object, Sel, id), + middle_mouse_down as extern "C" fn(&Object, Sel, id), ); class.add_method( sel!(otherMouseUp:), - middle_mouse_up:: as extern "C" fn(&Object, Sel, id), + middle_mouse_up as extern "C" fn(&Object, Sel, id), ); class.add_method( sel!(keyDown:), - key_down:: as extern "C" fn(&Object, Sel, id), + key_down as extern "C" fn(&Object, Sel, id), ); class.add_method( sel!(keyUp:), - key_up:: as extern "C" fn(&Object, Sel, id), + key_up as extern "C" fn(&Object, Sel, id), ); class.add_method( sel!(flagsChanged:), - flags_changed:: as extern "C" fn(&Object, Sel, id), + flags_changed as extern "C" fn(&Object, Sel, id), ); class.add_ivar::<*mut c_void>(WINDOW_STATE_IVAR_NAME); @@ -161,7 +161,7 @@ unsafe fn create_view_class() -> &'static Class { } -extern "C" fn property_yes( +extern "C" fn property_yes( _this: &Object, _sel: Sel, ) -> BOOL { @@ -169,7 +169,7 @@ extern "C" fn property_yes( } -extern "C" fn property_no( +extern "C" fn property_no( _this: &Object, _sel: Sel, ) -> BOOL { @@ -177,7 +177,7 @@ extern "C" fn property_no( } -extern "C" fn accepts_first_mouse( +extern "C" fn accepts_first_mouse( _this: &Object, _sel: Sel, _event: id @@ -186,12 +186,12 @@ extern "C" fn accepts_first_mouse( } -extern "C" fn trigger_on_frame( +extern "C" fn trigger_on_frame( this: &Object, _sel: Sel, _event: id ){ - let state: &mut WindowState = unsafe { + let state: &mut WindowState = unsafe { WindowState::from_field(this) }; @@ -199,7 +199,7 @@ extern "C" fn trigger_on_frame( } -extern "C" fn release(this: &Object, _sel: Sel) { +extern "C" fn release(this: &Object, _sel: Sel) { unsafe { let superclass = msg_send![this, superclass]; @@ -220,7 +220,7 @@ extern "C" fn release(this: &Object, _sel: Sel) { let state_ptr: *mut c_void = *this.get_ivar( WINDOW_STATE_IVAR_NAME ); - Arc::from_raw(state_ptr as *mut WindowState); + Arc::from_raw(state_ptr as *mut WindowState); // Delete class let class = msg_send![this, class]; @@ -261,7 +261,7 @@ unsafe fn reinit_tracking_area(this: &Object, tracking_area: *mut Object){ } -extern "C" fn view_will_move_to_window( +extern "C" fn view_will_move_to_window( this: &Object, _self: Sel, new_window: id @@ -305,7 +305,7 @@ extern "C" fn view_will_move_to_window( } -extern "C" fn update_tracking_areas( +extern "C" fn update_tracking_areas( this: &Object, _self: Sel, _: id @@ -319,7 +319,7 @@ extern "C" fn update_tracking_areas( } -extern "C" fn mouse_moved( +extern "C" fn mouse_moved( this: &Object, _sel: Sel, event: id @@ -337,7 +337,7 @@ extern "C" fn mouse_moved( let event = Event::Mouse(MouseEvent::CursorMoved { position }); - let state: &mut WindowState = unsafe { + let state: &mut WindowState = unsafe { WindowState::from_field(this) }; @@ -347,12 +347,12 @@ extern "C" fn mouse_moved( macro_rules! mouse_simple_extern_fn { ($fn:ident, $event:expr) => { - extern "C" fn $fn( + extern "C" fn $fn( this: &Object, _sel: Sel, _event: id, ){ - let state: &mut WindowState = unsafe { + let state: &mut WindowState = unsafe { WindowState::from_field(this) }; @@ -375,8 +375,8 @@ mouse_simple_extern_fn!(mouse_entered, MouseEvent::CursorEntered); mouse_simple_extern_fn!(mouse_exited, MouseEvent::CursorLeft); -extern "C" fn key_down(this: &Object, _: Sel, event: id){ - let state: &mut WindowState = unsafe { +extern "C" fn key_down(this: &Object, _: Sel, event: id){ + let state: &mut WindowState = unsafe { WindowState::from_field(this) }; @@ -386,8 +386,8 @@ extern "C" fn key_down(this: &Object, _: Sel, event: id){ } -extern "C" fn key_up(this: &Object, _: Sel, event: id){ - let state: &mut WindowState = unsafe { +extern "C" fn key_up(this: &Object, _: Sel, event: id){ + let state: &mut WindowState = unsafe { WindowState::from_field(this) }; @@ -397,8 +397,8 @@ extern "C" fn key_up(this: &Object, _: Sel, event: id){ } -extern "C" fn flags_changed(this: &Object, _: Sel, event: id){ - let state: &mut WindowState = unsafe { +extern "C" fn flags_changed(this: &Object, _: Sel, event: id){ + let state: &mut WindowState = unsafe { WindowState::from_field(this) }; diff --git a/src/macos/window.rs b/src/macos/window.rs index 0436576..5b00674 100644 --- a/src/macos/window.rs +++ b/src/macos/window.rs @@ -71,7 +71,7 @@ impl Window { let ns_view = handle.ns_view as *mut objc::runtime::Object; unsafe { - let subview = create_view::(&options); + let subview = create_view(&options); let _: id = msg_send![ns_view, addSubview: subview]; @@ -88,7 +88,7 @@ impl Window { }, Parent::AsIfParented => { let ns_view = unsafe { - create_view::(&options) + create_view(&options) }; let window = Window { @@ -147,7 +147,7 @@ impl Window { ns_window.makeKeyAndOrderFront_(nil); - let subview = create_view::(&options); + let subview = create_view(&options); ns_window.setContentView_(subview); @@ -161,7 +161,7 @@ impl Window { }, }; - let window_handler = build(&mut crate::Window(&mut window)); + let window_handler = Box::new(build(&mut crate::Window(&mut window))); let window_state_arc = Arc::new(WindowState { window, @@ -208,14 +208,14 @@ impl Window { } -pub(super) struct WindowState { +pub(super) struct WindowState { window: Window, - window_handler: H, + window_handler: Box, keyboard_state: KeyboardState, } -impl WindowState { +impl WindowState { /// Returns a mutable reference to a WindowState from an Objective-C field /// /// Don't use this to create two simulataneous references to a single