From 4a53350bcc185de0916885cfc3a9f04c2d864083 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Mon, 24 Jun 2024 07:05:58 +0200 Subject: [PATCH] Don't register an application delegate This allows the user to override the application delegate themselves, which opens several doors for customization that were previously closed. --- src/changelog/unreleased.md | 5 +- src/platform/ios.rs | 15 +- src/platform/macos.rs | 70 ++++++- src/platform_impl/apple/appkit/app.rs | 20 +- src/platform_impl/apple/appkit/app_state.rs | 183 ++++++++---------- .../apple/appkit/event_handler.rs | 2 +- src/platform_impl/apple/appkit/event_loop.rs | 155 +++++++-------- src/platform_impl/apple/appkit/observer.rs | 6 +- src/platform_impl/apple/appkit/view.rs | 15 +- src/platform_impl/apple/appkit/window.rs | 5 +- .../apple/appkit/window_delegate.rs | 19 +- src/platform_impl/apple/mod.rs | 1 + .../apple/notification_center.rs | 27 +++ src/platform_impl/apple/uikit/app_delegate.rs | 14 -- src/platform_impl/apple/uikit/event_loop.rs | 32 +-- src/platform_impl/apple/uikit/mod.rs | 1 - 16 files changed, 308 insertions(+), 262 deletions(-) create mode 100644 src/platform_impl/apple/notification_center.rs delete mode 100644 src/platform_impl/apple/uikit/app_delegate.rs diff --git a/src/changelog/unreleased.md b/src/changelog/unreleased.md index 8db5cec221..8e40c4bb1e 100644 --- a/src/changelog/unreleased.md +++ b/src/changelog/unreleased.md @@ -52,7 +52,10 @@ changelog entry. to send specific data to be processed on the main thread. - Changed `EventLoopProxy::send_event` to `EventLoopProxy::wake_up`, it now only wakes up the loop. -- On iOS, no longer act as-if the application successfully open all URLs. +- On iOS and macOS, remove custom application delegates. You are now allowed to override the + application delegate yourself. +- On iOS, no longer act as-if the application successfully open all URLs. Override + `application:didFinishLaunchingWithOptions:` and provide the desired behaviour yourself. ### Removed diff --git a/src/platform/ios.rs b/src/platform/ios.rs index 38243411ec..1e5bbd2250 100644 --- a/src/platform/ios.rs +++ b/src/platform/ios.rs @@ -3,11 +3,14 @@ //! Winit has an OS requirement of iOS 8 or higher, and is regularly tested on //! iOS 9.3. //! +//! ## Window initialization +//! //! iOS's main `UIApplicationMain` does some init work that's required by all //! UI-related code (see issue [#1705]). It is best to create your windows -//! inside `Event::Resumed`. +//! inside [`ApplicationHandler::resumed`]. //! //! [#1705]: https://github.com/rust-windowing/winit/issues/1705 +//! [`ApplicationHandler::resumed`]: crate::application::ApplicationHandler::resumed //! //! ## Building app //! @@ -63,6 +66,16 @@ //! opengl will result in segfault. //! //! Also note that app may not receive the LoopExiting event if suspended; it might be SIGKILL'ed. +//! +//! ## Custom `UIApplicationDelegate` +//! +//! Winit usually handles everything related to the lifecycle events of the application. Sometimes, +//! though, you might want to access some of the more niche stuff that [the application +//! delegate][app-delegate] provides. This functionality is not exposed directly in Winit, since it +//! would increase the API surface by quite a lot. Instead, Winit guarantees that it will not +//! register an application delegate, so you can set up a custom one in a nib file instead. +//! +//! [app-delegate]: https://developer.apple.com/documentation/uikit/uiapplicationdelegate?language=objc use std::os::raw::c_void; diff --git a/src/platform/macos.rs b/src/platform/macos.rs index 8938a5ca89..5d354417c5 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -3,16 +3,84 @@ //! Winit has an OS requirement of macOS 10.11 or higher (same as Rust //! itself), and is regularly tested on macOS 10.14. //! +//! ## Window initialization +//! //! A lot of functionality expects the application to be ready before you //! start doing anything; this includes creating windows, fetching monitors, //! drawing, and so on, see issues [#2238], [#2051] and [#2087]. //! //! If you encounter problems, you should try doing your initialization inside -//! `Event::Resumed`. +//! [`ApplicationHandler::resumed`]. //! //! [#2238]: https://github.com/rust-windowing/winit/issues/2238 //! [#2051]: https://github.com/rust-windowing/winit/issues/2051 //! [#2087]: https://github.com/rust-windowing/winit/issues/2087 +//! [`ApplicationHandler::resumed`]: crate::application::ApplicationHandler::resumed +//! +//! ## Custom `NSApplicationDelegate` +//! +//! Winit usually handles everything related to the lifecycle events of the application. Sometimes, +//! though, you might want to do more niche stuff, such as [handle when the user re-activates the +//! application][reopen]. Such functionality is not exposed directly in Winit, since it would +//! increase the API surface by quite a lot. +//! +//! [reopen]: https://developer.apple.com/documentation/appkit/nsapplicationdelegate/1428638-applicationshouldhandlereopen?language=objc +//! +//! Instead, Winit guarantees that it will not register an application delegate, so the solution is +//! to register your own application delegate, as outlined in the following example (see +//! `objc2-app-kit` for more detailed information). +#![cfg_attr(target_os = "macos", doc = "```")] +#![cfg_attr(not(target_os = "macos"), doc = "```ignore")] +//! use objc2::rc::Retained; +//! use objc2::runtime::ProtocolObject; +//! use objc2::{declare_class, msg_send_id, mutability, ClassType, DeclaredClass}; +//! use objc2_app_kit::{NSApplication, NSApplicationDelegate}; +//! use objc2_foundation::{NSArray, NSURL, MainThreadMarker, NSObject, NSObjectProtocol}; +//! use winit::event_loop::EventLoop; +//! +//! declare_class!( +//! struct AppDelegate; +//! +//! unsafe impl ClassType for AppDelegate { +//! type Super = NSObject; +//! type Mutability = mutability::MainThreadOnly; +//! const NAME: &'static str = "MyAppDelegate"; +//! } +//! +//! impl DeclaredClass for AppDelegate {} +//! +//! unsafe impl NSObjectProtocol for AppDelegate {} +//! +//! unsafe impl NSApplicationDelegate for AppDelegate { +//! #[method(application:openURLs:)] +//! fn application_openURLs(&self, application: &NSApplication, urls: &NSArray) { +//! // Note: To specifically get `application:openURLs:` to work, you _might_ +//! // have to bundle your application. This is not done in this example. +//! println!("open urls: {application:?}, {urls:?}"); +//! } +//! } +//! ); +//! +//! impl AppDelegate { +//! fn new(mtm: MainThreadMarker) -> Retained { +//! unsafe { msg_send_id![super(mtm.alloc().set_ivars(())), init] } +//! } +//! } +//! +//! fn main() -> Result<(), Box> { +//! let event_loop = EventLoop::new()?; +//! +//! let mtm = MainThreadMarker::new().unwrap(); +//! let delegate = AppDelegate::new(mtm); +//! // Important: Call `sharedApplication` after `EventLoop::new`, +//! // doing it before is not yet supported. +//! let app = NSApplication::sharedApplication(mtm); +//! app.setDelegate(Some(ProtocolObject::from_ref(&*delegate))); +//! +//! // event_loop.run_app(&mut my_app); +//! Ok(()) +//! } +//! ``` use std::os::raw::c_void; diff --git a/src/platform_impl/apple/appkit/app.rs b/src/platform_impl/apple/appkit/app.rs index 539bf6f68c..22df4e64aa 100644 --- a/src/platform_impl/apple/appkit/app.rs +++ b/src/platform_impl/apple/appkit/app.rs @@ -1,10 +1,12 @@ #![allow(clippy::unnecessary_cast)] +use std::rc::Rc; + use objc2::{declare_class, msg_send, mutability, ClassType, DeclaredClass}; use objc2_app_kit::{NSApplication, NSEvent, NSEventModifierFlags, NSEventType, NSResponder}; use objc2_foundation::{MainThreadMarker, NSObject}; -use super::app_state::ApplicationDelegate; +use super::app_state::AppState; use super::DEVICE_ID; use crate::event::{DeviceEvent, ElementState}; @@ -38,15 +40,15 @@ declare_class!( key_window.sendEvent(event); } } else { - let delegate = ApplicationDelegate::get(MainThreadMarker::from(self)); - maybe_dispatch_device_event(&delegate, event); + let app_state = AppState::get(MainThreadMarker::from(self)); + maybe_dispatch_device_event(&app_state, event); unsafe { msg_send![super(self), sendEvent: event] } } } } ); -fn maybe_dispatch_device_event(delegate: &ApplicationDelegate, event: &NSEvent) { +fn maybe_dispatch_device_event(app_state: &Rc, event: &NSEvent) { let event_type = unsafe { event.r#type() }; #[allow(non_upper_case_globals)] match event_type { @@ -58,7 +60,7 @@ fn maybe_dispatch_device_event(delegate: &ApplicationDelegate, event: &NSEvent) let delta_y = unsafe { event.deltaY() } as f64; if delta_x != 0.0 { - delegate.maybe_queue_with_handler(move |app, event_loop| { + app_state.maybe_queue_with_handler(move |app, event_loop| { app.device_event(event_loop, DEVICE_ID, DeviceEvent::Motion { axis: 0, value: delta_x, @@ -67,7 +69,7 @@ fn maybe_dispatch_device_event(delegate: &ApplicationDelegate, event: &NSEvent) } if delta_y != 0.0 { - delegate.maybe_queue_with_handler(move |app, event_loop| { + app_state.maybe_queue_with_handler(move |app, event_loop| { app.device_event(event_loop, DEVICE_ID, DeviceEvent::Motion { axis: 1, value: delta_y, @@ -76,7 +78,7 @@ fn maybe_dispatch_device_event(delegate: &ApplicationDelegate, event: &NSEvent) } if delta_x != 0.0 || delta_y != 0.0 { - delegate.maybe_queue_with_handler(move |app, event_loop| { + app_state.maybe_queue_with_handler(move |app, event_loop| { app.device_event(event_loop, DEVICE_ID, DeviceEvent::MouseMotion { delta: (delta_x, delta_y), }); @@ -85,7 +87,7 @@ fn maybe_dispatch_device_event(delegate: &ApplicationDelegate, event: &NSEvent) }, NSEventType::LeftMouseDown | NSEventType::RightMouseDown | NSEventType::OtherMouseDown => { let button = unsafe { event.buttonNumber() } as u32; - delegate.maybe_queue_with_handler(move |app, event_loop| { + app_state.maybe_queue_with_handler(move |app, event_loop| { app.device_event(event_loop, DEVICE_ID, DeviceEvent::Button { button, state: ElementState::Pressed, @@ -94,7 +96,7 @@ fn maybe_dispatch_device_event(delegate: &ApplicationDelegate, event: &NSEvent) }, NSEventType::LeftMouseUp | NSEventType::RightMouseUp | NSEventType::OtherMouseUp => { let button = unsafe { event.buttonNumber() } as u32; - delegate.maybe_queue_with_handler(move |app, event_loop| { + app_state.maybe_queue_with_handler(move |app, event_loop| { app.device_event(event_loop, DEVICE_ID, DeviceEvent::Button { button, state: ElementState::Released, diff --git a/src/platform_impl/apple/appkit/app_state.rs b/src/platform_impl/apple/appkit/app_state.rs index 82a39c38d6..1b95db7525 100644 --- a/src/platform_impl/apple/appkit/app_state.rs +++ b/src/platform_impl/apple/appkit/app_state.rs @@ -1,14 +1,12 @@ -use std::cell::{Cell, RefCell}; +use std::cell::{Cell, OnceCell, RefCell}; use std::mem; -use std::rc::Weak; +use std::rc::{Rc, Weak}; use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering}; use std::sync::Arc; use std::time::Instant; -use objc2::rc::Retained; -use objc2::{declare_class, msg_send_id, mutability, ClassType, DeclaredClass}; -use objc2_app_kit::{NSApplication, NSApplicationActivationPolicy, NSApplicationDelegate}; -use objc2_foundation::{MainThreadMarker, NSNotification, NSObject, NSObjectProtocol}; +use objc2_app_kit::{NSApplication, NSApplicationActivationPolicy}; +use objc2_foundation::{MainThreadMarker, NSNotification}; use crate::application::ApplicationHandler; use crate::event::{StartCause, WindowEvent}; @@ -22,6 +20,7 @@ use super::{menu, WindowId}; #[derive(Debug)] pub(super) struct AppState { + mtm: MainThreadMarker, activation_policy: NSApplicationActivationPolicy, default_menu: bool, activate_ignoring_other_apps: bool, @@ -47,34 +46,21 @@ pub(super) struct AppState { // as such should be careful to not add fields that, in turn, strongly reference those. } -declare_class!( - #[derive(Debug)] - pub(super) struct ApplicationDelegate; - - unsafe impl ClassType for ApplicationDelegate { - type Super = NSObject; - type Mutability = mutability::MainThreadOnly; - const NAME: &'static str = "WinitApplicationDelegate"; - } - - impl DeclaredClass for ApplicationDelegate { - type Ivars = AppState; - } - - unsafe impl NSObjectProtocol for ApplicationDelegate {} - - unsafe impl NSApplicationDelegate for ApplicationDelegate {} -); +// TODO(madsmtm): Use `MainThreadBound` once that is possible in `static`s. +thread_local! { + static GLOBAL: OnceCell> = const { OnceCell::new() }; +} -impl ApplicationDelegate { - pub(super) fn new( +impl AppState { + pub(super) fn setup_global( mtm: MainThreadMarker, activation_policy: NSApplicationActivationPolicy, proxy_wake_up: Arc, default_menu: bool, activate_ignoring_other_apps: bool, - ) -> Retained { - let this = mtm.alloc().set_ivars(AppState { + ) -> Rc { + let this = Rc::new(AppState { + mtm, activation_policy, proxy_wake_up, default_menu, @@ -94,33 +80,37 @@ impl ApplicationDelegate { wait_timeout: Cell::new(None), pending_redraw: RefCell::new(vec![]), }); - unsafe { msg_send_id![super(this), init] } + + GLOBAL.with(|key: &OnceCell>| { + key.set(this.clone()).expect("application state can only be set once"); + }); + + this } // NOTE: This notification will, globally, only be emitted once, // no matter how many `EventLoop`s the user creates. - pub fn did_finish_launching(&self, _notification: &NSNotification) { + pub fn did_finish_launching(self: &Rc, _notification: &NSNotification) { trace_scope!("NSApplicationDidFinishLaunchingNotification"); - self.ivars().is_launched.set(true); + self.is_launched.set(true); - let mtm = MainThreadMarker::from(self); - let app = NSApplication::sharedApplication(mtm); + let app = NSApplication::sharedApplication(self.mtm); // We need to delay setting the activation policy and activating the app // until `applicationDidFinishLaunching` has been called. Otherwise the // menu bar is initially unresponsive on macOS 10.15. - app.setActivationPolicy(self.ivars().activation_policy); + app.setActivationPolicy(self.activation_policy); window_activation_hack(&app); #[allow(deprecated)] - app.activateIgnoringOtherApps(self.ivars().activate_ignoring_other_apps); + app.activateIgnoringOtherApps(self.activate_ignoring_other_apps); - if self.ivars().default_menu { + if self.default_menu { // The menubar initialization should be before the `NewEvents` event, to allow // overriding of the default menu even if it's created menu::initialize(&app); } - self.ivars().waker.borrow_mut().start(); + self.waker.borrow_mut().start(); self.set_is_running(true); self.dispatch_init_events(); @@ -130,73 +120,73 @@ impl ApplicationDelegate { // // In this case we still want to consider Winit's `EventLoop` to be "running", // so we call `start_running()` above. - if self.ivars().stop_on_launch.get() { + if self.stop_on_launch.get() { // NOTE: the original idea had been to only stop the underlying `RunLoop` // for the app but that didn't work as expected (`-[NSApplication run]` // effectively ignored the attempt to stop the RunLoop and re-started it). // // So we return from `pump_events` by stopping the application. - let app = NSApplication::sharedApplication(mtm); + let app = NSApplication::sharedApplication(self.mtm); stop_app_immediately(&app); } } - pub fn will_terminate(&self, _notification: &NSNotification) { + pub fn will_terminate(self: &Rc, _notification: &NSNotification) { trace_scope!("NSApplicationWillTerminateNotification"); // TODO: Notify every window that it will be destroyed, like done in iOS? self.internal_exit(); } - pub fn get(mtm: MainThreadMarker) -> Retained { - let app = NSApplication::sharedApplication(mtm); - let delegate = - unsafe { app.delegate() }.expect("a delegate was not configured on the application"); - if delegate.is_kind_of::() { - // SAFETY: Just checked that the delegate is an instance of `ApplicationDelegate` - unsafe { Retained::cast(delegate) } - } else { - panic!("tried to get a delegate that was not the one Winit has registered") - } + pub fn get(mtm: MainThreadMarker) -> Rc { + let _ = mtm; + GLOBAL.with(|s| { + // We hold `MainThreadMarker`, so this should always be set on this thread. + Rc::clone(s.get().expect("tried to get application state before it was registered")) + }) } - /// Place the event handler in the application delegate for the duration + pub fn mtm(&self) -> MainThreadMarker { + self.mtm + } + + /// Place the event handler in the application state for the duration /// of the given closure. pub fn set_event_handler( &self, handler: &mut dyn ApplicationHandler, closure: impl FnOnce() -> R, ) -> R { - self.ivars().event_handler.set(handler, closure) + self.event_handler.set(handler, closure) } /// If `pump_events` is called to progress the event loop then we /// bootstrap the event loop via `-[NSApplication run]` but will use /// `CFRunLoopRunInMode` for subsequent calls to `pump_events`. pub fn set_stop_on_launch(&self) { - self.ivars().stop_on_launch.set(true); + self.stop_on_launch.set(true); } pub fn set_stop_before_wait(&self, value: bool) { - self.ivars().stop_before_wait.set(value) + self.stop_before_wait.set(value) } pub fn set_stop_after_wait(&self, value: bool) { - self.ivars().stop_after_wait.set(value) + self.stop_after_wait.set(value) } pub fn set_stop_on_redraw(&self, value: bool) { - self.ivars().stop_on_redraw.set(value) + self.stop_on_redraw.set(value) } pub fn set_wait_timeout(&self, value: Option) { - self.ivars().wait_timeout.set(value) + self.wait_timeout.set(value) } /// Clears the `running` state and resets the `control_flow` state when an `EventLoop` exits. /// /// NOTE: that if the `NSApplication` has been launched then that state is preserved, /// and we won't need to re-launch the app if subsequent EventLoops are run. - pub fn internal_exit(&self) { + pub fn internal_exit(self: &Rc) { self.with_handler(|app, event_loop| { app.exiting(event_loop); }); @@ -209,42 +199,41 @@ impl ApplicationDelegate { } pub fn is_launched(&self) -> bool { - self.ivars().is_launched.get() + self.is_launched.get() } pub fn set_is_running(&self, value: bool) { - self.ivars().is_running.set(value) + self.is_running.set(value) } pub fn is_running(&self) -> bool { - self.ivars().is_running.get() + self.is_running.get() } pub fn exit(&self) { - self.ivars().exit.set(true) + self.exit.set(true) } pub fn clear_exit(&self) { - self.ivars().exit.set(false) + self.exit.set(false) } pub fn exiting(&self) -> bool { - self.ivars().exit.get() + self.exit.get() } pub fn set_control_flow(&self, value: ControlFlow) { - self.ivars().control_flow.set(value) + self.control_flow.set(value) } pub fn control_flow(&self) -> ControlFlow { - self.ivars().control_flow.get() + self.control_flow.get() } - pub fn handle_redraw(&self, window_id: WindowId) { - let mtm = MainThreadMarker::from(self); + pub fn handle_redraw(self: &Rc, window_id: WindowId) { // Redraw request might come out of order from the OS. // -> Don't go back into the event handler when our callstack originates from there - if !self.ivars().event_handler.in_use() { + if !self.event_handler.in_use() { self.with_handler(|app, event_loop| { app.window_event(event_loop, RootWindowId(window_id), WindowEvent::RedrawRequested); }); @@ -252,24 +241,24 @@ impl ApplicationDelegate { // `pump_events` will request to stop immediately _after_ dispatching RedrawRequested // events as a way to ensure that `pump_events` can't block an external loop // indefinitely - if self.ivars().stop_on_redraw.get() { - let app = NSApplication::sharedApplication(mtm); + if self.stop_on_redraw.get() { + let app = NSApplication::sharedApplication(self.mtm); stop_app_immediately(&app); } } } pub fn queue_redraw(&self, window_id: WindowId) { - let mut pending_redraw = self.ivars().pending_redraw.borrow_mut(); + let mut pending_redraw = self.pending_redraw.borrow_mut(); if !pending_redraw.contains(&window_id) { pending_redraw.push(window_id); } - self.ivars().run_loop.wakeup(); + self.run_loop.wakeup(); } #[track_caller] pub fn maybe_queue_with_handler( - &self, + self: &Rc, callback: impl FnOnce(&mut dyn ApplicationHandler, &RootActiveEventLoop) + 'static, ) { // Most programmer actions in AppKit (e.g. change window fullscreen, set focused, etc.) @@ -278,12 +267,12 @@ impl ApplicationDelegate { // However, it is not documented which actions do this, and which ones are done immediately, // so to make sure that we don't encounter re-entrancy issues, we first check if we're // currently handling another event, and if we are, we queue the event instead. - if !self.ivars().event_handler.in_use() { + if !self.event_handler.in_use() { self.with_handler(callback); } else { tracing::debug!("had to queue event since another is currently being handled"); - let this = self.retain(); - self.ivars().run_loop.queue_closure(move || { + let this = Rc::clone(self); + self.run_loop.queue_closure(move || { this.with_handler(callback); }); } @@ -291,15 +280,15 @@ impl ApplicationDelegate { #[track_caller] fn with_handler( - &self, + self: &Rc, callback: impl FnOnce(&mut dyn ApplicationHandler, &RootActiveEventLoop), ) { - let event_loop = ActiveEventLoop::new_root(self.retain()); - self.ivars().event_handler.handle(callback, &event_loop); + let event_loop = ActiveEventLoop::new_root(Rc::clone(self)); + self.event_handler.handle(callback, &event_loop); } /// dispatch `NewEvents(Init)` + `Resumed` - pub fn dispatch_init_events(&self) { + pub fn dispatch_init_events(self: &Rc) { self.with_handler(|app, event_loop| app.new_events(event_loop, StartCause::Init)); // NB: For consistency all platforms must emit a 'resumed' event even though macOS // applications don't themselves have a formal suspend/resume lifecycle. @@ -307,23 +296,22 @@ impl ApplicationDelegate { } // Called by RunLoopObserver after finishing waiting for new events - pub fn wakeup(&self, panic_info: Weak) { - let mtm = MainThreadMarker::from(self); + pub fn wakeup(self: &Rc, panic_info: Weak) { let panic_info = panic_info .upgrade() .expect("The panic info must exist here. This failure indicates a developer error."); // Return when in event handler due to https://github.com/rust-windowing/winit/issues/1779 - if panic_info.is_panicking() || !self.ivars().event_handler.ready() || !self.is_running() { + if panic_info.is_panicking() || !self.event_handler.ready() || !self.is_running() { return; } - if self.ivars().stop_after_wait.get() { - let app = NSApplication::sharedApplication(mtm); + if self.stop_after_wait.get() { + let app = NSApplication::sharedApplication(self.mtm); stop_app_immediately(&app); } - let start = self.ivars().start_time.get().unwrap(); + let start = self.start_time.get().unwrap(); let cause = match self.control_flow() { ControlFlow::Poll => StartCause::Poll, ControlFlow::Wait => StartCause::WaitCancelled { start, requested_resume: None }, @@ -340,8 +328,7 @@ impl ApplicationDelegate { } // Called by RunLoopObserver before waiting for new events - pub fn cleared(&self, panic_info: Weak) { - let mtm = MainThreadMarker::from(self); + pub fn cleared(self: &Rc, panic_info: Weak) { let panic_info = panic_info .upgrade() .expect("The panic info must exist here. This failure indicates a developer error."); @@ -349,15 +336,15 @@ impl ApplicationDelegate { // Return when in event handler due to https://github.com/rust-windowing/winit/issues/1779 // XXX: how does it make sense that `event_handler.ready()` can ever return `false` here if // we're about to return to the `CFRunLoop` to poll for new events? - if panic_info.is_panicking() || !self.ivars().event_handler.ready() || !self.is_running() { + if panic_info.is_panicking() || !self.event_handler.ready() || !self.is_running() { return; } - if self.ivars().proxy_wake_up.swap(false, AtomicOrdering::Relaxed) { + if self.proxy_wake_up.swap(false, AtomicOrdering::Relaxed) { self.with_handler(|app, event_loop| app.proxy_wake_up(event_loop)); } - let redraw = mem::take(&mut *self.ivars().pending_redraw.borrow_mut()); + let redraw = mem::take(&mut *self.pending_redraw.borrow_mut()); for window_id in redraw { self.with_handler(|app, event_loop| { app.window_event(event_loop, RootWindowId(window_id), WindowEvent::RedrawRequested); @@ -368,22 +355,22 @@ impl ApplicationDelegate { }); if self.exiting() { - let app = NSApplication::sharedApplication(mtm); + let app = NSApplication::sharedApplication(self.mtm); stop_app_immediately(&app); } - if self.ivars().stop_before_wait.get() { - let app = NSApplication::sharedApplication(mtm); + if self.stop_before_wait.get() { + let app = NSApplication::sharedApplication(self.mtm); stop_app_immediately(&app); } - self.ivars().start_time.set(Some(Instant::now())); - let wait_timeout = self.ivars().wait_timeout.get(); // configured by pump_events + self.start_time.set(Some(Instant::now())); + let wait_timeout = self.wait_timeout.get(); // configured by pump_events let app_timeout = match self.control_flow() { ControlFlow::Wait => None, ControlFlow::Poll => Some(Instant::now()), ControlFlow::WaitUntil(instant) => Some(instant), }; - self.ivars().waker.borrow_mut().start_at(min_timeout(wait_timeout, app_timeout)); + self.waker.borrow_mut().start_at(min_timeout(wait_timeout, app_timeout)); } } diff --git a/src/platform_impl/apple/appkit/event_handler.rs b/src/platform_impl/apple/appkit/event_handler.rs index c8a33ad915..fd9c865e67 100644 --- a/src/platform_impl/apple/appkit/event_handler.rs +++ b/src/platform_impl/apple/appkit/event_handler.rs @@ -124,7 +124,7 @@ impl EventHandler { callback(*user_app, event_loop); }, Ok(None) => { - // `NSApplication`, our app delegate and this handler are all + // `NSApplication`, our app state and this handler are all // global state and so it's not impossible that we could get // an event after the application has exited the `EventLoop`. tracing::error!("tried to run event handler, but no handler was set"); diff --git a/src/platform_impl/apple/appkit/event_loop.rs b/src/platform_impl/apple/appkit/event_loop.rs index f21f594668..c14f79c042 100644 --- a/src/platform_impl/apple/appkit/event_loop.rs +++ b/src/platform_impl/apple/appkit/event_loop.rs @@ -4,31 +4,28 @@ use std::collections::VecDeque; use std::marker::PhantomData; use std::os::raw::c_void; use std::panic::{catch_unwind, resume_unwind, RefUnwindSafe, UnwindSafe}; -use std::ptr::{self, NonNull}; +use std::ptr; use std::rc::{Rc, Weak}; use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering}; use std::sync::Arc; use std::time::{Duration, Instant}; -use block2::RcBlock; use core_foundation::base::{CFIndex, CFRelease}; use core_foundation::runloop::{ kCFRunLoopCommonModes, CFRunLoopAddSource, CFRunLoopGetMain, CFRunLoopSourceContext, CFRunLoopSourceCreate, CFRunLoopSourceRef, CFRunLoopSourceSignal, CFRunLoopWakeUp, }; use objc2::rc::{autoreleasepool, Retained}; -use objc2::runtime::ProtocolObject; use objc2::{msg_send_id, ClassType}; use objc2_app_kit::{ NSApplication, NSApplicationActivationPolicy, NSApplicationDidFinishLaunchingNotification, NSApplicationWillTerminateNotification, NSWindow, }; -use objc2_foundation::{ - MainThreadMarker, NSNotification, NSNotificationCenter, NSObject, NSObjectProtocol, -}; +use objc2_foundation::{MainThreadMarker, NSNotificationCenter, NSObject, NSObjectProtocol}; +use super::super::notification_center::create_observer; use super::app::WinitApplication; -use super::app_state::ApplicationDelegate; +use super::app_state::AppState; use super::cursor::CustomCursor; use super::event::dummy_event; use super::monitor::{self, MonitorHandle}; @@ -72,19 +69,19 @@ impl PanicInfo { #[derive(Debug)] pub struct ActiveEventLoop { - delegate: Retained, + app_state: Rc, pub(super) mtm: MainThreadMarker, } impl ActiveEventLoop { - pub(super) fn new_root(delegate: Retained) -> RootWindowTarget { - let mtm = MainThreadMarker::from(&*delegate); - let p = Self { delegate, mtm }; + pub(super) fn new_root(app_state: Rc) -> RootWindowTarget { + let mtm = app_state.mtm(); + let p = Self { app_state, mtm }; RootWindowTarget { p, _marker: PhantomData } } - pub(super) fn app_delegate(&self) -> &ApplicationDelegate { - &self.delegate + pub(super) fn app_state(&self) -> &Rc { + &self.app_state } pub fn create_custom_cursor(&self, source: CustomCursorSource) -> RootCustomCursor { @@ -120,23 +117,23 @@ impl ActiveEventLoop { } pub(crate) fn set_control_flow(&self, control_flow: ControlFlow) { - self.delegate.set_control_flow(control_flow) + self.app_state.set_control_flow(control_flow) } pub(crate) fn control_flow(&self) -> ControlFlow { - self.delegate.control_flow() + self.app_state.control_flow() } pub(crate) fn exit(&self) { - self.delegate.exit() + self.app_state.exit() } pub(crate) fn clear_exit(&self) { - self.delegate.clear_exit() + self.app_state.clear_exit() } pub(crate) fn exiting(&self) -> bool { - self.delegate.exiting() + self.app_state.exiting() } pub(crate) fn owned_display_handle(&self) -> OwnedDisplayHandle { @@ -166,11 +163,7 @@ pub struct EventLoop { /// We intentionally don't store `WinitApplication` since we want to have /// the possibility of swapping that out at some point. app: Retained, - /// The application delegate that we've registered. - /// - /// The delegate is only weakly referenced by NSApplication, so we must - /// keep it around here as well. - delegate: Retained, + app_state: Rc, proxy_wake_up: Arc, @@ -227,7 +220,7 @@ impl EventLoop { let proxy_wake_up = Arc::new(AtomicBool::new(false)); - let delegate = ApplicationDelegate::new( + let app_state = AppState::setup_global( mtm, activation_policy, proxy_wake_up.clone(), @@ -237,52 +230,40 @@ impl EventLoop { let center = unsafe { NSNotificationCenter::defaultCenter() }; - let block = { - let delegate = objc2::rc::Weak::new(&*delegate); - RcBlock::new(move |notification: NonNull| { - if let Some(delegate) = delegate.load() { - delegate.did_finish_launching(unsafe { notification.as_ref() }); - } - }) - }; - let _did_finish_launching_observer = unsafe { - center.addObserverForName_object_queue_usingBlock( - Some(NSApplicationDidFinishLaunchingNotification), - None, // No object filter - None, // No queue, run on posting thread (i.e. main thread) - &block, + let weak_app_state = Rc::downgrade(&app_state); + let _did_finish_launching_observer = { + create_observer( + ¢er, + unsafe { NSApplicationDidFinishLaunchingNotification }, + move |notification| { + if let Some(app_state) = weak_app_state.upgrade() { + app_state.did_finish_launching(notification); + } + }, ) }; - let block = { - let delegate = objc2::rc::Weak::new(&*delegate); - RcBlock::new(move |notification: NonNull| { - if let Some(delegate) = delegate.load() { - delegate.will_terminate(unsafe { notification.as_ref() }); - } - }) - }; - let _will_terminate_observer = unsafe { - center.addObserverForName_object_queue_usingBlock( - Some(NSApplicationWillTerminateNotification), - None, // No object filter - None, // No queue, run on posting thread (i.e. main thread) - &block, + let weak_app_state = Rc::downgrade(&app_state); + let _will_terminate_observer = { + create_observer( + ¢er, + unsafe { NSApplicationWillTerminateNotification }, + move |notification| { + if let Some(app_state) = weak_app_state.upgrade() { + app_state.will_terminate(notification); + } + }, ) }; - autoreleasepool(|_| { - app.setDelegate(Some(ProtocolObject::from_ref(&*delegate))); - }); - let panic_info: Rc = Default::default(); setup_control_flow_observers(mtm, Rc::downgrade(&panic_info)); Ok(EventLoop { app, - delegate: delegate.clone(), + app_state: app_state.clone(), window_target: RootWindowTarget { - p: ActiveEventLoop { delegate, mtm }, + p: ActiveEventLoop { app_state, mtm }, _marker: PhantomData, }, proxy_wake_up, @@ -308,18 +289,18 @@ impl EventLoop { &mut self, app: &mut A, ) -> Result<(), EventLoopError> { - self.delegate.set_event_handler(app, || { + self.app_state.set_event_handler(app, || { autoreleasepool(|_| { // clear / normalize pump_events state - self.delegate.set_wait_timeout(None); - self.delegate.set_stop_before_wait(false); - self.delegate.set_stop_after_wait(false); - self.delegate.set_stop_on_redraw(false); - - if self.delegate.is_launched() { - debug_assert!(!self.delegate.is_running()); - self.delegate.set_is_running(true); - self.delegate.dispatch_init_events(); + self.app_state.set_wait_timeout(None); + self.app_state.set_stop_before_wait(false); + self.app_state.set_stop_after_wait(false); + self.app_state.set_stop_on_redraw(false); + + if self.app_state.is_launched() { + debug_assert!(!self.app_state.is_running()); + self.app_state.set_is_running(true); + self.app_state.dispatch_init_events(); } // SAFETY: We do not run the application re-entrantly @@ -334,7 +315,7 @@ impl EventLoop { resume_unwind(panic); } - self.delegate.internal_exit() + self.app_state.internal_exit() }) }); @@ -346,47 +327,47 @@ impl EventLoop { timeout: Option, app: &mut A, ) -> PumpStatus { - self.delegate.set_event_handler(app, || { + self.app_state.set_event_handler(app, || { autoreleasepool(|_| { // As a special case, if the application hasn't been launched yet then we at least // run the loop until it has fully launched. - if !self.delegate.is_launched() { - debug_assert!(!self.delegate.is_running()); + if !self.app_state.is_launched() { + debug_assert!(!self.app_state.is_running()); - self.delegate.set_stop_on_launch(); + self.app_state.set_stop_on_launch(); // SAFETY: We do not run the application re-entrantly unsafe { self.app.run() }; // Note: we dispatch `NewEvents(Init)` + `Resumed` events after the application // has launched - } else if !self.delegate.is_running() { + } else if !self.app_state.is_running() { // Even though the application may have been launched, it's possible we aren't // running if the `EventLoop` was run before and has since // exited. This indicates that we just starting to re-run // the same `EventLoop` again. - self.delegate.set_is_running(true); - self.delegate.dispatch_init_events(); + self.app_state.set_is_running(true); + self.app_state.dispatch_init_events(); } else { // Only run for as long as the given `Duration` allows so we don't block the // external loop. match timeout { Some(Duration::ZERO) => { - self.delegate.set_wait_timeout(None); - self.delegate.set_stop_before_wait(true); + self.app_state.set_wait_timeout(None); + self.app_state.set_stop_before_wait(true); }, Some(duration) => { - self.delegate.set_stop_before_wait(false); + self.app_state.set_stop_before_wait(false); let timeout = Instant::now() + duration; - self.delegate.set_wait_timeout(Some(timeout)); - self.delegate.set_stop_after_wait(true); + self.app_state.set_wait_timeout(Some(timeout)); + self.app_state.set_stop_after_wait(true); }, None => { - self.delegate.set_wait_timeout(None); - self.delegate.set_stop_before_wait(false); - self.delegate.set_stop_after_wait(true); + self.app_state.set_wait_timeout(None); + self.app_state.set_stop_before_wait(false); + self.app_state.set_stop_after_wait(true); }, } - self.delegate.set_stop_on_redraw(true); + self.app_state.set_stop_on_redraw(true); // SAFETY: We do not run the application re-entrantly unsafe { self.app.run() }; } @@ -400,8 +381,8 @@ impl EventLoop { resume_unwind(panic); } - if self.delegate.exiting() { - self.delegate.internal_exit(); + if self.app_state.exiting() { + self.app_state.internal_exit(); PumpStatus::Exit(0) } else { PumpStatus::Continue diff --git a/src/platform_impl/apple/appkit/observer.rs b/src/platform_impl/apple/appkit/observer.rs index 8339803086..426b90bb96 100644 --- a/src/platform_impl/apple/appkit/observer.rs +++ b/src/platform_impl/apple/appkit/observer.rs @@ -22,7 +22,7 @@ use core_foundation::runloop::{ use objc2_foundation::MainThreadMarker; use tracing::error; -use super::app_state::ApplicationDelegate; +use super::app_state::AppState; use super::event_loop::{stop_app_on_panic, PanicInfo}; use super::ffi; @@ -59,7 +59,7 @@ extern "C" fn control_flow_begin_handler( match activity { kCFRunLoopAfterWaiting => { // trace!("Triggered `CFRunLoopAfterWaiting`"); - ApplicationDelegate::get(MainThreadMarker::new().unwrap()).wakeup(panic_info); + AppState::get(MainThreadMarker::new().unwrap()).wakeup(panic_info); // trace!("Completed `CFRunLoopAfterWaiting`"); }, _ => unreachable!(), @@ -81,7 +81,7 @@ extern "C" fn control_flow_end_handler( match activity { kCFRunLoopBeforeWaiting => { // trace!("Triggered `CFRunLoopBeforeWaiting`"); - ApplicationDelegate::get(MainThreadMarker::new().unwrap()).cleared(panic_info); + AppState::get(MainThreadMarker::new().unwrap()).cleared(panic_info); // trace!("Completed `CFRunLoopBeforeWaiting`"); }, kCFRunLoopExit => (), // unimplemented!(), // not expected to ever happen diff --git a/src/platform_impl/apple/appkit/view.rs b/src/platform_impl/apple/appkit/view.rs index a5b048ea67..9392a283fc 100644 --- a/src/platform_impl/apple/appkit/view.rs +++ b/src/platform_impl/apple/appkit/view.rs @@ -2,6 +2,7 @@ use std::cell::{Cell, RefCell}; use std::collections::{HashMap, VecDeque}; use std::ptr; +use std::rc::Rc; use objc2::rc::{Retained, WeakId}; use objc2::runtime::{AnyObject, Sel}; @@ -16,7 +17,7 @@ use objc2_foundation::{ NSPoint, NSRange, NSRect, NSSize, NSString, NSUInteger, }; -use super::app_state::ApplicationDelegate; +use super::app_state::AppState; use super::cursor::{default_cursor, invisible_cursor}; use super::event::{ code_to_key, code_to_location, create_key_event, event_mods, lalt_pressed, ralt_pressed, @@ -112,7 +113,7 @@ fn get_left_modifier_code(key: &Key) -> KeyCode { #[derive(Debug)] pub struct ViewState { /// Strong reference to the global application state. - app_delegate: Retained, + app_state: Rc, cursor_state: RefCell, ime_position: Cell, @@ -206,7 +207,7 @@ declare_class!( // It's a workaround for https://github.com/rust-windowing/winit/issues/2640, don't replace with `self.window_id()`. if let Some(window) = self.ivars()._ns_window.load() { - self.ivars().app_delegate.handle_redraw(window.id()); + self.ivars().app_state.handle_redraw(window.id()); } // This is a direct subclass of NSView, no need to call superclass' drawRect: @@ -687,7 +688,7 @@ declare_class!( self.update_modifiers(event, false); - self.ivars().app_delegate.maybe_queue_with_handler(move |app, event_loop| + self.ivars().app_state.maybe_queue_with_handler(move |app, event_loop| app.device_event(event_loop, DEVICE_ID, DeviceEvent::MouseWheel { delta }) ); self.queue_event(WindowEvent::MouseWheel { @@ -782,14 +783,14 @@ declare_class!( impl WinitView { pub(super) fn new( - app_delegate: &ApplicationDelegate, + app_state: &Rc, window: &WinitWindow, accepts_first_mouse: bool, option_as_alt: OptionAsAlt, ) -> Retained { let mtm = MainThreadMarker::from(window); let this = mtm.alloc().set_ivars(ViewState { - app_delegate: app_delegate.retain(), + app_state: Rc::clone(app_state), cursor_state: Default::default(), ime_position: Default::default(), ime_size: Default::default(), @@ -834,7 +835,7 @@ impl WinitView { fn queue_event(&self, event: WindowEvent) { let window_id = RootWindowId(self.window().id()); - self.ivars().app_delegate.maybe_queue_with_handler(move |app, event_loop| { + self.ivars().app_state.maybe_queue_with_handler(move |app, event_loop| { app.window_event(event_loop, window_id, event); }); } diff --git a/src/platform_impl/apple/appkit/window.rs b/src/platform_impl/apple/appkit/window.rs index eb1f75deea..b1d6f573eb 100644 --- a/src/platform_impl/apple/appkit/window.rs +++ b/src/platform_impl/apple/appkit/window.rs @@ -28,9 +28,8 @@ impl Window { attributes: WindowAttributes, ) -> Result { let mtm = window_target.mtm; - let delegate = autoreleasepool(|_| { - WindowDelegate::new(window_target.app_delegate(), attributes, mtm) - })?; + let delegate = + autoreleasepool(|_| WindowDelegate::new(window_target.app_state(), attributes, mtm))?; Ok(Window { window: MainThreadBound::new(delegate.window().retain(), mtm), delegate: MainThreadBound::new(delegate, mtm), diff --git a/src/platform_impl/apple/appkit/window_delegate.rs b/src/platform_impl/apple/appkit/window_delegate.rs index c50af9b6f9..9d0aa0dc13 100644 --- a/src/platform_impl/apple/appkit/window_delegate.rs +++ b/src/platform_impl/apple/appkit/window_delegate.rs @@ -3,6 +3,7 @@ use std::cell::{Cell, RefCell}; use std::collections::VecDeque; use std::ffi::c_void; use std::ptr; +use std::rc::Rc; use std::sync::{Arc, Mutex}; use core_graphics::display::{CGDisplay, CGPoint}; @@ -26,7 +27,7 @@ use objc2_foundation::{ }; use tracing::{trace, warn}; -use super::app_state::ApplicationDelegate; +use super::app_state::AppState; use super::cursor::cursor_from_icon; use super::monitor::{self, flip_window_screen_coordinates, get_display_id}; use super::observer::RunLoop; @@ -79,7 +80,7 @@ impl Default for PlatformSpecificWindowAttributes { #[derive(Debug)] pub(crate) struct State { /// Strong reference to the global application state. - app_delegate: Retained, + app_state: Rc, window: Retained, @@ -482,7 +483,7 @@ impl Drop for WindowDelegate { } fn new_window( - app_delegate: &ApplicationDelegate, + app_state: &Rc, attrs: &WindowAttributes, mtm: MainThreadMarker, ) -> Option> { @@ -622,7 +623,7 @@ fn new_window( } let view = WinitView::new( - app_delegate, + app_state, &window, attrs.platform_specific.accepts_first_mouse, attrs.platform_specific.option_as_alt, @@ -665,11 +666,11 @@ fn new_window( impl WindowDelegate { pub(super) fn new( - app_delegate: &ApplicationDelegate, + app_state: &Rc, attrs: WindowAttributes, mtm: MainThreadMarker, ) -> Result, RootOsError> { - let window = new_window(app_delegate, &attrs, mtm) + let window = new_window(app_state, &attrs, mtm) .ok_or_else(|| os_error!(OsError::CreationError("couldn't create `NSWindow`")))?; #[cfg(feature = "rwh_06")] @@ -709,7 +710,7 @@ impl WindowDelegate { } let delegate = mtm.alloc().set_ivars(State { - app_delegate: app_delegate.retain(), + app_state: Rc::clone(app_state), window: window.retain(), previous_position: Cell::new(None), previous_scale_factor: Cell::new(scale_factor), @@ -808,7 +809,7 @@ impl WindowDelegate { pub(crate) fn queue_event(&self, event: WindowEvent) { let window_id = RootWindowId(self.window().id()); - self.ivars().app_delegate.maybe_queue_with_handler(move |app, event_loop| { + self.ivars().app_state.maybe_queue_with_handler(move |app, event_loop| { app.window_event(event_loop, window_id, event); }); } @@ -907,7 +908,7 @@ impl WindowDelegate { } pub fn request_redraw(&self) { - self.ivars().app_delegate.queue_redraw(self.window().id()); + self.ivars().app_state.queue_redraw(self.window().id()); } #[inline] diff --git a/src/platform_impl/apple/mod.rs b/src/platform_impl/apple/mod.rs index 807aa61d9a..705ad1fc22 100644 --- a/src/platform_impl/apple/mod.rs +++ b/src/platform_impl/apple/mod.rs @@ -2,6 +2,7 @@ #[cfg(target_os = "macos")] mod appkit; +mod notification_center; #[cfg(not(target_os = "macos"))] mod uikit; diff --git a/src/platform_impl/apple/notification_center.rs b/src/platform_impl/apple/notification_center.rs new file mode 100644 index 0000000000..652bf1d079 --- /dev/null +++ b/src/platform_impl/apple/notification_center.rs @@ -0,0 +1,27 @@ +use std::ptr::NonNull; + +use block2::RcBlock; +use objc2::rc::Retained; +use objc2_foundation::{NSNotification, NSNotificationCenter, NSNotificationName, NSObject}; + +/// Observe the given notification. +/// +/// This is used in Winit as an alternative to declaring an application delegate, as we want to +/// give the user full control over those. +pub fn create_observer( + center: &NSNotificationCenter, + name: &NSNotificationName, + handler: impl Fn(&NSNotification) + 'static, +) -> Retained { + let block = RcBlock::new(move |notification: NonNull| { + handler(unsafe { notification.as_ref() }); + }); + unsafe { + center.addObserverForName_object_queue_usingBlock( + Some(name), + None, // No sender filter + None, // No queue, run on posting thread (i.e. main thread) + &block, + ) + } +} diff --git a/src/platform_impl/apple/uikit/app_delegate.rs b/src/platform_impl/apple/uikit/app_delegate.rs deleted file mode 100644 index 2e8389ff6b..0000000000 --- a/src/platform_impl/apple/uikit/app_delegate.rs +++ /dev/null @@ -1,14 +0,0 @@ -use objc2::{declare_class, mutability, ClassType, DeclaredClass}; -use objc2_foundation::NSObject; - -declare_class!( - pub struct AppDelegate; - - unsafe impl ClassType for AppDelegate { - type Super = NSObject; - type Mutability = mutability::InteriorMutable; - const NAME: &'static str = "WinitApplicationDelegate"; - } - - impl DeclaredClass for AppDelegate {} -); diff --git a/src/platform_impl/apple/uikit/event_loop.rs b/src/platform_impl/apple/uikit/event_loop.rs index fabf854ddc..e6f66bc778 100644 --- a/src/platform_impl/apple/uikit/event_loop.rs +++ b/src/platform_impl/apple/uikit/event_loop.rs @@ -5,7 +5,6 @@ use std::ptr::{self, NonNull}; use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering}; use std::sync::Arc; -use block2::RcBlock; use core_foundation::base::{CFIndex, CFRelease}; use core_foundation::runloop::{ kCFRunLoopAfterWaiting, kCFRunLoopBeforeWaiting, kCFRunLoopCommonModes, kCFRunLoopDefaultMode, @@ -15,9 +14,7 @@ use core_foundation::runloop::{ }; use objc2::rc::Retained; use objc2::{msg_send_id, ClassType}; -use objc2_foundation::{ - MainThreadMarker, NSNotification, NSNotificationCenter, NSNotificationName, NSObject, NSString, -}; +use objc2_foundation::{MainThreadMarker, NSNotificationCenter, NSObject}; use objc2_ui_kit::{ UIApplication, UIApplicationDidBecomeActiveNotification, UIApplicationDidEnterBackgroundNotification, UIApplicationDidFinishLaunchingNotification, @@ -26,6 +23,7 @@ use objc2_ui_kit::{ UIApplicationWillTerminateNotification, UIScreen, }; +use super::super::notification_center::create_observer; use super::app_state::{send_occluded_event_for_all_windows, EventLoopHandler, EventWrapper}; use crate::application::ApplicationHandler; use crate::error::EventLoopError; @@ -33,7 +31,6 @@ use crate::event::Event; use crate::event_loop::{ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents}; use crate::window::{CustomCursor, CustomCursorSource}; -use super::app_delegate::AppDelegate; use super::app_state::AppState; use super::{app_state, monitor, MonitorHandle}; @@ -162,24 +159,6 @@ pub struct EventLoop { #[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Hash)] pub(crate) struct PlatformSpecificEventLoopAttributes {} -fn create_observer( - center: &NSNotificationCenter, - name: &NSNotificationName, - handler: impl Fn(&NSNotification) + 'static, -) -> Retained { - let block = RcBlock::new(move |notification: NonNull| { - handler(unsafe { notification.as_ref() }); - }); - unsafe { - center.addObserverForName_object_queue_usingBlock( - Some(name), - None, // No object filter - None, // No queue, run on posting thread (i.e. main thread) - &block, - ) - } -} - impl EventLoop { pub(crate) fn new( _: &PlatformSpecificEventLoopAttributes, @@ -328,9 +307,6 @@ impl EventLoop { app_state::will_launch(self.mtm, handler); - // Ensure application delegate is initialized - let _ = AppDelegate::class(); - extern "C" { // These functions are in crt_externs.h. fn _NSGetArgc() -> *mut c_int; @@ -341,8 +317,10 @@ impl EventLoop { UIApplicationMain( *_NSGetArgc(), NonNull::new(*_NSGetArgv()).unwrap(), + // We intentionally don't override neither the application nor the delegate, to + // allow the user to do so themselves! + None, None, - Some(&NSString::from_str(AppDelegate::NAME)), ) }; unreachable!() diff --git a/src/platform_impl/apple/uikit/mod.rs b/src/platform_impl/apple/uikit/mod.rs index f69ac21b79..5831be2520 100644 --- a/src/platform_impl/apple/uikit/mod.rs +++ b/src/platform_impl/apple/uikit/mod.rs @@ -1,6 +1,5 @@ #![allow(clippy::let_unit_value)] -mod app_delegate; mod app_state; mod event_loop; mod monitor;