window_getter/window.rs
1use crate::{Bounds, Error, WindowId, platform_impl::PlatformWindow};
2
3/// A wrapper around a platform-specific window.
4/// This struct provides a cross-platform interface to interact with window properties.
5#[derive(Clone, Debug)]
6pub struct Window(pub(crate) PlatformWindow);
7
8impl Window {
9 /// Creates a new [`Window`] instance from a platform-specific window.
10 ///
11 /// # Notes
12 /// You can get a `Window` instance by using the [`get_window`](crate::get_window) function
13 /// or [`get_windows`](crate::get_windows) function so you don't need to create it manually
14 /// in most use cases.
15 pub fn new(inner: PlatformWindow) -> Self {
16 Self(inner)
17 }
18
19 /// Retrieves the underlying platform-specific window.
20 pub fn platform_window(&self) -> &PlatformWindow {
21 &self.0
22 }
23
24 /// Consumes the `Window` and returns the underlying platform-specific window.
25 pub fn into_platform_window(self) -> PlatformWindow {
26 self.0
27 }
28
29 /// Returns the unique identifier of the window.
30 pub fn id(&self) -> WindowId {
31 #[cfg(target_os = "macos")]
32 {
33 WindowId(self.0.id())
34 }
35 #[cfg(target_os = "windows")]
36 {
37 WindowId(self.0.hwnd())
38 }
39 }
40
41 /// Returns the title of the window.
42 ///
43 /// # Platform-specific
44 /// - **Windows**: If you don't have permission to access the title,
45 /// it will return [`Error::PermissionDenied`](crate::Error::PermissionDenied).
46 /// - **macOS**: It will always return [`Ok`]. Apple's documentation does not
47 /// explicitly state this, but it returns `None` when the permission is not granted.
48 pub fn title(&self) -> Result<Option<String>, Error> {
49 #[cfg(target_os = "macos")]
50 {
51 Ok(self.0.title())
52 }
53
54 #[cfg(target_os = "windows")]
55 {
56 Ok(self.0.title()?)
57 }
58 }
59
60 /// Returns the bounds of the window.
61 pub fn bounds(&self) -> Result<Bounds, Error> {
62 #[cfg(target_os = "macos")]
63 {
64 Ok(self.0.bounds()?)
65 }
66 #[cfg(target_os = "windows")]
67 {
68 Ok(self.0.visible_bounds()?)
69 }
70 }
71
72 /// Returns the process ID of the window's owner.
73 ///
74 /// # Platform-specific
75 /// - **macOS**: It will always return [`Ok`].
76 pub fn owner_pid(&self) -> Result<i32, Error> {
77 #[cfg(target_os = "macos")]
78 {
79 Ok(self.0.owner_pid())
80 }
81
82 #[cfg(target_os = "windows")]
83 {
84 Ok(self.0.owner_pid()? as _)
85 }
86 }
87
88 /// Returns the name of the process that owns the window.
89 ///
90 /// # Platform-specific
91 /// - **Windows**: If you don't have permission to access the owner name,
92 /// it will return [`Error::PermissionDenied`](crate::Error::PermissionDenied).
93 /// Also, it will return the name of the executable file when owner name is available.
94 /// - **macOS**: It will always return [`Ok`].
95 pub fn owner_name(&self) -> Result<Option<String>, Error> {
96 #[cfg(target_os = "macos")]
97 {
98 Ok(self.0.owner_name())
99 }
100
101 #[cfg(target_os = "windows")]
102 {
103 Ok(self.0.owner_name().map(Some)?)
104 }
105 }
106}