Audio plugin host https://kx.studio/carla
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.

juce_AccessibilityHandler.h 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 AccessibilityNativeHandle;
  21. /** Base class for accessible Components.
  22. This class wraps a Component and provides methods that allow an accessibility client,
  23. such as VoiceOver on macOS, or Narrator on Windows, to control it.
  24. It handles hierarchical navigation, properties, state, and various interfaces.
  25. @tags{Accessibility}
  26. */
  27. class JUCE_API AccessibilityHandler
  28. {
  29. public:
  30. /** Utility struct which holds one or more accessibility interfaces.
  31. The main purpose of this class is to provide convenience constructors from each
  32. of the four types of accessibility interface.
  33. */
  34. struct JUCE_API Interfaces
  35. {
  36. Interfaces() = default;
  37. Interfaces (std::unique_ptr<AccessibilityValueInterface> ptr) : value (std::move (ptr)) {}
  38. Interfaces (std::unique_ptr<AccessibilityTextInterface> ptr) : text (std::move (ptr)) {}
  39. Interfaces (std::unique_ptr<AccessibilityTableInterface> ptr) : table (std::move (ptr)) {}
  40. Interfaces (std::unique_ptr<AccessibilityCellInterface> ptr) : cell (std::move (ptr)) {}
  41. Interfaces (std::unique_ptr<AccessibilityValueInterface> valueIn,
  42. std::unique_ptr<AccessibilityTextInterface> textIn,
  43. std::unique_ptr<AccessibilityTableInterface> tableIn,
  44. std::unique_ptr<AccessibilityCellInterface> cellIn)
  45. : value (std::move (valueIn)),
  46. text (std::move (textIn)),
  47. table (std::move (tableIn)),
  48. cell (std::move (cellIn))
  49. {
  50. }
  51. std::unique_ptr<AccessibilityValueInterface> value;
  52. std::unique_ptr<AccessibilityTextInterface> text;
  53. std::unique_ptr<AccessibilityTableInterface> table;
  54. std::unique_ptr<AccessibilityCellInterface> cell;
  55. };
  56. /** Constructor.
  57. This will create a AccessibilityHandler which wraps the provided Component and makes
  58. it visible to accessibility clients. You must also specify a role for the UI element
  59. from the `AccessibilityRole` list which best describes it.
  60. To enable users to interact with the UI element you should provide the set of supported
  61. actions and their associated callbacks via the `accessibilityActions` parameter.
  62. For UI elements that support more complex interaction the value, text, table, and cell
  63. interfaces should be implemented as required and passed as the final argument of this
  64. constructor. See the documentation of these classes for more information about the
  65. types of control they represent and which methods need to be implemented.
  66. */
  67. AccessibilityHandler (Component& componentToWrap,
  68. AccessibilityRole accessibilityRole,
  69. AccessibilityActions actions = {},
  70. Interfaces interfaces = {});
  71. /** Destructor. */
  72. virtual ~AccessibilityHandler();
  73. //==============================================================================
  74. /** Returns the Component that this handler represents. */
  75. const Component& getComponent() const noexcept { return component; }
  76. /** Returns the Component that this handler represents. */
  77. Component& getComponent() noexcept { return component; }
  78. //==============================================================================
  79. /** The type of UI element that this accessibility handler represents.
  80. @see AccessibilityRole
  81. */
  82. AccessibilityRole getRole() const noexcept { return role; }
  83. /** The title of the UI element.
  84. This will be read out by the system and should be concise, preferably matching
  85. the visible title of the UI element (if any). For example, this might be the
  86. text of a button or a simple label.
  87. The default implementation will call `Component::getTitle()`, but you can override
  88. this to return a different string if required.
  89. If neither a name nor a description is provided then the UI element may be
  90. ignored by accessibility clients.
  91. This must be a localised string.
  92. */
  93. virtual String getTitle() const { return component.getTitle(); }
  94. /** A short description of the UI element.
  95. This may be read out by the system. It should not include the type of the UI
  96. element and should ideally be a single word, for example "Open" for a button
  97. that opens a window.
  98. The default implementation will call `Component::getDescription()`, but you
  99. can override this to return a different string if required.
  100. If neither a name nor a description is provided then the UI element may be
  101. ignored by accessibility clients.
  102. This must be a localised string.
  103. */
  104. virtual String getDescription() const { return component.getDescription(); }
  105. /** Some help text for the UI element (if required).
  106. This may be read out by the system. This string functions in a similar way to
  107. a tooltip, for example "Click to open window." for a button which opens a window.
  108. The default implementation will call `Component::getHelpText()`, but you can
  109. override this to return a different string if required.
  110. This must be a localised string.
  111. */
  112. virtual String getHelp() const { return component.getHelpText(); }
  113. /** Returns the current state of the UI element.
  114. The default implementation of this method will set the focusable flag and, if
  115. this UI element is currently focused, will also set the focused flag.
  116. */
  117. virtual AccessibleState getCurrentState() const;
  118. /** Returns true if this UI element should be ignored by accessibility clients. */
  119. bool isIgnored() const;
  120. /** Returns true if this UI element is visible within its parent.
  121. This will always return true for UI elements with the AccessibleState::accessibleOffscreen
  122. flag set.
  123. */
  124. bool isVisibleWithinParent() const;
  125. //==============================================================================
  126. /** Returns the set of actions that the UI element supports and the associated
  127. callbacks.
  128. */
  129. const AccessibilityActions& getActions() const noexcept;
  130. /** Returns the value interface for this UI element, or nullptr if it is not supported.
  131. @see AccessibilityValueInterface
  132. */
  133. AccessibilityValueInterface* getValueInterface() const;
  134. /** Returns the table interface for this UI element, or nullptr if it is not supported.
  135. @see AccessibilityTableInterface
  136. */
  137. AccessibilityTableInterface* getTableInterface() const;
  138. /** Returns the cell interface for this UI element, or nullptr if it is not supported.
  139. @see AccessibilityCellInterface
  140. */
  141. AccessibilityCellInterface* getCellInterface() const;
  142. /** Returns the text interface for this UI element, or nullptr if it is not supported.
  143. @see AccessibilityTextInterface
  144. */
  145. AccessibilityTextInterface* getTextInterface() const;
  146. //==============================================================================
  147. /** Returns the first unignored parent of this UI element in the accessibility hierarchy,
  148. or nullptr if this is a root element without a parent.
  149. */
  150. AccessibilityHandler* getParent() const;
  151. /** Returns the unignored children of this UI element in the accessibility hierarchy. */
  152. std::vector<AccessibilityHandler*> getChildren() const;
  153. /** Checks whether a given UI element is a child of this one in the accessibility
  154. hierarchy.
  155. */
  156. bool isParentOf (const AccessibilityHandler* possibleChild) const noexcept;
  157. /** Returns the deepest child of this UI element in the accessibility hierarchy that
  158. contains the given screen point, or nullptr if there is no child at this point.
  159. */
  160. AccessibilityHandler* getChildAt (Point<int> screenPoint);
  161. /** Returns the deepest UI element which currently has focus.
  162. This can be a child of this UI element or, if no child is focused,
  163. this element itself.
  164. Note that this can be different to the value of the Component with keyboard
  165. focus returned by Component::getCurrentlyFocusedComponent().
  166. @see hasFocus
  167. */
  168. AccessibilityHandler* getChildFocus();
  169. /** Returns true if this UI element has the focus.
  170. @param trueIfChildFocused if this is true, this method will also return true
  171. if any child of this UI element in the accessibility
  172. hierarchy has focus
  173. */
  174. bool hasFocus (bool trueIfChildFocused) const;
  175. /** Tries to give focus to this UI element.
  176. If the UI element is focusable and not ignored this will update the currently focused
  177. element, try to give keyboard focus to the Component it represents, and notify any
  178. listening accessibility clients that the current focus has changed.
  179. @see hasFocus, giveAwayFocus
  180. */
  181. void grabFocus();
  182. /** If this UI element or any of its children in the accessibility hierarchy currently
  183. have focus, this will defocus it.
  184. This will also give away the keyboard focus from the Component it represents, and
  185. notify any listening accessibility clients that the current focus has changed.
  186. @see hasFocus, grabFocus
  187. */
  188. void giveAwayFocus() const;
  189. //==============================================================================
  190. /** Used to send a notification to any observing accessibility clients that something
  191. has changed in the UI element.
  192. @see AccessibilityEvent
  193. */
  194. void notifyAccessibilityEvent (AccessibilityEvent event) const;
  195. /** A priority level that can help an accessibility client determine how to handle
  196. an announcement request.
  197. Exactly what this controls is platform-specific, but generally a low priority
  198. announcement will be read when the screen reader is free, whereas a high priority
  199. announcement will interrupt the current speech.
  200. */
  201. enum class AnnouncementPriority
  202. {
  203. low,
  204. medium,
  205. high
  206. };
  207. /** Posts an announcement to be made to the user.
  208. @param announcementString a localised string containing the announcement to be read out
  209. @param priority the appropriate priority level for the announcement
  210. */
  211. static void postAnnouncement (const String& announcementString, AnnouncementPriority priority);
  212. //==============================================================================
  213. /** @internal */
  214. AccessibilityNativeHandle* getNativeImplementation() const;
  215. /** @internal */
  216. std::type_index getTypeIndex() const { return typeIndex; }
  217. private:
  218. //==============================================================================
  219. friend class AccessibilityNativeHandle;
  220. //==============================================================================
  221. void grabFocusInternal (bool);
  222. void giveAwayFocusInternal() const;
  223. void takeFocus();
  224. static AccessibilityHandler* currentlyFocusedHandler;
  225. //==============================================================================
  226. Component& component;
  227. std::type_index typeIndex;
  228. const AccessibilityRole role;
  229. AccessibilityActions actions;
  230. Interfaces interfaces;
  231. //==============================================================================
  232. class AccessibilityNativeImpl;
  233. std::unique_ptr<AccessibilityNativeImpl> nativeImpl;
  234. static std::unique_ptr<AccessibilityNativeImpl> createNativeImpl (AccessibilityHandler&);
  235. //==============================================================================
  236. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AccessibilityHandler)
  237. };
  238. } // namespace juce