The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
4.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce::detail
  19. {
  20. /**
  21. Sets up a native control to be hosted on top of a JUCE component.
  22. */
  23. class NativeModalWrapperComponent : public Component
  24. {
  25. public:
  26. void parentHierarchyChanged() final
  27. {
  28. auto* newPeer = dynamic_cast<UIViewComponentPeer*> (getPeer());
  29. if (std::exchange (peer, newPeer) == newPeer)
  30. return;
  31. if (peer == nullptr)
  32. return;
  33. if (isIPad())
  34. {
  35. getViewController().preferredContentSize = peer->view.frame.size;
  36. if (auto* popoverController = getViewController().popoverPresentationController)
  37. {
  38. popoverController.sourceView = peer->view;
  39. popoverController.sourceRect = CGRectMake (0.0f, (float) getHeight() - 10.0f, (float) getWidth(), 10.0f);
  40. popoverController.canOverlapSourceViewRect = YES;
  41. popoverController.delegate = popoverDelegate.get();
  42. }
  43. }
  44. if (auto* parentController = peer->controller)
  45. [parentController showViewController: getViewController() sender: parentController];
  46. peer->toFront (false);
  47. }
  48. void displayNativeWindowModally (Component* parent)
  49. {
  50. setOpaque (false);
  51. if (parent != nullptr)
  52. {
  53. [getViewController() setModalPresentationStyle: UIModalPresentationPageSheet];
  54. setBounds (parent->getLocalBounds());
  55. setAlwaysOnTop (true);
  56. parent->addAndMakeVisible (this);
  57. }
  58. else
  59. {
  60. if (SystemStats::isRunningInAppExtensionSandbox())
  61. {
  62. // Opening a native top-level window in an AUv3 is not allowed (sandboxing). You need to specify a
  63. // parent component (for example your editor) to parent the native file chooser window. To do this
  64. // specify a parent component in the FileChooser's constructor!
  65. jassertfalse;
  66. return;
  67. }
  68. auto chooserBounds = Desktop::getInstance().getDisplays().getPrimaryDisplay()->userArea;
  69. setBounds (chooserBounds);
  70. setAlwaysOnTop (true);
  71. setVisible (true);
  72. addToDesktop (0);
  73. }
  74. }
  75. private:
  76. virtual UIViewController* getViewController() const = 0;
  77. static bool isIPad()
  78. {
  79. return [UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad;
  80. }
  81. struct PopoverDelegateClass : public ObjCClass<NSObject<UIPopoverPresentationControllerDelegate>>
  82. {
  83. PopoverDelegateClass()
  84. : ObjCClass ("PopoverDelegateClass_")
  85. {
  86. addMethod (@selector (popoverPresentationController:willRepositionPopoverToRect:inView:), [] (id, SEL, UIPopoverPresentationController*, CGRect* rect, UIView*)
  87. {
  88. auto screenBounds = [UIScreen mainScreen].bounds;
  89. rect->origin.x = 0.f;
  90. rect->origin.y = screenBounds.size.height - 10.f;
  91. rect->size.width = screenBounds.size.width;
  92. rect->size.height = 10.f;
  93. });
  94. registerClass();
  95. }
  96. };
  97. UIViewComponentPeer* peer = nullptr;
  98. NSUniquePtr<NSObject<UIPopoverPresentationControllerDelegate>> popoverDelegate { []
  99. {
  100. static PopoverDelegateClass cls;
  101. return cls.createInstance();
  102. }() };
  103. };
  104. } // namespace juce::detail