From 19536fc7a7048abd4a22c40c78394807eb88cf8c Mon Sep 17 00:00:00 2001 From: reuk Date: Tue, 1 Feb 2022 16:47:01 +0000 Subject: [PATCH] NSViewComponentPeer: Allow restoring previous size when zooming a non-native window --- .../native/juce_mac_NSViewComponentPeer.mm | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm b/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm index 762077e21d..d8b8b0fb02 100644 --- a/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm +++ b/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm @@ -1625,6 +1625,7 @@ public: String stringBeingComposed; NSNotificationCenter* notificationCenter = nil; + Rectangle lastSizeBeforeZoom; RectangleList deferredRepaints; uint32 lastRepaintTime; @@ -2384,39 +2385,42 @@ private: #endif } - static NSRect windowWillUseStandardFrame (id self, SEL, NSWindow*, NSRect r) + static NSRect windowWillUseStandardFrame (id self, SEL, NSWindow* window, NSRect r) { if (auto* owner = getOwner (self)) { if (auto* constrainer = owner->getConstrainer()) { - const auto originalBounds = [&]() -> Rectangle + if (auto* screen = [window screen]) { - const auto screenBounds = owner->getComponent().getScreenBounds(); + const auto safeScreenBounds = convertToRectFloat (flippedScreenRect (owner->hasNativeTitleBar() ? r : [screen visibleFrame])); + const auto originalBounds = owner->getFrameSize().addedTo (owner->getComponent().getScreenBounds()).toFloat(); + const auto expanded = originalBounds.withWidth ((float) constrainer->getMaximumWidth()) + .withHeight ((float) constrainer->getMaximumHeight()); + const auto constrained = expanded.constrainedWithin (safeScreenBounds); - if (const auto frameSize = owner->getFrameSizeIfPresent()) - return frameSize->addedTo (screenBounds).toFloat(); - - return screenBounds.toFloat(); - }(); + return flippedScreenRect (makeNSRect ([&] + { + if (constrained == owner->getBounds().toFloat()) + return owner->lastSizeBeforeZoom.toFloat(); - const auto expanded = originalBounds.withWidth ((float) constrainer->getMaximumWidth()) - .withHeight ((float) constrainer->getMaximumHeight()); - const auto constrained = expanded.constrainedWithin (convertToRectFloat (flippedScreenRect (r))); - return flippedScreenRect (makeNSRect (constrained)); + owner->lastSizeBeforeZoom = owner->getBounds().toFloat(); + return constrained; + }())); + } } } return r; } - static BOOL windowShouldZoomToFrame (id self, SEL, NSWindow* window, NSRect frame) + static BOOL windowShouldZoomToFrame (id self, SEL, NSWindow*, NSRect) { if (auto* owner = getOwner (self)) if (owner->hasNativeTitleBar() && (owner->getStyleFlags() & ComponentPeer::windowIsResizable) == 0) return NO; - return convertToRectFloat ([window frame]).withZeroOrigin() != convertToRectFloat (frame).withZeroOrigin(); + return YES; } static BOOL canBecomeKeyWindow (id self, SEL)