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.

116 lines
4.8KB

  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 AndroidMessageBox final : public ScopedMessageBoxInterface
  23. {
  24. public:
  25. explicit AndroidMessageBox (const MessageBoxOptions& o) : opts (o) {}
  26. void runAsync (std::function<void (int)> recipient) override
  27. {
  28. const auto makeDialogListener = [&recipient] (int result)
  29. {
  30. return new DialogListener ([recipient, result] { recipient (result); });
  31. };
  32. auto* env = getEnv();
  33. LocalRef<jobject> builder (env->NewObject (AndroidAlertDialogBuilder, AndroidAlertDialogBuilder.construct, getMainActivity().get()));
  34. const auto setText = [&] (auto method, const String& text)
  35. {
  36. builder = LocalRef<jobject> (env->CallObjectMethod (builder, method, javaString (text).get()));
  37. };
  38. setText (AndroidAlertDialogBuilder.setTitle, opts.getTitle());
  39. setText (AndroidAlertDialogBuilder.setMessage, opts.getMessage());
  40. builder = LocalRef<jobject> (env->CallObjectMethod (builder, AndroidAlertDialogBuilder.setCancelable, true));
  41. builder = LocalRef<jobject> (env->CallObjectMethod (builder, AndroidAlertDialogBuilder.setOnCancelListener,
  42. CreateJavaInterface (makeDialogListener (0),
  43. "android/content/DialogInterface$OnCancelListener").get()));
  44. const auto addButton = [&] (auto method, int index)
  45. {
  46. builder = LocalRef<jobject> (env->CallObjectMethod (builder,
  47. method,
  48. javaString (opts.getButtonText (index)).get(),
  49. CreateJavaInterface (makeDialogListener (index),
  50. "android/content/DialogInterface$OnClickListener").get()));
  51. };
  52. addButton (AndroidAlertDialogBuilder.setPositiveButton, 0);
  53. if (opts.getButtonText (1).isNotEmpty())
  54. addButton (AndroidAlertDialogBuilder.setNegativeButton, 1);
  55. if (opts.getButtonText (2).isNotEmpty())
  56. addButton (AndroidAlertDialogBuilder.setNeutralButton, 2);
  57. dialog = GlobalRef (LocalRef<jobject> (env->CallObjectMethod (builder, AndroidAlertDialogBuilder.create)));
  58. LocalRef<jobject> window (env->CallObjectMethod (dialog, AndroidDialog.getWindow));
  59. if (Desktop::getInstance().getKioskModeComponent() != nullptr)
  60. {
  61. env->CallVoidMethod (window, AndroidWindow.setFlags, FLAG_NOT_FOCUSABLE, FLAG_NOT_FOCUSABLE);
  62. LocalRef<jobject> decorView (env->CallObjectMethod (window, AndroidWindow.getDecorView));
  63. env->CallVoidMethod (decorView, AndroidView.setSystemUiVisibility, fullScreenFlags);
  64. }
  65. env->CallVoidMethod (dialog, AndroidDialog.show);
  66. if (Desktop::getInstance().getKioskModeComponent() != nullptr)
  67. env->CallVoidMethod (window, AndroidWindow.clearFlags, FLAG_NOT_FOCUSABLE);
  68. }
  69. int runSync() override
  70. {
  71. // Not implemented on this platform.
  72. jassertfalse;
  73. return 0;
  74. }
  75. void close() override
  76. {
  77. if (dialog != nullptr)
  78. getEnv()->CallVoidMethod (dialog, AndroidDialogInterface.dismiss);
  79. }
  80. private:
  81. const MessageBoxOptions opts;
  82. GlobalRef dialog;
  83. };
  84. return std::make_unique<AndroidMessageBox> (options);
  85. }
  86. } // namespace juce::detail