window_getter/window_id.rs
1use crate::platform_impl::PlatformWindowId;
2
3/// A unique identifier for a window.
4/// It is used to track windows across different platforms.
5///
6/// # Platform-specific
7/// - **Windows**: The ID is a value of [`HWND`](windows::Win32::Foundation::HWND).
8/// - **macOS**: The ID is a unique within the current user session.
9/// It is called a window number and same as [`CGWindowID`][CGWindowID].
10///
11/// [CGWindowID]: https://developer.apple.com/documentation/coregraphics/cgwindowid?language=objc
12#[derive(Clone, Debug, Copy, PartialEq, Eq)]
13pub struct WindowId(pub(crate) PlatformWindowId);
14
15unsafe impl Send for WindowId {}
16unsafe impl Sync for WindowId {}
17
18impl WindowId {
19 pub const fn new(id: PlatformWindowId) -> Self {
20 Self(id)
21 }
22
23 /// Returns the underlying platform-specific window identifier as a reference.
24 pub fn platform_window_id(&self) -> &PlatformWindowId {
25 &self.0
26 }
27
28 /// Returns the underlying platform-specific window identifier.
29 pub const fn into_platform_window_id(self) -> PlatformWindowId {
30 self.0
31 }
32
33 /// Converts the [`WindowId`] to a [`u32`].
34 ///
35 /// # Platform-specific
36 /// - **macOS**: Returns the window number. It is same as [`WindowId::platform_window_id`].
37 /// - **Windows**: Returns the window handle as a `u32`.
38 pub fn as_u32(&self) -> u32 {
39 #[cfg(target_os = "macos")]
40 {
41 self.0
42 }
43 #[cfg(target_os = "windows")]
44 {
45 self.0.0 as _
46 }
47 }
48}
49
50impl From<u32> for WindowId {
51 fn from(id: u32) -> Self {
52 #[cfg(target_os = "macos")]
53 {
54 Self(id)
55 }
56 #[cfg(target_os = "windows")]
57 {
58 Self(windows::Win32::Foundation::HWND(id as _))
59 }
60 }
61}
62
63impl std::hash::Hash for WindowId {
64 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
65 self.as_u32().hash(state);
66 }
67}