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.

207 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. addToDesktop (0);
  98. enterModalState (true,
  99. ModalCallbackFunction::create ([this] (int)
  100. {
  101. owner.sharingFinished (succeeded, errorDescription);
  102. }),
  103. false);
  104. }
  105. static bool isIPad()
  106. {
  107. return [UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad;
  108. }
  109. //==============================================================================
  110. void parentHierarchyChanged() override
  111. {
  112. auto* newPeer = dynamic_cast<UIViewComponentPeer*> (getPeer());
  113. if (peer != newPeer)
  114. {
  115. peer = newPeer;
  116. if (isIPad())
  117. {
  118. controller.get().preferredContentSize = peer->view.frame.size;
  119. auto screenBounds = [UIScreen mainScreen].bounds;
  120. auto* popoverController = controller.get().popoverPresentationController;
  121. popoverController.sourceView = peer->view;
  122. popoverController.sourceRect = CGRectMake (0.f, screenBounds.size.height - 10.f, screenBounds.size.width, 10.f);
  123. popoverController.canOverlapSourceViewRect = YES;
  124. popoverController.delegate = popoverDelegate.get();
  125. }
  126. if (auto* parentController = peer->controller)
  127. [parentController showViewController: controller.get() sender: parentController];
  128. }
  129. }
  130. //==============================================================================
  131. struct PopoverDelegateClass : public ObjCClass<NSObject<UIPopoverPresentationControllerDelegate>>
  132. {
  133. PopoverDelegateClass() : ObjCClass<NSObject<UIPopoverPresentationControllerDelegate>> ("PopoverDelegateClass_")
  134. {
  135. addMethod (@selector (popoverPresentationController:willRepositionPopoverToRect:inView:), willRepositionPopover, "v@:@@@");
  136. registerClass();
  137. }
  138. //==============================================================================
  139. static void willRepositionPopover (id, SEL, UIPopoverPresentationController*, CGRect* rect, UIView*)
  140. {
  141. auto screenBounds = [UIScreen mainScreen].bounds;
  142. rect->origin.x = 0.f;
  143. rect->origin.y = screenBounds.size.height - 10.f;
  144. rect->size.width = screenBounds.size.width;
  145. rect->size.height = 10.f;
  146. }
  147. };
  148. ContentSharer& owner;
  149. UIViewComponentPeer* peer = nullptr;
  150. std::unique_ptr<UIActivityViewController, NSObjectDeleter> controller;
  151. std::unique_ptr<NSObject<UIPopoverPresentationControllerDelegate>, NSObjectDeleter> popoverDelegate;
  152. bool succeeded = false;
  153. String errorDescription;
  154. };
  155. //==============================================================================
  156. ContentSharer::Pimpl* ContentSharer::createPimpl()
  157. {
  158. return new ContentSharerNativeImpl (*this);
  159. }
  160. } // namespace juce