Skip to repository content

tenant.openagents/omega

No repository description is available.

OpenAgents Git authority 2026-07-28T03:36:12.610Z Public web read
NIP-34 coordinate30617:7649603503856e5148d571eac2766b288a8ff1e9e35d380337a1d2b0015b4f92:omega
MaintainersHidden in public view
References2 branches · 1 tag
Read-only clonegit clone https://openagents.com/git/tenant.openagents/omega.git
Browse files

display.rs

170 lines · 6.0 KB · rust
1use crate::ns_string;
2use anyhow::Result;
3use cocoa::{
4    appkit::NSScreen,
5    base::{id, nil},
6    foundation::{NSArray, NSDictionary},
7};
8use core_foundation::base::CFRelease;
9use core_foundation::uuid::{CFUUIDGetUUIDBytes, CFUUIDRef};
10use core_graphics::display::{CGDirectDisplayID, CGDisplayBounds, CGGetActiveDisplayList};
11use gpui::{Bounds, DisplayId, Pixels, PlatformDisplay, point, px, size};
12use objc::{msg_send, sel, sel_impl};
13use uuid::Uuid;
14
15#[derive(Debug)]
16pub(crate) struct MacDisplay(pub(crate) CGDirectDisplayID);
17
18unsafe impl Send for MacDisplay {}
19
20impl MacDisplay {
21    /// Get the screen with the given [`DisplayId`].
22    pub fn find_by_id(id: DisplayId) -> Option<Self> {
23        Self::all().find(|screen| screen.id() == id)
24    }
25
26    /// Get the primary screen - the one with the menu bar, and whose bottom left
27    /// corner is at the origin of the AppKit coordinate system.
28    pub fn primary() -> Self {
29        // Instead of iterating through all active systems displays via `all()` we use the first
30        // NSScreen and gets its CGDirectDisplayID, because we can't be sure that `CGGetActiveDisplayList`
31        // will always return a list of active displays (machine might be sleeping).
32        //
33        // The following is what Chromium does too:
34        //
35        // https://chromium.googlesource.com/chromium/src/+/66.0.3359.158/ui/display/mac/screen_mac.mm#56
36        unsafe {
37            let screens = NSScreen::screens(nil);
38            let screen = cocoa::foundation::NSArray::objectAtIndex(screens, 0);
39            let device_description = NSScreen::deviceDescription(screen);
40            let screen_number_key: id = ns_string("NSScreenNumber");
41            let screen_number = device_description.objectForKey_(screen_number_key);
42            let screen_number: CGDirectDisplayID = msg_send![screen_number, unsignedIntegerValue];
43            Self(screen_number)
44        }
45    }
46
47    /// Obtains an iterator over all currently active system displays.
48    pub fn all() -> impl Iterator<Item = Self> {
49        unsafe {
50            // We're assuming there aren't more than 32 displays connected to the system.
51            let mut displays = Vec::with_capacity(32);
52            let mut display_count = 0;
53            let result = CGGetActiveDisplayList(
54                displays.capacity() as u32,
55                displays.as_mut_ptr(),
56                &mut display_count,
57            );
58
59            if result == 0 {
60                displays.set_len(display_count as usize);
61                displays.into_iter().map(MacDisplay)
62            } else {
63                panic!("Failed to get active display list. Result: {result}");
64            }
65        }
66    }
67}
68
69#[link(name = "ApplicationServices", kind = "framework")]
70unsafe extern "C" {
71    fn CGDisplayCreateUUIDFromDisplayID(display: CGDirectDisplayID) -> CFUUIDRef;
72}
73
74impl PlatformDisplay for MacDisplay {
75    fn id(&self) -> DisplayId {
76        DisplayId::new(self.0 as u64)
77    }
78
79    fn uuid(&self) -> Result<Uuid> {
80        let cfuuid = unsafe { CGDisplayCreateUUIDFromDisplayID(self.0 as CGDirectDisplayID) };
81        anyhow::ensure!(
82            !cfuuid.is_null(),
83            "AppKit returned a null from CGDisplayCreateUUIDFromDisplayID"
84        );
85
86        let bytes = unsafe { CFUUIDGetUUIDBytes(cfuuid) };
87        unsafe { CFRelease(cfuuid as _) };
88        Ok(Uuid::from_bytes([
89            bytes.byte0,
90            bytes.byte1,
91            bytes.byte2,
92            bytes.byte3,
93            bytes.byte4,
94            bytes.byte5,
95            bytes.byte6,
96            bytes.byte7,
97            bytes.byte8,
98            bytes.byte9,
99            bytes.byte10,
100            bytes.byte11,
101            bytes.byte12,
102            bytes.byte13,
103            bytes.byte14,
104            bytes.byte15,
105        ]))
106    }
107
108    fn bounds(&self) -> Bounds<Pixels> {
109        unsafe {
110            // CGDisplayBounds is in "global display" coordinates, where 0 is
111            // the top left of the primary display.
112            let bounds = CGDisplayBounds(self.0);
113
114            Bounds {
115                origin: Default::default(),
116                size: size(px(bounds.size.width as f32), px(bounds.size.height as f32)),
117            }
118        }
119    }
120
121    fn visible_bounds(&self) -> Bounds<Pixels> {
122        unsafe {
123            let dominated_screen = self.get_nsscreen();
124
125            if dominated_screen == nil {
126                return self.bounds();
127            }
128
129            let screen_frame = NSScreen::frame(dominated_screen);
130            let visible_frame = NSScreen::visibleFrame(dominated_screen);
131
132            // Convert from bottom-left origin (AppKit) to top-left origin
133            let origin_y =
134                screen_frame.size.height - visible_frame.origin.y - visible_frame.size.height
135                    + screen_frame.origin.y;
136
137            Bounds {
138                origin: point(
139                    px(visible_frame.origin.x as f32 - screen_frame.origin.x as f32),
140                    px(origin_y as f32),
141                ),
142                size: size(
143                    px(visible_frame.size.width as f32),
144                    px(visible_frame.size.height as f32),
145                ),
146            }
147        }
148    }
149}
150
151impl MacDisplay {
152    /// Find the NSScreen corresponding to this display
153    unsafe fn get_nsscreen(&self) -> id {
154        let screens = unsafe { NSScreen::screens(nil) };
155        let count = unsafe { NSArray::count(screens) };
156        let screen_number_key: id = unsafe { ns_string("NSScreenNumber") };
157
158        for i in 0..count {
159            let screen = unsafe { NSArray::objectAtIndex(screens, i) };
160            let device_description = unsafe { NSScreen::deviceDescription(screen) };
161            let screen_number = unsafe { device_description.objectForKey_(screen_number_key) };
162            let screen_id: CGDirectDisplayID = msg_send![screen_number, unsignedIntegerValue];
163            if screen_id == self.0 {
164                return screen;
165            }
166        }
167        nil
168    }
169}
170
Served at tenant.openagents/omega Member data and write actions are omitted.