1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use c_void;
use NonNull;
use DisplayHandle;
/// Raw display handle for AppKit.
/// 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 objc2_app_kit::NSView;
/// # #[cfg(requires_objc2)]
/// use objc2_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"),
/// }
/// # }
/// ```