From 6d0cf6a27513a32ac3d6dcb4d0cfe4f63c1c3f29 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 23 Nov 2022 15:53:06 +0100 Subject: [PATCH] Remove WindowBuilderExtIOS::with_root_view_class (#2459) --- CHANGELOG.md | 1 + FEATURES.md | 1 - src/platform/ios.rs | 13 ------------- src/platform_impl/ios/view.rs | 30 ++++++++---------------------- src/platform_impl/ios/window.rs | 16 +--------------- 5 files changed, 10 insertions(+), 51 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32726d03..1e0d44c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ And please only add new entries to the top of this list, right below the `# Unre # Unreleased +- **Breaking:** Removed `WindowBuilderExtIOS::with_root_view_class`; instead, you should use `[[view layer] addSublayer: ...]` to add an instance of the desired layer class (e.g. `CAEAGLLayer` or `CAMetalLayer`). See `vulkano-win` or `wgpu` for examples of this. - On MacOS and Windows, add `Window::set_content_protected`. - On MacOS, add `EventLoopBuilderExtMacOS::with_activate_ignoring_other_apps`. - On Windows, fix icons specified on `WindowBuilder` not taking effect for windows created after the first one. diff --git a/FEATURES.md b/FEATURES.md index 23356189..313d58b7 100644 --- a/FEATURES.md +++ b/FEATURES.md @@ -151,7 +151,6 @@ If your PR makes notable changes to Winit's features, please update this section * Home indicator visibility * Status bar visibility * Deferrring system gestures -* Support for custom `UIView` derived class * Getting the device idiom * Getting the preferred video mode diff --git a/src/platform/ios.rs b/src/platform/ios.rs index ca572fc2..59d1ffba 100644 --- a/src/platform/ios.rs +++ b/src/platform/ios.rs @@ -139,13 +139,6 @@ impl WindowExtIOS for Window { /// Additional methods on [`WindowBuilder`] that are specific to iOS. pub trait WindowBuilderExtIOS { - /// Sets the root view class used by the [`Window`], otherwise a barebones [`UIView`] is provided. - /// - /// An instance of the class will be initialized by calling [`-[UIView initWithFrame:]`](https://developer.apple.com/documentation/uikit/uiview/1622488-initwithframe?language=objc). - /// - /// [`UIView`]: https://developer.apple.com/documentation/uikit/uiview?language=objc - fn with_root_view_class(self, root_view_class: *const c_void) -> WindowBuilder; - /// Sets the [`contentScaleFactor`] of the underlying [`UIWindow`] to `scale_factor`. /// /// The default value is device dependent, and it's recommended GLES or Metal applications set @@ -195,12 +188,6 @@ pub trait WindowBuilderExtIOS { } impl WindowBuilderExtIOS for WindowBuilder { - #[inline] - fn with_root_view_class(mut self, root_view_class: *const c_void) -> WindowBuilder { - self.platform_specific.root_view_class = unsafe { &*(root_view_class as *const _) }; - self - } - #[inline] fn with_scale_factor(mut self, scale_factor: f64) -> WindowBuilder { self.platform_specific.scale_factor = Some(scale_factor); diff --git a/src/platform_impl/ios/view.rs b/src/platform_impl/ios/view.rs index 21ab4f22..05c87e01 100644 --- a/src/platform_impl/ios/view.rs +++ b/src/platform_impl/ios/view.rs @@ -1,5 +1,3 @@ -use std::collections::HashMap; - use objc2::declare::ClassBuilder; use objc2::foundation::NSObject; use objc2::runtime::{Bool, Class, Object, Sel}; @@ -23,21 +21,9 @@ use crate::{ }; // requires main thread -unsafe fn get_view_class(root_view_class: &'static Class) -> &'static Class { - static mut CLASSES: Option> = None; - static mut ID: usize = 0; - - if CLASSES.is_none() { - CLASSES = Some(HashMap::default()); - } - - let classes = CLASSES.as_mut().unwrap(); - - classes.entry(root_view_class).or_insert_with(move || { - let uiview_class = class!(UIView); - let is_uiview: bool = msg_send![root_view_class, isSubclassOfClass: uiview_class]; - assert!(is_uiview, "`root_view_class` must inherit from `UIView`"); - +unsafe fn get_view_class() -> &'static Class { + static mut CLASS: Option<&'static Class> = None; + if CLASS.is_none() { extern "C" fn draw_rect(object: &Object, _: Sel, rect: CGRect) { unsafe { let window: id = msg_send![object, window]; @@ -223,9 +209,8 @@ unsafe fn get_view_class(root_view_class: &'static Class) -> &'static Class { } } - let mut decl = ClassBuilder::new(&format!("WinitUIView{}", ID), root_view_class) + let mut decl = ClassBuilder::new("WinitUIView", class!(UIView)) .expect("Failed to declare class `WinitUIView`"); - ID += 1; decl.add_method(sel!(drawRect:), draw_rect as extern "C" fn(_, _, _)); decl.add_method(sel!(layoutSubviews), layout_subviews as extern "C" fn(_, _)); decl.add_method( @@ -250,8 +235,9 @@ unsafe fn get_view_class(root_view_class: &'static Class) -> &'static Class { handle_touches as extern "C" fn(_, _, _, _), ); - decl.register() - }) + CLASS = Some(decl.register()); + } + CLASS.unwrap() } declare_class!( @@ -382,7 +368,7 @@ pub(crate) unsafe fn create_view( platform_attributes: &PlatformSpecificWindowBuilderAttributes, frame: CGRect, ) -> id { - let class = get_view_class(platform_attributes.root_view_class); + let class = get_view_class(); let view: id = msg_send![class, alloc]; assert!(!view.is_null(), "Failed to create `UIView` instance"); diff --git a/src/platform_impl/ios/window.rs b/src/platform_impl/ios/window.rs index ba2e3a3b..670ac899 100644 --- a/src/platform_impl/ios/window.rs +++ b/src/platform_impl/ios/window.rs @@ -688,25 +688,11 @@ impl From for WindowId { } } -#[derive(Clone)] +#[derive(Clone, Default)] pub struct PlatformSpecificWindowBuilderAttributes { - pub root_view_class: &'static Class, pub scale_factor: Option, pub valid_orientations: ValidOrientations, pub prefers_home_indicator_hidden: bool, pub prefers_status_bar_hidden: bool, pub preferred_screen_edges_deferring_system_gestures: ScreenEdge, } - -impl Default for PlatformSpecificWindowBuilderAttributes { - fn default() -> PlatformSpecificWindowBuilderAttributes { - PlatformSpecificWindowBuilderAttributes { - root_view_class: class!(UIView), - scale_factor: None, - valid_orientations: Default::default(), - prefers_home_indicator_hidden: false, - prefers_status_bar_hidden: false, - preferred_screen_edges_deferring_system_gestures: Default::default(), - } - } -}