Skip to contentSkip to conversation

Fixes for "ui/cocoa: Let the platform toggle fullscreen"

PATCH In review Akihiko Odaki 3 participants 6 messages Mar 23, 2024 – Mar 26, 2024

lore
Share & tools
lore
#0 lore raw

v2 cover Fixes for "ui/cocoa: Let the platform toggle fullscreen"

This series contains patches for regressions caused by commit 91aa508d0274 ("ui/cocoa: Let the platform toggle fullscreen").

---
Changes in v2:
- Added a comment to [QemuCocoaView fixAspectRatio:]. (Peter Maydell)
- Renamed [QemuCocoaView fixAspectRatio:] parameter to match the comment. (Peter Maydell)
- Noted that "ui/cocoa: Use NSTrackingInVisibleRect" fixes mouse movement tracking. (Peter Maydell)
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Akihiko Odaki (3):
ui/cocoa: Fix aspect ratio
ui/cocoa: Resize window after toggling zoom-to-fit ui/cocoa: Use NSTrackingInVisibleRect

 ui/cocoa.m | 90 ++++++++++++++++++++++++++++++++++++++------------------------
 1 file changed, 55 insertions(+), 35 deletions(-)

---
base-commit: ba49d760eb04630e7b15f423ebecf6c871b8f77b
change-id: 20240318-fixes-7b187ec236a0

Best regards,

Signature · 1 line
Akihiko Odaki <[email protected]>
#1 lore raw

PATCH v2 1/3 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")
---
 ui/cocoa.m | 41 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 40 insertions(+), 1 deletion(-)
ui/cocoa.m +40 -1
index fa879d7dcd4b..834ebf5f6175 100644
@@ -508,6 +508,43 @@ - (void) drawRect:(NSRect) rect
     }
 }
 
+- (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;
@@ -525,8 +562,10 @@ - (void) resizeWindow
         [[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]];
     }
 }
 
Signature · 1 line
2.44.0
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] ui/cocoa: Fix aspect ratio

Quoted 8 lines · Akihiko Odaki

[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")

---

thanks
-- PMM

#3 lore raw

PATCH v2 2/3 ui/cocoa: Resize window after toggling zoom-to-fit

In reply to Akihiko Odaki

Resize the window so that the content will fit without zooming.

Fixes 91aa508d0274 ("ui/cocoa: Let the platform toggle fullscreen")
---
 ui/cocoa.m | 1 +
 1 file changed, 1 insertion(+)
ui/cocoa.m +1 -0
index 834ebf5f6175..3a1b899ba768 100644
@@ -1396,6 +1396,7 @@ - (void)zoomToFit:(id) sender
 
     [[cocoaView window] setStyleMask:styleMask];
     [sender setState:styleMask & NSWindowStyleMaskResizable ? NSControlStateValueOn : NSControlStateValueOff];
+    [cocoaView resizeWindow];
 }
 
 - (void)toggleZoomInterpolation:(id) sender
Signature · 1 line
2.44.0
#4 lore raw

PATCH v2 3/3 ui/cocoa: Use NSTrackingInVisibleRect

In reply to Akihiko Odaki

I observed [NSTrackingArea rect] becomes de-synchronized with the view frame with some unknown condition, and fails to track mouse movement on some area of the view. Specify NSTrackingInVisibleRect option to let Cocoa automatically update NSTrackingArea, which also saves code for synchronization.

Fixes 91aa508d0274 ("ui/cocoa: Let the platform toggle fullscreen")
---
 ui/cocoa.m | 48 ++++++++++++++----------------------------------
 1 file changed, 14 insertions(+), 34 deletions(-)
ui/cocoa.m +14 -34
index 3a1b899ba768..fb60debb9a8e 100644
@@ -306,7 +306,6 @@ static void handleAnyDeviceErrors(Error * err)
 */
 @interface QemuCocoaView : NSView
 {
-    NSTrackingArea *trackingArea;
     QEMUScreen screen;
     pixman_image_t *pixman_image;
     QKbdState *kbd;
@@ -359,6 +358,19 @@ - (id)initWithFrame:(NSRect)frameRect
     self = [super initWithFrame:frameRect];
     if (self) {
 
+        NSTrackingAreaOptions options = NSTrackingActiveInKeyWindow |
+                                        NSTrackingMouseEnteredAndExited |
+                                        NSTrackingMouseMoved |
+                                        NSTrackingInVisibleRect;
+
+        NSTrackingArea *trackingArea =
+            [[NSTrackingArea alloc] initWithRect:CGRectZero
+                                         options:options
+                                           owner:self
+                                        userInfo:nil];
+
+        [self addTrackingArea:trackingArea];
+        [trackingArea release];
         screen.width = frameRect.size.width;
         screen.height = frameRect.size.height;
         kbd = qkbd_state_init(dcl.con);
@@ -392,41 +404,9 @@ - (BOOL) isOpaque
     return YES;
 }
 
-- (void) removeTrackingRect
-{
-    if (trackingArea) {
-        [self removeTrackingArea:trackingArea];
-        [trackingArea release];
-        trackingArea = nil;
-    }
-}
-
-- (void) frameUpdated
-{
-    [self removeTrackingRect];
-
-    if ([self window]) {
-        NSTrackingAreaOptions options = NSTrackingActiveInKeyWindow |
-                                        NSTrackingMouseEnteredAndExited |
-                                        NSTrackingMouseMoved;
-        trackingArea = [[NSTrackingArea alloc] initWithRect:[self frame]
-                                                    options:options
-                                                      owner:self
-                                                   userInfo:nil];
-        [self addTrackingArea:trackingArea];
-        [self updateUIInfo];
-    }
-}
-
 - (void) viewDidMoveToWindow
 {
     [self resizeWindow];
-    [self frameUpdated];
-}
-
-- (void) viewWillMoveToWindow:(NSWindow *)newWindow
-{
-    [self removeTrackingRect];
 }
 
 - (void) hideCursor
@@ -1302,7 +1282,7 @@ - (void)windowDidExitFullScreen:(NSNotification *)notification
 - (void)windowDidResize:(NSNotification *)notification
 {
     [cocoaView updateBounds];
-    [cocoaView frameUpdated];
+    [cocoaView updateUIInfo];
 }
 
 /* Called when the user clicks on a window's close button */
Signature · 1 line
2.44.0
#5 lore raw
In reply to Akihiko Odaki
Quoted 4 lines · Akihiko Odaki

This series contains patches for regressions caused by commit 91aa508d0274 ("ui/cocoa: Let the platform toggle fullscreen").

Quoted 8 lines

---
Akihiko Odaki (3):
ui/cocoa: Fix aspect ratio
ui/cocoa: Resize window after toggling zoom-to-fit ui/cocoa: Use NSTrackingInVisibleRect

  ui/cocoa.m | 90 ++++++++++++++++++++++++++++++++++++++------------------------
  1 file changed, 55 insertions(+), 35 deletions(-)

Thanks, series queued.