diff --git a/Cargo.toml b/Cargo.toml index 4bd127a0..ac824f2d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -108,13 +108,13 @@ ndk = { version = "0.9.0", default-features = false } [target.'cfg(any(target_os = "ios", target_os = "macos"))'.dependencies] core-foundation = "0.9.3" -objc2 = "0.5.1" +objc2 = "0.5.2" [target.'cfg(target_os = "macos")'.dependencies] core-graphics = "0.23.1" [target.'cfg(target_os = "macos")'.dependencies.objc2-foundation] -version = "0.2.0" +version = "0.2.2" features = [ "dispatch", "NSArray", @@ -134,7 +134,7 @@ features = [ ] [target.'cfg(target_os = "macos")'.dependencies.objc2-app-kit] -version = "0.2.0" +version = "0.2.2" features = [ "NSAppearance", "NSApplication", @@ -164,7 +164,7 @@ features = [ ] [target.'cfg(target_os = "ios")'.dependencies.objc2-foundation] -version = "0.2.0" +version = "0.2.2" features = [ "dispatch", "NSArray", @@ -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/ios.rs b/src/platform/ios.rs index daba5827..59f53961 100644 --- a/src/platform/ios.rs +++ b/src/platform/ios.rs @@ -357,7 +357,7 @@ impl MonitorHandleExtIOS for MonitorHandle { fn ui_screen(&self) -> *mut c_void { // SAFETY: The marker is only used to get the pointer of the screen let mtm = unsafe { objc2_foundation::MainThreadMarker::new_unchecked() }; - objc2::rc::Id::as_ptr(self.inner.ui_screen(mtm)) as *mut c_void + objc2::rc::Retained::as_ptr(self.inner.ui_screen(mtm)) as *mut c_void } #[inline] diff --git a/src/platform/macos.rs b/src/platform/macos.rs index 74d400f3..66f25c92 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -375,7 +375,7 @@ impl MonitorHandleExtMacOS for MonitorHandle { fn ns_screen(&self) -> Option<*mut c_void> { // SAFETY: We only use the marker to get a pointer let mtm = unsafe { objc2_foundation::MainThreadMarker::new_unchecked() }; - self.inner.ns_screen(mtm).map(|s| objc2::rc::Id::as_ptr(&s) as _) + self.inner.ns_screen(mtm).map(|s| objc2::rc::Retained::as_ptr(&s) as _) } } diff --git a/src/platform_impl/ios/app_delegate.rs b/src/platform_impl/ios/app_delegate.rs index 99ddf1e3..0be14d1b 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 d9c54c0e..630590ce 100644 --- a/src/platform_impl/ios/app_state.rs +++ b/src/platform_impl/ios/app_state.rs @@ -13,15 +13,15 @@ use core_foundation::runloop::{ kCFRunLoopCommonModes, CFRunLoopAddTimer, CFRunLoopGetMain, CFRunLoopRef, CFRunLoopTimerCreate, CFRunLoopTimerInvalidate, CFRunLoopTimerRef, CFRunLoopTimerSetNextFireDate, }; -use objc2::rc::Id; +use objc2::rc::Retained; use objc2::runtime::AnyObject; use objc2::{msg_send, sel}; 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}; @@ -72,7 +72,7 @@ pub(crate) enum EventWrapper { #[derive(Debug)] pub struct ScaleFactorChanged { - pub(super) window: Id, + pub(super) window: Retained, pub(super) suggested_size: PhysicalSize, pub(super) scale_factor: f64, } @@ -99,25 +99,25 @@ impl Event { #[must_use = "dropping `AppStateImpl` without inspecting it is probably a bug"] enum AppStateImpl { NotLaunched { - queued_windows: Vec>, + queued_windows: Vec>, queued_events: Vec, - queued_gpu_redraws: HashSet>, + queued_gpu_redraws: HashSet>, }, Launching { - queued_windows: Vec>, + queued_windows: Vec>, queued_events: Vec, queued_handler: EventLoopHandler, - queued_gpu_redraws: HashSet>, + queued_gpu_redraws: HashSet>, }, ProcessingEvents { handler: EventLoopHandler, - queued_gpu_redraws: HashSet>, + queued_gpu_redraws: HashSet>, active_control_flow: ControlFlow, }, // special state to deal with reentrancy and prevent mutable aliasing. InUserCallback { queued_events: Vec, - queued_gpu_redraws: HashSet>, + queued_gpu_redraws: HashSet>, }, ProcessingRedraws { handler: EventLoopHandler, @@ -228,7 +228,9 @@ impl AppState { }); } - fn did_finish_launching_transition(&mut self) -> (Vec>, Vec) { + fn did_finish_launching_transition( + &mut self, + ) -> (Vec>, Vec) { let (windows, events, handler, queued_gpu_redraws) = match self.take_state() { AppStateImpl::Launching { queued_windows, @@ -344,7 +346,7 @@ impl AppState { UserCallbackTransitionResult::Success { handler, active_control_flow, processing_redraws } } - fn main_events_cleared_transition(&mut self) -> HashSet> { + fn main_events_cleared_transition(&mut self) -> HashSet> { let (handler, queued_gpu_redraws, active_control_flow) = match self.take_state() { AppStateImpl::ProcessingEvents { handler, queued_gpu_redraws, active_control_flow } => { (handler, queued_gpu_redraws, active_control_flow) @@ -412,7 +414,7 @@ impl AppState { } } -pub(crate) fn set_key_window(mtm: MainThreadMarker, window: &Id) { +pub(crate) fn set_key_window(mtm: MainThreadMarker, window: &Retained) { let mut this = AppState::get_mut(mtm); match this.state_mut() { &mut AppStateImpl::NotLaunched { ref mut queued_windows, .. } => { @@ -432,7 +434,7 @@ pub(crate) fn set_key_window(mtm: MainThreadMarker, window: &Id) window.makeKeyAndVisible(); } -pub(crate) fn queue_gl_or_metal_redraw(mtm: MainThreadMarker, window: Id) { +pub(crate) fn queue_gl_or_metal_redraw(mtm: MainThreadMarker, window: Retained) { let mut this = AppState::get_mut(mtm); match this.state_mut() { &mut AppStateImpl::NotLaunched { ref mut queued_gpu_redraws, .. } @@ -722,7 +724,7 @@ fn handle_hidpi_proxy(handler: &mut EventLoopHandler, event: ScaleFactorChanged) view.setFrame(new_frame); } -fn get_view_and_screen_frame(window: &WinitUIWindow) -> (Id, CGRect) { +fn get_view_and_screen_frame(window: &WinitUIWindow) -> (Retained, CGRect) { let view_controller = window.rootViewController().unwrap(); let view = view_controller.view().unwrap(); let bounds = window.bounds(); diff --git a/src/platform_impl/ios/event_loop.rs b/src/platform_impl/ios/event_loop.rs index 665cb0a6..7881a7d1 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::Retained; +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 c90a21c5..bf2c011f 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 d85fe20e..1c707a3a 100644 --- a/src/platform_impl/ios/monitor.rs +++ b/src/platform_impl/ios/monitor.rs @@ -4,22 +4,22 @@ use std::collections::{BTreeSet, VecDeque}; use std::{fmt, hash, ptr}; use objc2::mutability::IsRetainable; -use objc2::rc::Id; +use objc2::rc::Retained; 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; // Workaround for `MainThreadBound` implementing almost no traits #[derive(Debug)] -struct MainThreadBoundDelegateImpls(MainThreadBound>); +struct MainThreadBoundDelegateImpls(MainThreadBound>); impl Clone for MainThreadBoundDelegateImpls { fn clone(&self) -> Self { - Self(run_on_main(|mtm| MainThreadBound::new(Id::clone(self.0.get(mtm)), mtm))) + Self(run_on_main(|mtm| MainThreadBound::new(Retained::clone(self.0.get(mtm)), mtm))) } } @@ -27,7 +27,7 @@ impl hash::Hash for MainThreadBoundDelegateImpls { fn hash(&self, state: &mut H) { // SAFETY: Marker only used to get the pointer let mtm = unsafe { MainThreadMarker::new_unchecked() }; - Id::as_ptr(self.0.get(mtm)).hash(state); + Retained::as_ptr(self.0.get(mtm)).hash(state); } } @@ -35,7 +35,7 @@ impl PartialEq for MainThreadBoundDelegateImpls { fn eq(&self, other: &Self) -> bool { // SAFETY: Marker only used to get the pointer let mtm = unsafe { MainThreadMarker::new_unchecked() }; - Id::as_ptr(self.0.get(mtm)) == Id::as_ptr(other.0.get(mtm)) + Retained::as_ptr(self.0.get(mtm)) == Retained::as_ptr(other.0.get(mtm)) } } @@ -52,8 +52,8 @@ pub struct VideoModeHandle { impl VideoModeHandle { fn new( - uiscreen: Id, - screen_mode: Id, + uiscreen: Retained, + screen_mode: Retained, mtm: MainThreadMarker, ) -> VideoModeHandle { let refresh_rate_millihertz = refresh_rate_millihertz(&uiscreen); @@ -83,13 +83,13 @@ impl VideoModeHandle { self.monitor.clone() } - pub(super) fn screen_mode(&self, mtm: MainThreadMarker) -> &Id { + pub(super) fn screen_mode(&self, mtm: MainThreadMarker) -> &Retained { self.screen_mode.0.get(mtm) } } pub struct MonitorHandle { - ui_screen: MainThreadBound>, + ui_screen: MainThreadBound>, } impl Clone for MonitorHandle { @@ -140,20 +140,22 @@ impl fmt::Debug for MonitorHandle { } impl MonitorHandle { - pub(crate) fn new(ui_screen: Id) -> Self { - // Holding `Id` implies we're on the main thread. + pub(crate) fn new(ui_screen: Retained) -> Self { + // Holding `Retained` implies we're on the main thread. let mtm = MainThreadMarker::new().unwrap(); Self { ui_screen: MainThreadBound::new(ui_screen, mtm) } } 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)) @@ -197,7 +199,7 @@ impl MonitorHandle { }) } - pub(crate) fn ui_screen(&self, mtm: MainThreadMarker) -> &Id { + pub(crate) fn ui_screen(&self, mtm: MainThreadMarker) -> &Retained { self.ui_screen.get(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 ce9685ca..00000000 --- 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 3c3b9303..00000000 --- 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 24c57a94..00000000 --- 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 d72159e9..00000000 --- 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 022160b6..00000000 --- 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 ab5e7a32..00000000 --- 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 c415cac0..00000000 --- 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 10811362..00000000 --- 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 578fff5d..00000000 --- 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 1d86c96f..00000000 --- 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 7b0685a3..00000000 --- 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 6db47642..00000000 --- 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 13c6834f..00000000 --- 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 abd9bc53..00000000 --- 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 80d9e896..00000000 --- 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 28cbef87..00000000 --- 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 39316dd1..75386d36 100644 --- a/src/platform_impl/ios/view.rs +++ b/src/platform_impl/ios/view.rs @@ -1,20 +1,18 @@ #![allow(clippy::unnecessary_cast)] 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::rc::Retained; +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}; @@ -22,10 +20,10 @@ use crate::platform_impl::platform::DEVICE_ID; use crate::window::{WindowAttributes, WindowId as RootWindowId}; pub struct WinitViewState { - pinch_gesture_recognizer: RefCell>>, - doubletap_gesture_recognizer: RefCell>>, - rotation_gesture_recognizer: RefCell>>, - pan_gesture_recognizer: RefCell>>, + pinch_gesture_recognizer: RefCell>>, + doubletap_gesture_recognizer: RefCell>>, + rotation_gesture_recognizer: RefCell>>, + pan_gesture_recognizer: RefCell>>, // for iOS delta references the start of the Gesture rotation_last_delta: Cell, @@ -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 { + ) -> Retained { + 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), @@ -361,7 +342,7 @@ impl WinitView { pinch_last_delta: Cell::new(0.0), pan_last_delta: Cell::new(CGPoint { x: 0.0, y: 0.0 }), }); - let this: Id = unsafe { msg_send_id![super(this), initWithFrame: frame] }; + let this: Retained = unsafe { msg_send_id![super(this), initWithFrame: frame] }; this.setMultipleTouchEnabled(true); @@ -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 { Retained::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 e0872859..dd658682 100644 --- a/src/platform_impl/ios/view_controller.rs +++ b/src/platform_impl/ios/view_controller.rs @@ -1,14 +1,14 @@ use std::cell::Cell; -use objc2::rc::Id; +use objc2::rc::Retained; 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,23 +129,24 @@ impl WinitViewController { }, }; self.ivars().supported_orientations.set(mask); - UIViewController::attemptRotationToDeviceOrientation(); + #[allow(deprecated)] + UIViewController::attemptRotationToDeviceOrientation(mtm); } pub(crate) fn new( mtm: MainThreadMarker, window_attributes: &WindowAttributes, view: &UIView, - ) -> Id { + ) -> Retained { // 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] }; + let this: Retained = unsafe { msg_send_id![super(this), init] }; this.set_prefers_status_bar_hidden( window_attributes.platform_specific.prefers_status_bar_hidden, diff --git a/src/platform_impl/ios/window.rs b/src/platform_impl/ios/window.rs index f42eed37..8ea7aeae 100644 --- a/src/platform_impl/ios/window.rs +++ b/src/platform_impl/ios/window.rs @@ -2,18 +2,19 @@ use std::collections::VecDeque; -use objc2::rc::Id; +use objc2::rc::Retained; use objc2::runtime::{AnyObject, NSObject}; use objc2::{class, declare_class, msg_send, msg_send_id, mutability, ClassType, DeclaredClass}; 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"; } @@ -78,8 +79,8 @@ impl WinitUIWindow { window_attributes: &WindowAttributes, frame: CGRect, view_controller: &UIViewController, - ) -> Id { - let this: Id = unsafe { msg_send_id![Self::alloc(), initWithFrame: frame] }; + ) -> Retained { + let this: Retained = unsafe { msg_send_id![mtm.alloc(), initWithFrame: frame] }; this.setRootViewController(Some(view_controller)); @@ -106,9 +107,9 @@ impl WinitUIWindow { } pub struct Inner { - window: Id, - view_controller: Id, - view: Id, + window: Retained, + view_controller: Retained, + view: Retained, gl_or_metal_backed: bool, } @@ -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 { @@ -406,18 +408,18 @@ impl Inner { #[cfg(feature = "rwh_04")] pub fn raw_window_handle_rwh_04(&self) -> rwh_04::RawWindowHandle { let mut window_handle = rwh_04::UiKitHandle::empty(); - window_handle.ui_window = Id::as_ptr(&self.window) as _; - window_handle.ui_view = Id::as_ptr(&self.view) as _; - window_handle.ui_view_controller = Id::as_ptr(&self.view_controller) as _; + window_handle.ui_window = Retained::as_ptr(&self.window) as _; + window_handle.ui_view = Retained::as_ptr(&self.view) as _; + window_handle.ui_view_controller = Retained::as_ptr(&self.view_controller) as _; rwh_04::RawWindowHandle::UiKit(window_handle) } #[cfg(feature = "rwh_05")] pub fn raw_window_handle_rwh_05(&self) -> rwh_05::RawWindowHandle { let mut window_handle = rwh_05::UiKitWindowHandle::empty(); - window_handle.ui_window = Id::as_ptr(&self.window) as _; - window_handle.ui_view = Id::as_ptr(&self.view) as _; - window_handle.ui_view_controller = Id::as_ptr(&self.view_controller) as _; + window_handle.ui_window = Retained::as_ptr(&self.window) as _; + window_handle.ui_view = Retained::as_ptr(&self.view) as _; + window_handle.ui_view_controller = Retained::as_ptr(&self.view_controller) as _; rwh_05::RawWindowHandle::UiKit(window_handle) } @@ -429,11 +431,11 @@ impl Inner { #[cfg(feature = "rwh_06")] pub fn raw_window_handle_rwh_06(&self) -> rwh_06::RawWindowHandle { let mut window_handle = rwh_06::UiKitWindowHandle::new({ - let ui_view = Id::as_ptr(&self.view) as _; - std::ptr::NonNull::new(ui_view).expect("Id should never be null") + let ui_view = Retained::as_ptr(&self.view) as _; + std::ptr::NonNull::new(ui_view).expect("Retained should never be null") }); window_handle.ui_view_controller = - std::ptr::NonNull::new(Id::as_ptr(&self.view_controller) as _); + std::ptr::NonNull::new(Retained::as_ptr(&self.view_controller) as _); rwh_06::RawWindowHandle::UiKit(window_handle) } @@ -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 { diff --git a/src/platform_impl/macos/app.rs b/src/platform_impl/macos/app.rs index f3544dd2..23a1b071 100644 --- a/src/platform_impl/macos/app.rs +++ b/src/platform_impl/macos/app.rs @@ -5,7 +5,6 @@ use objc2_app_kit::{NSApplication, NSEvent, NSEventModifierFlags, NSEventType, N use objc2_foundation::{MainThreadMarker, NSObject}; use super::app_delegate::ApplicationDelegate; -use super::event::flags_contains; use crate::event::{DeviceEvent, ElementState}; declare_class!( @@ -32,7 +31,7 @@ declare_class!( let event_type = unsafe { event.r#type() }; let modifier_flags = unsafe { event.modifierFlags() }; if event_type == NSEventType::KeyUp - && flags_contains(modifier_flags, NSEventModifierFlags::NSEventModifierFlagCommand) + && modifier_flags.contains(NSEventModifierFlags::NSEventModifierFlagCommand) { if let Some(key_window) = self.keyWindow() { key_window.sendEvent(event); diff --git a/src/platform_impl/macos/app_delegate.rs b/src/platform_impl/macos/app_delegate.rs index ca0d08ae..9de572aa 100644 --- a/src/platform_impl/macos/app_delegate.rs +++ b/src/platform_impl/macos/app_delegate.rs @@ -5,7 +5,7 @@ use std::rc::Weak; use std::sync::{Arc, Mutex}; use std::time::Instant; -use objc2::rc::Id; +use objc2::rc::Retained; use objc2::runtime::AnyObject; use objc2::{declare_class, msg_send_id, mutability, ClassType, DeclaredClass}; use objc2_app_kit::{NSApplication, NSApplicationActivationPolicy, NSApplicationDelegate}; @@ -133,7 +133,7 @@ impl ApplicationDelegate { activation_policy: NSApplicationActivationPolicy, default_menu: bool, activate_ignoring_other_apps: bool, - ) -> Id { + ) -> Retained { let this = mtm.alloc().set_ivars(State { activation_policy: Policy(activation_policy), default_menu, @@ -143,13 +143,13 @@ impl ApplicationDelegate { unsafe { msg_send_id![super(this), init] } } - pub fn get(mtm: MainThreadMarker) -> Id { + pub fn get(mtm: MainThreadMarker) -> Retained { let app = NSApplication::sharedApplication(mtm); let delegate = unsafe { app.delegate() }.expect("a delegate was not configured on the application"); if delegate.is_kind_of::() { // SAFETY: Just checked that the delegate is an instance of `ApplicationDelegate` - unsafe { Id::cast(delegate) } + unsafe { Retained::cast(delegate) } } else { panic!("tried to get a delegate that was not the one Winit has registered") } @@ -247,7 +247,7 @@ impl ApplicationDelegate { pub fn queue_static_scale_factor_changed_event( &self, - window: Id, + window: Retained, suggested_size: PhysicalSize, scale_factor: f64, ) { @@ -425,7 +425,7 @@ pub(crate) enum QueuedEvent { WindowEvent(WindowId, WindowEvent), DeviceEvent(DeviceEvent), ScaleFactorChanged { - window: Id, + window: Retained, suggested_size: PhysicalSize, scale_factor: f64, }, diff --git a/src/platform_impl/macos/cursor.rs b/src/platform_impl/macos/cursor.rs index 4084be12..cc8f5f30 100644 --- a/src/platform_impl/macos/cursor.rs +++ b/src/platform_impl/macos/cursor.rs @@ -2,7 +2,7 @@ use std::ffi::c_uchar; use std::slice; use std::sync::OnceLock; -use objc2::rc::Id; +use objc2::rc::Retained; use objc2::runtime::Sel; use objc2::{msg_send_id, sel, ClassType}; use objc2_app_kit::{NSBitmapImageRep, NSCursor, NSDeviceRGBColorSpace, NSImage}; @@ -15,7 +15,7 @@ use crate::cursor::{CursorImage, OnlyCursorImageSource}; use crate::window::CursorIcon; #[derive(Clone, Debug, PartialEq, Eq, Hash)] -pub struct CustomCursor(pub(crate) Id); +pub struct CustomCursor(pub(crate) Retained); // SAFETY: NSCursor is immutable and thread-safe // TODO(madsmtm): Put this logic in objc2-app-kit itself @@ -28,7 +28,7 @@ impl CustomCursor { } } -pub(crate) fn cursor_from_image(cursor: &CursorImage) -> Id { +pub(crate) fn cursor_from_image(cursor: &CursorImage) -> Retained { let width = cursor.width; let height = cursor.height; @@ -60,14 +60,14 @@ pub(crate) fn cursor_from_image(cursor: &CursorImage) -> Id { NSCursor::initWithImage_hotSpot(NSCursor::alloc(), &image, hotspot) } -pub(crate) fn default_cursor() -> Id { +pub(crate) fn default_cursor() -> Retained { NSCursor::arrowCursor() } -unsafe fn try_cursor_from_selector(sel: Sel) -> Option> { +unsafe fn try_cursor_from_selector(sel: Sel) -> Option> { let cls = NSCursor::class(); if cls.responds_to(sel) { - let cursor: Id = unsafe { msg_send_id![cls, performSelector: sel] }; + let cursor: Retained = unsafe { msg_send_id![cls, performSelector: sel] }; Some(cursor) } else { tracing::warn!("cursor `{sel}` appears to be invalid"); @@ -82,7 +82,7 @@ macro_rules! def_undocumented_cursor { )*} => {$( $(#[$($m)*])* #[allow(non_snake_case)] - fn $name() -> Id { + fn $name() -> Retained { unsafe { try_cursor_from_selector(sel!($name)).unwrap_or_else(|| default_cursor()) } } )*}; @@ -112,7 +112,7 @@ def_undocumented_cursor!( // Note that loading `busybutclickable` with this code won't animate // the frames; instead you'll just get them all in a column. -unsafe fn load_webkit_cursor(name: &NSString) -> Id { +unsafe fn load_webkit_cursor(name: &NSString) -> Retained { // Snatch a cursor from WebKit; They fit the style of the native // cursors, and will seem completely standard to macOS users. // @@ -128,7 +128,7 @@ unsafe fn load_webkit_cursor(name: &NSString) -> Id { // TODO: Handle PLists better let info_path = cursor_path.stringByAppendingPathComponent(ns_string!("info.plist")); - let info: Id> = unsafe { + let info: Retained> = unsafe { msg_send_id![ >::class(), dictionaryWithContentsOfFile: &*info_path, @@ -155,15 +155,15 @@ unsafe fn load_webkit_cursor(name: &NSString) -> Id { NSCursor::initWithImage_hotSpot(NSCursor::alloc(), &image, hotspot) } -fn webkit_move() -> Id { +fn webkit_move() -> Retained { unsafe { load_webkit_cursor(ns_string!("move")) } } -fn webkit_cell() -> Id { +fn webkit_cell() -> Retained { unsafe { load_webkit_cursor(ns_string!("cell")) } } -pub(crate) fn invisible_cursor() -> Id { +pub(crate) fn invisible_cursor() -> Retained { // 16x16 GIF data for invisible cursor // You can reproduce this via ImageMagick. // $ convert -size 16x16 xc:none cursor.gif @@ -174,7 +174,7 @@ pub(crate) fn invisible_cursor() -> Id { 0xa3, 0x9c, 0xb4, 0xda, 0x8b, 0xb3, 0x3e, 0x05, 0x00, 0x3b, ]; - fn new_invisible() -> Id { + fn new_invisible() -> Retained { // TODO: Consider using `dataWithBytesNoCopy:` let data = NSData::with_bytes(CURSOR_BYTES); let image = NSImage::initWithData(NSImage::alloc(), &data).unwrap(); @@ -187,7 +187,7 @@ pub(crate) fn invisible_cursor() -> Id { CURSOR.get_or_init(|| CustomCursor(new_invisible())).0.clone() } -pub(crate) fn cursor_from_icon(icon: CursorIcon) -> Id { +pub(crate) fn cursor_from_icon(icon: CursorIcon) -> Retained { match icon { CursorIcon::Default => default_cursor(), CursorIcon::Pointer => NSCursor::pointingHandCursor(), diff --git a/src/platform_impl/macos/event.rs b/src/platform_impl/macos/event.rs index a22cccc8..602ab627 100644 --- a/src/platform_impl/macos/event.rs +++ b/src/platform_impl/macos/event.rs @@ -2,7 +2,7 @@ use std::ffi::c_void; use core_foundation::base::CFRelease; use core_foundation::data::{CFDataGetBytePtr, CFDataRef}; -use objc2::rc::Id; +use objc2::rc::Retained; use objc2_app_kit::{NSEvent, NSEventModifierFlags, NSEventSubtype, NSEventType}; use objc2_foundation::{run_on_main, NSPoint}; use smol_str::SmolStr; @@ -133,8 +133,8 @@ pub(crate) fn create_key_event( let key_without_modifiers = get_modifierless_char(scancode); let modifiers = unsafe { ns_event.modifierFlags() }; - let has_ctrl = flags_contains(modifiers, NSEventModifierFlags::NSEventModifierFlagControl); - let has_cmd = flags_contains(modifiers, NSEventModifierFlags::NSEventModifierFlagCommand); + let has_ctrl = modifiers.contains(NSEventModifierFlags::NSEventModifierFlagControl); + let has_cmd = modifiers.contains(NSEventModifierFlags::NSEventModifierFlagCommand); let logical_key = match text_with_all_modifiers.as_ref() { // Only checking for ctrl and cmd here, not checking for alt because we DO want to @@ -305,16 +305,12 @@ const NX_DEVICELALTKEYMASK: NSEventModifierFlags = NSEventModifierFlags(0x000000 const NX_DEVICERALTKEYMASK: NSEventModifierFlags = NSEventModifierFlags(0x00000040); const NX_DEVICERCTLKEYMASK: NSEventModifierFlags = NSEventModifierFlags(0x00002000); -pub(super) fn flags_contains(flags: NSEventModifierFlags, value: NSEventModifierFlags) -> bool { - flags.0 & value.0 == value.0 -} - pub(super) fn lalt_pressed(event: &NSEvent) -> bool { - flags_contains(unsafe { event.modifierFlags() }, NX_DEVICELALTKEYMASK) + unsafe { event.modifierFlags() }.contains(NX_DEVICELALTKEYMASK) } pub(super) fn ralt_pressed(event: &NSEvent) -> bool { - flags_contains(unsafe { event.modifierFlags() }, NX_DEVICERALTKEYMASK) + unsafe { event.modifierFlags() }.contains(NX_DEVICERALTKEYMASK) } pub(super) fn event_mods(event: &NSEvent) -> Modifiers { @@ -322,38 +318,33 @@ pub(super) fn event_mods(event: &NSEvent) -> Modifiers { let mut state = ModifiersState::empty(); let mut pressed_mods = ModifiersKeys::empty(); - state.set( - ModifiersState::SHIFT, - flags_contains(flags, NSEventModifierFlags::NSEventModifierFlagShift), - ); - pressed_mods.set(ModifiersKeys::LSHIFT, flags_contains(flags, NX_DEVICELSHIFTKEYMASK)); - pressed_mods.set(ModifiersKeys::RSHIFT, flags_contains(flags, NX_DEVICERSHIFTKEYMASK)); + state + .set(ModifiersState::SHIFT, flags.contains(NSEventModifierFlags::NSEventModifierFlagShift)); + pressed_mods.set(ModifiersKeys::LSHIFT, flags.contains(NX_DEVICELSHIFTKEYMASK)); + pressed_mods.set(ModifiersKeys::RSHIFT, flags.contains(NX_DEVICERSHIFTKEYMASK)); state.set( ModifiersState::CONTROL, - flags_contains(flags, NSEventModifierFlags::NSEventModifierFlagControl), + flags.contains(NSEventModifierFlags::NSEventModifierFlagControl), ); - pressed_mods.set(ModifiersKeys::LCONTROL, flags_contains(flags, NX_DEVICELCTLKEYMASK)); - pressed_mods.set(ModifiersKeys::RCONTROL, flags_contains(flags, NX_DEVICERCTLKEYMASK)); + pressed_mods.set(ModifiersKeys::LCONTROL, flags.contains(NX_DEVICELCTLKEYMASK)); + pressed_mods.set(ModifiersKeys::RCONTROL, flags.contains(NX_DEVICERCTLKEYMASK)); - state.set( - ModifiersState::ALT, - flags_contains(flags, NSEventModifierFlags::NSEventModifierFlagOption), - ); - pressed_mods.set(ModifiersKeys::LALT, flags_contains(flags, NX_DEVICELALTKEYMASK)); - pressed_mods.set(ModifiersKeys::RALT, flags_contains(flags, NX_DEVICERALTKEYMASK)); + state.set(ModifiersState::ALT, flags.contains(NSEventModifierFlags::NSEventModifierFlagOption)); + pressed_mods.set(ModifiersKeys::LALT, flags.contains(NX_DEVICELALTKEYMASK)); + pressed_mods.set(ModifiersKeys::RALT, flags.contains(NX_DEVICERALTKEYMASK)); state.set( ModifiersState::SUPER, - flags_contains(flags, NSEventModifierFlags::NSEventModifierFlagCommand), + flags.contains(NSEventModifierFlags::NSEventModifierFlagCommand), ); - pressed_mods.set(ModifiersKeys::LSUPER, flags_contains(flags, NX_DEVICELCMDKEYMASK)); - pressed_mods.set(ModifiersKeys::RSUPER, flags_contains(flags, NX_DEVICERCMDKEYMASK)); + pressed_mods.set(ModifiersKeys::LSUPER, flags.contains(NX_DEVICELCMDKEYMASK)); + pressed_mods.set(ModifiersKeys::RSUPER, flags.contains(NX_DEVICERCMDKEYMASK)); Modifiers { state, pressed_mods } } -pub(super) fn dummy_event() -> Option> { +pub(super) fn dummy_event() -> Option> { unsafe { NSEvent::otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2( NSEventType::ApplicationDefined, diff --git a/src/platform_impl/macos/event_loop.rs b/src/platform_impl/macos/event_loop.rs index ccdc9da2..68f411ce 100644 --- a/src/platform_impl/macos/event_loop.rs +++ b/src/platform_impl/macos/event_loop.rs @@ -14,7 +14,7 @@ use core_foundation::runloop::{ kCFRunLoopCommonModes, CFRunLoopAddSource, CFRunLoopGetMain, CFRunLoopSourceContext, CFRunLoopSourceCreate, CFRunLoopSourceRef, CFRunLoopSourceSignal, CFRunLoopWakeUp, }; -use objc2::rc::{autoreleasepool, Id}; +use objc2::rc::{autoreleasepool, Retained}; use objc2::runtime::ProtocolObject; use objc2::{msg_send_id, ClassType}; use objc2_app_kit::{NSApplication, NSApplicationActivationPolicy, NSWindow}; @@ -68,12 +68,12 @@ impl PanicInfo { #[derive(Debug)] pub struct ActiveEventLoop { - delegate: Id, + delegate: Retained, pub(super) mtm: MainThreadMarker, } impl ActiveEventLoop { - pub(super) fn new_root(delegate: Id) -> RootWindowTarget { + pub(super) fn new_root(delegate: Retained) -> RootWindowTarget { let mtm = MainThreadMarker::from(&*delegate); let p = Self { delegate, mtm }; RootWindowTarget { p, _marker: PhantomData } @@ -186,12 +186,12 @@ pub struct EventLoop { /// /// We intentionally don't store `WinitApplication` since we want to have /// the possibility of swapping that out at some point. - app: Id, + app: Retained, /// The application delegate that we've registered. /// /// The delegate is only weakly referenced by NSApplication, so we must /// keep it around here as well. - delegate: Id, + delegate: Retained, // Event sender and receiver, used for EventLoopProxy. sender: mpsc::Sender, @@ -225,7 +225,7 @@ impl EventLoop { let mtm = MainThreadMarker::new() .expect("on macOS, `EventLoop` must be created on the main thread!"); - let app: Id = + let app: Retained = unsafe { msg_send_id![WinitApplication::class(), sharedApplication] }; if !app.is_kind_of::() { diff --git a/src/platform_impl/macos/menu.rs b/src/platform_impl/macos/menu.rs index 6f93bfd9..cdebea0b 100644 --- a/src/platform_impl/macos/menu.rs +++ b/src/platform_impl/macos/menu.rs @@ -1,4 +1,4 @@ -use objc2::rc::Id; +use objc2::rc::Retained; use objc2::runtime::Sel; use objc2::sel; use objc2_app_kit::{NSApplication, NSEventModifierFlags, NSMenu, NSMenuItem}; @@ -48,10 +48,10 @@ pub fn initialize(app: &NSApplication) { Some(sel!(hideOtherApplications:)), Some(KeyEquivalent { key: ns_string!("h"), - masks: Some(NSEventModifierFlags( - NSEventModifierFlags::NSEventModifierFlagOption.0 - | NSEventModifierFlags::NSEventModifierFlagCommand.0, - )), + masks: Some( + NSEventModifierFlags::NSEventModifierFlagOption + | NSEventModifierFlags::NSEventModifierFlagCommand, + ), }), ); @@ -91,7 +91,7 @@ fn menu_item( title: &NSString, selector: Option, key_equivalent: Option>, -) -> Id { +) -> Retained { let (key, masks) = match key_equivalent { Some(ke) => (ke.key, ke.masks), None => (ns_string!(""), None), diff --git a/src/platform_impl/macos/monitor.rs b/src/platform_impl/macos/monitor.rs index 6229e507..e78d84f0 100644 --- a/src/platform_impl/macos/monitor.rs +++ b/src/platform_impl/macos/monitor.rs @@ -9,7 +9,7 @@ use core_foundation::string::CFString; use core_graphics::display::{ CGDirectDisplayID, CGDisplay, CGDisplayBounds, CGDisplayCopyDisplayMode, }; -use objc2::rc::Id; +use objc2::rc::Retained; use objc2::runtime::AnyObject; use objc2_app_kit::NSScreen; use objc2_foundation::{ns_string, run_on_main, MainThreadMarker, NSNumber, NSPoint, NSRect}; @@ -295,7 +295,7 @@ impl MonitorHandle { } } - pub(crate) fn ns_screen(&self, mtm: MainThreadMarker) -> Option> { + pub(crate) fn ns_screen(&self, mtm: MainThreadMarker) -> Option> { let uuid = unsafe { ffi::CGDisplayCreateUUIDFromDisplayID(self.0) }; NSScreen::screens(mtm).into_iter().find(|screen| { let other_native_id = get_display_id(screen); diff --git a/src/platform_impl/macos/view.rs b/src/platform_impl/macos/view.rs index 19d8ea53..f6aa85c6 100644 --- a/src/platform_impl/macos/view.rs +++ b/src/platform_impl/macos/view.rs @@ -3,7 +3,7 @@ use std::cell::{Cell, RefCell}; use std::collections::{HashMap, VecDeque}; use std::ptr; -use objc2::rc::{Id, WeakId}; +use objc2::rc::{Retained, WeakId}; use objc2::runtime::{AnyObject, Sel}; use objc2::{declare_class, msg_send_id, mutability, sel, ClassType, DeclaredClass}; use objc2_app_kit::{ @@ -35,7 +35,7 @@ use crate::platform::macos::OptionAsAlt; #[derive(Debug)] struct CursorState { visible: bool, - cursor: Id, + cursor: Retained, } impl Default for CursorState { @@ -111,7 +111,7 @@ fn get_left_modifier_code(key: &Key) -> KeyCode { #[derive(Debug)] pub struct ViewState { /// Strong reference to the global application state. - app_delegate: Id, + app_delegate: Retained, cursor_state: RefCell, ime_position: Cell, @@ -131,7 +131,7 @@ pub struct ViewState { /// to the application, even during IME forward_key_to_app: Cell, - marked_text: RefCell>, + marked_text: RefCell>, accepts_first_mouse: bool, // Weak reference because the window keeps a strong reference to the view @@ -221,7 +221,7 @@ declare_class!( // IMKInputSession [0x7fc573576ff0 presentFunctionRowItemTextInputViewWithEndpoint:completionHandler:] : [self textInputContext]=0x7fc573558e10 *NO* NSRemoteViewController to client, NSError=Error Domain=NSCocoaErrorDomain Code=4099 "The connection from pid 0 was invalidated from this process." UserInfo={NSDebugDescription=The connection from pid 0 was invalidated from this process.}, com.apple.inputmethod.EmojiFunctionRowItem // TODO: Add an API extension for using `NSTouchBar` #[method_id(touchBar)] - fn touch_bar(&self) -> Option> { + fn touch_bar(&self) -> Option> { trace_scope!("touchBar"); None } @@ -340,7 +340,7 @@ declare_class!( } #[method_id(validAttributesForMarkedText)] - fn valid_attributes_for_marked_text(&self) -> Id> { + fn valid_attributes_for_marked_text(&self) -> Retained> { trace_scope!("validAttributesForMarkedText"); NSArray::new() } @@ -350,7 +350,7 @@ declare_class!( &self, _range: NSRange, _actual_range: *mut NSRange, - ) -> Option> { + ) -> Option> { trace_scope!("attributedSubstringForProposedRange:actualRange:"); None } @@ -776,7 +776,7 @@ impl WinitView { window: &WinitWindow, accepts_first_mouse: bool, option_as_alt: OptionAsAlt, - ) -> Id { + ) -> Retained { let mtm = MainThreadMarker::from(window); let this = mtm.alloc().set_ivars(ViewState { app_delegate: app_delegate.retain(), @@ -795,7 +795,7 @@ impl WinitView { _ns_window: WeakId::new(&window.retain()), option_as_alt: Cell::new(option_as_alt), }); - let this: Id = unsafe { msg_send_id![super(this), init] }; + let this: Retained = unsafe { msg_send_id![super(this), init] }; this.setPostsFrameChangedNotifications(true); let notification_center = unsafe { NSNotificationCenter::defaultCenter() }; @@ -813,7 +813,7 @@ impl WinitView { this } - fn window(&self) -> Id { + fn window(&self) -> Retained { // TODO: Simply use `window` property on `NSView`. // That only returns a window _after_ the view has been attached though! // (which is incompatible with `frameDidChange:`) @@ -846,11 +846,11 @@ impl WinitView { .unwrap_or_default() } - pub(super) fn cursor_icon(&self) -> Id { + pub(super) fn cursor_icon(&self) -> Retained { self.ivars().cursor_state.borrow().cursor.clone() } - pub(super) fn set_cursor_icon(&self, icon: Id) { + pub(super) fn set_cursor_icon(&self, icon: Retained) { let mut cursor_state = self.ivars().cursor_state.borrow_mut(); cursor_state.cursor = icon; } @@ -1083,7 +1083,7 @@ fn mouse_button(event: &NSEvent) -> MouseButton { // NOTE: to get option as alt working we need to rewrite events // we're getting from the operating system, which makes it // impossible to provide such events as extra in `KeyEvent`. -fn replace_event(event: &NSEvent, option_as_alt: OptionAsAlt) -> Id { +fn replace_event(event: &NSEvent, option_as_alt: OptionAsAlt) -> Retained { let ev_mods = event_mods(event).state; let ignore_alt_characters = match option_as_alt { OptionAsAlt::OnlyLeft if lalt_pressed(event) => true, diff --git a/src/platform_impl/macos/window.rs b/src/platform_impl/macos/window.rs index d19ea917..eb1f75de 100644 --- a/src/platform_impl/macos/window.rs +++ b/src/platform_impl/macos/window.rs @@ -1,6 +1,6 @@ #![allow(clippy::unnecessary_cast)] -use objc2::rc::{autoreleasepool, Id}; +use objc2::rc::{autoreleasepool, Retained}; use objc2::{declare_class, mutability, ClassType, DeclaredClass}; use objc2_app_kit::{NSResponder, NSWindow}; use objc2_foundation::{MainThreadBound, MainThreadMarker, NSObject}; @@ -11,9 +11,9 @@ use crate::error::OsError as RootOsError; use crate::window::WindowAttributes; pub(crate) struct Window { - window: MainThreadBound>, + window: MainThreadBound>, /// The window only keeps a weak reference to this, so we must keep it around here. - delegate: MainThreadBound>, + delegate: MainThreadBound>, } impl Drop for Window { diff --git a/src/platform_impl/macos/window_delegate.rs b/src/platform_impl/macos/window_delegate.rs index 02856107..3909b98c 100644 --- a/src/platform_impl/macos/window_delegate.rs +++ b/src/platform_impl/macos/window_delegate.rs @@ -4,7 +4,7 @@ use std::collections::VecDeque; use core_graphics::display::{CGDisplay, CGPoint}; use monitor::VideoModeHandle; -use objc2::rc::{autoreleasepool, Id}; +use objc2::rc::{autoreleasepool, Retained}; use objc2::runtime::{AnyObject, ProtocolObject}; use objc2::{declare_class, msg_send_id, mutability, sel, ClassType, DeclaredClass}; use objc2_app_kit::{ @@ -73,9 +73,9 @@ impl Default for PlatformSpecificWindowAttributes { #[derive(Debug)] pub(crate) struct State { /// Strong reference to the global application state. - app_delegate: Id, + app_delegate: Retained, - window: Id, + window: Retained, current_theme: Cell>, @@ -231,7 +231,7 @@ declare_class!( None => { let current_monitor = self.current_monitor_inner(); *fullscreen = Some(Fullscreen::Borderless(current_monitor)); - } + }, } self.ivars().in_fullscreen_transition.set(true); } @@ -262,11 +262,9 @@ declare_class!( let mut options = proposed_options; let fullscreen = self.ivars().fullscreen.borrow(); if let Some(Fullscreen::Exclusive(_)) = &*fullscreen { - options = NSApplicationPresentationOptions( - NSApplicationPresentationOptions::NSApplicationPresentationFullScreen.0 - | NSApplicationPresentationOptions::NSApplicationPresentationHideDock.0 - | NSApplicationPresentationOptions::NSApplicationPresentationHideMenuBar.0, - ); + options = NSApplicationPresentationOptions::NSApplicationPresentationFullScreen + | NSApplicationPresentationOptions::NSApplicationPresentationHideDock + | NSApplicationPresentationOptions::NSApplicationPresentationHideMenuBar; } options @@ -333,8 +331,7 @@ declare_class!( #[method(windowDidChangeOcclusionState:)] fn window_did_change_occlusion_state(&self, _: Option<&AnyObject>) { trace_scope!("windowDidChangeOcclusionState:"); - let visible = self.window().occlusionState().0 & NSWindowOcclusionState::Visible.0 - == NSWindowOcclusionState::Visible.0; + let visible = self.window().occlusionState().contains(NSWindowOcclusionState::Visible); self.queue_event(WindowEvent::Occluded(!visible)); } @@ -358,11 +355,9 @@ declare_class!( use std::path::PathBuf; - let pb: Id = unsafe { msg_send_id![sender, draggingPasteboard] }; - let filenames = pb - .propertyListForType(unsafe { NSFilenamesPboardType }) - .unwrap(); - let filenames: Id> = unsafe { Id::cast(filenames) }; + let pb: Retained = unsafe { msg_send_id![sender, draggingPasteboard] }; + let filenames = pb.propertyListForType(unsafe { NSFilenamesPboardType }).unwrap(); + let filenames: Retained> = unsafe { Retained::cast(filenames) }; filenames.into_iter().for_each(|file| { let path = PathBuf::from(file.to_string()); @@ -386,11 +381,9 @@ declare_class!( use std::path::PathBuf; - let pb: Id = unsafe { msg_send_id![sender, draggingPasteboard] }; - let filenames = pb - .propertyListForType(unsafe { NSFilenamesPboardType }) - .unwrap(); - let filenames: Id> = unsafe { Id::cast(filenames) }; + let pb: Retained = unsafe { msg_send_id![sender, draggingPasteboard] }; + let filenames = pb.propertyListForType(unsafe { NSFilenamesPboardType }).unwrap(); + let filenames: Retained> = unsafe { Retained::cast(filenames) }; filenames.into_iter().for_each(|file| { let path = PathBuf::from(file.to_string()); @@ -444,7 +437,7 @@ fn new_window( app_delegate: &ApplicationDelegate, attrs: &WindowAttributes, mtm: MainThreadMarker, -) -> Option> { +) -> Option> { autoreleasepool(|_| { let screen = match attrs.fullscreen.clone().map(Into::into) { Some(Fullscreen::Borderless(Some(monitor))) @@ -489,38 +482,38 @@ fn new_window( // if decorations is set to false, ignore pl_attrs // // if the titlebar is hidden, ignore other pl_attrs - NSWindowStyleMask::Borderless.0 - | NSWindowStyleMask::Resizable.0 - | NSWindowStyleMask::Miniaturizable.0 + NSWindowStyleMask::Borderless + | NSWindowStyleMask::Resizable + | NSWindowStyleMask::Miniaturizable } else { // default case, resizable window with titlebar and titlebar buttons - NSWindowStyleMask::Closable.0 - | NSWindowStyleMask::Miniaturizable.0 - | NSWindowStyleMask::Resizable.0 - | NSWindowStyleMask::Titled.0 + NSWindowStyleMask::Closable + | NSWindowStyleMask::Miniaturizable + | NSWindowStyleMask::Resizable + | NSWindowStyleMask::Titled }; if !attrs.resizable { - masks &= !NSWindowStyleMask::Resizable.0; + masks &= !NSWindowStyleMask::Resizable; } if !attrs.enabled_buttons.contains(WindowButtons::MINIMIZE) { - masks &= !NSWindowStyleMask::Miniaturizable.0; + masks &= !NSWindowStyleMask::Miniaturizable; } if !attrs.enabled_buttons.contains(WindowButtons::CLOSE) { - masks &= !NSWindowStyleMask::Closable.0; + masks &= !NSWindowStyleMask::Closable; } if attrs.platform_specific.fullsize_content_view { - masks |= NSWindowStyleMask::FullSizeContentView.0; + masks |= NSWindowStyleMask::FullSizeContentView; } - let window: Option> = unsafe { + let window: Option> = unsafe { msg_send_id![ super(mtm.alloc().set_ivars(())), initWithContentRect: frame, - styleMask: NSWindowStyleMask(masks), + styleMask: masks, backing: NSBackingStoreType::NSBackingStoreBuffered, defer: false, ] @@ -625,7 +618,7 @@ impl WindowDelegate { app_delegate: &ApplicationDelegate, attrs: WindowAttributes, mtm: MainThreadMarker, - ) -> Result, RootOsError> { + ) -> Result, RootOsError> { let window = new_window(app_delegate, &attrs, mtm) .ok_or_else(|| os_error!(OsError::CreationError("couldn't create `NSWindow`")))?; @@ -634,8 +627,8 @@ impl WindowDelegate { Some(rwh_06::RawWindowHandle::AppKit(handle)) => { // SAFETY: Caller ensures the pointer is valid or NULL // Unwrap is fine, since the pointer comes from `NonNull`. - let parent_view: Id = - unsafe { Id::retain(handle.ns_view.as_ptr().cast()) }.unwrap(); + let parent_view: Retained = + unsafe { Retained::retain(handle.ns_view.as_ptr().cast()) }.unwrap(); let parent = parent_view.window().ok_or_else(|| { os_error!(OsError::CreationError("parent view should be installed in a window")) })?; @@ -685,7 +678,7 @@ impl WindowDelegate { is_simple_fullscreen: Cell::new(false), saved_style: Cell::new(None), }); - let delegate: Id = unsafe { msg_send_id![super(delegate), init] }; + let delegate: Retained = unsafe { msg_send_id![super(delegate), init] }; if scale_factor != 1.0 { delegate.queue_static_scale_factor_changed_event(); @@ -745,9 +738,9 @@ impl WindowDelegate { } #[track_caller] - pub(super) fn view(&self) -> Id { + pub(super) fn view(&self) -> Retained { // SAFETY: The view inside WinitWindow is always `WinitView` - unsafe { Id::cast(self.window().contentView().unwrap()) } + unsafe { Retained::cast(self.window().contentView().unwrap()) } } #[track_caller] @@ -960,13 +953,13 @@ impl WindowDelegate { self.ivars().resizable.set(resizable); let fullscreen = self.ivars().fullscreen.borrow().is_some(); if !fullscreen { - let mut mask = self.window().styleMask().0; + let mut mask = self.window().styleMask(); if resizable { - mask |= NSWindowStyleMask::Resizable.0; + mask |= NSWindowStyleMask::Resizable; } else { - mask &= !NSWindowStyleMask::Resizable.0; + mask &= !NSWindowStyleMask::Resizable; } - self.set_style_mask(NSWindowStyleMask(mask)); + self.set_style_mask(mask); } // Otherwise, we don't change the mask until we exit fullscreen. } @@ -978,23 +971,23 @@ impl WindowDelegate { #[inline] pub fn set_enabled_buttons(&self, buttons: WindowButtons) { - let mut mask = self.window().styleMask().0; + let mut mask = self.window().styleMask(); if buttons.contains(WindowButtons::CLOSE) { - mask |= NSWindowStyleMask::Closable.0; + mask |= NSWindowStyleMask::Closable; } else { - mask &= !NSWindowStyleMask::Closable.0; + mask &= !NSWindowStyleMask::Closable; } if buttons.contains(WindowButtons::MINIMIZE) { - mask |= NSWindowStyleMask::Miniaturizable.0; + mask |= NSWindowStyleMask::Miniaturizable; } else { - mask &= !NSWindowStyleMask::Miniaturizable.0; + mask &= !NSWindowStyleMask::Miniaturizable; } // This must happen before the button's "enabled" status has been set, // hence we do it synchronously. - self.set_style_mask(NSWindowStyleMask(mask)); + self.set_style_mask(mask); // We edit the button directly instead of using `NSResizableWindowMask`, // since that mask also affect the resizability of the window (which is @@ -1115,9 +1108,8 @@ impl WindowDelegate { // we make it resizable temporarily. let curr_mask = self.window().styleMask(); - let required = - NSWindowStyleMask(NSWindowStyleMask::Titled.0 | NSWindowStyleMask::Resizable.0); - let needs_temp_mask = !mask_contains(curr_mask, required); + let required = NSWindowStyleMask::Titled | NSWindowStyleMask::Resizable; + let needs_temp_mask = !curr_mask.contains(required); if needs_temp_mask { self.set_style_mask(required); } @@ -1134,12 +1126,12 @@ impl WindowDelegate { fn saved_style(&self) -> NSWindowStyleMask { let base_mask = - self.ivars().saved_style.take().unwrap_or_else(|| self.window().styleMask()).0; - NSWindowStyleMask(if self.ivars().resizable.get() { - base_mask | NSWindowStyleMask::Resizable.0 + self.ivars().saved_style.take().unwrap_or_else(|| self.window().styleMask()); + if self.ivars().resizable.get() { + base_mask | NSWindowStyleMask::Resizable } else { - base_mask & !NSWindowStyleMask::Resizable.0 - }) + base_mask & !NSWindowStyleMask::Resizable + } } /// This is called when the window is exiting fullscreen, whether by the @@ -1194,7 +1186,7 @@ impl WindowDelegate { return; } - if mask_contains(self.window().styleMask(), NSWindowStyleMask::Resizable) { + if self.window().styleMask().contains(NSWindowStyleMask::Resizable) { // Just use the native zoom if resizable self.window().zoom(None); } else { @@ -1346,9 +1338,8 @@ impl WindowDelegate { // set a normal style temporarily. The previous state will be // restored in `WindowDelegate::window_did_exit_fullscreen`. let curr_mask = self.window().styleMask(); - let required = - NSWindowStyleMask(NSWindowStyleMask::Titled.0 | NSWindowStyleMask::Resizable.0); - if !mask_contains(curr_mask, required) { + let required = NSWindowStyleMask::Titled | NSWindowStyleMask::Resizable; + if !curr_mask.contains(required) { self.set_style_mask(required); self.ivars().saved_style.set(Some(curr_mask)); } @@ -1379,11 +1370,10 @@ impl WindowDelegate { // delegate in `window:willUseFullScreenPresentationOptions:`. self.ivars().save_presentation_opts.set(Some(app.presentationOptions())); - let presentation_options = NSApplicationPresentationOptions( - NSApplicationPresentationOptions::NSApplicationPresentationFullScreen.0 - | NSApplicationPresentationOptions::NSApplicationPresentationHideDock.0 - | NSApplicationPresentationOptions::NSApplicationPresentationHideMenuBar.0, - ); + let presentation_options = + NSApplicationPresentationOptions::NSApplicationPresentationFullScreen + | NSApplicationPresentationOptions::NSApplicationPresentationHideDock + | NSApplicationPresentationOptions::NSApplicationPresentationHideMenuBar; app.setPresentationOptions(presentation_options); let window_level = unsafe { ffi::CGShieldingWindowLevel() } as NSWindowLevel + 1; @@ -1391,9 +1381,9 @@ impl WindowDelegate { }, (Some(Fullscreen::Exclusive(ref video_mode)), Some(Fullscreen::Borderless(_))) => { let presentation_options = self.ivars().save_presentation_opts.get().unwrap_or( - NSApplicationPresentationOptions(NSApplicationPresentationOptions::NSApplicationPresentationFullScreen.0 - | NSApplicationPresentationOptions::NSApplicationPresentationAutoHideDock.0 - | NSApplicationPresentationOptions::NSApplicationPresentationAutoHideMenuBar.0), + NSApplicationPresentationOptions::NSApplicationPresentationFullScreen + | NSApplicationPresentationOptions::NSApplicationPresentationAutoHideDock + | NSApplicationPresentationOptions::NSApplicationPresentationAutoHideMenuBar ); app.setPresentationOptions(presentation_options); @@ -1432,19 +1422,19 @@ impl WindowDelegate { let new_mask = { let mut new_mask = if decorations { - NSWindowStyleMask::Closable.0 - | NSWindowStyleMask::Miniaturizable.0 - | NSWindowStyleMask::Resizable.0 - | NSWindowStyleMask::Titled.0 + NSWindowStyleMask::Closable + | NSWindowStyleMask::Miniaturizable + | NSWindowStyleMask::Resizable + | NSWindowStyleMask::Titled } else { - NSWindowStyleMask::Borderless.0 | NSWindowStyleMask::Resizable.0 + NSWindowStyleMask::Borderless | NSWindowStyleMask::Resizable }; if !resizable { - new_mask &= !NSWindowStyleMask::Resizable.0; + new_mask &= !NSWindowStyleMask::Resizable; } new_mask }; - self.set_style_mask(NSWindowStyleMask(new_mask)); + self.set_style_mask(new_mask); } #[inline] @@ -1547,7 +1537,7 @@ impl WindowDelegate { pub fn raw_window_handle_rwh_04(&self) -> rwh_04::RawWindowHandle { let mut window_handle = rwh_04::AppKitHandle::empty(); window_handle.ns_window = self.window() as *const WinitWindow as *mut _; - window_handle.ns_view = Id::as_ptr(&self.contentView().unwrap()) as *mut _; + window_handle.ns_view = Retained::as_ptr(&self.contentView().unwrap()) as *mut _; rwh_04::RawWindowHandle::AppKit(window_handle) } @@ -1556,7 +1546,7 @@ impl WindowDelegate { pub fn raw_window_handle_rwh_05(&self) -> rwh_05::RawWindowHandle { let mut window_handle = rwh_05::AppKitWindowHandle::empty(); window_handle.ns_window = self.window() as *const WinitWindow as *mut _; - window_handle.ns_view = Id::as_ptr(&self.view()) as *mut _; + window_handle.ns_view = Retained::as_ptr(&self.view()) as *mut _; rwh_05::RawWindowHandle::AppKit(window_handle) } @@ -1570,8 +1560,8 @@ impl WindowDelegate { #[inline] pub fn raw_window_handle_rwh_06(&self) -> rwh_06::RawWindowHandle { let window_handle = rwh_06::AppKitWindowHandle::new({ - let ptr = Id::as_ptr(&self.view()) as *mut _; - std::ptr::NonNull::new(ptr).expect("Id should never be null") + let ptr = Retained::as_ptr(&self.view()) as *mut _; + std::ptr::NonNull::new(ptr).expect("Retained should never be null") }); rwh_06::RawWindowHandle::AppKit(window_handle) } @@ -1579,9 +1569,9 @@ impl WindowDelegate { fn toggle_style_mask(&self, mask: NSWindowStyleMask, on: bool) { let current_style_mask = self.window().styleMask(); if on { - self.set_style_mask(NSWindowStyleMask(current_style_mask.0 | mask.0)); + self.set_style_mask(current_style_mask | mask); } else { - self.set_style_mask(NSWindowStyleMask(current_style_mask.0 & (!mask.0))); + self.set_style_mask(current_style_mask & !mask); } } @@ -1654,10 +1644,9 @@ impl WindowExtMacOS for WindowDelegate { self.ivars().is_simple_fullscreen.set(true); // Simulate pre-Lion fullscreen by hiding the dock and menu bar - let presentation_options = NSApplicationPresentationOptions( - NSApplicationPresentationOptions::NSApplicationPresentationAutoHideDock.0 - | NSApplicationPresentationOptions::NSApplicationPresentationAutoHideMenuBar.0, - ); + let presentation_options = + NSApplicationPresentationOptions::NSApplicationPresentationAutoHideDock + | NSApplicationPresentationOptions::NSApplicationPresentationAutoHideMenuBar; app.setPresentationOptions(presentation_options); // Hide the titlebar @@ -1755,10 +1744,6 @@ impl WindowExtMacOS for WindowDelegate { } } -fn mask_contains(mask: NSWindowStyleMask, value: NSWindowStyleMask) -> bool { - mask.0 & value.0 == value.0 -} - const DEFAULT_STANDARD_FRAME: NSRect = NSRect::new(NSPoint::new(50.0, 50.0), NSSize::new(800.0, 600.0));