Skip to content

Commit

Permalink
iOS: Split classes in view.rs into separate files (#3511)
Browse files Browse the repository at this point in the history
* Move application delegate to its own file
* Move window subclass to window.rs
* Split view controller to separate file
  • Loading branch information
madsmtm committed Feb 22, 2024
1 parent 4a8648b commit a127bd6
Show file tree
Hide file tree
Showing 7 changed files with 382 additions and 356 deletions.
103 changes: 103 additions & 0 deletions src/platform_impl/ios/app_delegate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
use icrate::Foundation::{MainThreadMarker, NSObject, NSObjectProtocol};
use objc2::{declare_class, mutability, ClassType, DeclaredClass};

use super::app_state::{self, EventWrapper};
use super::uikit::{UIApplication, UIWindow};
use super::window::WinitUIWindow;
use crate::{
event::{Event, WindowEvent},
window::WindowId as RootWindowId,
};

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 {}

// UIApplicationDelegate protocol
unsafe impl AppDelegate {
#[method(application:didFinishLaunchingWithOptions:)]
fn did_finish_launching(&self, _application: &UIApplication, _: *mut NSObject) -> bool {
app_state::did_finish_launching(MainThreadMarker::new().unwrap());
true
}

#[method(applicationDidBecomeActive:)]
fn did_become_active(&self, _application: &UIApplication) {
let mtm = MainThreadMarker::new().unwrap();
app_state::handle_nonuser_event(mtm, EventWrapper::StaticEvent(Event::Resumed))
}

#[method(applicationWillResignActive:)]
fn will_resign_active(&self, _application: &UIApplication) {
let mtm = MainThreadMarker::new().unwrap();
app_state::handle_nonuser_event(mtm, EventWrapper::StaticEvent(Event::Suspended))
}

#[method(applicationWillEnterForeground:)]
fn will_enter_foreground(&self, application: &UIApplication) {
self.send_occluded_event_for_all_windows(application, false);
}

#[method(applicationDidEnterBackground:)]
fn did_enter_background(&self, application: &UIApplication) {
self.send_occluded_event_for_all_windows(application, true);
}

#[method(applicationWillTerminate:)]
fn will_terminate(&self, application: &UIApplication) {
let mut events = Vec::new();
for window in application.windows().iter() {
if window.is_kind_of::<WinitUIWindow>() {
// SAFETY: We just checked that the window is a `winit` window
let window = unsafe {
let ptr: *const UIWindow = window;
let ptr: *const WinitUIWindow = ptr.cast();
&*ptr
};
events.push(EventWrapper::StaticEvent(Event::WindowEvent {
window_id: RootWindowId(window.id()),
event: WindowEvent::Destroyed,
}));
}
}
let mtm = MainThreadMarker::new().unwrap();
app_state::handle_nonuser_events(mtm, events);
app_state::terminated(mtm);
}

#[method(applicationDidReceiveMemoryWarning:)]
fn did_receive_memory_warning(&self, _application: &UIApplication) {
let mtm = MainThreadMarker::new().unwrap();
app_state::handle_nonuser_event(mtm, EventWrapper::StaticEvent(Event::MemoryWarning))
}
}
);

impl AppDelegate {
fn send_occluded_event_for_all_windows(&self, application: &UIApplication, occluded: bool) {
let mut events = Vec::new();
for window in application.windows().iter() {
if window.is_kind_of::<WinitUIWindow>() {
// SAFETY: We just checked that the window is a `winit` window
let window = unsafe {
let ptr: *const UIWindow = window;
let ptr: *const WinitUIWindow = ptr.cast();
&*ptr
};
events.push(EventWrapper::StaticEvent(Event::WindowEvent {
window_id: RootWindowId(window.id()),
event: WindowEvent::Occluded(occluded),
}));
}
}
let mtm = MainThreadMarker::new().unwrap();
app_state::handle_nonuser_events(mtm, events);
}
}
2 changes: 1 addition & 1 deletion src/platform_impl/ios/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use objc2::{msg_send, sel};
use once_cell::sync::Lazy;

use super::uikit::UIView;
use super::view::WinitUIWindow;
use super::window::WinitUIWindow;
use crate::{
dpi::PhysicalSize,
event::{Event, InnerSizeWriter, StartCause, WindowEvent},
Expand Down
7 changes: 4 additions & 3 deletions src/platform_impl/ios/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ use crate::{
window::{CustomCursor, CustomCursorSource},
};

use super::{app_state, monitor, view, MonitorHandle};
use super::app_delegate::AppDelegate;
use super::{app_state, monitor, MonitorHandle};
use super::{
app_state::AppState,
uikit::{UIApplication, UIApplicationMain, UIDevice, UIScreen},
Expand Down Expand Up @@ -201,14 +202,14 @@ impl<T: 'static> EventLoop<T> {
app_state::will_launch(self.mtm, handler);

// Ensure application delegate is initialized
view::WinitApplicationDelegate::class();
let _ = AppDelegate::class();

unsafe {
UIApplicationMain(
0,
ptr::null(),
None,
Some(&NSString::from_str("WinitApplicationDelegate")),
Some(&NSString::from_str(AppDelegate::NAME)),
)
};
unreachable!()
Expand Down
2 changes: 2 additions & 0 deletions src/platform_impl/ios/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@
#![cfg(ios_platform)]
#![allow(clippy::let_unit_value)]

mod app_delegate;
mod app_state;
mod event_loop;
mod ffi;
mod monitor;
mod uikit;
mod view;
mod view_controller;
mod window;

use std::fmt;
Expand Down
Loading

0 comments on commit a127bd6

Please sign in to comment.