Skip to content

Commit 568b378

Browse files
committed
Fix window position conversion for macOS
Added NSPointExt::bottomLeftForWindow to correctly convert top-left window positions to bottom-left coordinates by accounting for window height. Updated Window::SetPosition to use the new method, ensuring accurate window placement.
1 parent 7867950 commit 568b378

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/platform/macos/coordinate_utils_macos.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,26 @@ struct NSRectExt {
2626

2727
// NSPoint extension-like helper for coordinate system conversion
2828
struct NSPointExt {
29+
// Convert bottom-left point to top-left point
2930
static CGPoint topLeft(NSPoint point) {
3031
NSRect primaryScreenFrame = [[NSScreen screens][0] frame];
3132
return CGPointMake(point.x, primaryScreenFrame.size.height - point.y);
3233
}
3334

34-
// Convert from top-left coordinate system to bottom-left (macOS default)
35+
// Convert top-left point to bottom-left point
36+
// Note: For window positions, use bottomLeftForWindow instead, as it requires window height
3537
static NSPoint bottomLeft(CGPoint topLeftPoint) {
3638
NSRect primaryScreenFrame = [[NSScreen screens][0] frame];
3739
return NSMakePoint(topLeftPoint.x, primaryScreenFrame.size.height - topLeftPoint.y);
3840
}
41+
42+
// Convert top-left window position to bottom-left window position
43+
// This is the correct method for converting window positions, as it accounts for window height
44+
static NSPoint bottomLeftForWindow(CGPoint topLeftPoint, CGFloat windowHeight) {
45+
NSRect primaryScreenFrame = [[NSScreen screens][0] frame];
46+
double bottomY = primaryScreenFrame.size.height - topLeftPoint.y - windowHeight;
47+
return NSMakePoint(topLeftPoint.x, bottomY);
48+
}
3949
};
4050

4151
} // namespace nativeapi

src/platform/macos/window_macos.mm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,10 @@
280280

281281
void Window::SetPosition(Point point) {
282282
// Convert from topLeft coordinate system to bottom-left (macOS default)
283-
NSPoint topLeftPoint = {point.x, point.y};
284-
NSPoint bottomLeft = NSPointExt::bottomLeft(topLeftPoint);
283+
// We need the window height to correctly convert the top-left position
284+
NSRect frame = [pimpl_->ns_window_ frame];
285+
CGPoint topLeftPoint = {point.x, point.y};
286+
NSPoint bottomLeft = NSPointExt::bottomLeftForWindow(topLeftPoint, frame.size.height);
285287
[pimpl_->ns_window_ setFrameOrigin:bottomLeft];
286288
}
287289

0 commit comments

Comments
 (0)