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.

205 lines
7.2KB

  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
  19. {
  20. class ContentSharer::ContentSharerNativeImpl : public ContentSharer::Pimpl,
  21. private Component
  22. {
  23. public:
  24. ContentSharerNativeImpl (ContentSharer& cs)
  25. : owner (cs)
  26. {
  27. static PopoverDelegateClass cls;
  28. popoverDelegate.reset ([cls.createInstance() init]);
  29. }
  30. ~ContentSharerNativeImpl() override
  31. {
  32. exitModalState (0);
  33. }
  34. void shareFiles (const Array<URL>& files) override
  35. {
  36. auto urls = [NSMutableArray arrayWithCapacity: (NSUInteger) files.size()];
  37. for (const auto& f : files)
  38. {
  39. NSString* nativeFilePath = nil;
  40. if (f.isLocalFile())
  41. {
  42. nativeFilePath = juceStringToNS (f.getLocalFile().getFullPathName());
  43. }
  44. else
  45. {
  46. auto filePath = f.toString (false);
  47. auto* fileDirectory = filePath.contains ("/")
  48. ? juceStringToNS (filePath.upToLastOccurrenceOf ("/", false, false))
  49. : [NSString string];
  50. auto fileName = juceStringToNS (filePath.fromLastOccurrenceOf ("/", false, false)
  51. .upToLastOccurrenceOf (".", false, false));
  52. auto fileExt = juceStringToNS (filePath.fromLastOccurrenceOf (".", false, false));
  53. if ([fileDirectory length] == NSUInteger (0))
  54. nativeFilePath = [[NSBundle mainBundle] pathForResource: fileName
  55. ofType: fileExt];
  56. else
  57. nativeFilePath = [[NSBundle mainBundle] pathForResource: fileName
  58. ofType: fileExt
  59. inDirectory: fileDirectory];
  60. }
  61. if (nativeFilePath != nil)
  62. [urls addObject: [NSURL fileURLWithPath: nativeFilePath]];
  63. }
  64. share (urls);
  65. }
  66. void shareText (const String& text) override
  67. {
  68. auto array = [NSArray arrayWithObject: juceStringToNS (text)];
  69. share (array);
  70. }
  71. private:
  72. void share (NSArray* items)
  73. {
  74. if ([items count] == 0)
  75. {
  76. jassertfalse;
  77. owner.sharingFinished (false, "No valid items found for sharing.");
  78. return;
  79. }
  80. controller.reset ([[UIActivityViewController alloc] initWithActivityItems: items
  81. applicationActivities: nil]);
  82. controller.get().excludedActivityTypes = nil;
  83. controller.get().completionWithItemsHandler = ^([[maybe_unused]] UIActivityType type, BOOL completed,
  84. [[maybe_unused]] NSArray* returnedItems, NSError* error)
  85. {
  86. succeeded = completed;
  87. if (error != nil)
  88. errorDescription = nsStringToJuce ([error localizedDescription]);
  89. exitModalState (0);
  90. };
  91. controller.get().modalTransitionStyle = UIModalTransitionStyleCoverVertical;
  92. auto bounds = Desktop::getInstance().getDisplays().getPrimaryDisplay()->userArea;
  93. setBounds (bounds);
  94. setAlwaysOnTop (true);
  95. setVisible (true);
  96. addToDesktop (0);
  97. enterModalState (true,
  98. ModalCallbackFunction::create ([this] (int)
  99. {
  100. owner.sharingFinished (succeeded, errorDescription);
  101. }),
  102. false);
  103. }
  104. static bool isIPad()
  105. {
  106. return [UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad;
  107. }
  108. //==============================================================================
  109. void parentHierarchyChanged() override
  110. {
  111. auto* newPeer = dynamic_cast<UIViewComponentPeer*> (getPeer());
  112. if (peer != newPeer)
  113. {
  114. peer = newPeer;
  115. if (isIPad())
  116. {
  117. controller.get().preferredContentSize = peer->view.frame.size;
  118. auto screenBounds = [UIScreen mainScreen].bounds;
  119. auto* popoverController = controller.get().popoverPresentationController;
  120. popoverController.sourceView = peer->view;
  121. popoverController.sourceRect = CGRectMake (0.f, screenBounds.size.height - 10.f, screenBounds.size.width, 10.f);
  122. popoverController.canOverlapSourceViewRect = YES;
  123. popoverController.delegate = popoverDelegate.get();
  124. }
  125. if (auto* parentController = peer->controller)
  126. [parentController showViewController: controller.get() sender: parentController];
  127. }
  128. }
  129. //==============================================================================
  130. struct PopoverDelegateClass : public ObjCClass<NSObject<UIPopoverPresentationControllerDelegate>>
  131. {
  132. PopoverDelegateClass() : ObjCClass<NSObject<UIPopoverPresentationControllerDelegate>> ("PopoverDelegateClass_")
  133. {
  134. addMethod (@selector (popoverPresentationController:willRepositionPopoverToRect:inView:), willRepositionPopover);
  135. registerClass();
  136. }
  137. //==============================================================================
  138. static void willRepositionPopover (id, SEL, UIPopoverPresentationController*, CGRect* rect, UIView*)
  139. {
  140. auto screenBounds = [UIScreen mainScreen].bounds;
  141. rect->origin.x = 0.f;
  142. rect->origin.y = screenBounds.size.height - 10.f;
  143. rect->size.width = screenBounds.size.width;
  144. rect->size.height = 10.f;
  145. }
  146. };
  147. ContentSharer& owner;
  148. UIViewComponentPeer* peer = nullptr;
  149. NSUniquePtr<UIActivityViewController> controller;
  150. NSUniquePtr<NSObject<UIPopoverPresentationControllerDelegate>> popoverDelegate;
  151. bool succeeded = false;
  152. String errorDescription;
  153. };
  154. //==============================================================================
  155. ContentSharer::Pimpl* ContentSharer::createPimpl()
  156. {
  157. return new ContentSharerNativeImpl (*this);
  158. }
  159. } // namespace juce