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.

208 lines
7.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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 = ^ (UIActivityType type, BOOL completed,
  84. NSArray* returnedItems, NSError* error)
  85. {
  86. ignoreUnused (type);
  87. ignoreUnused (returnedItems);
  88. succeeded = completed;
  89. if (error != nil)
  90. errorDescription = nsStringToJuce ([error localizedDescription]);
  91. exitModalState (0);
  92. };
  93. controller.get().modalTransitionStyle = UIModalTransitionStyleCoverVertical;
  94. auto bounds = Desktop::getInstance().getDisplays().getMainDisplay().userArea;
  95. setBounds (bounds);
  96. setAlwaysOnTop (true);
  97. setVisible (true);
  98. addToDesktop (0);
  99. enterModalState (true,
  100. ModalCallbackFunction::create ([this] (int)
  101. {
  102. owner.sharingFinished (succeeded, errorDescription);
  103. }),
  104. false);
  105. }
  106. static bool isIPad()
  107. {
  108. return [UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad;
  109. }
  110. //==============================================================================
  111. void parentHierarchyChanged() override
  112. {
  113. auto* newPeer = dynamic_cast<UIViewComponentPeer*> (getPeer());
  114. if (peer != newPeer)
  115. {
  116. peer = newPeer;
  117. if (isIPad())
  118. {
  119. controller.get().preferredContentSize = peer->view.frame.size;
  120. auto screenBounds = [UIScreen mainScreen].bounds;
  121. auto* popoverController = controller.get().popoverPresentationController;
  122. popoverController.sourceView = peer->view;
  123. popoverController.sourceRect = CGRectMake (0.f, screenBounds.size.height - 10.f, screenBounds.size.width, 10.f);
  124. popoverController.canOverlapSourceViewRect = YES;
  125. popoverController.delegate = popoverDelegate.get();
  126. }
  127. if (auto* parentController = peer->controller)
  128. [parentController showViewController: controller.get() sender: parentController];
  129. }
  130. }
  131. //==============================================================================
  132. struct PopoverDelegateClass : public ObjCClass<NSObject<UIPopoverPresentationControllerDelegate>>
  133. {
  134. PopoverDelegateClass() : ObjCClass<NSObject<UIPopoverPresentationControllerDelegate>> ("PopoverDelegateClass_")
  135. {
  136. addMethod (@selector (popoverPresentationController:willRepositionPopoverToRect:inView:), willRepositionPopover, "v@:@@@");
  137. registerClass();
  138. }
  139. //==============================================================================
  140. static void willRepositionPopover (id, SEL, UIPopoverPresentationController*, CGRect* rect, UIView*)
  141. {
  142. auto screenBounds = [UIScreen mainScreen].bounds;
  143. rect->origin.x = 0.f;
  144. rect->origin.y = screenBounds.size.height - 10.f;
  145. rect->size.width = screenBounds.size.width;
  146. rect->size.height = 10.f;
  147. }
  148. };
  149. ContentSharer& owner;
  150. UIViewComponentPeer* peer = nullptr;
  151. std::unique_ptr<UIActivityViewController, NSObjectDeleter> controller;
  152. std::unique_ptr<NSObject<UIPopoverPresentationControllerDelegate>, NSObjectDeleter> popoverDelegate;
  153. bool succeeded = false;
  154. String errorDescription;
  155. };
  156. //==============================================================================
  157. ContentSharer::Pimpl* ContentSharer::createPimpl()
  158. {
  159. return new ContentSharerNativeImpl (*this);
  160. }
  161. } // namespace juce