Skip to repository content205 lines · 6.2 KB · rust
tenant.openagents/omega
No repository description is available.
OpenAgents Git authority 2026-07-28T01:52:13.151Z Public web read
NIP-34 coordinate
30617:7649603503856e5148d571eac2766b288a8ff1e9e35d380337a1d2b0015b4f92:omegaMaintainersHidden in public view
References2 branches · 1 tag
Read-only clone
git clone https://openagents.com/git/tenant.openagents/omega.gitBrowse files
display.rs
1use gpui_util::ResultExt;
2use itertools::Itertools;
3use smallvec::SmallVec;
4use std::rc::Rc;
5use uuid::Uuid;
6use windows::{
7 Win32::{
8 Foundation::*,
9 Graphics::Gdi::*,
10 UI::{
11 HiDpi::{GetDpiForMonitor, MDT_EFFECTIVE_DPI},
12 WindowsAndMessaging::USER_DEFAULT_SCREEN_DPI,
13 },
14 },
15 core::*,
16};
17
18use crate::logical_point;
19use gpui::{Bounds, DevicePixels, DisplayId, Pixels, PlatformDisplay, point, size};
20
21#[derive(Debug, Clone, Copy)]
22pub(crate) struct WindowsDisplay {
23 pub handle: HMONITOR,
24 pub display_id: DisplayId,
25 scale_factor: f32,
26 bounds: Bounds<Pixels>,
27 visible_bounds: Bounds<Pixels>,
28 physical_bounds: Bounds<DevicePixels>,
29 uuid: Uuid,
30}
31
32// The `HMONITOR` is thread-safe.
33unsafe impl Send for WindowsDisplay {}
34unsafe impl Sync for WindowsDisplay {}
35
36impl WindowsDisplay {
37 pub(crate) fn new(display_id: DisplayId) -> Option<Self> {
38 let handle = HMONITOR(u64::from(display_id) as _);
39 let info = get_monitor_info(handle).log_err()?;
40 let monitor_size = info.monitorInfo.rcMonitor;
41 let work_area = info.monitorInfo.rcWork;
42 let uuid = generate_uuid(&info.szDevice);
43 let scale_factor = get_scale_factor_for_monitor(handle).log_err()?;
44 let physical_size = size(
45 (monitor_size.right - monitor_size.left).into(),
46 (monitor_size.bottom - monitor_size.top).into(),
47 );
48
49 Some(WindowsDisplay {
50 handle,
51 display_id,
52 scale_factor,
53 bounds: Bounds {
54 origin: logical_point(
55 monitor_size.left as f32,
56 monitor_size.top as f32,
57 scale_factor,
58 ),
59 size: physical_size.to_pixels(scale_factor),
60 },
61 visible_bounds: Bounds {
62 origin: logical_point(work_area.left as f32, work_area.top as f32, scale_factor),
63 size: size(
64 (work_area.right - work_area.left) as f32 / scale_factor,
65 (work_area.bottom - work_area.top) as f32 / scale_factor,
66 )
67 .map(gpui::px),
68 },
69 physical_bounds: Bounds {
70 origin: point(monitor_size.left.into(), monitor_size.top.into()),
71 size: physical_size,
72 },
73 uuid,
74 })
75 }
76
77 pub(crate) fn display_id_for_monitor(monitor: HMONITOR) -> DisplayId {
78 DisplayId::new(monitor.0 as u64)
79 }
80
81 pub fn primary_monitor() -> Option<Self> {
82 // https://devblogs.microsoft.com/oldnewthing/20070809-00/?p=25643
83 const POINT_ZERO: POINT = POINT { x: 0, y: 0 };
84 let monitor = unsafe { MonitorFromPoint(POINT_ZERO, MONITOR_DEFAULTTOPRIMARY) };
85 if monitor.is_invalid() {
86 log::error!(
87 "can not find the primary monitor: {}",
88 std::io::Error::last_os_error()
89 );
90 return None;
91 }
92 WindowsDisplay::new(Self::display_id_for_monitor(monitor))
93 }
94
95 /// Check if the center point of given bounds is inside this monitor
96 pub fn check_given_bounds(&self, bounds: Bounds<Pixels>) -> bool {
97 let center = bounds.center();
98 let center = POINT {
99 x: (center.x.as_f32() * self.scale_factor) as i32,
100 y: (center.y.as_f32() * self.scale_factor) as i32,
101 };
102 let monitor = unsafe { MonitorFromPoint(center, MONITOR_DEFAULTTONULL) };
103 if monitor.is_invalid() {
104 false
105 } else {
106 let Some(display) = WindowsDisplay::new(Self::display_id_for_monitor(monitor)) else {
107 return false;
108 };
109 display.uuid == self.uuid
110 }
111 }
112
113 pub fn displays() -> Vec<Rc<dyn PlatformDisplay>> {
114 available_monitors()
115 .into_iter()
116 .filter_map(|handle| {
117 Some(
118 Rc::new(WindowsDisplay::new(Self::display_id_for_monitor(handle))?)
119 as Rc<dyn PlatformDisplay>,
120 )
121 })
122 .collect()
123 }
124
125 pub fn physical_bounds(&self) -> Bounds<DevicePixels> {
126 self.physical_bounds
127 }
128}
129
130impl PlatformDisplay for WindowsDisplay {
131 fn id(&self) -> DisplayId {
132 self.display_id
133 }
134
135 fn uuid(&self) -> anyhow::Result<Uuid> {
136 Ok(self.uuid)
137 }
138
139 fn bounds(&self) -> Bounds<Pixels> {
140 self.bounds
141 }
142
143 fn visible_bounds(&self) -> Bounds<Pixels> {
144 self.visible_bounds
145 }
146}
147
148fn available_monitors() -> SmallVec<[HMONITOR; 4]> {
149 let mut monitors: SmallVec<[HMONITOR; 4]> = SmallVec::new();
150 unsafe {
151 EnumDisplayMonitors(
152 None,
153 None,
154 Some(monitor_enum_proc),
155 LPARAM(&mut monitors as *mut _ as _),
156 )
157 .ok()
158 .log_err();
159 }
160 monitors
161}
162
163unsafe extern "system" fn monitor_enum_proc(
164 hmonitor: HMONITOR,
165 _hdc: HDC,
166 _place: *mut RECT,
167 data: LPARAM,
168) -> BOOL {
169 let monitors = data.0 as *mut SmallVec<[HMONITOR; 4]>;
170 unsafe { (*monitors).push(hmonitor) };
171 BOOL(1)
172}
173
174fn get_monitor_info(hmonitor: HMONITOR) -> anyhow::Result<MONITORINFOEXW> {
175 let mut monitor_info: MONITORINFOEXW = unsafe { std::mem::zeroed() };
176 monitor_info.monitorInfo.cbSize = std::mem::size_of::<MONITORINFOEXW>() as u32;
177 let status = unsafe {
178 GetMonitorInfoW(
179 hmonitor,
180 &mut monitor_info as *mut MONITORINFOEXW as *mut MONITORINFO,
181 )
182 };
183 if status.as_bool() {
184 Ok(monitor_info)
185 } else {
186 Err(anyhow::anyhow!(std::io::Error::last_os_error()))
187 }
188}
189
190fn generate_uuid(device_name: &[u16]) -> Uuid {
191 let name = device_name
192 .iter()
193 .flat_map(|&a| a.to_be_bytes())
194 .collect_vec();
195 Uuid::new_v5(&Uuid::NAMESPACE_DNS, &name)
196}
197
198fn get_scale_factor_for_monitor(monitor: HMONITOR) -> Result<f32> {
199 let mut dpi_x = 0;
200 let mut dpi_y = 0;
201 unsafe { GetDpiForMonitor(monitor, MDT_EFFECTIVE_DPI, &mut dpi_x, &mut dpi_y) }?;
202 assert_eq!(dpi_x, dpi_y);
203 Ok(dpi_x as f32 / USER_DEFAULT_SCREEN_DPI as f32)
204}
205