fix(darwin): make Position() and SetPosition() use consistent coordinate systems (#4818)

* fix(darwin): make Position() and SetPosition() use consistent coordinate systems

On macOS, windowGetPosition() was returning raw Cocoa coordinates (Y=0 at
bottom of screen) while windowSetPosition() expected coordinates with Y=0
at the top of the screen. This caused window positions to drift when saving
and restoring window state across application sessions.

The fix updates windowGetPosition() to:
1. Convert Y coordinates to top-origin (matching windowSetPosition)
2. Apply DPI scale factor (matching windowSetPosition)

This ensures Position() returns values that can be directly passed back to
SetPosition() for consistent round-trip behavior.

Fixes #4816

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix(darwin): make Position() and SetPosition() use consistent coordinate systems

On macOS, windowGetPosition() was returning raw Cocoa coordinates (Y=0 at
bottom of screen) while windowSetPosition() expected coordinates with Y=0
at the top of the screen. This caused window positions to drift when saving
and restoring window state across application sessions.

The fix updates windowGetPosition() to:
1. Convert Y coordinates to top-origin (matching windowSetPosition)
2. Apply DPI scale factor (matching windowSetPosition)

This ensures Position() returns values that can be directly passed back to
SetPosition() for consistent round-trip behavior.

Fixes #4816

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Lea Anthony 2026-01-25 12:40:25 +11:00 committed by GitHub
commit 537c74327d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 5 deletions

View file

@ -1,6 +1,6 @@
# Unreleased Changes
<!--
<!--
This file is used to collect changelog entries for the next v3-alpha release.
Add your changes under the appropriate sections below.
@ -23,6 +23,7 @@ After processing, the content will be moved to the main changelog and this file
## Fixed
<!-- Bug fixes -->
- Fix `Position()` and `SetPosition()` using inconsistent coordinate systems on macOS, causing window position drift when saving/restoring state (#4816) by @leaanthony
## Deprecated
<!-- Soon-to-be removed features -->

View file

@ -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) {