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.

259 lines
8.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. struct AccessibleObjCClassDeleter
  17. {
  18. template <typename ElementType>
  19. void operator() (ElementType* element) const
  20. {
  21. juceFreeAccessibilityPlatformSpecificData (element);
  22. object_setInstanceVariable (element, "handler", nullptr);
  23. [element release];
  24. }
  25. };
  26. template <typename Base>
  27. class AccessibleObjCClass : public ObjCClass<Base>
  28. {
  29. public:
  30. using Holder = std::unique_ptr<Base, AccessibleObjCClassDeleter>;
  31. protected:
  32. AccessibleObjCClass() : ObjCClass<Base> ("JUCEAccessibilityElement_")
  33. {
  34. ObjCClass<Base>::template addIvar<AccessibilityHandler*> ("handler");
  35. }
  36. //==============================================================================
  37. static AccessibilityHandler* getHandler (id self)
  38. {
  39. return getIvar<AccessibilityHandler*> (self, "handler");
  40. }
  41. template <typename MemberFn>
  42. static auto getInterface (id self, MemberFn fn) noexcept -> decltype ((std::declval<AccessibilityHandler>().*fn)())
  43. {
  44. if (auto* handler = getHandler (self))
  45. return (handler->*fn)();
  46. return nullptr;
  47. }
  48. static AccessibilityTextInterface* getTextInterface (id self) noexcept { return getInterface (self, &AccessibilityHandler::getTextInterface); }
  49. static AccessibilityValueInterface* getValueInterface (id self) noexcept { return getInterface (self, &AccessibilityHandler::getValueInterface); }
  50. static AccessibilityTableInterface* getTableInterface (id self) noexcept { return getInterface (self, &AccessibilityHandler::getTableInterface); }
  51. static AccessibilityCellInterface* getCellInterface (id self) noexcept { return getInterface (self, &AccessibilityHandler::getCellInterface); }
  52. template <typename MemberFn>
  53. static auto getEnclosingInterface (AccessibilityHandler* handler, MemberFn fn) noexcept -> decltype ((std::declval<AccessibilityHandler>().*fn)())
  54. {
  55. if (handler == nullptr)
  56. return nullptr;
  57. if (auto* interface = (handler->*fn)())
  58. return interface;
  59. return getEnclosingInterface (handler->getParent(), fn);
  60. }
  61. static bool hasEditableText (AccessibilityHandler& handler) noexcept
  62. {
  63. return handler.getRole() == AccessibilityRole::editableText
  64. && handler.getTextInterface() != nullptr
  65. && ! handler.getTextInterface()->isReadOnly();
  66. }
  67. static id getAccessibilityValueFromInterfaces (const AccessibilityHandler& handler)
  68. {
  69. if (auto* textInterface = handler.getTextInterface())
  70. return juceStringToNS (textInterface->getText ({ 0, textInterface->getTotalNumCharacters() }));
  71. if (auto* valueInterface = handler.getValueInterface())
  72. return juceStringToNS (valueInterface->getCurrentValueAsString());
  73. return nil;
  74. }
  75. //==============================================================================
  76. static BOOL getIsAccessibilityElement (id self, SEL)
  77. {
  78. if (auto* handler = getHandler (self))
  79. return ! handler->isIgnored()
  80. && handler->getRole() != AccessibilityRole::window;
  81. return NO;
  82. }
  83. static void setAccessibilityValue (id self, SEL, NSString* value)
  84. {
  85. if (auto* handler = getHandler (self))
  86. {
  87. if (hasEditableText (*handler))
  88. {
  89. handler->getTextInterface()->setText (nsStringToJuce (value));
  90. return;
  91. }
  92. if (auto* valueInterface = handler->getValueInterface())
  93. if (! valueInterface->isReadOnly())
  94. valueInterface->setValueAsString (nsStringToJuce (value));
  95. }
  96. }
  97. static BOOL performActionIfSupported (id self, AccessibilityActionType actionType)
  98. {
  99. if (auto* handler = getHandler (self))
  100. if (handler->getActions().invoke (actionType))
  101. return YES;
  102. return NO;
  103. }
  104. static BOOL accessibilityPerformPress (id self, SEL)
  105. {
  106. if (auto* handler = getHandler (self))
  107. if (handler->getCurrentState().isCheckable() && handler->getActions().invoke (AccessibilityActionType::toggle))
  108. return YES;
  109. return performActionIfSupported (self, AccessibilityActionType::press);
  110. }
  111. static BOOL accessibilityPerformIncrement (id self, SEL)
  112. {
  113. if (auto* valueInterface = getValueInterface (self))
  114. {
  115. if (! valueInterface->isReadOnly())
  116. {
  117. auto range = valueInterface->getRange();
  118. if (range.isValid())
  119. {
  120. valueInterface->setValue (jlimit (range.getMinimumValue(),
  121. range.getMaximumValue(),
  122. valueInterface->getCurrentValue() + range.getInterval()));
  123. return YES;
  124. }
  125. }
  126. }
  127. return NO;
  128. }
  129. static BOOL accessibilityPerformDecrement (id self, SEL)
  130. {
  131. if (auto* valueInterface = getValueInterface (self))
  132. {
  133. if (! valueInterface->isReadOnly())
  134. {
  135. auto range = valueInterface->getRange();
  136. if (range.isValid())
  137. {
  138. valueInterface->setValue (jlimit (range.getMinimumValue(),
  139. range.getMaximumValue(),
  140. valueInterface->getCurrentValue() - range.getInterval()));
  141. return YES;
  142. }
  143. }
  144. }
  145. return NO;
  146. }
  147. static NSString* getAccessibilityTitle (id self, SEL)
  148. {
  149. if (auto* handler = getHandler (self))
  150. {
  151. auto title = handler->getTitle();
  152. if (title.isEmpty() && handler->getComponent().isOnDesktop())
  153. title = getAccessibleApplicationOrPluginName();
  154. NSString* nsString = juceStringToNS (title);
  155. #if ! JUCE_IOS
  156. if (nsString != nil && [[self accessibilityValue] isEqual: nsString])
  157. return @"";
  158. #endif
  159. return nsString;
  160. }
  161. return nil;
  162. }
  163. static NSString* getAccessibilityHelp (id self, SEL)
  164. {
  165. if (auto* handler = getHandler (self))
  166. return juceStringToNS (handler->getHelp());
  167. return nil;
  168. }
  169. static BOOL getIsAccessibilityModal (id self, SEL)
  170. {
  171. if (auto* handler = getHandler (self))
  172. return handler->getComponent().isCurrentlyModal();
  173. return NO;
  174. }
  175. static NSInteger getAccessibilityRowCount (id self, SEL)
  176. {
  177. if (auto* tableInterface = getTableInterface (self))
  178. return tableInterface->getNumRows();
  179. return 0;
  180. }
  181. static NSInteger getAccessibilityColumnCount (id self, SEL)
  182. {
  183. if (auto* tableInterface = getTableInterface (self))
  184. return tableInterface->getNumColumns();
  185. return 0;
  186. }
  187. static NSRange getAccessibilityRowIndexRange (id self, SEL)
  188. {
  189. if (auto* cellInterface = getCellInterface (self))
  190. return NSMakeRange ((NSUInteger) cellInterface->getRowIndex(),
  191. (NSUInteger) cellInterface->getRowSpan());
  192. return NSMakeRange (0, 0);
  193. }
  194. static NSRange getAccessibilityColumnIndexRange (id self, SEL)
  195. {
  196. if (auto* cellInterface = getCellInterface (self))
  197. return NSMakeRange ((NSUInteger) cellInterface->getColumnIndex(),
  198. (NSUInteger) cellInterface->getColumnSpan());
  199. return NSMakeRange (0, 0);
  200. }
  201. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AccessibleObjCClass)
  202. };
  203. }