diff options
author | Akihiko Odaki <akihiko.odaki@daynix.com> | 2024-03-23 15:20:01 +0900 |
---|---|---|
committer | Philippe Mathieu-Daudé <philmd@linaro.org> | 2024-03-26 14:32:54 +0100 |
commit | d2ee0420a394bb0affb06659bdb5d706af24157b (patch) | |
tree | 4d50aee8d9bc90fa1371d21a20e815fbcb1eca65 | |
parent | 5c5d00df67a33d3931c35d566329f3d90013732b (diff) |
ui/cocoa: Fix aspect ratio
[NSWindow setContentAspectRatio:] does not trigger window resize itself,
so the wrong aspect ratio will persist if nothing resizes the window.
Call [NSWindow setContentSize:] in such a case.
Fixes: 91aa508d0274 ("ui/cocoa: Let the platform toggle fullscreen")
Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Message-ID: <20240323-fixes-v2-1-18651a2b0394@daynix.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
-rw-r--r-- | ui/cocoa.m | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/ui/cocoa.m b/ui/cocoa.m index 810751cf26..17e125d66d 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -519,6 +519,43 @@ static CGEventRef handleTapEvent(CGEventTapProxy proxy, CGEventType type, CGEven } } +- (NSSize)fixAspectRatio:(NSSize)max +{ + NSSize scaled; + NSSize fixed; + + scaled.width = screen.width * max.height; + scaled.height = screen.height * max.width; + + /* + * Here screen is our guest's output size, and max is the size of the + * largest possible area of the screen we can display on. + * We want to scale up (screen.width x screen.height) by either: + * 1) max.height / screen.height + * 2) max.width / screen.width + * With the first scale factor the scale will result in an output height of + * max.height (i.e. we will fill the whole height of the available screen + * space and have black bars left and right) and with the second scale + * factor the scaling will result in an output width of max.width (i.e. we + * fill the whole width of the available screen space and have black bars + * top and bottom). We need to pick whichever keeps the whole of the guest + * output on the screen, which is to say the smaller of the two scale + * factors. + * To avoid doing more division than strictly necessary, instead of directly + * comparing scale factors 1 and 2 we instead calculate and compare those + * two scale factors multiplied by (screen.height * screen.width). + */ + if (scaled.width < scaled.height) { + fixed.width = scaled.width / screen.height; + fixed.height = max.height; + } else { + fixed.width = max.width; + fixed.height = scaled.height / screen.width; + } + + return fixed; +} + - (NSSize) screenSafeAreaSize { NSSize size = [[[self window] screen] frame].size; @@ -536,8 +573,10 @@ static CGEventRef handleTapEvent(CGEventTapProxy proxy, CGEventType type, CGEven [[self window] setContentSize:NSMakeSize(screen.width, screen.height)]; [[self window] center]; } else if ([[self window] styleMask] & NSWindowStyleMaskFullScreen) { - [[self window] setContentSize:[self screenSafeAreaSize]]; + [[self window] setContentSize:[self fixAspectRatio:[self screenSafeAreaSize]]]; [[self window] center]; + } else { + [[self window] setContentSize:[self fixAspectRatio:[self frame].size]]; } } |