Skip to content

Commit 29ff5aa

Browse files
authored
docs: Improve documentation on AppKit and UIKit variants
* Update availability hints to include visionOS. * Use objc2 v0.6 in examples. * Add more detailed examples of how to create UIKit and AppKit handles.
1 parent 5fda8e8 commit 29ff5aa

File tree

4 files changed

+51
-40
lines changed

4 files changed

+51
-40
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
* Improve documentation on AppKit and UIKit handles.
6+
37
## 0.6.2 (2024-05-17)
48

59
* Add OpenHarmony OS support (#164)

src/appkit.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,25 @@ impl DisplayHandle<'static> {
5757
/// # fn inner() {
5858
/// #![cfg(target_os = "macos")]
5959
/// # #[cfg(requires_objc2)]
60-
/// use objc2_app_kit::NSView;
60+
/// use objc2::MainThreadMarker;
6161
/// # #[cfg(requires_objc2)]
62-
/// use objc2_foundation::is_main_thread;
62+
/// use objc2::rc::Retained;
6363
/// # #[cfg(requires_objc2)]
64-
/// use objc2::rc::Id;
64+
/// use objc2_app_kit::NSView;
6565
/// use raw_window_handle::{WindowHandle, RawWindowHandle};
6666
///
6767
/// let handle: WindowHandle<'_>; // Get the window handle from somewhere else
6868
/// # handle = unimplemented!();
6969
/// match handle.as_raw() {
7070
/// # #[cfg(requires_objc2)]
7171
/// RawWindowHandle::AppKit(handle) => {
72-
/// assert!(is_main_thread(), "can only access AppKit handles on the main thread");
72+
/// assert!(MainThreadMarker::new().is_some(), "can only access AppKit handles on the main thread");
7373
/// let ns_view = handle.ns_view.as_ptr();
7474
/// // SAFETY: The pointer came from `WindowHandle`, which ensures
7575
/// // that the `AppKitWindowHandle` contains a valid pointer to an
7676
/// // `NSView`.
7777
/// // Unwrap is fine, since the pointer came from `NonNull`.
78-
/// let ns_view: Id<NSView> = unsafe { Id::retain(ns_view.cast()) }.unwrap();
78+
/// let ns_view: Retained<NSView> = unsafe { Retained::retain(ns_view.cast()) }.unwrap();
7979
/// // Do something with the NSView here, like getting the `NSWindow`
8080
/// let ns_window = ns_view.window().expect("view was not installed in a window");
8181
/// }
@@ -96,14 +96,18 @@ impl AppKitWindowHandle {
9696
///
9797
/// # Example
9898
///
99-
/// ```
100-
/// # use core::ptr::NonNull;
101-
/// # use raw_window_handle::AppKitWindowHandle;
102-
/// # type NSView = ();
103-
/// #
104-
/// let view: &NSView;
105-
/// # view = &();
106-
/// let handle = AppKitWindowHandle::new(NonNull::from(view).cast());
99+
/// Create a handle from the content view of a `NSWindow`.
100+
///
101+
/// ```ignore
102+
/// use std::ptr::NonNull;
103+
/// use objc2::rc::Retained;
104+
/// use objc2_app_kit::{NSWindow, NSView};
105+
/// use raw_window_handle::AppKitWindowHandle;
106+
///
107+
/// let ns_window: Retained<NSWindow> = ...;
108+
/// let ns_view: Retained<NSView> = window.contentView();
109+
/// let ns_view: NonNull<NSView> = NonNull::from(&*ns_view);
110+
/// let handle = AppKitWindowHandle::new(ns_view.cast());
107111
/// ```
108112
pub fn new(ns_view: NonNull<c_void>) -> Self {
109113
Self { ns_view }

src/lib.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,17 @@ pub enum RawWindowHandle {
114114
/// A raw window handle for UIKit (Apple's non-macOS windowing library).
115115
///
116116
/// ## Availability Hints
117-
/// This variant is likely to be used on iOS, tvOS, (in theory) watchOS, and
118-
/// Mac Catalyst (`$arch-apple-ios-macabi` targets, which can notably use
119-
/// UIKit *or* AppKit), as these are the targets that (currently) support
120-
/// UIKit.
117+
/// This variant is used on iOS, tvOS, watchOS, visionOS, and Mac
118+
/// Catalyst, as these are the targets that (currently) support UIKit.
119+
///
120+
/// Note that Mac Catalyst (`$arch-apple-ios-macabi` targets), can use
121+
/// UIKit *or* AppKit.
121122
UiKit(UiKitWindowHandle),
122123
/// A raw window handle for AppKit.
123124
///
124125
/// ## Availability Hints
125-
/// This variant is likely to be used on macOS, although Mac Catalyst
126-
/// (`$arch-apple-ios-macabi` targets, which can notably use UIKit *or*
127-
/// AppKit) can also use it despite being `target_os = "ios"`.
126+
/// This variant is used on macOS, although Mac Catalyst can also use it
127+
/// despite being `target_os = "ios"`.
128128
AppKit(AppKitWindowHandle),
129129
/// A raw window handle for the Redox operating system.
130130
///
@@ -267,17 +267,17 @@ pub enum RawDisplayHandle {
267267
/// A raw display handle for UIKit (Apple's non-macOS windowing library).
268268
///
269269
/// ## Availability Hints
270-
/// This variant is likely to be used on iOS, tvOS, (in theory) watchOS, and
271-
/// Mac Catalyst (`$arch-apple-ios-macabi` targets, which can notably use
272-
/// UIKit *or* AppKit), as these are the targets that (currently) support
273-
/// UIKit.
270+
/// This variant is used on iOS, tvOS, watchOS, visionOS, and Mac
271+
/// Catalyst, as these are the targets that (currently) support UIKit.
272+
///
273+
/// Note that Mac Catalyst (`$arch-apple-ios-macabi` targets), can use
274+
/// UIKit *or* AppKit.
274275
UiKit(UiKitDisplayHandle),
275276
/// A raw display handle for AppKit.
276277
///
277278
/// ## Availability Hints
278-
/// This variant is likely to be used on macOS, although Mac Catalyst
279-
/// (`$arch-apple-ios-macabi` targets, which can notably use UIKit *or*
280-
/// AppKit) can also use it despite being `target_os = "ios"`.
279+
/// This variant is used on macOS, although Mac Catalyst can also use it
280+
/// despite being `target_os = "ios"`.
281281
AppKit(AppKitDisplayHandle),
282282
/// A raw display handle for the Redox operating system.
283283
///

src/uikit.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ impl DisplayHandle<'static> {
5757
/// # fn inner() {
5858
/// #![cfg(any(target_os = "ios", target_os = "tvos", target_os = "watchos", target_os = "xros"))]
5959
/// # #[cfg(requires_objc2)]
60-
/// use objc2_foundation::is_main_thread;
60+
/// use objc2::MainThreadMarker;
6161
/// # #[cfg(requires_objc2)]
62-
/// use objc2::rc::Id;
62+
/// use objc2::rc::Retained;
6363
/// # #[cfg(requires_objc2)]
6464
/// use objc2_ui_kit::UIView;
6565
/// use raw_window_handle::{WindowHandle, RawWindowHandle};
@@ -69,13 +69,13 @@ impl DisplayHandle<'static> {
6969
/// match handle.as_raw() {
7070
/// # #[cfg(requires_objc2)]
7171
/// RawWindowHandle::UIKit(handle) => {
72-
/// assert!(is_main_thread(), "can only access UIKit handles on the main thread");
72+
/// assert!(MainThreadMarker::new().is_some(), "can only access UIKit handles on the main thread");
7373
/// let ui_view = handle.ui_view.as_ptr();
7474
/// // SAFETY: The pointer came from `WindowHandle`, which ensures
7575
/// // that the `UiKitWindowHandle` contains a valid pointer to an
7676
/// // `UIView`.
7777
/// // Unwrap is fine, since the pointer came from `NonNull`.
78-
/// let ui_view: Id<UIView> = unsafe { Id::retain(ui_view.cast()) }.unwrap();
78+
/// let ui_view: Retained<UIView> = unsafe { Retained::retain(ui_view.cast()) }.unwrap();
7979
/// // Do something with the UIView here.
8080
/// }
8181
/// handle => unreachable!("unknown handle {handle:?} for platform"),
@@ -97,15 +97,18 @@ impl UiKitWindowHandle {
9797
///
9898
/// # Example
9999
///
100-
/// ```
101-
/// # use core::ptr::NonNull;
102-
/// # use raw_window_handle::UiKitWindowHandle;
103-
/// # type UIView = ();
104-
/// #
105-
/// let view: &UIView;
106-
/// # view = &();
107-
/// let mut handle = UiKitWindowHandle::new(NonNull::from(view).cast());
108-
/// // Optionally set the view controller.
100+
/// Create a handle from a `UIView`.
101+
///
102+
/// ```ignore
103+
/// use std::ptr::NonNull;
104+
/// use objc2::rc::Retained;
105+
/// use objc2_ui_kit::UIView;
106+
/// use raw_window_handle::UiKitWindowHandle;
107+
///
108+
/// let ui_view: Retained<UIView> = ...;
109+
/// let ui_view: NonNull<UIView> = NonNull::from(&*ui_view);
110+
/// let mut handle = UiKitWindowHandle::new(ui_view.cast());
111+
/// // Optionally, set the view controller too.
109112
/// handle.ui_view_controller = None;
110113
/// ```
111114
pub fn new(ui_view: NonNull<c_void>) -> Self {

0 commit comments

Comments
 (0)