Skip to content

Commit 10848a2

Browse files
committed
Refactor macOS coordinate conversions to utility header
Moved coordinate system conversion helpers to a new coordinate_utils_macos.h file for reuse. Updated display, display_manager, and window implementations to use these utilities, improving consistency and maintainability of coordinate conversions between top-left and bottom-left origins.
1 parent 163442d commit 10848a2

File tree

4 files changed

+64
-21
lines changed

4 files changed

+64
-21
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#pragma once
2+
3+
// Import Cocoa and Core Graphics headers
4+
#import <Cocoa/Cocoa.h>
5+
#import <CoreGraphics/CoreGraphics.h>
6+
7+
namespace nativeapi {
8+
9+
// NSRect extension-like helper for coordinate system conversion
10+
struct NSRectExt {
11+
// Convert NSRect with bottom-left origin to topLeft point
12+
static CGPoint topLeft(NSRect rect) {
13+
NSRect primaryScreenFrame = [[NSScreen screens][0] frame];
14+
return CGPointMake(rect.origin.x,
15+
primaryScreenFrame.size.height - rect.origin.y - rect.size.height);
16+
}
17+
18+
// Convert NSRect with topLeft origin to NSRect with bottom-left origin
19+
static NSRect bottomLeft(NSRect rect) {
20+
NSRect primaryScreenFrame = [[NSScreen screens][0] frame];
21+
// Convert topLeft origin to bottom-left origin
22+
double bottomY = primaryScreenFrame.size.height - rect.origin.y - rect.size.height;
23+
return NSMakeRect(rect.origin.x, bottomY, rect.size.width, rect.size.height);
24+
}
25+
};
26+
27+
// NSPoint extension-like helper for coordinate system conversion
28+
struct NSPointExt {
29+
static CGPoint topLeft(NSPoint point) {
30+
NSRect primaryScreenFrame = [[NSScreen screens][0] frame];
31+
return CGPointMake(point.x, primaryScreenFrame.size.height - point.y);
32+
}
33+
34+
// Convert from top-left coordinate system to bottom-left (macOS default)
35+
static NSPoint bottomLeft(CGPoint topLeftPoint) {
36+
NSRect primaryScreenFrame = [[NSScreen screens][0] frame];
37+
return NSMakePoint(topLeftPoint.x, primaryScreenFrame.size.height - topLeftPoint.y);
38+
}
39+
};
40+
41+
} // namespace nativeapi

src/platform/macos/display_macos.mm

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
#include "../../display.h"
2+
#include "coordinate_utils_macos.h"
23

34
// Import Cocoa and Core Graphics headers
45
#import <Cocoa/Cocoa.h>
56
#import <CoreGraphics/CoreGraphics.h>
67

78
namespace nativeapi {
89

9-
// NSRect extension-like helper for coordinate system conversion
10-
struct NSRectExt {
11-
static CGPoint topLeft(NSRect rect) {
12-
NSRect primaryScreenFrame = [[NSScreen screens][0] frame];
13-
return CGPointMake(rect.origin.x,
14-
primaryScreenFrame.size.height - rect.origin.y - rect.size.height);
15-
}
16-
};
17-
1810
// Private implementation class
1911
class Display::Impl {
2012
public:

src/platform/macos/display_manager_macos.mm

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "../../display.h"
88
#include "../../display_manager.h"
9+
#include "coordinate_utils_macos.h"
910

1011
// Import Cocoa and Core Graphics headers
1112
#import <Cocoa/Cocoa.h>
@@ -80,9 +81,13 @@ static Display CreateDisplayFromNSScreen(NSScreen* screen, bool isPrimary) {
8081

8182
Point DisplayManager::GetCursorPosition() {
8283
NSPoint mouseLocation = [NSEvent mouseLocation];
84+
85+
// Convert from bottom-left (macOS default) to top-left coordinate system
86+
CGPoint topLeftPoint = NSPointExt::topLeft(mouseLocation);
87+
8388
Point point;
84-
point.x = mouseLocation.x;
85-
point.y = mouseLocation.y;
89+
point.x = topLeftPoint.x;
90+
point.y = topLeftPoint.y;
8691
return point;
8792
}
8893

src/platform/macos/window_macos.mm

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <iostream>
22
#include "../../window.h"
33
#include "../../window_manager.h"
4+
#include "coordinate_utils_macos.h"
45

56
// Import Cocoa headers
67
#import <Cocoa/Cocoa.h>
@@ -107,15 +108,18 @@
107108
//// Color Window::GetBackgroundColor() const;
108109

109110
void Window::SetBounds(Rectangle bounds) {
110-
[pimpl_->ns_window_ setFrame:NSMakeRect(bounds.x, bounds.y, bounds.width, bounds.height)
111-
display:YES];
111+
// Convert from topLeft coordinate system to bottom-left (macOS default)
112+
NSRect topLeftRect = NSMakeRect(bounds.x, bounds.y, bounds.width, bounds.height);
113+
NSRect nsRect = NSRectExt::bottomLeft(topLeftRect);
114+
[pimpl_->ns_window_ setFrame:nsRect display:YES];
112115
}
113116

114117
Rectangle Window::GetBounds() const {
115118
NSRect frame = [pimpl_->ns_window_ frame];
116-
Rectangle bounds = {static_cast<double>(frame.origin.x), static_cast<double>(frame.origin.y),
117-
static_cast<double>(frame.size.height),
118-
static_cast<double>(frame.size.width)};
119+
// Convert from bottom-left (macOS default) to top-left coordinate system
120+
CGPoint topLeft = NSRectExt::topLeft(frame);
121+
Rectangle bounds = {topLeft.x, topLeft.y, static_cast<double>(frame.size.width),
122+
static_cast<double>(frame.size.height)};
119123
return bounds;
120124
}
121125

@@ -246,16 +250,17 @@
246250
}
247251

248252
void Window::SetPosition(Point point) {
249-
NSRect screenFrameRect = [[NSScreen screens][0] frame];
250-
// Convert point to bottom left origin
251-
NSPoint bottomLeft =
252-
NSMakePoint(point.x, screenFrameRect.size.height - point.y - GetSize().height);
253+
// Convert from topLeft coordinate system to bottom-left (macOS default)
254+
NSPoint topLeftPoint = {point.x, point.y};
255+
NSPoint bottomLeft = NSPointExt::bottomLeft(topLeftPoint);
253256
[pimpl_->ns_window_ setFrameOrigin:bottomLeft];
254257
}
255258

256259
Point Window::GetPosition() const {
257260
NSRect frame = [pimpl_->ns_window_ frame];
258-
Point point = {static_cast<double>(frame.origin.x), static_cast<double>(frame.origin.y)};
261+
// Convert from bottom-left (macOS default) to top-left coordinate system
262+
CGPoint topLeft = NSRectExt::topLeft(frame);
263+
Point point = {topLeft.x, topLeft.y};
259264
return point;
260265
}
261266

0 commit comments

Comments
 (0)