iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
IGraphicsAccessibility.cpp
1/*
2 ==============================================================================
3
4 This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5
6 See LICENSE.txt for more info.
7
8 ==============================================================================
9*/
10
11// N.B. this file is included by IGraphics.cpp, it should not be added to projects
12
13#ifdef IGRAPHICS_ACCESSKIT
14
16#include "IGraphics.h"
17#include "IControl.h"
18#include "IPlugPluginBase.h"
19
20#include <cstdlib>
21
22using namespace iplug;
23using namespace igraphics;
24
25namespace
26{
27accesskit_role ToAccessKitRole(EAccessibleRole role)
28{
29 switch (role)
30 {
31 case EAccessibleRole::Group: return ACCESSKIT_ROLE_GROUP;
32 case EAccessibleRole::Label: return ACCESSKIT_ROLE_LABEL;
33 case EAccessibleRole::Button: return ACCESSKIT_ROLE_BUTTON;
34 case EAccessibleRole::Toggle: return ACCESSKIT_ROLE_CHECK_BOX;
35 case EAccessibleRole::Slider: return ACCESSKIT_ROLE_SLIDER;
36 case EAccessibleRole::ComboBox: return ACCESSKIT_ROLE_COMBO_BOX;
37 case EAccessibleRole::RadioGroup: return ACCESSKIT_ROLE_RADIO_GROUP;
38 case EAccessibleRole::RadioButton: return ACCESSKIT_ROLE_RADIO_BUTTON;
39 case EAccessibleRole::TabList: return ACCESSKIT_ROLE_TAB_LIST;
40 case EAccessibleRole::Tab: return ACCESSKIT_ROLE_TAB;
41 case EAccessibleRole::Meter: return ACCESSKIT_ROLE_METER;
42 case EAccessibleRole::Image: return ACCESSKIT_ROLE_IMAGE;
43 case EAccessibleRole::TextInput: return ACCESSKIT_ROLE_TEXT_INPUT;
44 case EAccessibleRole::Menu: return ACCESSKIT_ROLE_MENU;
45 case EAccessibleRole::MenuItem: return ACCESSKIT_ROLE_MENU_ITEM;
46 default: return ACCESSKIT_ROLE_GENERIC_CONTAINER;
47 }
48}
49} // namespace
50
51IGraphicsAccessibility::IGraphicsAccessibility(IGraphics& graphics)
52: mGraphics(graphics)
53{
54}
55
56IGraphicsAccessibility::~IGraphicsAccessibility()
57{
58}
59
60double IGraphicsAccessibility::GetRootScale() const
61{
62 return mGraphics.GetTotalScale();
63}
64
65uint32_t IGraphicsAccessibility::GetControlId(IControl* pControl)
66{
67 auto result = mControlIds.insert({pControl, mNextControlId});
68
69 if (result.second)
70 mNextControlId++;
71
72 return result.first->second;
73}
74
75IControl* IGraphicsAccessibility::GetControlForNode(accesskit_node_id id, int& nodeIdx) const
76{
77 const uint32_t controlId = static_cast<uint32_t>(id >> kNodeIdxBits);
78 nodeIdx = static_cast<int>(id & ((1 << kNodeIdxBits) - 1));
79
80 for (auto& entry : mControlIds)
81 {
82 // The control may have been removed since the tree was last updated
83 if (entry.second == controlId && (mGraphics.GetControlIdx(entry.first) >= 0 || entry.first == mGraphics.GetPopupMenuControl()))
84 return entry.first;
85 }
86
87 return nullptr;
88}
89
90void IGraphicsAccessibility::BuildSnapshot(std::vector<Node>& nodes, accesskit_node_id& focus)
91{
92 const int nControls = mGraphics.NControls();
93
94 nodes.clear();
95 nodes.reserve(nControls + 1);
96 focus = kRootId;
97
98 int focusNodeIdx = 0;
99 IControl* pFocus = mGraphics.GetKeyboardFocus(&focusNodeIdx);
100 IControl* pModal = mGraphics.GetModalControl();
101
102 nodes.emplace_back();
103 nodes[0].id = kRootId;
104 nodes[0].info.bounds = mGraphics.GetBounds();
105 nodes[0].scale = GetRootScale();
106
107 if (auto* pPlug = dynamic_cast<IPluginBase*>(mGraphics.GetDelegate()))
108 nodes[0].info.label.Set(pPlug->GetPluginName());
109
110 std::unordered_map<IControl*, size_t> controlNodes; // Indices of the first node of each control that is exposed
111 std::unordered_map<IControl*, uint32_t> controlIds;
112 IAccessibleNodeList controlNodeList;
113
114 // N.B. a pop-up menu that is drawn by IGraphics is not in the control stack. While it is open it is modal
115 IControl* pPopupMenu = mGraphics.GetPopupMenuControl();
116
117 for (int i = 0; i < nControls + (pPopupMenu ? 1 : 0); i++)
118 {
119 IControl* pControl = i < nControls ? mGraphics.GetControl(i) : pPopupMenu;
120
121 // While a control is modal, only it and its children are exposed
122 if (pControl->IsHidden() || !mGraphics.ControlIsInModalScope(pControl))
123 continue;
124
125 controlNodeList.clear();
126 pControl->GetAccessibleNodes(controlNodeList);
127
128 if (controlNodeList.empty() || controlNodeList[0].role == EAccessibleRole::None)
129 continue;
130
131 const uint32_t controlId = GetControlId(pControl);
132 controlIds[pControl] = controlId;
133
134 // The parent is the closest container that is itself exposed
135 size_t parentNode = 0;
136
137 for (IControl* pParent = pControl->GetParent(); pParent; pParent = pParent->GetParent())
138 {
139 auto itr = controlNodes.find(pParent);
140
141 if (itr != controlNodes.end())
142 {
143 parentNode = itr->second;
144 break;
145 }
146 }
147
148 const size_t firstNode = nodes.size();
149 const accesskit_node_id firstId = static_cast<accesskit_node_id>(controlId) << kNodeIdxBits;
150 const int nNodes = std::min(static_cast<int>(controlNodeList.size()), 1 << kNodeIdxBits);
151
152 controlNodes[pControl] = firstNode;
153 nodes[parentNode].children.push_back(firstId);
154
155 for (int n = 0; n < nNodes; n++)
156 {
157 nodes.emplace_back();
158 Node& node = nodes.back();
159 node.id = firstId | static_cast<accesskit_node_id>(n);
160 node.info = controlNodeList[n];
161 node.modal = n == 0 && pControl == pModal;
162
163 // Nodes after the first are children of the first, unless they name another node that came before them
164 if (n > 0)
165 {
166 const int parent = controlNodeList[n].parent;
167 nodes[firstNode + (parent > 0 && parent < n ? parent : 0)].children.push_back(node.id);
168 }
169
170 if (pControl == pFocus && n == focusNodeIdx)
171 focus = node.id;
172 }
173 }
174
175 // Forget controls that are no longer exposed, so that the map doesn't grow
176 mControlIds.swap(controlIds);
177}
178
179accesskit_node* IGraphicsAccessibility::MakeNode(const Node& node) const
180{
181 const IAccessibleNode& info = node.info;
182 const bool isRoot = node.id == kRootId;
183
184 accesskit_role role = isRoot ? ACCESSKIT_ROLE_WINDOW : ToAccessKitRole(info.role);
185
186 if (info.role == EAccessibleRole::MenuItem && info.toggled >= 0)
187 role = ACCESSKIT_ROLE_MENU_ITEM_CHECK_BOX;
188
189 accesskit_node* pNode = accesskit_node_new(role);
190
191 accesskit_node_set_bounds(pNode, accesskit_rect { info.bounds.L, info.bounds.T, info.bounds.R, info.bounds.B });
192
193 if (isRoot)
194 accesskit_node_set_transform(pNode, accesskit_affine_scale(node.scale));
195
196 if (!node.children.empty())
197 accesskit_node_set_children(pNode, node.children.size(), node.children.data());
198
199 if (info.role == EAccessibleRole::Label)
200 {
201 // The text of a label is its value
202 accesskit_node_set_value(pNode, info.label.Get());
203 }
204 else
205 {
206 if (info.label.GetLength())
207 accesskit_node_set_label(pNode, info.label.Get());
208
209 if (info.valueText.GetLength())
210 accesskit_node_set_value(pNode, info.valueText.Get());
211 }
212
213 if (info.description.GetLength())
214 accesskit_node_set_description(pNode, info.description.Get());
215
216 if (info.hasNumericValue)
217 {
218 accesskit_node_set_numeric_value(pNode, info.value);
219 accesskit_node_set_min_numeric_value(pNode, info.min);
220 accesskit_node_set_max_numeric_value(pNode, info.max);
221 accesskit_node_set_numeric_value_step(pNode, info.step);
222 }
223
224 if (info.toggled >= 0)
225 accesskit_node_set_toggled(pNode, info.toggled ? ACCESSKIT_TOGGLED_TRUE : ACCESSKIT_TOGGLED_FALSE);
226
227 if (info.expanded >= 0)
228 accesskit_node_set_expanded(pNode, info.expanded > 0);
229
230 if (node.modal)
231 accesskit_node_set_modal(pNode);
232
233 if (info.role == EAccessibleRole::RadioButton)
234 accesskit_node_set_toggled(pNode, info.selected ? ACCESSKIT_TOGGLED_TRUE : ACCESSKIT_TOGGLED_FALSE);
235 else if (info.role == EAccessibleRole::Tab)
236 accesskit_node_set_selected(pNode, info.selected);
237
238 if (info.disabled)
239 {
240 accesskit_node_set_disabled(pNode);
241 }
242 else
243 {
244 if (info.focusable)
245 accesskit_node_add_action(pNode, ACCESSKIT_ACTION_FOCUS);
246
247 switch (info.role)
248 {
249 case EAccessibleRole::Button:
250 case EAccessibleRole::Toggle:
251 case EAccessibleRole::ComboBox:
252 case EAccessibleRole::RadioButton:
253 case EAccessibleRole::Tab:
254 case EAccessibleRole::MenuItem:
255 accesskit_node_add_action(pNode, ACCESSKIT_ACTION_CLICK);
256 break;
257 case EAccessibleRole::Slider:
258 accesskit_node_add_action(pNode, ACCESSKIT_ACTION_INCREMENT);
259 accesskit_node_add_action(pNode, ACCESSKIT_ACTION_DECREMENT);
260 accesskit_node_add_action(pNode, ACCESSKIT_ACTION_SET_VALUE);
261 break;
262 default:
263 break;
264 }
265 }
266
267 return pNode;
268}
269
270accesskit_tree_update* IGraphicsAccessibility::MakeUpdate(bool full)
271{
272 std::vector<Node> nodes;
273 accesskit_node_id focus = kRootId;
274 BuildSnapshot(nodes, focus);
275
276 accesskit_tree_update* pUpdate = accesskit_tree_update_with_capacity_and_focus(full ? nodes.size() : 0, focus);
277
278 if (full)
279 {
280 accesskit_tree_info* pTreeInfo = accesskit_tree_info_new(kRootId);
281 accesskit_tree_info_set_toolkit_name(pTreeInfo, "iPlug2 IGraphics");
282 accesskit_tree_update_set_tree_info(pUpdate, pTreeInfo);
283
284 for (auto& node : nodes)
285 accesskit_tree_update_push_node(pUpdate, node.id, MakeNode(node));
286 }
287 else
288 {
289 std::unordered_map<accesskit_node_id, const Node*> prevNodes;
290 prevNodes.reserve(mNodes.size());
291
292 for (auto& node : mNodes)
293 prevNodes[node.id] = &node;
294
295 for (auto& node : nodes)
296 {
297 auto itr = prevNodes.find(node.id);
298
299 // Nodes that were removed don't need to be listed, since they are no longer amongst the children of any node
300 if (itr == prevNodes.end() || itr->second->info != node.info || itr->second->children != node.children || itr->second->scale != node.scale || itr->second->modal != node.modal)
301 accesskit_tree_update_push_node(pUpdate, node.id, MakeNode(node));
302 }
303 }
304
305 mNodes.swap(nodes);
306
307 return pUpdate;
308}
309
310accesskit_tree_update* IGraphicsAccessibility::ActivationHandler(void* pUserData)
311{
312 auto* pThis = static_cast<IGraphicsAccessibility*>(pUserData);
313 pThis->mActive = true;
314 return pThis->MakeUpdate(true);
315}
316
317accesskit_tree_update* IGraphicsAccessibility::UpdateFactory(void* pUserData)
318{
319 auto* pThis = static_cast<IGraphicsAccessibility*>(pUserData);
320 const bool full = !pThis->mActive.exchange(true);
321 return pThis->MakeUpdate(full);
322}
323
324void IGraphicsAccessibility::ActionHandler(accesskit_action_request* pRequest, void* pUserData)
325{
326 auto* pThis = static_cast<IGraphicsAccessibility*>(pUserData);
327
328 Action action;
329 action.action = pRequest->action;
330 action.target = pRequest->target_node;
331
332 if (pRequest->data.has_value)
333 {
334 if (pRequest->data.value.tag == ACCESSKIT_ACTION_DATA_NUMERIC_VALUE)
335 {
336 action.hasValue = true;
337 action.value = pRequest->data.value.numeric_value;
338 }
339 else if (pRequest->data.value.tag == ACCESSKIT_ACTION_DATA_VALUE && pRequest->data.value.value)
340 {
341 action.hasValue = true;
342 action.value = std::atof(pRequest->data.value.value);
343 }
344 }
345
346 accesskit_action_request_free(pRequest);
347
348 // This can be called on a thread other than the UI thread, so the action is performed on the next frame
349 std::lock_guard<std::mutex> lock(pThis->mActionMutex);
350 pThis->mActions.push_back(action);
351}
352
353void IGraphicsAccessibility::ProcessActions()
354{
355 std::vector<Action> actions;
356
357 {
358 std::lock_guard<std::mutex> lock(mActionMutex);
359 actions.swap(mActions);
360 }
361
362 for (auto& action : actions)
363 {
364 int nodeIdx = 0;
365 IControl* pControl = GetControlForNode(action.target, nodeIdx);
366
367 if (!pControl || pControl->IsHidden() || pControl->IsDisabled() || !mGraphics.ControlIsInModalScope(pControl))
368 continue;
369
370 switch (action.action)
371 {
372 case ACCESSKIT_ACTION_FOCUS:
373 // A control that keeps track of the focus within itself, such as a menu, handles this
374 if (!pControl->OnAccessibleAction(EAccessibleAction::Focus, nodeIdx))
375 mGraphics.SetKeyboardFocus(pControl, nodeIdx);
376 break;
377 case ACCESSKIT_ACTION_CLICK:
378 pControl->OnAccessibleAction(EAccessibleAction::Click, nodeIdx);
379 break;
380 case ACCESSKIT_ACTION_INCREMENT:
381 pControl->OnAccessibleAction(EAccessibleAction::Increment, nodeIdx);
382 break;
383 case ACCESSKIT_ACTION_DECREMENT:
384 pControl->OnAccessibleAction(EAccessibleAction::Decrement, nodeIdx);
385 break;
386 case ACCESSKIT_ACTION_SET_VALUE:
387 if (action.hasValue)
388 pControl->OnAccessibleAction(EAccessibleAction::SetValue, nodeIdx, action.value);
389 break;
390 default:
391 break;
392 }
393 }
394}
395
396void IGraphicsAccessibility::RequestFullUpdate()
397{
398 mActive = false;
399 mUpdateRequested = true;
400}
401
402void IGraphicsAccessibility::SetInactive()
403{
404 mActive = false;
405}
406
407void IGraphicsAccessibility::OnFrame(bool controlsDirty)
408{
409 const bool updateRequested = mUpdateRequested.exchange(false);
410
411 if (controlsDirty || updateRequested)
412 UpdateIfActive(UpdateFactory, this);
413}
414
415#endif // IGRAPHICS_ACCESSKIT
This file contains the base IControl implementation, along with some base classes for specific types ...
Bridges the IGraphics control hierarchy to platform accessibility APIs, via AccessKit (https://access...
The lowest level base class of an IGraphics control.
Definition: IControl.h:49
bool IsDisabled() const
Definition: IControl.h:367
virtual bool OnAccessibleAction(EAccessibleAction action, int nodeIdx=0, double value=1.)
Perform an action requested by assistive technology or keyboard navigation.
Definition: IControl.cpp:344
IContainerBase * GetParent() const
Definition: IControl.h:467
bool IsHidden() const
Definition: IControl.h:360
virtual void GetAccessibleNodes(IAccessibleNodeList &nodes)
Describe the control for assistive technology (e.g.
Definition: IControl.cpp:300
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:87
Base class that contains plug-in info and state manipulation methods.
std::vector< IAccessibleNode > IAccessibleNodeList
A list of IAccessibleNode.
EAccessibleRole
The semantic role of a control (or part of a control), reported to assistive technology such as scree...
Used to describe a control (or a part of a control) to assistive technology.