Audio plugin host https://kx.studio/carla
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.

212 lines
7.4KB

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