Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve documentation for UIKit and AppKit handles #160

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/appkit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,45 @@ impl DisplayHandle<'static> {
}

/// Raw window handle for AppKit.
///
/// Note that `NSView` can only be accessed from the main thread of the
/// application. This struct is `!Send` and `!Sync` to help with ensuring
/// that.
///
/// # Example
///
/// Getting the view from a [`WindowHandle`][crate::WindowHandle].
///
/// ```no_run
/// # fn inner() {
/// #![cfg(target_os = "macos")]
/// # #[cfg(requires_objc2)]
/// use icrate::AppKit::NSView;
/// # #[cfg(requires_objc2)]
/// use icrate::Foundation::is_main_thread;
/// # #[cfg(requires_objc2)]
/// use objc2::rc::Id;
/// use raw_window_handle::{WindowHandle, RawWindowHandle};
///
/// let handle: WindowHandle<'_>; // Get the window handle from somewhere else
/// # handle = unimplemented!();
/// match handle.as_raw() {
/// # #[cfg(requires_objc2)]
/// RawWindowHandle::AppKit(handle) => {
/// assert!(is_main_thread(), "can only access AppKit handles on the main thread");
/// let ns_view = handle.ns_view.as_ptr();
/// // SAFETY: The pointer came from `WindowHandle`, which ensures
/// // that the `AppKitWindowHandle` contains a valid pointer to an
/// // `NSView`.
/// // Unwrap is fine, since the pointer came from `NonNull`.
/// let ns_view: Id<NSView> = unsafe { Id::retain(ns_view.cast()) }.unwrap();
/// // Do something with the NSView here, like getting the `NSWindow`
/// let ns_window = ns_view.window().expect("view was not installed in a window");
/// }
/// handle => unreachable!("unknown handle {handle:?} for platform"),
/// }
/// # }
/// ```
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct AppKitWindowHandle {
Expand Down
39 changes: 39 additions & 0 deletions src/uikit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,45 @@ impl DisplayHandle<'static> {
}

/// Raw window handle for UIKit.
///
/// Note that `UIView` can only be accessed from the main thread of the
/// application. This struct is `!Send` and `!Sync` to help with ensuring
/// that.
///
/// # Example
///
/// Getting the view from a [`WindowHandle`][crate::WindowHandle].
///
/// ```no_run
/// # fn inner() {
/// #![cfg(any(target_os = "ios", target_os = "tvos", target_os = "watchos", target_os = "xros"))]
/// # #[cfg(requires_objc2)]
/// use icrate::Foundation::is_main_thread;
/// # #[cfg(requires_objc2)]
/// use objc2::rc::Id;
/// // TODO: Use `icrate::UIKit::UIView` when available
/// # #[cfg(requires_objc2)]
/// use objc2::runtime::NSObject;
/// use raw_window_handle::{WindowHandle, RawWindowHandle};
///
/// let handle: WindowHandle<'_>; // Get the window handle from somewhere else
/// # handle = unimplemented!();
/// match handle.as_raw() {
/// # #[cfg(requires_objc2)]
/// RawWindowHandle::UIKit(handle) => {
/// assert!(is_main_thread(), "can only access UIKit handles on the main thread");
/// let ui_view = handle.ui_view.as_ptr();
/// // SAFETY: The pointer came from `WindowHandle`, which ensures
/// // that the `UiKitWindowHandle` contains a valid pointer to an
/// // `UIView`.
/// // Unwrap is fine, since the pointer came from `NonNull`.
/// let ui_view: Id<NSObject> = unsafe { Id::retain(ui_view.cast()) }.unwrap();
/// // Do something with the UIView here.
/// }
/// handle => unreachable!("unknown handle {handle:?} for platform"),
/// }
/// # }
/// ```
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UiKitWindowHandle {
Expand Down