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`][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/// [HWND]: https://learn.microsoft.com/ja-jp/windows/win32/winprog/windows-data-types#HWND
12/// [CGWindowID]: https://developer.apple.com/documentation/coregraphics/cgwindowid?language=objc
13#[derive(Clone, Debug, Copy, PartialEq, Eq)]
14pub struct WindowId(pub(crate) PlatformWindowId);
15
16unsafe impl Send for WindowId {}
17unsafe impl Sync for WindowId {}
18
19impl WindowId {
20 pub const fn new(id: PlatformWindowId) -> Self {
21 Self(id)
22 }
23
24 /// Returns the underlying platform-specific window identifier as a reference.
25 pub fn platform_window_id(&self) -> &PlatformWindowId {
26 &self.0
27 }
28
29 /// Returns the underlying platform-specific window identifier.
30 pub const fn into_platform_window_id(self) -> PlatformWindowId {
31 self.0
32 }
33
34 /// Converts the [`WindowId`] to a [`u32`].
35 ///
36 /// # Platform-specific
37 /// - **macOS**: Returns the window number. It is same as [`WindowId::platform_window_id`].
38 /// - **Windows**: Returns the window handle as a `u32`.
39 pub fn as_u32(&self) -> u32 {
40 #[cfg(target_os = "macos")]
41 {
42 self.0
43 }
44 #[cfg(target_os = "windows")]
45 {
46 self.0.0 as _
47 }
48 }
49}
50
51impl From<u32> for WindowId {
52 fn from(id: u32) -> Self {
53 #[cfg(target_os = "macos")]
54 {
55 Self(id)
56 }
57 #[cfg(target_os = "windows")]
58 {
59 Self(windows::Win32::Foundation::HWND(id as _))
60 }
61 }
62}
63
64impl std::hash::Hash for WindowId {
65 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
66 self.as_u32().hash(state);
67 }
68}