Checkpoint: Fix a crash

Nathan Sobo created

Change summary

crates/gpui3/src/platform.rs             |  2 
crates/gpui3/src/platform/mac.rs         |  4 +-
crates/gpui3/src/platform/mac/display.rs | 40 ++++++-------------------
crates/gpui3/src/platform/mac/window.rs  |  6 +-
4 files changed, 16 insertions(+), 36 deletions(-)

Detailed changes

crates/gpui3/src/platform.rs 🔗

@@ -157,7 +157,7 @@ pub trait PlatformDispatcher: Send + Sync {
 }
 
 pub trait PlatformDisplayLink {
-    fn set_output_callback(&mut self, callback: Box<dyn FnMut(&VideoTimestamp, &VideoTimestamp)>);
+    // fn set_output_callback(&mut self, callback: Box<dyn FnMut(&VideoTimestamp, &VideoTimestamp)>);
     fn start(&mut self);
     fn stop(&mut self);
 }

crates/gpui3/src/platform/mac.rs 🔗

@@ -2,7 +2,7 @@
 ///! an origin at the bottom left of the main display.
 mod dispatcher;
 mod display;
-mod display_link;
+// mod display_link;
 mod events;
 mod metal_atlas;
 mod metal_renderer;
@@ -33,7 +33,7 @@ use std::{
 
 pub use dispatcher::*;
 pub use display::*;
-pub use display_link::*;
+// pub use display_link::*;
 pub use metal_atlas::*;
 pub use platform::*;
 pub use text_system::*;

crates/gpui3/src/platform/mac/display.rs 🔗

@@ -1,7 +1,6 @@
-use crate::{point, size, Bounds, DisplayId, GlobalPixels, MacDisplayLink, PlatformDisplay};
-
+use crate::{point, size, Bounds, DisplayId, GlobalPixels, PlatformDisplay};
 use core_graphics::{
-    display::{CGDirectDisplayID, CGGetActiveDisplayList},
+    display::{CGDirectDisplayID, CGDisplayBounds, CGGetActiveDisplayList},
     geometry::{CGPoint, CGRect, CGSize},
 };
 use std::any::Any;
@@ -49,13 +48,14 @@ impl MacDisplay {
 /// Conversely, in GPUI's coordinate system, the origin is placed at the top left of the primary
 /// screen, with the Y axis pointing downwards.
 pub(crate) fn display_bounds_from_native(rect: CGRect) -> Bounds<GlobalPixels> {
-    let primary_screen_height = MacDisplay::primary().bounds().size.height;
+    let primary_screen_size = unsafe { CGDisplayBounds(MacDisplay::primary().id().0) }.size;
+
     Bounds {
         origin: point(
             GlobalPixels(rect.origin.x as f32),
-            primary_screen_height
-                - GlobalPixels(rect.origin.y as f32)
-                - GlobalPixels(rect.size.height as f32),
+            GlobalPixels(
+                primary_screen_size.height as f32 - rect.origin.y as f32 - rect.size.height as f32,
+            ),
         ),
         size: size(
             GlobalPixels(rect.size.width as f32),
@@ -94,33 +94,13 @@ impl PlatformDisplay for MacDisplay {
 
     fn bounds(&self) -> Bounds<GlobalPixels> {
         unsafe {
-            use core_graphics::display::*;
-
-            let display_id = self.0;
-            // The `CGDisplayBounds` function gets the display bounds
-            // for this display. The bounds are returned as a CGRect
-            // and specify the display's location and size in
-            // pixel units, in the global coordinate space.
-            // // The global coordinate space is a coordinate system used by macOS. In this
-            // coordinate space, the origin {0, 0} represents the top-left corner of the primary
-            // display, and the positive X and Y axes extend from the origin to the right and downward,
-            // respectively, towards the bottom-right corner of the primary display. For any display
-            // connected to the system, the global coordinate space identifies the position and size
-            // of the display with respect to the primary display.
-
-            // The coordinates in this coordinate space are typically in the form of a CGRect,
-            // which represents the rectangle bounding the display in terms of pixels. The CGRect
-            // holds the origin for the rect's bottom-left corner and a CGSize, which
-            // represent width and height.
-
-            // With respect to the above `bounds` function in `PlatformDisplay` trait implementation,
-            // this coordinate space is used to fetch a display ID's CGRect and position of origin, and size.
-            let native_bounds = CGDisplayBounds(display_id);
+            let native_bounds = CGDisplayBounds(self.0);
             display_bounds_from_native(native_bounds)
         }
     }
 
     fn link(&self) -> Box<dyn crate::PlatformDisplayLink> {
-        Box::new(unsafe { MacDisplayLink::new(self.0) })
+        unimplemented!()
+        // Box::new(unsafe { MacDisplayLink::new(self.0) })
     }
 }

crates/gpui3/src/platform/mac/window.rs 🔗

@@ -456,9 +456,9 @@ impl MacWindow {
                 let screen = cocoa::foundation::NSArray::objectAtIndex(screens, i);
                 let device_description = NSScreen::deviceDescription(screen);
                 let screen_number_key: id = NSString::alloc(nil).init_str("NSScreenNumber");
-                let screen_number =
-                    NSDictionary::objectForKey_(device_description, screen_number_key);
-                if (*(screen_number as *const u32)) == display.id().0 {
+                let screen_number = device_description.objectForKey_(screen_number_key);
+                let screen_number: NSUInteger = msg_send![screen_number, unsignedIntegerValue];
+                if screen_number as u32 == display.id().0 {
                     target_screen = screen;
                     break;
                 }