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.

211 lines
7.5KB

  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. class ContentSharer::ContentSharerNativeImpl : public ContentSharer::Pimpl,
  22. private Component
  23. {
  24. public:
  25. ContentSharerNativeImpl (ContentSharer& cs)
  26. : owner (cs)
  27. {
  28. static PopoverDelegateClass cls;
  29. popoverDelegate.reset ([cls.createInstance() init]);
  30. }
  31. ~ContentSharerNativeImpl()
  32. {
  33. exitModalState (0);
  34. }
  35. void shareFiles (const Array<URL>& files) override
  36. {
  37. auto* urls = [NSMutableArray arrayWithCapacity: (NSUInteger) files.size()];
  38. for (const auto& f : files)
  39. {
  40. NSString* nativeFilePath = nil;
  41. if (f.isLocalFile())
  42. {
  43. nativeFilePath = juceStringToNS (f.getLocalFile().getFullPathName());
  44. }
  45. else
  46. {
  47. auto filePath = f.toString (false);
  48. auto* fileDirectory = filePath.contains ("/")
  49. ? juceStringToNS (filePath.upToLastOccurrenceOf ("/", false, false))
  50. : [NSString string];
  51. auto fileName = juceStringToNS (filePath.fromLastOccurrenceOf ("/", false, false)
  52. .upToLastOccurrenceOf (".", false, false));
  53. auto fileExt = juceStringToNS (filePath.fromLastOccurrenceOf (".", false, false));
  54. if ([fileDirectory length] == NSUInteger (0))
  55. nativeFilePath = [[NSBundle mainBundle] pathForResource: fileName
  56. ofType: fileExt];
  57. else
  58. nativeFilePath = [[NSBundle mainBundle] pathForResource: fileName
  59. ofType: fileExt
  60. inDirectory: fileDirectory];
  61. }
  62. if (nativeFilePath != nil)
  63. [urls addObject: [NSURL fileURLWithPath: nativeFilePath]];
  64. }
  65. share (urls);
  66. }
  67. void shareText (const String& text) override
  68. {
  69. auto* array = [NSArray arrayWithObject: juceStringToNS (text)];
  70. share (array);
  71. }
  72. private:
  73. void share (NSArray* items)
  74. {
  75. if ([items count] == 0)
  76. {
  77. jassertfalse;
  78. owner.sharingFinished (false, "No valid items found for sharing.");
  79. return;
  80. }
  81. controller.reset ([[UIActivityViewController alloc] initWithActivityItems: items
  82. applicationActivities: nil]);
  83. controller.get().excludedActivityTypes = nil;
  84. controller.get().completionWithItemsHandler = ^ (UIActivityType type, BOOL completed,
  85. NSArray* returnedItems, NSError* error)
  86. {
  87. ignoreUnused (type);
  88. ignoreUnused (returnedItems);
  89. succeeded = completed;
  90. if (error != nil)
  91. errorDescription = nsStringToJuce ([error localizedDescription]);
  92. exitModalState (0);
  93. };
  94. controller.get().modalTransitionStyle = UIModalTransitionStyleCoverVertical;
  95. auto bounds = Desktop::getInstance().getDisplays().getMainDisplay().userArea;
  96. setBounds (bounds);
  97. setAlwaysOnTop (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. if (peer->view.window != nil)
  130. peer->view.window.autoresizesSubviews = YES;
  131. }
  132. }
  133. //==============================================================================
  134. struct PopoverDelegateClass : public ObjCClass<NSObject<UIPopoverPresentationControllerDelegate>>
  135. {
  136. PopoverDelegateClass() : ObjCClass<NSObject<UIPopoverPresentationControllerDelegate>> ("PopoverDelegateClass_")
  137. {
  138. addMethod (@selector (popoverPresentationController:willRepositionPopoverToRect:inView:), willRepositionPopover, "v@:@@@");
  139. registerClass();
  140. }
  141. //==============================================================================
  142. static void willRepositionPopover (id, SEL, UIPopoverPresentationController*, CGRect* rect, UIView*)
  143. {
  144. auto screenBounds = [UIScreen mainScreen].bounds;
  145. rect->origin.x = 0.f;
  146. rect->origin.y = screenBounds.size.height - 10.f;
  147. rect->size.width = screenBounds.size.width;
  148. rect->size.height = 10.f;
  149. }
  150. };
  151. ContentSharer& owner;
  152. UIViewComponentPeer* peer = nullptr;
  153. std::unique_ptr<UIActivityViewController, NSObjectDeleter> controller;
  154. std::unique_ptr<NSObject<UIPopoverPresentationControllerDelegate>, NSObjectDeleter> popoverDelegate;
  155. bool succeeded = false;
  156. String errorDescription;
  157. };
  158. //==============================================================================
  159. ContentSharer::Pimpl* ContentSharer::createPimpl()
  160. {
  161. return new ContentSharerNativeImpl (*this);
  162. }
  163. } // namespace juce