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.

215 lines
7.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. template <> struct ContainerDeletePolicy<UIActivityViewController> { static void destroy (NSObject* o) { [o release]; } };
  22. template <> struct ContainerDeletePolicy<NSObject<UIPopoverPresentationControllerDelegate>> { static void destroy (NSObject* o) { [o release]; } };
  23. //==============================================================================
  24. class ContentSharer::ContentSharerNativeImpl : public ContentSharer::Pimpl,
  25. private Component
  26. {
  27. public:
  28. ContentSharerNativeImpl (ContentSharer& cs)
  29. : owner (cs)
  30. {
  31. static PopoverDelegateClass cls;
  32. popoverDelegate = [cls.createInstance() init];
  33. }
  34. ~ContentSharerNativeImpl()
  35. {
  36. exitModalState (0);
  37. }
  38. void shareFiles (const Array<URL>& files) override
  39. {
  40. auto* urls = [NSMutableArray arrayWithCapacity: (NSUInteger) files.size()];
  41. for (const auto& f : files)
  42. {
  43. NSString* nativeFilePath = nil;
  44. if (f.isLocalFile())
  45. {
  46. nativeFilePath = juceStringToNS (f.getLocalFile().getFullPathName());
  47. }
  48. else
  49. {
  50. auto filePath = f.toString (false);
  51. auto* fileDirectory = filePath.contains ("/")
  52. ? juceStringToNS (filePath.upToLastOccurrenceOf ("/", false, false))
  53. : [NSString string];
  54. auto fileName = juceStringToNS (filePath.fromLastOccurrenceOf ("/", false, false)
  55. .upToLastOccurrenceOf (".", false, false));
  56. auto fileExt = juceStringToNS (filePath.fromLastOccurrenceOf (".", false, false));
  57. if ([fileDirectory length] == NSUInteger (0))
  58. nativeFilePath = [[NSBundle mainBundle] pathForResource: fileName
  59. ofType: fileExt];
  60. else
  61. nativeFilePath = [[NSBundle mainBundle] pathForResource: fileName
  62. ofType: fileExt
  63. inDirectory: fileDirectory];
  64. }
  65. if (nativeFilePath != nil)
  66. [urls addObject: [NSURL fileURLWithPath: nativeFilePath]];
  67. }
  68. share (urls);
  69. }
  70. void shareText (const String& text) override
  71. {
  72. auto* array = [NSArray arrayWithObject: juceStringToNS (text)];
  73. share (array);
  74. }
  75. private:
  76. void share (NSArray* items)
  77. {
  78. if ([items count] == 0)
  79. {
  80. jassertfalse;
  81. owner.sharingFinished (false, "No valid items found for sharing.");
  82. return;
  83. }
  84. controller = [[UIActivityViewController alloc] initWithActivityItems: items
  85. applicationActivities: nil];
  86. controller.get().excludedActivityTypes = nil;
  87. controller.get().completionWithItemsHandler = ^ (UIActivityType type, BOOL completed,
  88. NSArray* returnedItems, NSError* error)
  89. {
  90. ignoreUnused (type);
  91. ignoreUnused (returnedItems);
  92. succeeded = completed;
  93. if (error != nil)
  94. errorDescription = nsStringToJuce ([error localizedDescription]);
  95. exitModalState (0);
  96. };
  97. controller.get().modalTransitionStyle = UIModalTransitionStyleCoverVertical;
  98. auto bounds = Desktop::getInstance().getDisplays().getMainDisplay().userArea;
  99. setBounds (bounds);
  100. setAlwaysOnTop (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 sender: parentController];
  132. if (peer->view.window != nil)
  133. peer->view.window.autoresizesSubviews = YES;
  134. }
  135. }
  136. //==============================================================================
  137. struct PopoverDelegateClass : public ObjCClass<NSObject<UIPopoverPresentationControllerDelegate>>
  138. {
  139. PopoverDelegateClass() : ObjCClass<NSObject<UIPopoverPresentationControllerDelegate>> ("PopoverDelegateClass_")
  140. {
  141. addMethod (@selector (popoverPresentationController:willRepositionPopoverToRect:inView:), willRepositionPopover, "v@:@@@");
  142. registerClass();
  143. }
  144. //==============================================================================
  145. static void willRepositionPopover (id, SEL, UIPopoverPresentationController*, CGRect* rect, UIView*)
  146. {
  147. auto screenBounds = [UIScreen mainScreen].bounds;
  148. rect->origin.x = 0.f;
  149. rect->origin.y = screenBounds.size.height - 10.f;
  150. rect->size.width = screenBounds.size.width;
  151. rect->size.height = 10.f;
  152. }
  153. };
  154. ContentSharer& owner;
  155. UIViewComponentPeer* peer = nullptr;
  156. ScopedPointer<UIActivityViewController> controller;
  157. ScopedPointer<NSObject<UIPopoverPresentationControllerDelegate>> popoverDelegate;
  158. bool succeeded = false;
  159. String errorDescription;
  160. };
  161. //==============================================================================
  162. ContentSharer::Pimpl* ContentSharer::createPimpl()
  163. {
  164. return new ContentSharerNativeImpl (*this);
  165. }
  166. } // namespace juce