1 | use core::ffi::c_void; |
2 | use core::num::NonZeroIsize; |
3 | use core::ptr::NonNull; |
4 | |
5 | use super::DisplayHandle; |
6 | |
7 | /// Raw display handle for Windows. |
8 | /// |
9 | /// It can be used regardless of Windows window backend. |
10 | #[non_exhaustive ] |
11 | #[derive (Debug, Clone, Copy, PartialEq, Eq, Hash)] |
12 | pub struct WindowsDisplayHandle {} |
13 | |
14 | impl WindowsDisplayHandle { |
15 | /// Create a new empty display handle. |
16 | /// |
17 | /// |
18 | /// # Example |
19 | /// |
20 | /// ``` |
21 | /// # use raw_window_handle::WindowsDisplayHandle; |
22 | /// let handle = WindowsDisplayHandle::new(); |
23 | /// ``` |
24 | pub fn new() -> Self { |
25 | Self {} |
26 | } |
27 | } |
28 | |
29 | impl DisplayHandle<'static> { |
30 | /// Create a Windows-based display handle. |
31 | /// |
32 | /// As no data is borrowed by this handle, it is completely safe to create. This function |
33 | /// may be useful to windowing framework implementations that want to avoid unsafe code. |
34 | /// |
35 | /// # Example |
36 | /// |
37 | /// ``` |
38 | /// # use raw_window_handle::{DisplayHandle, HasDisplayHandle}; |
39 | /// # fn do_something(rwh: impl HasDisplayHandle) { let _ = rwh; } |
40 | /// let handle = DisplayHandle::windows(); |
41 | /// do_something(handle); |
42 | /// ``` |
43 | pub fn windows() -> Self { |
44 | // SAFETY: No data is borrowed. |
45 | unsafe { Self::borrow_raw(WindowsDisplayHandle::new().into()) } |
46 | } |
47 | } |
48 | |
49 | /// Raw window handle for Win32. |
50 | #[non_exhaustive ] |
51 | #[derive (Debug, Clone, Copy, PartialEq, Eq, Hash)] |
52 | pub struct Win32WindowHandle { |
53 | /// A Win32 `HWND` handle. |
54 | pub hwnd: NonZeroIsize, |
55 | /// The `GWLP_HINSTANCE` associated with this type's `HWND`. |
56 | pub hinstance: Option<NonZeroIsize>, |
57 | } |
58 | |
59 | impl Win32WindowHandle { |
60 | /// Create a new handle to a window. |
61 | /// |
62 | /// # Safety |
63 | /// |
64 | /// It is assumed that the Win32 handle belongs to the current thread. This |
65 | /// is necessary for the handle to be considered "valid" in all cases. |
66 | /// |
67 | /// # Example |
68 | /// |
69 | /// ``` |
70 | /// # use core::num::NonZeroIsize; |
71 | /// # use raw_window_handle::Win32WindowHandle; |
72 | /// # struct HWND(isize); |
73 | /// # |
74 | /// let window: HWND; |
75 | /// # window = HWND(1); |
76 | /// let mut handle = Win32WindowHandle::new(NonZeroIsize::new(window.0).unwrap()); |
77 | /// // Optionally set the GWLP_HINSTANCE. |
78 | /// # #[cfg (only_for_showcase)] |
79 | /// let hinstance = NonZeroIsize::new(unsafe { GetWindowLongPtrW(window, GWLP_HINSTANCE) }).unwrap(); |
80 | /// # let hinstance = None; |
81 | /// handle.hinstance = hinstance; |
82 | /// ``` |
83 | pub fn new(hwnd: NonZeroIsize) -> Self { |
84 | Self { |
85 | hwnd, |
86 | hinstance: None, |
87 | } |
88 | } |
89 | } |
90 | |
91 | /// Raw window handle for WinRT. |
92 | #[non_exhaustive ] |
93 | #[derive (Debug, Clone, Copy, PartialEq, Eq, Hash)] |
94 | pub struct WinRtWindowHandle { |
95 | /// A WinRT `CoreWindow` handle. |
96 | pub core_window: NonNull<c_void>, |
97 | } |
98 | |
99 | impl WinRtWindowHandle { |
100 | /// Create a new handle to a window. |
101 | /// |
102 | /// |
103 | /// # Example |
104 | /// |
105 | /// ``` |
106 | /// # use core::ptr::NonNull; |
107 | /// # use raw_window_handle::WinRtWindowHandle; |
108 | /// # type CoreWindow = (); |
109 | /// # |
110 | /// let window: NonNull<CoreWindow>; |
111 | /// # window = NonNull::from(&()); |
112 | /// let handle = WinRtWindowHandle::new(window.cast()); |
113 | /// ``` |
114 | pub fn new(core_window: NonNull<c_void>) -> Self { |
115 | Self { core_window } |
116 | } |
117 | } |
118 | |