window_observer/
window.rs1use window_getter::Bounds;
2
3use crate::{Error, platform_impl::PlatformWindow};
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 #[cfg_attr(docsrs, doc(cfg(feature = "macos-private-api")))]
94 pub fn id(&self) -> Result<window_getter::WindowId, Error> {
95 #[cfg(target_os = "macos")]
96 {
97 Ok(window_getter::WindowId::new(self.0.id()?))
98 }
99 #[cfg(target_os = "windows")]
100 {
101 Ok(window_getter::WindowId::new(self.0.hwnd()))
102 }
103 }
104
105 #[cfg(feature = "macos-private-api")]
115 #[cfg_attr(docsrs, doc(cfg(feature = "macos-private-api")))]
116 pub fn create_window_getter_window(&self) -> Result<Option<window_getter::Window>, Error> {
117 #[cfg(target_os = "macos")]
118 {
119 Ok(window_getter::get_window(self.id()?).expect("No window environment found"))
120 }
121 #[cfg(target_os = "windows")]
122 {
123 let window = window_getter::platform_impl::PlatformWindow::new(self.inner().hwnd());
124 Ok(Some(window_getter::Window::new(window)))
125 }
126 }
127}
128
129#[derive(Debug, Clone, Default, PartialEq)]
131pub struct Size {
132 pub width: f64,
134 pub height: f64,
136}
137
138#[derive(Debug, Clone, Copy, Default, PartialEq)]
140pub struct Position {
141 pub x: f64,
143 pub y: f64,
145}
146
147impl From<Bounds> for Size {
148 fn from(value: Bounds) -> Self {
149 Size {
150 width: value.width,
151 height: value.height,
152 }
153 }
154}
155
156impl From<Bounds> for Position {
157 fn from(value: Bounds) -> Self {
158 Position {
159 x: value.x,
160 y: value.y,
161 }
162 }
163}