diff --git a/v3/UNRELEASED_CHANGELOG.md b/v3/UNRELEASED_CHANGELOG.md index 8e4648038..806f0183a 100644 --- a/v3/UNRELEASED_CHANGELOG.md +++ b/v3/UNRELEASED_CHANGELOG.md @@ -1,6 +1,6 @@ # Unreleased Changes - +- Fix `Position()` and `SetPosition()` using inconsistent coordinate systems on macOS, causing window position drift when saving/restoring state (#4816) by @leaanthony ## Deprecated diff --git a/v3/pkg/application/webview_window_darwin.go b/v3/pkg/application/webview_window_darwin.go index baf40556c..d4534b501 100644 --- a/v3/pkg/application/webview_window_darwin.go +++ b/v3/pkg/application/webview_window_darwin.go @@ -590,11 +590,19 @@ void windowGetRelativePosition(void* nsWindow, int* x, int* y) { *y = screenFrame.size.height - frame.origin.y - frame.size.height; } -// Get absolute window position +// Get absolute window position (in screen coordinates with Y=0 at top, scaled for DPI) void windowGetPosition(void* nsWindow, int* x, int* y) { - NSRect frame = [(WebviewWindow*)nsWindow frame]; - *x = frame.origin.x; - *y = frame.origin.y; + WebviewWindow* window = (WebviewWindow*)nsWindow; + NSScreen* screen = [window screen]; + if (screen == NULL) { + screen = [NSScreen mainScreen]; + } + CGFloat scale = [screen backingScaleFactor]; + NSRect frame = [window frame]; + NSRect screenFrame = [screen frame]; + // Convert to top-origin coordinates and apply scale (matching windowSetPosition) + *x = frame.origin.x * scale; + *y = (screenFrame.size.height - frame.origin.y - frame.size.height) * scale; } void windowSetPosition(void* nsWindow, int x, int y) {