From b612a2a9a88b41de4492f3fdab02c3061c56c11d Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Tue, 21 May 2024 22:51:55 +0200 Subject: [PATCH] Use objc2-ui-kit --- Cargo.toml | 23 +++ src/platform_impl/ios/app_delegate.rs | 4 +- src/platform_impl/ios/app_state.rs | 2 +- src/platform_impl/ios/event_loop.rs | 30 ++- src/platform_impl/ios/mod.rs | 1 - src/platform_impl/ios/monitor.rs | 9 +- src/platform_impl/ios/uikit/application.rs | 31 --- .../ios/uikit/coordinate_space.rs | 12 -- src/platform_impl/ios/uikit/device.rs | 41 ---- src/platform_impl/ios/uikit/event.rs | 12 -- src/platform_impl/ios/uikit/geometry.rs | 14 -- .../ios/uikit/gesture_recognizer.rs | 176 ------------------ src/platform_impl/ios/uikit/mod.rs | 53 ------ src/platform_impl/ios/uikit/responder.rs | 12 -- src/platform_impl/ios/uikit/screen.rs | 80 -------- src/platform_impl/ios/uikit/screen_mode.rs | 19 -- .../ios/uikit/status_bar_style.rs | 16 -- src/platform_impl/ios/uikit/touch.rs | 65 ------- .../ios/uikit/trait_collection.rs | 33 ---- src/platform_impl/ios/uikit/view.rs | 93 --------- .../ios/uikit/view_controller.rs | 57 ------ src/platform_impl/ios/uikit/window.rs | 36 ---- src/platform_impl/ios/view.rs | 98 +++++----- src/platform_impl/ios/view_controller.rs | 17 +- src/platform_impl/ios/window.rs | 23 +-- 25 files changed, 128 insertions(+), 829 deletions(-) delete mode 100644 src/platform_impl/ios/uikit/application.rs delete mode 100644 src/platform_impl/ios/uikit/coordinate_space.rs delete mode 100644 src/platform_impl/ios/uikit/device.rs delete mode 100644 src/platform_impl/ios/uikit/event.rs delete mode 100644 src/platform_impl/ios/uikit/geometry.rs delete mode 100644 src/platform_impl/ios/uikit/gesture_recognizer.rs delete mode 100644 src/platform_impl/ios/uikit/mod.rs delete mode 100644 src/platform_impl/ios/uikit/responder.rs delete mode 100644 src/platform_impl/ios/uikit/screen.rs delete mode 100644 src/platform_impl/ios/uikit/screen_mode.rs delete mode 100644 src/platform_impl/ios/uikit/status_bar_style.rs delete mode 100644 src/platform_impl/ios/uikit/touch.rs delete mode 100644 src/platform_impl/ios/uikit/trait_collection.rs delete mode 100644 src/platform_impl/ios/uikit/view.rs delete mode 100644 src/platform_impl/ios/uikit/view_controller.rs delete mode 100644 src/platform_impl/ios/uikit/window.rs diff --git a/Cargo.toml b/Cargo.toml index 86a4eaac13..fa6d5b528a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/platform_impl/ios/app_delegate.rs b/src/platform_impl/ios/app_delegate.rs index 99ddf1e384..0be14d1b66 100644 --- a/src/platform_impl/ios/app_delegate.rs +++ b/src/platform_impl/ios/app_delegate.rs @@ -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; @@ -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::() { // SAFETY: We just checked that the window is a `winit` window @@ -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::() { // SAFETY: We just checked that the window is a `winit` window diff --git a/src/platform_impl/ios/app_state.rs b/src/platform_impl/ios/app_state.rs index d9c54c0ebb..0b7ff7564b 100644 --- a/src/platform_impl/ios/app_state.rs +++ b/src/platform_impl/ios/app_state.rs @@ -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}; diff --git a/src/platform_impl/ios/event_loop.rs b/src/platform_impl/ios/event_loop.rs index 665cb0a698..02fe101834 100644 --- a/src/platform_impl/ios/event_loop.rs +++ b/src/platform_impl/ios/event_loop.rs @@ -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}; @@ -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; @@ -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)] @@ -45,7 +46,8 @@ impl ActiveEventLoop { } pub fn primary_monitor(&self) -> Option { - Some(MonitorHandle::new(UIScreen::main(self.mtm))) + #[allow(deprecated)] + Some(MonitorHandle::new(UIScreen::mainScreen(self.mtm))) } #[inline] @@ -172,7 +174,8 @@ impl EventLoop { } pub fn run_app>(self, app: &mut A) -> ! { - let application = UIApplication::shared(self.mtm); + let application: Option> = + unsafe { msg_send_id![UIApplication::class(), sharedApplication] }; assert!( application.is_none(), "\ @@ -196,8 +199,19 @@ impl EventLoop { // 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!() } @@ -214,7 +228,7 @@ impl EventLoop { // EventLoopExtIOS impl EventLoop { 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, diff --git a/src/platform_impl/ios/mod.rs b/src/platform_impl/ios/mod.rs index c90a21c5fd..bf2c011f25 100644 --- a/src/platform_impl/ios/mod.rs +++ b/src/platform_impl/ios/mod.rs @@ -5,7 +5,6 @@ mod app_delegate; mod app_state; mod event_loop; mod monitor; -mod uikit; mod view; mod view_controller; mod window; diff --git a/src/platform_impl/ios/monitor.rs b/src/platform_impl/ios/monitor.rs index d85fe20e63..477d1af9f5 100644 --- a/src/platform_impl/ios/monitor.rs +++ b/src/platform_impl/ios/monitor.rs @@ -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; @@ -148,12 +148,14 @@ impl MonitorHandle { pub fn name(&self) -> Option { 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)) @@ -237,5 +239,6 @@ fn refresh_rate_millihertz(uiscreen: &UIScreen) -> u32 { } pub fn uiscreens(mtm: MainThreadMarker) -> VecDeque { + #[allow(deprecated)] UIScreen::screens(mtm).into_iter().map(MonitorHandle::new).collect() } diff --git a/src/platform_impl/ios/uikit/application.rs b/src/platform_impl/ios/uikit/application.rs deleted file mode 100644 index ce9685cabe..0000000000 --- a/src/platform_impl/ios/uikit/application.rs +++ /dev/null @@ -1,31 +0,0 @@ -use objc2::rc::Id; -use objc2::{extern_class, extern_methods, msg_send_id, mutability, ClassType}; -use objc2_foundation::{CGRect, MainThreadMarker, NSArray, NSObject}; - -use super::{UIResponder, UIWindow}; - -extern_class!( - #[derive(Debug, PartialEq, Eq, Hash)] - pub(crate) struct UIApplication; - - unsafe impl ClassType for UIApplication { - #[inherits(NSObject)] - type Super = UIResponder; - type Mutability = mutability::InteriorMutable; - } -); - -extern_methods!( - unsafe impl UIApplication { - pub fn shared(_mtm: MainThreadMarker) -> Option> { - unsafe { msg_send_id![Self::class(), sharedApplication] } - } - - pub fn windows(&self) -> Id> { - unsafe { msg_send_id![self, windows] } - } - - #[method(statusBarFrame)] - pub fn statusBarFrame(&self) -> CGRect; - } -); diff --git a/src/platform_impl/ios/uikit/coordinate_space.rs b/src/platform_impl/ios/uikit/coordinate_space.rs deleted file mode 100644 index 3c3b9303d1..0000000000 --- a/src/platform_impl/ios/uikit/coordinate_space.rs +++ /dev/null @@ -1,12 +0,0 @@ -use objc2::{extern_class, mutability, ClassType}; -use objc2_foundation::NSObject; - -extern_class!( - #[derive(Debug, PartialEq, Eq, Hash)] - pub(crate) struct UICoordinateSpace; - - unsafe impl ClassType for UICoordinateSpace { - type Super = NSObject; - type Mutability = mutability::InteriorMutable; - } -); diff --git a/src/platform_impl/ios/uikit/device.rs b/src/platform_impl/ios/uikit/device.rs deleted file mode 100644 index 24c57a948c..0000000000 --- a/src/platform_impl/ios/uikit/device.rs +++ /dev/null @@ -1,41 +0,0 @@ -use objc2::encode::{Encode, Encoding}; -use objc2::rc::Id; -use objc2::{extern_class, extern_methods, msg_send_id, mutability, ClassType}; -use objc2_foundation::{MainThreadMarker, NSInteger, NSObject}; - -extern_class!( - #[derive(Debug, PartialEq, Eq, Hash)] - pub(crate) struct UIDevice; - - unsafe impl ClassType for UIDevice { - type Super = NSObject; - type Mutability = mutability::InteriorMutable; - } -); - -extern_methods!( - unsafe impl UIDevice { - pub fn current(_mtm: MainThreadMarker) -> Id { - unsafe { msg_send_id![Self::class(), currentDevice] } - } - - #[method(userInterfaceIdiom)] - pub fn userInterfaceIdiom(&self) -> UIUserInterfaceIdiom; - } -); - -#[repr(transparent)] -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct UIUserInterfaceIdiom(NSInteger); - -unsafe impl Encode for UIUserInterfaceIdiom { - const ENCODING: Encoding = NSInteger::ENCODING; -} - -impl UIUserInterfaceIdiom { - pub const Unspecified: UIUserInterfaceIdiom = UIUserInterfaceIdiom(-1); - pub const Phone: UIUserInterfaceIdiom = UIUserInterfaceIdiom(0); - pub const Pad: UIUserInterfaceIdiom = UIUserInterfaceIdiom(1); - pub const TV: UIUserInterfaceIdiom = UIUserInterfaceIdiom(2); - pub const CarPlay: UIUserInterfaceIdiom = UIUserInterfaceIdiom(3); -} diff --git a/src/platform_impl/ios/uikit/event.rs b/src/platform_impl/ios/uikit/event.rs deleted file mode 100644 index d72159e981..0000000000 --- a/src/platform_impl/ios/uikit/event.rs +++ /dev/null @@ -1,12 +0,0 @@ -use objc2::{extern_class, mutability, ClassType}; -use objc2_foundation::NSObject; - -extern_class!( - #[derive(Debug, PartialEq, Eq, Hash)] - pub(crate) struct UIEvent; - - unsafe impl ClassType for UIEvent { - type Super = NSObject; - type Mutability = mutability::InteriorMutable; - } -); diff --git a/src/platform_impl/ios/uikit/geometry.rs b/src/platform_impl/ios/uikit/geometry.rs deleted file mode 100644 index 022160b640..0000000000 --- a/src/platform_impl/ios/uikit/geometry.rs +++ /dev/null @@ -1,14 +0,0 @@ -use objc2::encode::{Encode, Encoding}; -use objc2_foundation::NSUInteger; - -#[repr(transparent)] -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct UIRectEdge(pub NSUInteger); - -impl UIRectEdge { - pub const NONE: Self = Self(0); -} - -unsafe impl Encode for UIRectEdge { - const ENCODING: Encoding = NSUInteger::ENCODING; -} diff --git a/src/platform_impl/ios/uikit/gesture_recognizer.rs b/src/platform_impl/ios/uikit/gesture_recognizer.rs deleted file mode 100644 index ab5e7a329d..0000000000 --- a/src/platform_impl/ios/uikit/gesture_recognizer.rs +++ /dev/null @@ -1,176 +0,0 @@ -use objc2::encode::{Encode, Encoding}; -use objc2::rc::Id; -use objc2::runtime::ProtocolObject; -use objc2::{extern_class, extern_methods, extern_protocol, mutability, ClassType, ProtocolType}; -use objc2_foundation::{CGFloat, CGPoint, NSInteger, NSObject, NSObjectProtocol, NSUInteger}; - -use super::UIView; - -extern_class!( - /// [`UIGestureRecognizer`](https://developer.apple.com/documentation/uikit/uigesturerecognizer) - #[derive(Debug, PartialEq, Eq, Hash)] - pub(crate) struct UIGestureRecognizer; - - unsafe impl ClassType for UIGestureRecognizer { - type Super = NSObject; - type Mutability = mutability::InteriorMutable; - } -); - -extern_methods!( - unsafe impl UIGestureRecognizer { - #[method(state)] - pub fn state(&self) -> UIGestureRecognizerState; - - /// [`delegate`](https://developer.apple.com/documentation/uikit/uigesturerecognizer/1624207-delegate?language=objc) - /// @property(nullable, nonatomic, weak) id delegate; - #[method(setDelegate:)] - pub fn setDelegate(&self, delegate: &ProtocolObject); - - #[method_id(delegate)] - pub fn delegate(&self) -> Id>; - } -); - -unsafe impl Encode for UIGestureRecognizer { - const ENCODING: Encoding = Encoding::Object; -} - -// [`UIGestureRecognizerState`](https://developer.apple.com/documentation/uikit/uigesturerecognizer/state) -#[repr(transparent)] -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct UIGestureRecognizerState(NSInteger); - -unsafe impl Encode for UIGestureRecognizerState { - const ENCODING: Encoding = NSInteger::ENCODING; -} - -#[allow(dead_code)] -impl UIGestureRecognizerState { - pub const Possible: Self = Self(0); - pub const Began: Self = Self(1); - pub const Changed: Self = Self(2); - pub const Ended: Self = Self(3); - pub const Cancelled: Self = Self(4); - pub const Failed: Self = Self(5); -} - -// [`UIPinchGestureRecognizer`](https://developer.apple.com/documentation/uikit/uipinchgesturerecognizer) -extern_class!( - #[derive(Debug, PartialEq, Eq, Hash)] - pub(crate) struct UIPinchGestureRecognizer; - - unsafe impl ClassType for UIPinchGestureRecognizer { - type Super = UIGestureRecognizer; - type Mutability = mutability::InteriorMutable; - } -); - -extern_methods!( - unsafe impl UIPinchGestureRecognizer { - #[method(scale)] - pub fn scale(&self) -> CGFloat; - - #[method(velocity)] - pub fn velocity(&self) -> CGFloat; - } -); - -unsafe impl Encode for UIPinchGestureRecognizer { - const ENCODING: Encoding = Encoding::Object; -} - -extern_class!( - /// [`UIRotationGestureRecognizer`](https://developer.apple.com/documentation/uikit/uirotationgesturerecognizer) - #[derive(Debug, PartialEq, Eq, Hash)] - pub(crate) struct UIRotationGestureRecognizer; - - unsafe impl ClassType for UIRotationGestureRecognizer { - type Super = UIGestureRecognizer; - type Mutability = mutability::InteriorMutable; - } -); - -extern_methods!( - unsafe impl UIRotationGestureRecognizer { - #[method(rotation)] - pub fn rotation(&self) -> CGFloat; - - #[method(velocity)] - pub fn velocity(&self) -> CGFloat; - } -); - -unsafe impl Encode for UIRotationGestureRecognizer { - const ENCODING: Encoding = Encoding::Object; -} - -extern_class!( - /// [`UITapGestureRecognizer`](https://developer.apple.com/documentation/uikit/uitapgesturerecognizer) - #[derive(Debug, PartialEq, Eq, Hash)] - pub(crate) struct UITapGestureRecognizer; - - unsafe impl ClassType for UITapGestureRecognizer { - type Super = UIGestureRecognizer; - type Mutability = mutability::InteriorMutable; - } -); - -extern_methods!( - unsafe impl UITapGestureRecognizer { - #[method(setNumberOfTapsRequired:)] - pub fn setNumberOfTapsRequired(&self, number_of_taps_required: NSUInteger); - - #[method(setNumberOfTouchesRequired:)] - pub fn setNumberOfTouchesRequired(&self, number_of_touches_required: NSUInteger); - } -); - -unsafe impl Encode for UITapGestureRecognizer { - const ENCODING: Encoding = Encoding::Object; -} - -extern_class!( - /// [`UIPanGestureRecognizer`](https://developer.apple.com/documentation/uikit/uipangesturerecognizer) - #[derive(Debug, PartialEq, Eq, Hash)] - pub(crate) struct UIPanGestureRecognizer; - - unsafe impl ClassType for UIPanGestureRecognizer { - type Super = UIGestureRecognizer; - type Mutability = mutability::InteriorMutable; - } -); - -extern_methods!( - unsafe impl UIPanGestureRecognizer { - #[method(translationInView:)] - pub fn translationInView(&self, view: &UIView) -> CGPoint; - - #[method(setTranslation:inView:)] - pub fn setTranslationInView(&self, translation: CGPoint, view: &UIView); - - #[method(velocityInView:)] - pub fn velocityInView(&self, view: &UIView) -> CGPoint; - - #[method(setMinimumNumberOfTouches:)] - pub fn setMinimumNumberOfTouches(&self, minimum_number_of_touches: NSUInteger); - - #[method(minimumNumberOfTouches)] - pub fn minimumNumberOfTouches(&self) -> NSUInteger; - - #[method(setMaximumNumberOfTouches:)] - pub fn setMaximumNumberOfTouches(&self, maximum_number_of_touches: NSUInteger); - - #[method(maximumNumberOfTouches)] - pub fn maximumNumberOfTouches(&self) -> NSUInteger; - } -); - -extern_protocol!( - /// (@protocol UIGestureRecognizerDelegate)[https://developer.apple.com/documentation/uikit/uigesturerecognizerdelegate?language=objc] - pub(crate) unsafe trait UIGestureRecognizerDelegate: NSObjectProtocol {} - - unsafe impl ProtocolType for dyn UIGestureRecognizerDelegate { - const NAME: &'static str = "UIGestureRecognizerDelegate"; - } -); diff --git a/src/platform_impl/ios/uikit/mod.rs b/src/platform_impl/ios/uikit/mod.rs deleted file mode 100644 index c415cac018..0000000000 --- a/src/platform_impl/ios/uikit/mod.rs +++ /dev/null @@ -1,53 +0,0 @@ -#![allow(non_snake_case)] -#![allow(non_upper_case_globals)] - -use std::os::raw::{c_char, c_int}; - -use objc2_foundation::NSString; - -mod application; -mod coordinate_space; -mod device; -mod event; -mod geometry; -mod gesture_recognizer; -mod responder; -mod screen; -mod screen_mode; -mod status_bar_style; -mod touch; -mod trait_collection; -mod view; -mod view_controller; -mod window; - -pub(crate) use self::application::UIApplication; -pub(crate) use self::coordinate_space::UICoordinateSpace; -pub(crate) use self::device::{UIDevice, UIUserInterfaceIdiom}; -pub(crate) use self::event::UIEvent; -pub(crate) use self::geometry::UIRectEdge; -pub(crate) use self::gesture_recognizer::{ - UIGestureRecognizer, UIGestureRecognizerDelegate, UIGestureRecognizerState, - UIPanGestureRecognizer, UIPinchGestureRecognizer, UIRotationGestureRecognizer, - UITapGestureRecognizer, -}; -pub(crate) use self::responder::UIResponder; -pub(crate) use self::screen::{UIScreen, UIScreenOverscanCompensation}; -pub(crate) use self::screen_mode::UIScreenMode; -pub(crate) use self::status_bar_style::UIStatusBarStyle; -pub(crate) use self::touch::{UITouch, UITouchPhase, UITouchType}; -pub(crate) use self::trait_collection::{UIForceTouchCapability, UITraitCollection}; -#[allow(unused_imports)] -pub(crate) use self::view::{UIEdgeInsets, UIView}; -pub(crate) use self::view_controller::{UIInterfaceOrientationMask, UIViewController}; -pub(crate) use self::window::UIWindow; - -#[link(name = "UIKit", kind = "framework")] -extern "C" { - pub fn UIApplicationMain( - argc: c_int, - argv: *const c_char, - principalClassName: Option<&NSString>, - delegateClassName: Option<&NSString>, - ) -> c_int; -} diff --git a/src/platform_impl/ios/uikit/responder.rs b/src/platform_impl/ios/uikit/responder.rs deleted file mode 100644 index 108113624d..0000000000 --- a/src/platform_impl/ios/uikit/responder.rs +++ /dev/null @@ -1,12 +0,0 @@ -use objc2::{extern_class, mutability, ClassType}; -use objc2_foundation::NSObject; - -extern_class!( - #[derive(Debug, PartialEq, Eq, Hash)] - pub(crate) struct UIResponder; - - unsafe impl ClassType for UIResponder { - type Super = NSObject; - type Mutability = mutability::InteriorMutable; - } -); diff --git a/src/platform_impl/ios/uikit/screen.rs b/src/platform_impl/ios/uikit/screen.rs deleted file mode 100644 index 578fff5d59..0000000000 --- a/src/platform_impl/ios/uikit/screen.rs +++ /dev/null @@ -1,80 +0,0 @@ -use objc2::encode::{Encode, Encoding}; -use objc2::rc::Id; -use objc2::{extern_class, extern_methods, msg_send_id, mutability, ClassType}; -use objc2_foundation::{CGFloat, CGRect, MainThreadMarker, NSArray, NSInteger, NSObject}; - -use super::{UICoordinateSpace, UIScreenMode}; - -extern_class!( - #[derive(Debug, PartialEq, Eq, Hash)] - pub(crate) struct UIScreen; - - unsafe impl ClassType for UIScreen { - type Super = NSObject; - type Mutability = mutability::InteriorMutable; - } -); - -extern_methods!( - unsafe impl UIScreen { - pub fn main(_mtm: MainThreadMarker) -> Id { - unsafe { msg_send_id![Self::class(), mainScreen] } - } - - pub fn screens(_mtm: MainThreadMarker) -> Id> { - unsafe { msg_send_id![Self::class(), screens] } - } - - #[method(bounds)] - pub fn bounds(&self) -> CGRect; - - #[method(scale)] - pub fn scale(&self) -> CGFloat; - - #[method(nativeBounds)] - pub fn nativeBounds(&self) -> CGRect; - - #[method(nativeScale)] - pub fn nativeScale(&self) -> CGFloat; - - #[method(maximumFramesPerSecond)] - pub fn maximumFramesPerSecond(&self) -> NSInteger; - - pub fn mirroredScreen(&self) -> Id { - unsafe { msg_send_id![Self::class(), mirroredScreen] } - } - - pub fn preferredMode(&self) -> Option> { - unsafe { msg_send_id![self, preferredMode] } - } - - #[method(setCurrentMode:)] - pub fn setCurrentMode(&self, mode: Option<&UIScreenMode>); - - pub fn availableModes(&self) -> Id> { - unsafe { msg_send_id![self, availableModes] } - } - - #[method(setOverscanCompensation:)] - pub fn setOverscanCompensation(&self, overscanCompensation: UIScreenOverscanCompensation); - - pub fn coordinateSpace(&self) -> Id { - unsafe { msg_send_id![self, coordinateSpace] } - } - } -); - -#[repr(transparent)] -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct UIScreenOverscanCompensation(NSInteger); - -unsafe impl Encode for UIScreenOverscanCompensation { - const ENCODING: Encoding = NSInteger::ENCODING; -} - -#[allow(dead_code)] -impl UIScreenOverscanCompensation { - pub const Scale: Self = Self(0); - pub const InsetBounds: Self = Self(1); - pub const None: Self = Self(2); -} diff --git a/src/platform_impl/ios/uikit/screen_mode.rs b/src/platform_impl/ios/uikit/screen_mode.rs deleted file mode 100644 index 1d86c96f00..0000000000 --- a/src/platform_impl/ios/uikit/screen_mode.rs +++ /dev/null @@ -1,19 +0,0 @@ -use objc2::{extern_class, extern_methods, mutability, ClassType}; -use objc2_foundation::{CGSize, NSObject}; - -extern_class!( - #[derive(Debug, PartialEq, Eq, Hash)] - pub(crate) struct UIScreenMode; - - unsafe impl ClassType for UIScreenMode { - type Super = NSObject; - type Mutability = mutability::InteriorMutable; - } -); - -extern_methods!( - unsafe impl UIScreenMode { - #[method(size)] - pub fn size(&self) -> CGSize; - } -); diff --git a/src/platform_impl/ios/uikit/status_bar_style.rs b/src/platform_impl/ios/uikit/status_bar_style.rs deleted file mode 100644 index 7b0685a321..0000000000 --- a/src/platform_impl/ios/uikit/status_bar_style.rs +++ /dev/null @@ -1,16 +0,0 @@ -use objc2::encode::{Encode, Encoding}; -use objc2_foundation::NSInteger; - -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] -#[allow(dead_code)] -#[repr(isize)] -pub enum UIStatusBarStyle { - #[default] - Default = 0, - LightContent = 1, - DarkContent = 3, -} - -unsafe impl Encode for UIStatusBarStyle { - const ENCODING: Encoding = NSInteger::ENCODING; -} diff --git a/src/platform_impl/ios/uikit/touch.rs b/src/platform_impl/ios/uikit/touch.rs deleted file mode 100644 index 6db47642b0..0000000000 --- a/src/platform_impl/ios/uikit/touch.rs +++ /dev/null @@ -1,65 +0,0 @@ -use objc2::encode::{Encode, Encoding}; -use objc2::{extern_class, extern_methods, mutability, ClassType}; -use objc2_foundation::{CGFloat, CGPoint, NSInteger, NSObject}; - -use super::UIView; - -extern_class!( - #[derive(Debug, PartialEq, Eq, Hash)] - pub(crate) struct UITouch; - - unsafe impl ClassType for UITouch { - type Super = NSObject; - type Mutability = mutability::InteriorMutable; - } -); - -extern_methods!( - unsafe impl UITouch { - #[method(locationInView:)] - pub fn locationInView(&self, view: Option<&UIView>) -> CGPoint; - - #[method(type)] - pub fn type_(&self) -> UITouchType; - - #[method(force)] - pub fn force(&self) -> CGFloat; - - #[method(maximumPossibleForce)] - pub fn maximumPossibleForce(&self) -> CGFloat; - - #[method(altitudeAngle)] - pub fn altitudeAngle(&self) -> CGFloat; - - #[method(phase)] - pub fn phase(&self) -> UITouchPhase; - } -); - -#[derive(Debug, PartialEq, Eq)] -#[allow(dead_code)] -#[repr(isize)] -pub enum UITouchType { - Direct = 0, - Indirect, - Pencil, -} - -unsafe impl Encode for UITouchType { - const ENCODING: Encoding = NSInteger::ENCODING; -} - -#[derive(Debug)] -#[allow(dead_code)] -#[repr(isize)] -pub enum UITouchPhase { - Began = 0, - Moved, - Stationary, - Ended, - Cancelled, -} - -unsafe impl Encode for UITouchPhase { - const ENCODING: Encoding = NSInteger::ENCODING; -} diff --git a/src/platform_impl/ios/uikit/trait_collection.rs b/src/platform_impl/ios/uikit/trait_collection.rs deleted file mode 100644 index 13c6834f2c..0000000000 --- a/src/platform_impl/ios/uikit/trait_collection.rs +++ /dev/null @@ -1,33 +0,0 @@ -use objc2::encode::{Encode, Encoding}; -use objc2::{extern_class, extern_methods, mutability, ClassType}; -use objc2_foundation::{NSInteger, NSObject}; - -extern_class!( - #[derive(Debug, PartialEq, Eq, Hash)] - pub(crate) struct UITraitCollection; - - unsafe impl ClassType for UITraitCollection { - type Super = NSObject; - type Mutability = mutability::InteriorMutable; - } -); - -extern_methods!( - unsafe impl UITraitCollection { - #[method(forceTouchCapability)] - pub fn forceTouchCapability(&self) -> UIForceTouchCapability; - } -); - -#[derive(Debug, PartialEq, Eq)] -#[allow(dead_code)] -#[repr(isize)] -pub enum UIForceTouchCapability { - Unknown = 0, - Unavailable, - Available, -} - -unsafe impl Encode for UIForceTouchCapability { - const ENCODING: Encoding = NSInteger::ENCODING; -} diff --git a/src/platform_impl/ios/uikit/view.rs b/src/platform_impl/ios/uikit/view.rs deleted file mode 100644 index abd9bc5341..0000000000 --- a/src/platform_impl/ios/uikit/view.rs +++ /dev/null @@ -1,93 +0,0 @@ -use objc2::encode::{Encode, Encoding}; -use objc2::rc::Id; -use objc2::{extern_class, extern_methods, msg_send_id, mutability, ClassType}; -use objc2_foundation::{CGFloat, CGRect, NSObject}; - -use super::{UICoordinateSpace, UIGestureRecognizer, UIResponder, UIViewController}; - -extern_class!( - #[derive(Debug, PartialEq, Eq, Hash)] - pub(crate) struct UIView; - - unsafe impl ClassType for UIView { - #[inherits(NSObject)] - type Super = UIResponder; - type Mutability = mutability::InteriorMutable; - } -); - -extern_methods!( - unsafe impl UIView { - #[method(bounds)] - pub fn bounds(&self) -> CGRect; - - #[method(setBounds:)] - pub fn setBounds(&self, value: CGRect); - - #[method(frame)] - pub fn frame(&self) -> CGRect; - - #[method(setFrame:)] - pub fn setFrame(&self, value: CGRect); - - #[method(contentScaleFactor)] - pub fn contentScaleFactor(&self) -> CGFloat; - - #[method(setContentScaleFactor:)] - pub fn setContentScaleFactor(&self, val: CGFloat); - - #[method(setMultipleTouchEnabled:)] - pub fn setMultipleTouchEnabled(&self, val: bool); - - pub fn rootViewController(&self) -> Option> { - unsafe { msg_send_id![self, rootViewController] } - } - - #[method(setRootViewController:)] - pub fn setRootViewController(&self, rootViewController: Option<&UIViewController>); - - #[method(convertRect:toCoordinateSpace:)] - pub fn convertRect_toCoordinateSpace( - &self, - rect: CGRect, - coordinateSpace: &UICoordinateSpace, - ) -> CGRect; - - #[method(convertRect:fromCoordinateSpace:)] - pub fn convertRect_fromCoordinateSpace( - &self, - rect: CGRect, - coordinateSpace: &UICoordinateSpace, - ) -> CGRect; - - #[method(safeAreaInsets)] - pub fn safeAreaInsets(&self) -> UIEdgeInsets; - - #[method(setNeedsDisplay)] - pub fn setNeedsDisplay(&self); - - #[method(addGestureRecognizer:)] - pub fn addGestureRecognizer(&self, gestureRecognizer: &UIGestureRecognizer); - - #[method(removeGestureRecognizer:)] - pub fn removeGestureRecognizer(&self, gestureRecognizer: &UIGestureRecognizer); - } -); - -#[repr(C)] -#[derive(Debug, Clone)] -pub struct UIEdgeInsets { - pub top: CGFloat, - pub left: CGFloat, - pub bottom: CGFloat, - pub right: CGFloat, -} - -unsafe impl Encode for UIEdgeInsets { - const ENCODING: Encoding = Encoding::Struct("UIEdgeInsets", &[ - CGFloat::ENCODING, - CGFloat::ENCODING, - CGFloat::ENCODING, - CGFloat::ENCODING, - ]); -} diff --git a/src/platform_impl/ios/uikit/view_controller.rs b/src/platform_impl/ios/uikit/view_controller.rs deleted file mode 100644 index 80d9e8960d..0000000000 --- a/src/platform_impl/ios/uikit/view_controller.rs +++ /dev/null @@ -1,57 +0,0 @@ -use objc2::encode::{Encode, Encoding}; -use objc2::rc::Id; -use objc2::{extern_class, extern_methods, msg_send_id, mutability, ClassType}; -use objc2_foundation::{NSObject, NSUInteger}; - -use super::{UIResponder, UIView}; - -extern_class!( - #[derive(Debug, PartialEq, Eq, Hash)] - pub(crate) struct UIViewController; - - unsafe impl ClassType for UIViewController { - #[inherits(NSObject)] - type Super = UIResponder; - type Mutability = mutability::InteriorMutable; - } -); - -extern_methods!( - unsafe impl UIViewController { - #[method(attemptRotationToDeviceOrientation)] - pub fn attemptRotationToDeviceOrientation(); - - #[method(setNeedsStatusBarAppearanceUpdate)] - pub fn setNeedsStatusBarAppearanceUpdate(&self); - - #[method(setNeedsUpdateOfHomeIndicatorAutoHidden)] - pub fn setNeedsUpdateOfHomeIndicatorAutoHidden(&self); - - #[method(setNeedsUpdateOfScreenEdgesDeferringSystemGestures)] - pub fn setNeedsUpdateOfScreenEdgesDeferringSystemGestures(&self); - - pub fn view(&self) -> Option> { - unsafe { msg_send_id![self, view] } - } - - #[method(setView:)] - pub fn setView(&self, view: Option<&UIView>); - } -); - -bitflags::bitflags! { - #[derive(Clone, Copy)] - pub struct UIInterfaceOrientationMask: NSUInteger { - const Portrait = 1 << 1; - const PortraitUpsideDown = 1 << 2; - const LandscapeRight = 1 << 3; - const LandscapeLeft = 1 << 4; - const Landscape = Self::LandscapeLeft.bits() | Self::LandscapeRight.bits(); - const AllButUpsideDown = Self::Landscape.bits() | Self::Portrait.bits(); - const All = Self::AllButUpsideDown.bits() | Self::PortraitUpsideDown.bits(); - } -} - -unsafe impl Encode for UIInterfaceOrientationMask { - const ENCODING: Encoding = NSUInteger::ENCODING; -} diff --git a/src/platform_impl/ios/uikit/window.rs b/src/platform_impl/ios/uikit/window.rs deleted file mode 100644 index 28cbef87c1..0000000000 --- a/src/platform_impl/ios/uikit/window.rs +++ /dev/null @@ -1,36 +0,0 @@ -use objc2::rc::Id; -use objc2::{extern_class, extern_methods, msg_send_id, mutability, ClassType}; -use objc2_foundation::NSObject; - -use super::{UIResponder, UIScreen, UIView}; - -extern_class!( - #[derive(Debug, PartialEq, Eq, Hash)] - pub(crate) struct UIWindow; - - unsafe impl ClassType for UIWindow { - #[inherits(UIResponder, NSObject)] - type Super = UIView; - type Mutability = mutability::InteriorMutable; - } -); - -extern_methods!( - unsafe impl UIWindow { - pub fn screen(&self) -> Id { - unsafe { msg_send_id![self, screen] } - } - - #[method(setScreen:)] - pub fn setScreen(&self, screen: &UIScreen); - - #[method(setHidden:)] - pub fn setHidden(&self, flag: bool); - - #[method(makeKeyAndVisible)] - pub fn makeKeyAndVisible(&self); - - #[method(isKeyWindow)] - pub fn isKeyWindow(&self) -> bool; - } -); diff --git a/src/platform_impl/ios/view.rs b/src/platform_impl/ios/view.rs index 39316dd1a0..6585d7013a 100644 --- a/src/platform_impl/ios/view.rs +++ b/src/platform_impl/ios/view.rs @@ -2,19 +2,17 @@ use std::cell::{Cell, RefCell}; use objc2::rc::Id; -use objc2::runtime::{AnyClass, NSObjectProtocol, ProtocolObject}; -use objc2::{ - declare_class, extern_methods, msg_send, msg_send_id, mutability, sel, ClassType, DeclaredClass, -}; +use objc2::runtime::{NSObjectProtocol, ProtocolObject}; +use objc2::{declare_class, msg_send, msg_send_id, mutability, sel, ClassType, DeclaredClass}; use objc2_foundation::{CGFloat, CGPoint, CGRect, MainThreadMarker, NSObject, NSSet}; +use objc2_ui_kit::{ + UICoordinateSpace, UIEvent, UIForceTouchCapability, UIGestureRecognizer, + UIGestureRecognizerDelegate, UIGestureRecognizerState, UIPanGestureRecognizer, + UIPinchGestureRecognizer, UIResponder, UIRotationGestureRecognizer, UITapGestureRecognizer, + UITouch, UITouchPhase, UITouchType, UITraitEnvironment, UIView, +}; use super::app_state::{self, EventWrapper}; -use super::uikit::{ - UIEvent, UIForceTouchCapability, UIGestureRecognizer, UIGestureRecognizerDelegate, - UIGestureRecognizerState, UIPanGestureRecognizer, UIPinchGestureRecognizer, UIResponder, - UIRotationGestureRecognizer, UITapGestureRecognizer, UITouch, UITouchPhase, UITouchType, - UITraitCollection, UIView, -}; use super::window::WinitUIWindow; use crate::dpi::PhysicalPosition; use crate::event::{Event, Force, Touch, TouchPhase, WindowEvent}; @@ -39,7 +37,7 @@ declare_class!( unsafe impl ClassType for WinitView { #[inherits(UIResponder, NSObject)] type Super = UIView; - type Mutability = mutability::InteriorMutable; + type Mutability = mutability::MainThreadOnly; const NAME: &'static str = "WinitUIView"; } @@ -270,7 +268,7 @@ declare_class!( fn pan_gesture(&self, recognizer: &UIPanGestureRecognizer) { let window = self.window().unwrap(); - let translation = recognizer.translationInView(self); + let translation = recognizer.translationInView(Some(self)); let (phase, dx, dy) = match recognizer.state() { UIGestureRecognizerState::Began => { @@ -328,30 +326,13 @@ declare_class!( } ); -extern_methods!( - #[allow(non_snake_case)] - unsafe impl WinitView { - fn window(&self) -> Option> { - unsafe { msg_send_id![self, window] } - } - - unsafe fn traitCollection(&self) -> Id { - msg_send_id![self, traitCollection] - } - - // TODO: Allow the user to customize this - #[method(layerClass)] - pub(crate) fn layerClass() -> &'static AnyClass; - } -); - impl WinitView { pub(crate) fn new( - _mtm: MainThreadMarker, + mtm: MainThreadMarker, window_attributes: &WindowAttributes, frame: CGRect, ) -> Id { - let this = Self::alloc().set_ivars(WinitViewState { + let this = mtm.alloc().set_ivars(WinitViewState { pinch_gesture_recognizer: RefCell::new(None), doubletap_gesture_recognizer: RefCell::new(None), rotation_gesture_recognizer: RefCell::new(None), @@ -372,13 +353,23 @@ impl WinitView { this } + fn window(&self) -> Option> { + // SAFETY: `WinitView`s are always installed in a `WinitUIWindow` + (**self).window().map(|window| unsafe { Id::cast(window) }) + } + pub(crate) fn recognize_pinch_gesture(&self, should_recognize: bool) { + let mtm = MainThreadMarker::from(self); if should_recognize { if self.ivars().pinch_gesture_recognizer.borrow().is_none() { - let pinch: Id = unsafe { - msg_send_id![UIPinchGestureRecognizer::alloc(), initWithTarget: self, action: sel!(pinchGesture:)] + let pinch = unsafe { + UIPinchGestureRecognizer::initWithTarget_action( + mtm.alloc(), + Some(self), + Some(sel!(pinchGesture:)), + ) }; - pinch.setDelegate(ProtocolObject::from_ref(self)); + pinch.setDelegate(Some(ProtocolObject::from_ref(self))); self.addGestureRecognizer(&pinch); self.ivars().pinch_gesture_recognizer.replace(Some(pinch)); } @@ -393,12 +384,17 @@ impl WinitView { minimum_number_of_touches: u8, maximum_number_of_touches: u8, ) { + let mtm = MainThreadMarker::from(self); if should_recognize { if self.ivars().pan_gesture_recognizer.borrow().is_none() { - let pan: Id = unsafe { - msg_send_id![UIPanGestureRecognizer::alloc(), initWithTarget: self, action: sel!(panGesture:)] + let pan = unsafe { + UIPanGestureRecognizer::initWithTarget_action( + mtm.alloc(), + Some(self), + Some(sel!(panGesture:)), + ) }; - pan.setDelegate(ProtocolObject::from_ref(self)); + pan.setDelegate(Some(ProtocolObject::from_ref(self))); pan.setMinimumNumberOfTouches(minimum_number_of_touches as _); pan.setMaximumNumberOfTouches(maximum_number_of_touches as _); self.addGestureRecognizer(&pan); @@ -410,12 +406,17 @@ impl WinitView { } pub(crate) fn recognize_doubletap_gesture(&self, should_recognize: bool) { + let mtm = MainThreadMarker::from(self); if should_recognize { if self.ivars().doubletap_gesture_recognizer.borrow().is_none() { - let tap: Id = unsafe { - msg_send_id![UITapGestureRecognizer::alloc(), initWithTarget: self, action: sel!(doubleTapGesture:)] + let tap = unsafe { + UITapGestureRecognizer::initWithTarget_action( + mtm.alloc(), + Some(self), + Some(sel!(doubleTapGesture:)), + ) }; - tap.setDelegate(ProtocolObject::from_ref(self)); + tap.setDelegate(Some(ProtocolObject::from_ref(self))); tap.setNumberOfTapsRequired(2); tap.setNumberOfTouchesRequired(1); self.addGestureRecognizer(&tap); @@ -427,12 +428,17 @@ impl WinitView { } pub(crate) fn recognize_rotation_gesture(&self, should_recognize: bool) { + let mtm = MainThreadMarker::from(self); if should_recognize { if self.ivars().rotation_gesture_recognizer.borrow().is_none() { - let rotation: Id = unsafe { - msg_send_id![UIRotationGestureRecognizer::alloc(), initWithTarget: self, action: sel!(rotationGesture:)] + let rotation = unsafe { + UIRotationGestureRecognizer::initWithTarget_action( + mtm.alloc(), + Some(self), + Some(sel!(rotationGesture:)), + ) }; - rotation.setDelegate(ProtocolObject::from_ref(self)); + rotation.setDelegate(Some(ProtocolObject::from_ref(self))); self.addGestureRecognizer(&rotation); self.ivars().rotation_gesture_recognizer.replace(Some(rotation)); } @@ -447,9 +453,9 @@ impl WinitView { let os_supports_force = app_state::os_capabilities().force_touch; for touch in touches { let logical_location = touch.locationInView(None); - let touch_type = touch.type_(); + let touch_type = touch.r#type(); let force = if os_supports_force { - let trait_collection = unsafe { self.traitCollection() }; + let trait_collection = self.traitCollection(); let touch_capability = trait_collection.forceTouchCapability(); // Both the OS _and_ the device need to be checked for force touch support. if touch_capability == UIForceTouchCapability::Available @@ -482,7 +488,7 @@ impl WinitView { // 2 is UITouchPhase::Stationary and is not expected here UITouchPhase::Ended => TouchPhase::Ended, UITouchPhase::Cancelled => TouchPhase::Cancelled, - _ => panic!("unexpected touch phase: {:?}", phase as i32), + _ => panic!("unexpected touch phase: {phase:?}"), }; let physical_location = { diff --git a/src/platform_impl/ios/view_controller.rs b/src/platform_impl/ios/view_controller.rs index e0872859d0..1f6785fdf1 100644 --- a/src/platform_impl/ios/view_controller.rs +++ b/src/platform_impl/ios/view_controller.rs @@ -3,12 +3,12 @@ use std::cell::Cell; use objc2::rc::Id; use objc2::{declare_class, msg_send_id, mutability, ClassType, DeclaredClass}; use objc2_foundation::{MainThreadMarker, NSObject}; - -use super::app_state::{self}; -use super::uikit::{ +use objc2_ui_kit::{ UIDevice, UIInterfaceOrientationMask, UIRectEdge, UIResponder, UIStatusBarStyle, UIUserInterfaceIdiom, UIView, UIViewController, }; + +use super::app_state::{self}; use crate::platform::ios::{ScreenEdge, StatusBarStyle, ValidOrientations}; use crate::window::WindowAttributes; @@ -26,7 +26,7 @@ declare_class!( unsafe impl ClassType for WinitViewController { #[inherits(UIResponder, NSObject)] type Super = UIViewController; - type Mutability = mutability::InteriorMutable; + type Mutability = mutability::MainThreadOnly; const NAME: &'static str = "WinitUIViewController"; } @@ -114,7 +114,7 @@ impl WinitViewController { mtm: MainThreadMarker, valid_orientations: ValidOrientations, ) { - let mask = match (valid_orientations, UIDevice::current(mtm).userInterfaceIdiom()) { + let mask = match (valid_orientations, UIDevice::currentDevice(mtm).userInterfaceIdiom()) { (ValidOrientations::LandscapeAndPortrait, UIUserInterfaceIdiom::Phone) => { UIInterfaceOrientationMask::AllButUpsideDown }, @@ -129,7 +129,8 @@ impl WinitViewController { }, }; self.ivars().supported_orientations.set(mask); - UIViewController::attemptRotationToDeviceOrientation(); + #[allow(deprecated)] + UIViewController::attemptRotationToDeviceOrientation(mtm); } pub(crate) fn new( @@ -138,12 +139,12 @@ impl WinitViewController { view: &UIView, ) -> Id { // These are set properly below, we just to set them to something in the meantime. - let this = Self::alloc().set_ivars(ViewControllerState { + let this = mtm.alloc().set_ivars(ViewControllerState { prefers_status_bar_hidden: Cell::new(false), preferred_status_bar_style: Cell::new(UIStatusBarStyle::Default), prefers_home_indicator_auto_hidden: Cell::new(false), supported_orientations: Cell::new(UIInterfaceOrientationMask::All), - preferred_screen_edges_deferring_system_gestures: Cell::new(UIRectEdge::NONE), + preferred_screen_edges_deferring_system_gestures: Cell::new(UIRectEdge::empty()), }); let this: Id = unsafe { msg_send_id![super(this), init] }; diff --git a/src/platform_impl/ios/window.rs b/src/platform_impl/ios/window.rs index f42eed3704..346aa5fb7e 100644 --- a/src/platform_impl/ios/window.rs +++ b/src/platform_impl/ios/window.rs @@ -8,12 +8,13 @@ use objc2::{class, declare_class, msg_send, msg_send_id, mutability, ClassType, use objc2_foundation::{ CGFloat, CGPoint, CGRect, CGSize, MainThreadBound, MainThreadMarker, NSObjectProtocol, }; +use objc2_ui_kit::{ + UIApplication, UICoordinateSpace, UIResponder, UIScreen, UIScreenOverscanCompensation, + UIViewController, UIWindow, +}; use tracing::{debug, warn}; use super::app_state::EventWrapper; -use super::uikit::{ - UIApplication, UIResponder, UIScreen, UIScreenOverscanCompensation, UIViewController, UIWindow, -}; use super::view::WinitView; use super::view_controller::WinitViewController; use crate::cursor::Cursor; @@ -37,7 +38,7 @@ declare_class!( unsafe impl ClassType for WinitUIWindow { #[inherits(UIResponder, NSObject)] type Super = UIWindow; - type Mutability = mutability::InteriorMutable; + type Mutability = mutability::MainThreadOnly; const NAME: &'static str = "WinitUIWindow"; } @@ -79,7 +80,7 @@ impl WinitUIWindow { frame: CGRect, view_controller: &UIViewController, ) -> Id { - let this: Id = unsafe { msg_send_id![Self::alloc(), initWithFrame: frame] }; + let this: Id = unsafe { msg_send_id![mtm.alloc(), initWithFrame: frame] }; this.setRootViewController(Some(view_controller)); @@ -396,7 +397,8 @@ impl Inner { } pub fn primary_monitor(&self) -> Option { - Some(MonitorHandle::new(UIScreen::main(MainThreadMarker::new().unwrap()))) + #[allow(deprecated)] + Some(MonitorHandle::new(UIScreen::mainScreen(MainThreadMarker::new().unwrap()))) } pub fn id(&self) -> WindowId { @@ -483,7 +485,8 @@ impl Window { // TODO: transparency, visible - let main_screen = UIScreen::main(mtm); + #[allow(deprecated)] + let main_screen = UIScreen::mainScreen(mtm); let fullscreen = window_attributes.fullscreen.clone().map(Into::into); let screen = match fullscreen { Some(Fullscreen::Exclusive(ref video_mode)) => video_mode.monitor.ui_screen(mtm), @@ -672,10 +675,8 @@ impl Inner { } else { let screen_frame = self.rect_to_screen_space(bounds); let status_bar_frame = { - let app = UIApplication::shared(MainThreadMarker::new().unwrap()).expect( - "`Window::get_inner_position` cannot be called before `EventLoop::run_app` on \ - iOS", - ); + let app = UIApplication::sharedApplication(MainThreadMarker::new().unwrap()); + #[allow(deprecated)] app.statusBarFrame() }; let (y, height) = if screen_frame.origin.y > status_bar_frame.size.height {