@@ -7,6 +7,7 @@ use std::num::{NonZeroU16, NonZeroU32};
7
7
use core_foundation:: array:: { CFArrayGetCount , CFArrayGetValueAtIndex } ;
8
8
use core_foundation:: base:: { CFRelease , TCFType } ;
9
9
use core_foundation:: string:: CFString ;
10
+ use core_foundation:: uuid:: CFUUIDRef ;
10
11
use core_graphics:: display:: {
11
12
CGDirectDisplayID , CGDisplay , CGDisplayBounds , CGDisplayCopyDisplayMode ,
12
13
} ;
@@ -133,15 +134,19 @@ impl VideoModeHandle {
133
134
#[ derive( Clone ) ]
134
135
pub struct MonitorHandle ( CGDirectDisplayID ) ;
135
136
137
+ impl MonitorHandle {
138
+ /// Internal comparisons of [`MonitorHandle`]s are done first requesting a UUID for the handle.
139
+ fn uuid ( & self ) -> ffi:: CfUuid {
140
+ ffi:: CfUuid :: from_display_id ( self . 0 )
141
+ }
142
+ }
143
+
136
144
// `CGDirectDisplayID` changes on video mode change, so we cannot rely on that
137
145
// for comparisons, but we can use `CGDisplayCreateUUIDFromDisplayID` to get an
138
146
// unique identifier that persists even across system reboots
139
147
impl PartialEq for MonitorHandle {
140
148
fn eq ( & self , other : & Self ) -> bool {
141
- unsafe {
142
- ffi:: CGDisplayCreateUUIDFromDisplayID ( self . 0 )
143
- == ffi:: CGDisplayCreateUUIDFromDisplayID ( other. 0 )
144
- }
149
+ self . uuid ( ) == other. uuid ( )
145
150
}
146
151
}
147
152
@@ -155,18 +160,13 @@ impl PartialOrd for MonitorHandle {
155
160
156
161
impl Ord for MonitorHandle {
157
162
fn cmp ( & self , other : & Self ) -> std:: cmp:: Ordering {
158
- unsafe {
159
- ffi:: CGDisplayCreateUUIDFromDisplayID ( self . 0 )
160
- . cmp ( & ffi:: CGDisplayCreateUUIDFromDisplayID ( other. 0 ) )
161
- }
163
+ self . uuid ( ) . cmp ( & other. uuid ( ) )
162
164
}
163
165
}
164
166
165
167
impl std:: hash:: Hash for MonitorHandle {
166
168
fn hash < H : std:: hash:: Hasher > ( & self , state : & mut H ) {
167
- unsafe {
168
- ffi:: CGDisplayCreateUUIDFromDisplayID ( self . 0 ) . hash ( state) ;
169
- }
169
+ self . uuid ( ) . hash ( state) ;
170
170
}
171
171
}
172
172
@@ -287,12 +287,10 @@ impl MonitorHandle {
287
287
}
288
288
289
289
pub ( crate ) fn ns_screen ( & self , mtm : MainThreadMarker ) -> Option < Retained < NSScreen > > {
290
- let uuid = unsafe { ffi :: CGDisplayCreateUUIDFromDisplayID ( self . 0 ) } ;
290
+ let uuid = self . uuid ( ) ;
291
291
NSScreen :: screens ( mtm) . into_iter ( ) . find ( |screen| {
292
292
let other_native_id = get_display_id ( screen) ;
293
- let other_uuid = unsafe {
294
- ffi:: CGDisplayCreateUUIDFromDisplayID ( other_native_id as CGDirectDisplayID )
295
- } ;
293
+ let other_uuid = ffi:: CfUuid :: from_display_id ( other_native_id) ;
296
294
uuid == other_uuid
297
295
} )
298
296
}
0 commit comments