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.

200 lines
7.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. class ContentSharer::ContentSharerNativeImpl : public ContentSharer::Pimpl,
  16. private Component
  17. {
  18. public:
  19. ContentSharerNativeImpl (ContentSharer& cs)
  20. : owner (cs)
  21. {
  22. static PopoverDelegateClass cls;
  23. popoverDelegate.reset ([cls.createInstance() init]);
  24. }
  25. ~ContentSharerNativeImpl() override
  26. {
  27. exitModalState (0);
  28. }
  29. void shareFiles (const Array<URL>& files) override
  30. {
  31. auto urls = [NSMutableArray arrayWithCapacity: (NSUInteger) files.size()];
  32. for (const auto& f : files)
  33. {
  34. NSString* nativeFilePath = nil;
  35. if (f.isLocalFile())
  36. {
  37. nativeFilePath = juceStringToNS (f.getLocalFile().getFullPathName());
  38. }
  39. else
  40. {
  41. auto filePath = f.toString (false);
  42. auto* fileDirectory = filePath.contains ("/")
  43. ? juceStringToNS (filePath.upToLastOccurrenceOf ("/", false, false))
  44. : [NSString string];
  45. auto fileName = juceStringToNS (filePath.fromLastOccurrenceOf ("/", false, false)
  46. .upToLastOccurrenceOf (".", false, false));
  47. auto fileExt = juceStringToNS (filePath.fromLastOccurrenceOf (".", false, false));
  48. if ([fileDirectory length] == NSUInteger (0))
  49. nativeFilePath = [[NSBundle mainBundle] pathForResource: fileName
  50. ofType: fileExt];
  51. else
  52. nativeFilePath = [[NSBundle mainBundle] pathForResource: fileName
  53. ofType: fileExt
  54. inDirectory: fileDirectory];
  55. }
  56. if (nativeFilePath != nil)
  57. [urls addObject: [NSURL fileURLWithPath: nativeFilePath]];
  58. }
  59. share (urls);
  60. }
  61. void shareText (const String& text) override
  62. {
  63. auto array = [NSArray arrayWithObject: juceStringToNS (text)];
  64. share (array);
  65. }
  66. private:
  67. void share (NSArray* items)
  68. {
  69. if ([items count] == 0)
  70. {
  71. jassertfalse;
  72. owner.sharingFinished (false, "No valid items found for sharing.");
  73. return;
  74. }
  75. controller.reset ([[UIActivityViewController alloc] initWithActivityItems: items
  76. applicationActivities: nil]);
  77. controller.get().excludedActivityTypes = nil;
  78. controller.get().completionWithItemsHandler = ^ (UIActivityType type, BOOL completed,
  79. NSArray* returnedItems, NSError* error)
  80. {
  81. ignoreUnused (type);
  82. ignoreUnused (returnedItems);
  83. succeeded = completed;
  84. if (error != nil)
  85. errorDescription = nsStringToJuce ([error localizedDescription]);
  86. exitModalState (0);
  87. };
  88. controller.get().modalTransitionStyle = UIModalTransitionStyleCoverVertical;
  89. auto bounds = Desktop::getInstance().getDisplays().getMainDisplay().userArea;
  90. setBounds (bounds);
  91. setAlwaysOnTop (true);
  92. addToDesktop (0);
  93. enterModalState (true,
  94. ModalCallbackFunction::create ([this] (int)
  95. {
  96. owner.sharingFinished (succeeded, errorDescription);
  97. }),
  98. false);
  99. }
  100. static bool isIPad()
  101. {
  102. return [UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad;
  103. }
  104. //==============================================================================
  105. void parentHierarchyChanged() override
  106. {
  107. auto* newPeer = dynamic_cast<UIViewComponentPeer*> (getPeer());
  108. if (peer != newPeer)
  109. {
  110. peer = newPeer;
  111. if (isIPad())
  112. {
  113. controller.get().preferredContentSize = peer->view.frame.size;
  114. auto screenBounds = [UIScreen mainScreen].bounds;
  115. auto* popoverController = controller.get().popoverPresentationController;
  116. popoverController.sourceView = peer->view;
  117. popoverController.sourceRect = CGRectMake (0.f, screenBounds.size.height - 10.f, screenBounds.size.width, 10.f);
  118. popoverController.canOverlapSourceViewRect = YES;
  119. popoverController.delegate = popoverDelegate.get();
  120. }
  121. if (auto* parentController = peer->controller)
  122. [parentController showViewController: controller.get() sender: parentController];
  123. }
  124. }
  125. //==============================================================================
  126. struct PopoverDelegateClass : public ObjCClass<NSObject<UIPopoverPresentationControllerDelegate>>
  127. {
  128. PopoverDelegateClass() : ObjCClass<NSObject<UIPopoverPresentationControllerDelegate>> ("PopoverDelegateClass_")
  129. {
  130. addMethod (@selector (popoverPresentationController:willRepositionPopoverToRect:inView:), willRepositionPopover, "v@:@@@");
  131. registerClass();
  132. }
  133. //==============================================================================
  134. static void willRepositionPopover (id, SEL, UIPopoverPresentationController*, CGRect* rect, UIView*)
  135. {
  136. auto screenBounds = [UIScreen mainScreen].bounds;
  137. rect->origin.x = 0.f;
  138. rect->origin.y = screenBounds.size.height - 10.f;
  139. rect->size.width = screenBounds.size.width;
  140. rect->size.height = 10.f;
  141. }
  142. };
  143. ContentSharer& owner;
  144. UIViewComponentPeer* peer = nullptr;
  145. std::unique_ptr<UIActivityViewController, NSObjectDeleter> controller;
  146. std::unique_ptr<NSObject<UIPopoverPresentationControllerDelegate>, NSObjectDeleter> popoverDelegate;
  147. bool succeeded = false;
  148. String errorDescription;
  149. };
  150. //==============================================================================
  151. ContentSharer::Pimpl* ContentSharer::createPimpl()
  152. {
  153. return new ContentSharerNativeImpl (*this);
  154. }
  155. } // namespace juce