window_observer/
window.rs1use window_getter::Bounds;
2
3use crate::{platform_impl::PlatformWindow, Error};
4
5#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct Window(pub(crate) PlatformWindow);
8
9impl Window {
10 pub fn new(platform_window: PlatformWindow) -> Self {
12 Self(platform_window)
13 }
14
15 pub fn inner(&self) -> &PlatformWindow {
17 &self.0
18 }
19
20 pub fn title(&self) -> Result<Option<String>, Error> {
25 #[cfg(target_os = "macos")]
26 {
27 Ok(Some(self.0.title()?))
28 }
29 #[cfg(target_os = "windows")]
30 {
31 self.0
32 .title()
33 .map_err(|e| Error::PlatformSpecificError(e.into()))
34 }
35 }
36
37 pub fn size(&self) -> Result<Size, Error> {
39 #[cfg(target_os = "macos")]
40 {
41 Ok(self.0.size()?)
42 }
43 #[cfg(target_os = "windows")]
44 {
45 Ok(self
46 .0
47 .visible_bounds()
48 .map_err(|e| Error::PlatformSpecificError(e.into()))?
49 .into())
50 }
51 }
52
53 pub fn position(&self) -> Result<Position, Error> {
55 #[cfg(target_os = "macos")]
56 {
57 Ok(self.0.position()?)
58 }
59 #[cfg(target_os = "windows")]
60 {
61 Ok(self
62 .0
63 .visible_bounds()
64 .map_err(|e| Error::PlatformSpecificError(e.into()))?
65 .into())
66 }
67 }
68
69 pub fn is_focused(&self) -> Result<bool, Error> {
74 #[cfg(target_os = "macos")]
75 {
76 Ok(self.0.is_focused()?)
77 }
78 #[cfg(target_os = "windows")]
79 {
80 Ok(self.0.is_foreground())
81 }
82 }
83
84 #[cfg(feature = "macos-private-api")]
93 pub fn id(&self) -> Result<window_getter::WindowId, Error> {
94 #[cfg(target_os = "macos")]
95 {
96 Ok(window_getter::WindowId::new(self.0.id()?))
97 }
98 #[cfg(target_os = "windows")]
99 {
100 Ok(window_getter::WindowId::new(self.0.hwnd()))
101 }
102 }
103
104 #[cfg(feature = "macos-private-api")]
114 pub fn create_window_getter_window(&self) -> Result<Option<window_getter::Window>, Error> {
115 #[cfg(target_os = "macos")]
116 {
117 Ok(window_getter::get_window(self.id()?).expect("No window environment found"))
118 }
119 #[cfg(target_os = "windows")]
120 {
121 let window = window_getter::platform_impl::PlatformWindow::new(self.inner().hwnd());
122 Ok(Some(window_getter::Window::new(window)))
123 }
124 }
125}
126
127#[derive(Debug, Clone, Default, PartialEq)]
129pub struct Size {
130 pub width: f64,
132 pub height: f64,
134}
135
136#[derive(Debug, Clone, Default, PartialEq)]
138pub struct Position {
139 pub x: f64,
141 pub y: f64,
143}
144
145impl From<Bounds> for Size {
146 fn from(value: Bounds) -> Self {
147 Size {
148 width: value.width,
149 height: value.height,
150 }
151 }
152}
153
154impl From<Bounds> for Position {
155 fn from(value: Bounds) -> Self {
156 Position {
157 x: value.x,
158 y: value.y,
159 }
160 }
161}