Skip to content

Commit

Permalink
Use objc2-ui-kit
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed May 21, 2024
1 parent 74c76d5 commit b612a2a
Show file tree
Hide file tree
Showing 25 changed files with 128 additions and 829 deletions.
23 changes: 23 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,29 @@ features = [
"NSSet",
]

[target.'cfg(target_os = "ios")'.dependencies.objc2-ui-kit]
version = "0.2.2"
features = [
"UIApplication",
"UIDevice",
"UIEvent",
"UIGeometry",
"UIGestureRecognizer",
"UIOrientation",
"UIPanGestureRecognizer",
"UIPinchGestureRecognizer",
"UIResponder",
"UIRotationGestureRecognizer",
"UIScreen",
"UIScreenMode",
"UITapGestureRecognizer",
"UITouch",
"UITraitCollection",
"UIView",
"UIViewController",
"UIWindow",
]

[target.'cfg(target_os = "windows")'.dependencies]
unicode-segmentation = "1.7.1"

Expand Down
4 changes: 3 additions & 1 deletion src/platform_impl/ios/app_delegate.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use objc2::{declare_class, mutability, ClassType, DeclaredClass};
use objc2_foundation::{MainThreadMarker, NSObject, NSObjectProtocol};
use objc2_ui_kit::{UIApplication, UIWindow};

use super::app_state::{self, EventWrapper};
use super::uikit::{UIApplication, UIWindow};
use super::window::WinitUIWindow;
use crate::event::{Event, WindowEvent};
use crate::window::WindowId as RootWindowId;
Expand Down Expand Up @@ -51,6 +51,7 @@ declare_class!(
#[method(applicationWillTerminate:)]
fn will_terminate(&self, application: &UIApplication) {
let mut events = Vec::new();
#[allow(deprecated)]
for window in application.windows().iter() {
if window.is_kind_of::<WinitUIWindow>() {
// SAFETY: We just checked that the window is a `winit` window
Expand Down Expand Up @@ -81,6 +82,7 @@ declare_class!(
impl AppDelegate {
fn send_occluded_event_for_all_windows(&self, application: &UIApplication, occluded: bool) {
let mut events = Vec::new();
#[allow(deprecated)]
for window in application.windows().iter() {
if window.is_kind_of::<WinitUIWindow>() {
// SAFETY: We just checked that the window is a `winit` window
Expand Down
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 @@ -20,8 +20,8 @@ use objc2_foundation::{
CGRect, CGSize, MainThreadMarker, NSInteger, NSObjectProtocol, NSOperatingSystemVersion,
NSProcessInfo,
};
use objc2_ui_kit::{UICoordinateSpace, UIView};

use super::uikit::UIView;
use super::window::WinitUIWindow;
use crate::dpi::PhysicalSize;
use crate::event::{Event, InnerSizeWriter, StartCause, WindowEvent};
Expand Down
30 changes: 22 additions & 8 deletions src/platform_impl/ios/event_loop.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::VecDeque;
use std::ffi::c_void;
use std::ffi::{c_char, c_int, c_void};
use std::marker::PhantomData;
use std::ptr;
use std::ptr::{self, NonNull};
use std::sync::mpsc::{self, Receiver, Sender};

use core_foundation::base::{CFIndex, CFRelease};
Expand All @@ -11,8 +11,10 @@ use core_foundation::runloop::{
CFRunLoopObserverCreate, CFRunLoopObserverRef, CFRunLoopSourceContext, CFRunLoopSourceCreate,
CFRunLoopSourceInvalidate, CFRunLoopSourceRef, CFRunLoopSourceSignal, CFRunLoopWakeUp,
};
use objc2::ClassType;
use objc2::rc::Id;
use objc2::{msg_send_id, ClassType};
use objc2_foundation::{MainThreadMarker, NSString};
use objc2_ui_kit::{UIApplication, UIApplicationMain, UIDevice, UIScreen, UIUserInterfaceIdiom};

use crate::application::ApplicationHandler;
use crate::error::EventLoopError;
Expand All @@ -26,7 +28,6 @@ use crate::window::{CustomCursor, CustomCursorSource};

use super::app_delegate::AppDelegate;
use super::app_state::AppState;
use super::uikit::{UIApplication, UIApplicationMain, UIDevice, UIScreen, UIUserInterfaceIdiom};
use super::{app_state, monitor, MonitorHandle};

#[derive(Debug)]
Expand All @@ -45,7 +46,8 @@ impl ActiveEventLoop {
}

pub fn primary_monitor(&self) -> Option<MonitorHandle> {
Some(MonitorHandle::new(UIScreen::main(self.mtm)))
#[allow(deprecated)]
Some(MonitorHandle::new(UIScreen::mainScreen(self.mtm)))
}

#[inline]
Expand Down Expand Up @@ -172,7 +174,8 @@ impl<T: 'static> EventLoop<T> {
}

pub fn run_app<A: ApplicationHandler<T>>(self, app: &mut A) -> ! {
let application = UIApplication::shared(self.mtm);
let application: Option<Id<UIApplication>> =
unsafe { msg_send_id![UIApplication::class(), sharedApplication] };
assert!(
application.is_none(),
"\
Expand All @@ -196,8 +199,19 @@ impl<T: 'static> EventLoop<T> {
// Ensure application delegate is initialized
let _ = AppDelegate::class();

extern "C" {
// These functions are in crt_externs.h.
fn _NSGetArgc() -> *mut c_int;
fn _NSGetArgv() -> *mut *mut *mut c_char;
}

unsafe {
UIApplicationMain(0, ptr::null(), None, Some(&NSString::from_str(AppDelegate::NAME)))
UIApplicationMain(
*_NSGetArgc(),
NonNull::new(*_NSGetArgv()).unwrap(),
None,
Some(&NSString::from_str(AppDelegate::NAME)),
)
};
unreachable!()
}
Expand All @@ -214,7 +228,7 @@ impl<T: 'static> EventLoop<T> {
// EventLoopExtIOS
impl<T: 'static> EventLoop<T> {
pub fn idiom(&self) -> Idiom {
match UIDevice::current(self.mtm).userInterfaceIdiom() {
match UIDevice::currentDevice(self.mtm).userInterfaceIdiom() {
UIUserInterfaceIdiom::Unspecified => Idiom::Unspecified,
UIUserInterfaceIdiom::Phone => Idiom::Phone,
UIUserInterfaceIdiom::Pad => Idiom::Pad,
Expand Down
1 change: 0 additions & 1 deletion src/platform_impl/ios/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ mod app_delegate;
mod app_state;
mod event_loop;
mod monitor;
mod uikit;
mod view;
mod view_controller;
mod window;
Expand Down
9 changes: 6 additions & 3 deletions src/platform_impl/ios/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use objc2::mutability::IsRetainable;
use objc2::rc::Id;
use objc2::Message;
use objc2_foundation::{run_on_main, MainThreadBound, MainThreadMarker, NSInteger};
use objc2_ui_kit::{UIScreen, UIScreenMode};

use super::uikit::{UIScreen, UIScreenMode};
use crate::dpi::{PhysicalPosition, PhysicalSize};
use crate::monitor::VideoModeHandle as RootVideoModeHandle;
use crate::platform_impl::platform::app_state;
Expand Down Expand Up @@ -148,12 +148,14 @@ impl MonitorHandle {

pub fn name(&self) -> Option<String> {
run_on_main(|mtm| {
let main = UIScreen::main(mtm);
#[allow(deprecated)]
let main = UIScreen::mainScreen(mtm);
if *self.ui_screen(mtm) == main {
Some("Primary".to_string())
} else if *self.ui_screen(mtm) == main.mirroredScreen() {
} else if Some(self.ui_screen(mtm)) == main.mirroredScreen().as_ref() {
Some("Mirrored".to_string())
} else {
#[allow(deprecated)]
UIScreen::screens(mtm)
.iter()
.position(|rhs| rhs == &**self.ui_screen(mtm))
Expand Down Expand Up @@ -237,5 +239,6 @@ fn refresh_rate_millihertz(uiscreen: &UIScreen) -> u32 {
}

pub fn uiscreens(mtm: MainThreadMarker) -> VecDeque<MonitorHandle> {
#[allow(deprecated)]
UIScreen::screens(mtm).into_iter().map(MonitorHandle::new).collect()
}
31 changes: 0 additions & 31 deletions src/platform_impl/ios/uikit/application.rs

This file was deleted.

12 changes: 0 additions & 12 deletions src/platform_impl/ios/uikit/coordinate_space.rs

This file was deleted.

41 changes: 0 additions & 41 deletions src/platform_impl/ios/uikit/device.rs

This file was deleted.

12 changes: 0 additions & 12 deletions src/platform_impl/ios/uikit/event.rs

This file was deleted.

14 changes: 0 additions & 14 deletions src/platform_impl/ios/uikit/geometry.rs

This file was deleted.

Loading

0 comments on commit b612a2a

Please sign in to comment.