Skip to content

Commit ca46e29

Browse files
authored
macOS: Fix surface position (#4027)
1 parent 132fbe1 commit ca46e29

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/platform_impl/apple/appkit/window_delegate.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -945,8 +945,21 @@ impl WindowDelegate {
945945
}
946946

947947
pub fn surface_position(&self) -> PhysicalPosition<i32> {
948-
let content_rect = self.window().contentRectForFrameRect(self.window().frame());
949-
let logical = LogicalPosition::new(content_rect.origin.x, content_rect.origin.y);
948+
// The calculation here is a bit awkward because we've gotta reconcile the
949+
// different origins (Winit prefers top-left vs. NSWindow's bottom-left),
950+
// and I couldn't find a built-in way to do so.
951+
952+
// The position of the window and the view, both in Winit screen coordinates.
953+
let window_position = flip_window_screen_coordinates(self.window().frame());
954+
let view_position = flip_window_screen_coordinates(
955+
self.window().contentRectForFrameRect(self.window().frame()),
956+
);
957+
958+
// And use that to convert the view position to window coordinates.
959+
let surface_position =
960+
NSPoint::new(view_position.x - window_position.x, view_position.y - window_position.y);
961+
962+
let logical = LogicalPosition::new(surface_position.x, surface_position.y);
950963
logical.to_physical(self.scale_factor())
951964
}
952965

0 commit comments

Comments
 (0)