@@ -44,6 +44,45 @@ impl DisplayHandle<'static> {
44
44
}
45
45
46
46
/// Raw window handle for UIKit.
47
+ ///
48
+ /// Note that `UIView` can only be accessed from the main thread of the
49
+ /// application. This struct is `!Send` and `!Sync` to help with ensuring
50
+ /// that.
51
+ ///
52
+ /// # Example
53
+ ///
54
+ /// Getting the view from a [`WindowHandle`][crate::WindowHandle].
55
+ ///
56
+ /// ```no_run
57
+ /// # fn inner() {
58
+ /// #![cfg(any(target_os = "ios", target_os = "tvos", target_os = "watchos", target_os = "xros"))]
59
+ /// # #[cfg(requires_objc2)]
60
+ /// use icrate::Foundation::is_main_thread;
61
+ /// # #[cfg(requires_objc2)]
62
+ /// use objc2::rc::Id;
63
+ /// // TODO: Use `icrate::UIKit::UIView` when available
64
+ /// # #[cfg(requires_objc2)]
65
+ /// use objc2::runtime::NSObject;
66
+ /// use raw_window_handle::{WindowHandle, RawWindowHandle};
67
+ ///
68
+ /// let handle: WindowHandle<'_>; // Get the window handle from somewhere else
69
+ /// # handle = unimplemented!();
70
+ /// match handle.as_raw() {
71
+ /// # #[cfg(requires_objc2)]
72
+ /// RawWindowHandle::UIKit(handle) => {
73
+ /// assert!(is_main_thread(), "can only access UIKit handles on the main thread");
74
+ /// let ui_view = handle.ui_view.as_ptr();
75
+ /// // SAFETY: The pointer came from `WindowHandle`, which ensures
76
+ /// // that the `UiKitWindowHandle` contains a valid pointer to an
77
+ /// // `UIView`.
78
+ /// // Unwrap is fine, since the pointer came from `NonNull`.
79
+ /// let ui_view: Id<NSObject> = unsafe { Id::retain(ui_view.cast()) }.unwrap();
80
+ /// // Do something with the UIView here.
81
+ /// }
82
+ /// handle => unreachable!("unknown handle {handle:?} for platform"),
83
+ /// }
84
+ /// # }
85
+ /// ```
47
86
#[ non_exhaustive]
48
87
#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
49
88
pub struct UiKitWindowHandle {
0 commit comments