Skip to content

Commit

Permalink
cocoa: fixed resizing windows with fixed aspect ratio
Browse files Browse the repository at this point in the history
The existing algorithm works well for min-max ratios, but didn't allow edge expansion of fixed aspect ratio windows. Use NSWindow setContentAspectRatio instead.
  • Loading branch information
slouken committed Jan 15, 2025
1 parent 49dd24e commit 339d3dd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/video/cocoa/SDL_cocoavideo.m
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ static void Cocoa_DeleteDevice(SDL_VideoDevice *device)
device->SetWindowSize = Cocoa_SetWindowSize;
device->SetWindowMinimumSize = Cocoa_SetWindowMinimumSize;
device->SetWindowMaximumSize = Cocoa_SetWindowMaximumSize;
device->SetWindowAspectRatio = Cocoa_SetWindowAspectRatio;
device->SetWindowOpacity = Cocoa_SetWindowOpacity;
device->GetWindowSizeInPixels = Cocoa_GetWindowSizeInPixels;
device->ShowWindow = Cocoa_ShowWindow;
Expand Down
1 change: 1 addition & 0 deletions src/video/cocoa/SDL_cocoawindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ extern bool Cocoa_SetWindowPosition(SDL_VideoDevice *_this, SDL_Window *window);
extern void Cocoa_SetWindowSize(SDL_VideoDevice *_this, SDL_Window *window);
extern void Cocoa_SetWindowMinimumSize(SDL_VideoDevice *_this, SDL_Window *window);
extern void Cocoa_SetWindowMaximumSize(SDL_VideoDevice *_this, SDL_Window *window);
extern void Cocoa_SetWindowAspectRatio(SDL_VideoDevice *_this, SDL_Window *window);
extern void Cocoa_GetWindowSizeInPixels(SDL_VideoDevice *_this, SDL_Window *window, int *w, int *h);
extern bool Cocoa_SetWindowOpacity(SDL_VideoDevice *_this, SDL_Window *window, float opacity);
extern void Cocoa_ShowWindow(SDL_VideoDevice *_this, SDL_Window *window);
Expand Down
21 changes: 18 additions & 3 deletions src/video/cocoa/SDL_cocoawindow.m
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ - (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize
{
SDL_Window *window = _data.window;

if (window->min_aspect > 0.0f || window->max_aspect > 0.0f) {
if (window->min_aspect != window->max_aspect) {
NSWindow *nswindow = _data.nswindow;
NSRect newContentRect = [nswindow contentRectForFrameRect:NSMakeRect(0, 0, frameSize.width, frameSize.height)];
NSSize newSize = newContentRect.size;
Expand All @@ -1119,9 +1119,9 @@ - (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize
aspectRatio = newSize.width / newSize.height;

if (maxAspectRatio > 0.0f && aspectRatio > maxAspectRatio) {
newSize.width = (int)SDL_roundf(newSize.height * maxAspectRatio);
newSize.width = SDL_roundf(newSize.height * maxAspectRatio);
} else if (minAspectRatio > 0.0f && aspectRatio < minAspectRatio) {
newSize.height = (int)SDL_roundf(newSize.width / minAspectRatio);
newSize.height = SDL_roundf(newSize.width / minAspectRatio);
}

NSRect newFrameRect = [nswindow frameRectForContentRect:NSMakeRect(0, 0, newSize.width, newSize.height)];
Expand Down Expand Up @@ -2513,6 +2513,21 @@ void Cocoa_SetWindowMaximumSize(SDL_VideoDevice *_this, SDL_Window *window)
}
}

void Cocoa_SetWindowAspectRatio(SDL_VideoDevice *_this, SDL_Window *window)
{
@autoreleasepool {
SDL_CocoaWindowData *windata = (__bridge SDL_CocoaWindowData *)window->internal;

if (window->min_aspect > 0.0f && window->min_aspect == window->max_aspect) {
int numerator = 0, denominator = 1;
SDL_CalculateFraction(window->max_aspect, &numerator, &denominator);
[windata.nswindow setContentAspectRatio:NSMakeSize(numerator, denominator)];
} else {
[windata.nswindow setContentAspectRatio:NSMakeSize(0, 0)];
}
}
}

void Cocoa_GetWindowSizeInPixels(SDL_VideoDevice *_this, SDL_Window *window, int *w, int *h)
{
@autoreleasepool {
Expand Down

0 comments on commit 339d3dd

Please sign in to comment.