Skip to content

Commit a127bd6

Browse files
authored
iOS: Split classes in view.rs into separate files (#3511)
* Move application delegate to its own file * Move window subclass to window.rs * Split view controller to separate file
1 parent 4a8648b commit a127bd6

File tree

7 files changed

+382
-356
lines changed

7 files changed

+382
-356
lines changed

src/platform_impl/ios/app_delegate.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
}

src/platform_impl/ios/app_state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use objc2::{msg_send, sel};
2525
use once_cell::sync::Lazy;
2626

2727
use super::uikit::UIView;
28-
use super::view::WinitUIWindow;
28+
use super::window::WinitUIWindow;
2929
use crate::{
3030
dpi::PhysicalSize,
3131
event::{Event, InnerSizeWriter, StartCause, WindowEvent},

src/platform_impl/ios/event_loop.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ use crate::{
2727
window::{CustomCursor, CustomCursorSource},
2828
};
2929

30-
use super::{app_state, monitor, view, MonitorHandle};
30+
use super::app_delegate::AppDelegate;
31+
use super::{app_state, monitor, MonitorHandle};
3132
use super::{
3233
app_state::AppState,
3334
uikit::{UIApplication, UIApplicationMain, UIDevice, UIScreen},
@@ -201,14 +202,14 @@ impl<T: 'static> EventLoop<T> {
201202
app_state::will_launch(self.mtm, handler);
202203

203204
// Ensure application delegate is initialized
204-
view::WinitApplicationDelegate::class();
205+
let _ = AppDelegate::class();
205206

206207
unsafe {
207208
UIApplicationMain(
208209
0,
209210
ptr::null(),
210211
None,
211-
Some(&NSString::from_str("WinitApplicationDelegate")),
212+
Some(&NSString::from_str(AppDelegate::NAME)),
212213
)
213214
};
214215
unreachable!()

src/platform_impl/ios/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,14 @@
5858
#![cfg(ios_platform)]
5959
#![allow(clippy::let_unit_value)]
6060

61+
mod app_delegate;
6162
mod app_state;
6263
mod event_loop;
6364
mod ffi;
6465
mod monitor;
6566
mod uikit;
6667
mod view;
68+
mod view_controller;
6769
mod window;
6870

6971
use std::fmt;

0 commit comments

Comments
 (0)