Skip to content

Commit 5e5dbed

Browse files
authoredFeb 22, 2024··
Improve documentation for UIKit and AppKit handles (#160)
* Document that Apple handles are main thread only * Add example usage to AppKitWindowHandle and UiKitWindowHandle
1 parent 4295645 commit 5e5dbed

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
 

‎src/appkit.rs

+39
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,45 @@ impl DisplayHandle<'static> {
4444
}
4545

4646
/// Raw window handle for AppKit.
47+
///
48+
/// Note that `NSView` 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(target_os = "macos")]
59+
/// # #[cfg(requires_objc2)]
60+
/// use icrate::AppKit::NSView;
61+
/// # #[cfg(requires_objc2)]
62+
/// use icrate::Foundation::is_main_thread;
63+
/// # #[cfg(requires_objc2)]
64+
/// use objc2::rc::Id;
65+
/// use raw_window_handle::{WindowHandle, RawWindowHandle};
66+
///
67+
/// let handle: WindowHandle<'_>; // Get the window handle from somewhere else
68+
/// # handle = unimplemented!();
69+
/// match handle.as_raw() {
70+
/// # #[cfg(requires_objc2)]
71+
/// RawWindowHandle::AppKit(handle) => {
72+
/// assert!(is_main_thread(), "can only access AppKit handles on the main thread");
73+
/// let ns_view = handle.ns_view.as_ptr();
74+
/// // SAFETY: The pointer came from `WindowHandle`, which ensures
75+
/// // that the `AppKitWindowHandle` contains a valid pointer to an
76+
/// // `NSView`.
77+
/// // Unwrap is fine, since the pointer came from `NonNull`.
78+
/// let ns_view: Id<NSView> = unsafe { Id::retain(ns_view.cast()) }.unwrap();
79+
/// // Do something with the NSView here, like getting the `NSWindow`
80+
/// let ns_window = ns_view.window().expect("view was not installed in a window");
81+
/// }
82+
/// handle => unreachable!("unknown handle {handle:?} for platform"),
83+
/// }
84+
/// # }
85+
/// ```
4786
#[non_exhaustive]
4887
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4988
pub struct AppKitWindowHandle {

‎src/uikit.rs

+39
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,45 @@ impl DisplayHandle<'static> {
4444
}
4545

4646
/// 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+
/// ```
4786
#[non_exhaustive]
4887
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4988
pub struct UiKitWindowHandle {

0 commit comments

Comments
 (0)
Please sign in to comment.