|
| 1 | +use icrate::Foundation::{MainThreadMarker, NSObject, NSObjectProtocol}; |
| 2 | +use objc2::{declare_class, mutability, ClassType, DeclaredClass}; |
| 3 | + |
| 4 | +use super::app_state::{self, EventWrapper}; |
| 5 | +use super::uikit::{UIApplication, UIWindow}; |
| 6 | +use super::window::WinitUIWindow; |
| 7 | +use crate::{ |
| 8 | + event::{Event, WindowEvent}, |
| 9 | + window::WindowId as RootWindowId, |
| 10 | +}; |
| 11 | + |
| 12 | +declare_class!( |
| 13 | + pub struct AppDelegate; |
| 14 | + |
| 15 | + unsafe impl ClassType for AppDelegate { |
| 16 | + type Super = NSObject; |
| 17 | + type Mutability = mutability::InteriorMutable; |
| 18 | + const NAME: &'static str = "WinitApplicationDelegate"; |
| 19 | + } |
| 20 | + |
| 21 | + impl DeclaredClass for AppDelegate {} |
| 22 | + |
| 23 | + // UIApplicationDelegate protocol |
| 24 | + unsafe impl AppDelegate { |
| 25 | + #[method(application:didFinishLaunchingWithOptions:)] |
| 26 | + fn did_finish_launching(&self, _application: &UIApplication, _: *mut NSObject) -> bool { |
| 27 | + app_state::did_finish_launching(MainThreadMarker::new().unwrap()); |
| 28 | + true |
| 29 | + } |
| 30 | + |
| 31 | + #[method(applicationDidBecomeActive:)] |
| 32 | + fn did_become_active(&self, _application: &UIApplication) { |
| 33 | + let mtm = MainThreadMarker::new().unwrap(); |
| 34 | + app_state::handle_nonuser_event(mtm, EventWrapper::StaticEvent(Event::Resumed)) |
| 35 | + } |
| 36 | + |
| 37 | + #[method(applicationWillResignActive:)] |
| 38 | + fn will_resign_active(&self, _application: &UIApplication) { |
| 39 | + let mtm = MainThreadMarker::new().unwrap(); |
| 40 | + app_state::handle_nonuser_event(mtm, EventWrapper::StaticEvent(Event::Suspended)) |
| 41 | + } |
| 42 | + |
| 43 | + #[method(applicationWillEnterForeground:)] |
| 44 | + fn will_enter_foreground(&self, application: &UIApplication) { |
| 45 | + self.send_occluded_event_for_all_windows(application, false); |
| 46 | + } |
| 47 | + |
| 48 | + #[method(applicationDidEnterBackground:)] |
| 49 | + fn did_enter_background(&self, application: &UIApplication) { |
| 50 | + self.send_occluded_event_for_all_windows(application, true); |
| 51 | + } |
| 52 | + |
| 53 | + #[method(applicationWillTerminate:)] |
| 54 | + fn will_terminate(&self, application: &UIApplication) { |
| 55 | + let mut events = Vec::new(); |
| 56 | + for window in application.windows().iter() { |
| 57 | + if window.is_kind_of::<WinitUIWindow>() { |
| 58 | + // SAFETY: We just checked that the window is a `winit` window |
| 59 | + let window = unsafe { |
| 60 | + let ptr: *const UIWindow = window; |
| 61 | + let ptr: *const WinitUIWindow = ptr.cast(); |
| 62 | + &*ptr |
| 63 | + }; |
| 64 | + events.push(EventWrapper::StaticEvent(Event::WindowEvent { |
| 65 | + window_id: RootWindowId(window.id()), |
| 66 | + event: WindowEvent::Destroyed, |
| 67 | + })); |
| 68 | + } |
| 69 | + } |
| 70 | + let mtm = MainThreadMarker::new().unwrap(); |
| 71 | + app_state::handle_nonuser_events(mtm, events); |
| 72 | + app_state::terminated(mtm); |
| 73 | + } |
| 74 | + |
| 75 | + #[method(applicationDidReceiveMemoryWarning:)] |
| 76 | + fn did_receive_memory_warning(&self, _application: &UIApplication) { |
| 77 | + let mtm = MainThreadMarker::new().unwrap(); |
| 78 | + app_state::handle_nonuser_event(mtm, EventWrapper::StaticEvent(Event::MemoryWarning)) |
| 79 | + } |
| 80 | + } |
| 81 | +); |
| 82 | + |
| 83 | +impl AppDelegate { |
| 84 | + fn send_occluded_event_for_all_windows(&self, application: &UIApplication, occluded: bool) { |
| 85 | + let mut events = Vec::new(); |
| 86 | + for window in application.windows().iter() { |
| 87 | + if window.is_kind_of::<WinitUIWindow>() { |
| 88 | + // SAFETY: We just checked that the window is a `winit` window |
| 89 | + let window = unsafe { |
| 90 | + let ptr: *const UIWindow = window; |
| 91 | + let ptr: *const WinitUIWindow = ptr.cast(); |
| 92 | + &*ptr |
| 93 | + }; |
| 94 | + events.push(EventWrapper::StaticEvent(Event::WindowEvent { |
| 95 | + window_id: RootWindowId(window.id()), |
| 96 | + event: WindowEvent::Occluded(occluded), |
| 97 | + })); |
| 98 | + } |
| 99 | + } |
| 100 | + let mtm = MainThreadMarker::new().unwrap(); |
| 101 | + app_state::handle_nonuser_events(mtm, events); |
| 102 | + } |
| 103 | +} |
0 commit comments