1use std::{any::Any, ffi::c_void};
2
3use crate::platform;
4use cocoa::{
5 appkit::NSScreen,
6 base::{id, nil},
7 foundation::{NSArray, NSDictionary},
8};
9use core_foundation::{
10 number::{kCFNumberIntType, CFNumberGetValue, CFNumberRef},
11 uuid::{CFUUIDGetUUIDBytes, CFUUIDRef},
12};
13use core_graphics::display::CGDirectDisplayID;
14use pathfinder_geometry::rect::RectF;
15use uuid::Uuid;
16
17use super::{geometry::NSRectExt, ns_string};
18
19#[link(name = "ApplicationServices", kind = "framework")]
20extern "C" {
21 pub fn CGDisplayCreateUUIDFromDisplayID(display: CGDirectDisplayID) -> CFUUIDRef;
22}
23
24#[derive(Debug)]
25pub struct Screen {
26 pub(crate) native_screen: id,
27}
28
29impl Screen {
30 pub fn find_by_id(uuid: Uuid) -> Option<Self> {
31 unsafe {
32 let native_screens = NSScreen::screens(nil);
33 (0..NSArray::count(native_screens))
34 .into_iter()
35 .map(|ix| Screen {
36 native_screen: native_screens.objectAtIndex(ix),
37 })
38 .find(|screen| platform::Screen::display_uuid(screen) == Some(uuid))
39 }
40 }
41
42 pub fn all() -> Vec<Self> {
43 let mut screens = Vec::new();
44 unsafe {
45 let native_screens = NSScreen::screens(nil);
46 for ix in 0..NSArray::count(native_screens) {
47 screens.push(Screen {
48 native_screen: native_screens.objectAtIndex(ix),
49 });
50 }
51 }
52 screens
53 }
54}
55
56impl platform::Screen for Screen {
57 fn as_any(&self) -> &dyn Any {
58 self
59 }
60
61 fn display_uuid(&self) -> Option<uuid::Uuid> {
62 unsafe {
63 // Screen ids are not stable. Further, the default device id is also unstable across restarts.
64 // CGDisplayCreateUUIDFromDisplayID is stable but not exposed in the bindings we use.
65 // This approach is similar to that which winit takes
66 // https://github.com/rust-windowing/winit/blob/402cbd55f932e95dbfb4e8b5e8551c49e56ff9ac/src/platform_impl/macos/monitor.rs#L99
67 let device_description = self.native_screen.deviceDescription();
68 let key = ns_string("NSScreenNumber");
69 let device_id_obj = device_description.objectForKey_(key);
70 let mut device_id: u32 = 0;
71 CFNumberGetValue(
72 device_id_obj as CFNumberRef,
73 kCFNumberIntType,
74 (&mut device_id) as *mut _ as *mut c_void,
75 );
76 let cfuuid = CGDisplayCreateUUIDFromDisplayID(device_id as CGDirectDisplayID);
77 if cfuuid.is_null() {
78 return None;
79 }
80
81 let bytes = CFUUIDGetUUIDBytes(cfuuid);
82 Some(Uuid::from_bytes([
83 bytes.byte0,
84 bytes.byte1,
85 bytes.byte2,
86 bytes.byte3,
87 bytes.byte4,
88 bytes.byte5,
89 bytes.byte6,
90 bytes.byte7,
91 bytes.byte8,
92 bytes.byte9,
93 bytes.byte10,
94 bytes.byte11,
95 bytes.byte12,
96 bytes.byte13,
97 bytes.byte14,
98 bytes.byte15,
99 ]))
100 }
101 }
102
103 fn bounds(&self) -> RectF {
104 unsafe {
105 let frame = self.native_screen.frame();
106 frame.to_rectf()
107 }
108 }
109}