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.

111 lines
3.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::detail
  19. {
  20. std::unique_ptr<ScopedMessageBoxInterface> ScopedMessageBoxInterface::create (const MessageBoxOptions& options)
  21. {
  22. class MessageBox final : public ScopedMessageBoxInterface
  23. {
  24. public:
  25. explicit MessageBox (const MessageBoxOptions& opts) : options (opts) {}
  26. void runAsync (std::function<void (int)> recipient) override
  27. {
  28. if (iOSGlobals::currentlyFocusedPeer == nullptr)
  29. {
  30. // Since iOS8, alert windows need to be associated with a window, so you need to
  31. // have at least one window on screen when you use this
  32. jassertfalse;
  33. return;
  34. }
  35. alert.reset ([[UIAlertController alertControllerWithTitle: juceStringToNS (options.getTitle())
  36. message: juceStringToNS (options.getMessage())
  37. preferredStyle: UIAlertControllerStyleAlert] retain]);
  38. for (auto i = 0; i < options.getNumButtons(); ++i)
  39. {
  40. const auto text = options.getButtonText (i);
  41. if (text.isEmpty())
  42. continue;
  43. auto* action = [UIAlertAction actionWithTitle: juceStringToNS (text)
  44. style: UIAlertActionStyleDefault
  45. handler: ^(UIAlertAction*)
  46. {
  47. MessageManager::callAsync ([recipient, i] { NullCheckedInvocation::invoke (recipient, i); });
  48. }];
  49. [alert.get() addAction: action];
  50. if (i == 0)
  51. [alert.get() setPreferredAction: action];
  52. }
  53. [iOSGlobals::currentlyFocusedPeer->controller presentViewController: alert.get()
  54. animated: YES
  55. completion: nil];
  56. }
  57. int runSync() override
  58. {
  59. int result = -1;
  60. JUCE_AUTORELEASEPOOL
  61. {
  62. runAsync ([&result] (int r) { result = r; });
  63. while (result < 0)
  64. {
  65. JUCE_AUTORELEASEPOOL
  66. {
  67. [[NSRunLoop mainRunLoop] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.01]];
  68. }
  69. }
  70. }
  71. return result;
  72. }
  73. void close() override
  74. {
  75. if (auto* alertViewController = alert.get())
  76. [alertViewController dismissViewControllerAnimated: YES completion: nil];
  77. }
  78. private:
  79. const MessageBoxOptions options;
  80. NSUniquePtr<UIAlertController> alert;
  81. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MessageBox)
  82. };
  83. return std::make_unique<MessageBox> (options);
  84. }
  85. } // namespace juce::detail