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.

126 lines
4.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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 NativeScopedContentSharerInterface final : public detail::ScopedContentSharerInterface,
  21. public detail::NativeModalWrapperComponent
  22. {
  23. public:
  24. NativeScopedContentSharerInterface (Component* parentIn, NSUniquePtr<NSArray> itemsIn)
  25. : parent (parentIn), items (std::move (itemsIn)) {}
  26. void runAsync (std::function<void (bool, const String&)> callback) override
  27. {
  28. if ([items.get() count] == 0)
  29. {
  30. jassertfalse;
  31. NullCheckedInvocation::invoke (callback, false, "No valid items found for sharing.");
  32. return;
  33. }
  34. controller.reset ([[UIActivityViewController alloc] initWithActivityItems: items.get()
  35. applicationActivities: nil]);
  36. controller.get().excludedActivityTypes = nil;
  37. controller.get().completionWithItemsHandler = ^([[maybe_unused]] UIActivityType type, BOOL completed,
  38. [[maybe_unused]] NSArray* returnedItems, NSError* error)
  39. {
  40. const auto errorDescription = error != nil ? nsStringToJuce ([error localizedDescription])
  41. : String();
  42. exitModalState (0);
  43. NullCheckedInvocation::invoke (callback, completed && errorDescription.isEmpty(), errorDescription);
  44. };
  45. displayNativeWindowModally (parent);
  46. enterModalState (true, nullptr, false);
  47. }
  48. void close() override
  49. {
  50. [controller.get() dismissViewControllerAnimated: YES completion: nil];
  51. }
  52. private:
  53. UIViewController* getViewController() const override { return controller.get(); }
  54. Component* parent = nullptr;
  55. NSUniquePtr<UIActivityViewController> controller;
  56. NSUniquePtr<NSArray> items;
  57. };
  58. auto detail::ScopedContentSharerInterface::shareFiles (const Array<URL>& files, Component* parent) -> std::unique_ptr<ScopedContentSharerInterface>
  59. {
  60. NSUniquePtr<NSMutableArray> urls ([[NSMutableArray arrayWithCapacity: (NSUInteger) files.size()] retain]);
  61. for (const auto& f : files)
  62. {
  63. NSString* nativeFilePath = nil;
  64. if (f.isLocalFile())
  65. {
  66. nativeFilePath = juceStringToNS (f.getLocalFile().getFullPathName());
  67. }
  68. else
  69. {
  70. auto filePath = f.toString (false);
  71. auto* fileDirectory = filePath.contains ("/")
  72. ? juceStringToNS (filePath.upToLastOccurrenceOf ("/", false, false))
  73. : [NSString string];
  74. auto fileName = juceStringToNS (filePath.fromLastOccurrenceOf ("/", false, false)
  75. .upToLastOccurrenceOf (".", false, false));
  76. auto fileExt = juceStringToNS (filePath.fromLastOccurrenceOf (".", false, false));
  77. if ([fileDirectory length] == NSUInteger (0))
  78. nativeFilePath = [[NSBundle mainBundle] pathForResource: fileName
  79. ofType: fileExt];
  80. else
  81. nativeFilePath = [[NSBundle mainBundle] pathForResource: fileName
  82. ofType: fileExt
  83. inDirectory: fileDirectory];
  84. }
  85. if (nativeFilePath != nil)
  86. [urls.get() addObject: [NSURL fileURLWithPath: nativeFilePath]];
  87. }
  88. return std::make_unique<NativeScopedContentSharerInterface> (parent, std::move (urls));
  89. }
  90. auto detail::ScopedContentSharerInterface::shareText (const String& text, Component* parent) -> std::unique_ptr<ScopedContentSharerInterface>
  91. {
  92. NSUniquePtr<NSArray> array ([[NSArray arrayWithObject: juceStringToNS (text)] retain]);
  93. return std::make_unique<NativeScopedContentSharerInterface> (parent, std::move (array));
  94. }
  95. } // namespace juce