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.

205 lines
7.2KB

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