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
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. //==============================================================================
  21. class ButtonAccessibilityHandler : public AccessibilityHandler
  22. {
  23. public:
  24. ButtonAccessibilityHandler (Button& buttonToWrap, AccessibilityRole roleIn)
  25. : AccessibilityHandler (buttonToWrap,
  26. isRadioButton (buttonToWrap) ? AccessibilityRole::radioButton : roleIn,
  27. getAccessibilityActions (buttonToWrap),
  28. getAccessibilityInterfaces (buttonToWrap)),
  29. button (buttonToWrap)
  30. {}
  31. AccessibleState getCurrentState() const override
  32. {
  33. auto state = AccessibilityHandler::getCurrentState();
  34. if (button.isToggleable())
  35. {
  36. state = state.withCheckable();
  37. if (button.getToggleState())
  38. state = state.withChecked();
  39. }
  40. return state;
  41. }
  42. String getTitle() const override
  43. {
  44. auto title = AccessibilityHandler::getTitle();
  45. if (title.isEmpty())
  46. return button.getButtonText();
  47. return title;
  48. }
  49. String getHelp() const override
  50. {
  51. return button.getTooltip();
  52. }
  53. private:
  54. class ButtonValueInterface : public AccessibilityTextValueInterface
  55. {
  56. public:
  57. explicit ButtonValueInterface (Button& buttonToWrap)
  58. : button (buttonToWrap)
  59. {
  60. }
  61. bool isReadOnly() const override { return true; }
  62. String getCurrentValueAsString() const override { return button.getToggleState() ? "On" : "Off"; }
  63. void setValueAsString (const String&) override {}
  64. private:
  65. Button& button;
  66. //==============================================================================
  67. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ButtonValueInterface)
  68. };
  69. static bool isRadioButton (const Button& button) noexcept
  70. {
  71. return button.getRadioGroupId() != 0;
  72. }
  73. static AccessibilityActions getAccessibilityActions (Button& button)
  74. {
  75. auto actions = AccessibilityActions().addAction (AccessibilityActionType::press,
  76. [&button] { button.triggerClick(); });
  77. if (button.isToggleable())
  78. actions = actions.addAction (AccessibilityActionType::toggle,
  79. [&button] { button.setToggleState (! button.getToggleState(), sendNotification); });
  80. return actions;
  81. }
  82. static Interfaces getAccessibilityInterfaces (Button& button)
  83. {
  84. if (button.isToggleable())
  85. return { std::make_unique<ButtonValueInterface> (button) };
  86. return {};
  87. }
  88. Button& button;
  89. //==============================================================================
  90. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ButtonAccessibilityHandler)
  91. };
  92. } // namespace juce::detail