Skip to content

Commit 05ce978

Browse files
committed
cocoa: Use cached viewport dimensions when querying the window pixel size
Recalculate the backing viewport dimensions in the resize handler, otherwise, this data can be out-of-sync with the logical window size if queried during transition animations.
1 parent 97e2951 commit 05ce978

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/video/cocoa/SDL_cocoawindow.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ typedef enum
141141
@property(nonatomic) SDL_Window *window;
142142
@property(nonatomic) NSWindow *nswindow;
143143
@property(nonatomic) NSView *sdlContentView;
144+
@property(nonatomic) NSRect viewport;
144145
@property(nonatomic) NSMutableArray *nscontexts;
145146
@property(nonatomic) BOOL in_blocking_transition;
146147
@property(nonatomic) BOOL fullscreen_space_requested;

src/video/cocoa/SDL_cocoawindow.m

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,12 @@ - (void)windowDidResize:(NSNotification *)aNotification
12421242
w = (int)rect.size.width;
12431243
h = (int)rect.size.height;
12441244

1245+
_data.viewport = [_data.sdlContentView bounds];
1246+
if (window->flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) {
1247+
// This gives us the correct viewport for a Retina-enabled view.
1248+
_data.viewport = [_data.sdlContentView convertRectToBacking:_data.viewport];
1249+
}
1250+
12451251
ScheduleContextUpdates(_data);
12461252

12471253
/* The OS can resize the window automatically if the display density
@@ -2274,6 +2280,12 @@ static bool SetupWindowData(SDL_VideoDevice *_this, SDL_Window *window, NSWindow
22742280
data.nscontexts = [[NSMutableArray alloc] init];
22752281
data.sdlContentView = nsview;
22762282

2283+
data.viewport = [data.sdlContentView bounds];
2284+
if (window->flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) {
2285+
// This gives us the correct viewport for a Retina-enabled view.
2286+
data.viewport = [data.sdlContentView convertRectToBacking:data.viewport];
2287+
}
2288+
22772289
// Create an event listener for the window
22782290
data.listener = [[SDL3Cocoa_WindowListener alloc] init];
22792291

@@ -2703,16 +2715,9 @@ void Cocoa_GetWindowSizeInPixels(SDL_VideoDevice *_this, SDL_Window *window, int
27032715
{
27042716
@autoreleasepool {
27052717
SDL_CocoaWindowData *windata = (__bridge SDL_CocoaWindowData *)window->internal;
2706-
NSView *contentView = windata.sdlContentView;
2707-
NSRect viewport = [contentView bounds];
2708-
2709-
if (window->flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) {
2710-
// This gives us the correct viewport for a Retina-enabled view.
2711-
viewport = [contentView convertRectToBacking:viewport];
2712-
}
27132718

2714-
*w = (int)viewport.size.width;
2715-
*h = (int)viewport.size.height;
2719+
*w = (int)windata.viewport.size.width;
2720+
*h = (int)windata.viewport.size.height;
27162721
}
27172722
}
27182723

0 commit comments

Comments
 (0)