@@ -651,42 +651,78 @@ int juce_iOSMain (int argc, const char* argv[], void* customDelegatePtr)
651651 return Orientations::convertToJuce (orientation);
652652}
653653
654- // The most straightforward way of retrieving the screen area available to an iOS app
655- // seems to be to create a new window (which will take up all available space) and to
656- // query its frame.
657- struct TemporaryWindow
654+ struct WindowInfo
655+ {
656+ explicit WindowInfo (const UIWindow* window)
657+ : bounds (convertToRectInt (window.frame)),
658+ safeInsets (window.safeAreaInsets.top,
659+ window.safeAreaInsets.left,
660+ window.safeAreaInsets.bottom,
661+ window.safeAreaInsets.right)
662+ {}
663+
664+ Rectangle<int > bounds;
665+ BorderSize<double > safeInsets;
666+ };
667+
668+ static const UIWindow* findWindow (const UIView* view)
658669{
659- UIWindow* window = std::invoke ([&]
670+ if (view == nullptr )
671+ return nullptr ;
672+
673+ if (view.window != nullptr )
674+ return view.window ;
675+
676+ return findWindow (view.superview );
677+ }
678+
679+ static const UIWindow* findWindow (const Desktop& desktop)
680+ {
681+ if (auto * c = desktop.getComponent (0 ))
682+ if (auto * p = static_cast <UIViewComponentPeer*> (c->getPeer ()))
683+ if (auto * w = findWindow (p->view ))
684+ return w;
685+
686+ return {};
687+ }
688+
689+ static WindowInfo getWindowInfo (const Desktop& desktop)
690+ {
691+ if (! JUCEApplication::isStandaloneApp ())
692+ if (const auto * window = findWindow (desktop))
693+ return WindowInfo { window };
694+
695+ const auto createTemporaryWindow = []()
660696 {
661697 if (@available (iOS 13 , *))
662698 {
663699 SharedResourcePointer<WindowSceneTracker> windowSceneTracker;
664700
665701 if (auto * scene = windowSceneTracker->getWindowScene ())
666- return [[UIWindow alloc ] initWithWindowScene: scene];
702+ return NSUniquePtr<UIWindow> { [[UIWindow alloc ] initWithWindowScene: scene] } ;
667703 }
668704
669- return [[UIWindow alloc ] init ];
670- });
671- ~TemporaryWindow () noexcept { [window release ]; }
672- };
705+ return NSUniquePtr<UIWindow> { [[UIWindow alloc ] init ] };
706+ };
707+
708+ auto window (createTemporaryWindow ());
709+ return WindowInfo { window.get () };
710+ }
673711
674- static Rectangle<int > getRecommendedWindowBounds ( )
712+ static Rectangle<int > getRecommendedWindowBounds ( const Desktop& desktop )
675713{
676- return convertToRectInt ( TemporaryWindow (). window . frame ) ;
714+ return getWindowInfo (desktop). bounds ;
677715}
678716
679- static BorderSize<int > getSafeAreaInsets (float masterScale )
717+ static BorderSize<int > getSafeAreaInsets (const Desktop& desktop )
680718{
681- UIEdgeInsets safeInsets = TemporaryWindow ().window .safeAreaInsets ;
682- return detail::WindowingHelpers::roundToInt (BorderSize<double > { safeInsets.top ,
683- safeInsets.left ,
684- safeInsets.bottom ,
685- safeInsets.right }.multipliedBy (1.0 / (double ) masterScale));
719+ const auto masterScale = (double ) desktop.getGlobalScaleFactor ();
720+ const auto safeInsets = getWindowInfo (desktop).safeInsets ;
721+ return detail::WindowingHelpers::roundToInt (safeInsets.multipliedBy (1.0 / masterScale));
686722}
687723
688724// ==============================================================================
689- void Displays::findDisplays (float masterScale )
725+ void Displays::findDisplays (const Desktop& desktop )
690726{
691727 JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE (" -Wundeclared-selector" )
692728 static const auto keyboardShownSelector = @selector (juceKeyboardShown: );
@@ -716,7 +752,7 @@ int juce_iOSMain (int argc, const char* argv[], void* customDelegatePtr)
716752
717753 addMethod (keyboardShownSelector, [] (id self, SEL , NSNotification * notification)
718754 {
719- setKeyboardScreenBounds (self, [&]() -> BorderSize<double >
755+ setKeyboardScreenBounds (self, std::invoke ( [&]() -> BorderSize<double >
720756 {
721757 auto * info = [notification userInfo ];
722758
@@ -744,7 +780,7 @@ int juce_iOSMain (int argc, const char* argv[], void* customDelegatePtr)
744780 result.setBottom (rect.getHeight ());
745781
746782 return result;
747- }( ));
783+ }));
748784 });
749785
750786 addMethod (keyboardHiddenSelector, [] (id self, SEL , NSNotification *)
@@ -777,9 +813,10 @@ static void setKeyboardScreenBounds (id self, BorderSize<double> insets)
777813 UIScreen* s = [UIScreen mainScreen ];
778814
779815 Display d;
816+ const auto masterScale = desktop.getGlobalScaleFactor ();
780817 d.totalArea = convertToRectInt ([s bounds ]) / masterScale;
781- d.userArea = getRecommendedWindowBounds ( ) / masterScale;
782- d.safeAreaInsets = getSafeAreaInsets (masterScale );
818+ d.userArea = getRecommendedWindowBounds (desktop ) / masterScale;
819+ d.safeAreaInsets = getSafeAreaInsets (desktop );
783820 const auto scaledInsets = keyboardChangeDetector.getInsets ().multipliedBy (1.0 / (double ) masterScale);
784821 d.keyboardInsets = detail::WindowingHelpers::roundToInt (scaledInsets);
785822 d.isMain = true ;
0 commit comments