From 501be6f218a7d21e81a5c2e88355d7144b8b7e6d Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Mon, 2 Dec 2024 21:17:09 +0100 Subject: [PATCH] macOS: Fix surface position --- .../apple/appkit/window_delegate.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/platform_impl/apple/appkit/window_delegate.rs b/src/platform_impl/apple/appkit/window_delegate.rs index 62707277e8..4b373ad8ab 100644 --- a/src/platform_impl/apple/appkit/window_delegate.rs +++ b/src/platform_impl/apple/appkit/window_delegate.rs @@ -955,8 +955,21 @@ impl WindowDelegate { } pub fn surface_position(&self) -> PhysicalPosition { - let content_rect = self.window().contentRectForFrameRect(self.window().frame()); - let logical = LogicalPosition::new(content_rect.origin.x, content_rect.origin.y); + // The calculation here is a bit awkward because we've gotta reconcile the + // different origins (Winit prefers top-left vs. NSWindow's bottom-left), + // and I couldn't find a built-in way to do so. + + // The position of the window and the view, both in Winit screen coordinates. + let window_position = flip_window_screen_coordinates(self.window().frame()); + let view_position = flip_window_screen_coordinates( + self.window().contentRectForFrameRect(self.window().frame()), + ); + + // And use that to convert the view position to window coordinates. + let surface_position = + NSPoint::new(view_position.x - window_position.x, view_position.y - window_position.y); + + let logical = LogicalPosition::new(surface_position.x, surface_position.y); logical.to_physical(self.scale_factor()) }