annotate dep/animone/src/a11y/win32.cc @ 341:052ec053ee37

dep/animone: a11y/win32: fix missing ComInterface definition
author Paper <paper@paper.us.eu.org>
date Wed, 19 Jun 2024 23:21:19 -0400
parents 74e2365326c6
children adb79bdde329
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1 #include <functional>
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2 #include <string>
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
3 #include <vector>
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
4
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
5 #include <windows.h>
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
6 #include <uiautomation.h>
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
7
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
8 #include "animone/a11y.h"
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
9 #include "animone/a11y/win32.h"
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
10
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
11 namespace animone::internal::win32 {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
12
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
13 // Windows Accessibility API reference:
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
14 // https://msdn.microsoft.com/en-us/library/windows/desktop/ff486375.aspx
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
15
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
16 // Commonly used interfaces
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
17 using Element = IUIAutomationElement;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
18 using TreeWalker = IUIAutomationTreeWalker;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
19 using ValuePattern = IUIAutomationValuePattern;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
20
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
21 using element_proc_t = std::function<TreeScope(Element&)>;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
22 using properties_t = std::vector<std::pair<long, bool>>;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
23
341
052ec053ee37 dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
24 /* ------------------------------------------------------------------- */
052ec053ee37 dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
25
052ec053ee37 dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
26 template <typename T>
052ec053ee37 dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
27 struct ComInterfaceDeleter {
052ec053ee37 dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
28 static_assert(std::is_base_of<IUnknown, T>::value, "Invalid COM interface");
052ec053ee37 dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
29 using pointer = T*;
052ec053ee37 dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
30 void operator()(pointer p) const { if (p) p->Release(); }
052ec053ee37 dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
31 };
052ec053ee37 dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
32
052ec053ee37 dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
33 template <typename T>
052ec053ee37 dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
34 using ComInterface = std::unique_ptr<T, ComInterfaceDeleter<T>>;
052ec053ee37 dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
35
052ec053ee37 dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
36 /* ------------------------------------------------------------------- */
052ec053ee37 dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
37
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
38 // The main interface that is used throughout this file. Must be initialized
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
39 // before it can be used for the first time.
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
40 static ComInterface<IUIAutomation> ui_automation;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
41
341
052ec053ee37 dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
42 /* ------------------------------------------------------------------- */
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
43
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
44 static bool InitializeUIAutomation() {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
45 if (ui_automation)
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
46 return true;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
47
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
48 // COM library must be initialized on the current thread before calling
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
49 // CoCreateInstance.
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
50 ::CoInitialize(nullptr);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
51
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
52 IUIAutomation* ui_automation_interface = nullptr;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
53 const auto result = ::CoCreateInstance(
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
54 CLSID_CUIAutomation, nullptr, CLSCTX_INPROC_SERVER, IID_IUIAutomation,
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
55 reinterpret_cast<void**>(&ui_automation_interface));
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
56 ui_automation.reset(ui_automation_interface);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
57
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
58 return SUCCEEDED(result);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
59 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
60
341
052ec053ee37 dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
61 /* ------------------------------------------------------------------- */
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
62
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
63 static Element* GetElementFromHandle(HWND hwnd) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
64 Element* element = nullptr;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
65 ui_automation->ElementFromHandle(static_cast<UIA_HWND>(hwnd), &element);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
66 return element;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
67 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
68
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
69 static std::wstring GetElementName(Element& element) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
70 std::wstring element_name;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
71
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
72 BSTR bstr = nullptr;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
73 if (SUCCEEDED(element.get_CurrentName(&bstr)) && bstr) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
74 element_name = bstr;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
75 ::SysFreeString(bstr);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
76 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
77
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
78 return element_name;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
79 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
80
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
81 static std::wstring GetElementValue(Element& element) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
82 std::wstring element_value;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
83
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
84 ValuePattern* value_pattern_interface = nullptr;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
85 element.GetCurrentPatternAs(
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
86 UIA_ValuePatternId, IID_PPV_ARGS(&value_pattern_interface));
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
87 ComInterface<ValuePattern> value_pattern(value_pattern_interface);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
88
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
89 if (value_pattern) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
90 BSTR bstr = nullptr;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
91 if (SUCCEEDED(value_pattern->get_CurrentValue(&bstr)) && bstr) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
92 element_value = bstr;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
93 ::SysFreeString(bstr);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
94 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
95 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
96
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
97 return element_value;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
98 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
99
341
052ec053ee37 dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
100 /* ------------------------------------------------------------------- */
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
101
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
102 static bool VerifyElementProperties(Element& element, const properties_t& properties) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
103 VARIANT v = {};
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
104 for (const auto& pair : properties) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
105 if (FAILED(element.GetCurrentPropertyValue(pair.first, &v)))
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
106 return false;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
107 if (v.boolVal != (pair.second ? VARIANT_TRUE : VARIANT_FALSE))
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
108 return false;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
109 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
110
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
111 return true;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
112 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
113
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
114 static bool IsAddressBarElement(Element& element) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
115 static const properties_t properties = {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
116 {UIA_IsEnabledPropertyId, true},
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
117 {UIA_IsKeyboardFocusablePropertyId, true},
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
118 {UIA_IsValuePatternAvailablePropertyId, true},
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
119 {UIA_ValueIsReadOnlyPropertyId, false},
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
120 };
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
121
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
122 return VerifyElementProperties(element, properties);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
123 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
124
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
125 static bool IsTabsElement(Element& element) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
126 static const properties_t properties = {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
127 {UIA_ValueIsReadOnlyPropertyId, true},
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
128 };
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
129
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
130 return VerifyElementProperties(element, properties);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
131 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
132
341
052ec053ee37 dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents: 340
diff changeset
133 /* ------------------------------------------------------------------- */
340
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
134
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
135 static void WalkElements(TreeWalker& tree_walker, Element& parent, TreeScope scope,
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
136 size_t depth, element_proc_t element_proc) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
137 constexpr size_t kMaxTreeDepth = 16; // arbitrary value
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
138 if (depth > kMaxTreeDepth)
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
139 return;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
140
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
141 if (scope & TreeScope_Element)
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
142 scope = element_proc(parent);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
143
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
144 auto descend = [](TreeScope scope) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
145 return (scope & TreeScope_Children) || (scope & TreeScope_Descendants);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
146 };
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
147
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
148 if (descend(scope)) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
149 Element* first_element = nullptr;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
150 tree_walker.GetFirstChildElement(&parent, &first_element);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
151 ComInterface<Element> element(first_element);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
152
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
153 while (element) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
154 scope = element_proc(*element);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
155
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
156 if (descend(scope))
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
157 WalkElements(tree_walker, *element, scope, depth + 1, element_proc);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
158
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
159 Element* next_element = nullptr;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
160 tree_walker.GetNextSiblingElement(element.get(), &next_element);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
161 element.reset(next_element);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
162 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
163 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
164 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
165
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
166 static bool FindWebBrowserElements(Element& parent, std::wstring& address,
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
167 std::vector<std::wstring>& tabs) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
168 TreeWalker* tree_walker_interface = nullptr;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
169 ui_automation->get_ControlViewWalker(&tree_walker_interface);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
170 ComInterface<TreeWalker> tree_walker(tree_walker_interface);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
171
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
172 if (!tree_walker)
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
173 return false;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
174
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
175 auto element_proc = [&](Element& element) -> TreeScope {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
176 CONTROLTYPEID control_type_id = 0;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
177 element.get_CurrentControlType(&control_type_id);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
178
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
179 switch (control_type_id) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
180 default:
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
181 // Are we done?
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
182 if (!address.empty() && !tabs.empty())
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
183 return TreeScope_Element;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
184 // Otherwise continue descending the tree.
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
185 return TreeScope_Descendants;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
186
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
187 case UIA_DocumentControlTypeId:
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
188 case UIA_MenuBarControlTypeId:
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
189 case UIA_TitleBarControlTypeId:
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
190 // We do not need to walk through these nodes. In fact, skipping
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
191 // documents dramatically improves our performance on worst case
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
192 // scenarios. This is the whole reason we are walking the tree rather
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
193 // than using FindFirst and FindAll methods.
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
194 return TreeScope_Element;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
195
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
196 case UIA_EditControlTypeId:
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
197 // Here we assume that the first edit control that fits our properties
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
198 // is the address bar (e.g. "Omnibox" on Chrome, "Awesome Bar" on
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
199 // Firefox). This element is named differently on each web browser
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
200 // (e.g. "Address and search bar" on Chrome, "Search or enter address"
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
201 // on Firefox). This name can change depending on the browser
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
202 // language. However, we are only interested in the element value,
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
203 // which usually gives us the URL of the current page.
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
204 if (address.empty() && IsAddressBarElement(element)) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
205 address = GetElementValue(element);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
206 return TreeScope_Element;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
207 } else {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
208 // Opera has an edit control ("Address field") within another edit
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
209 // control ("Address bar").
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
210 return TreeScope_Descendants;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
211 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
212
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
213 case UIA_TabControlTypeId:
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
214 if (tabs.empty() && IsTabsElement(element))
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
215 return TreeScope_Children;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
216 return TreeScope_Element;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
217
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
218 case UIA_TabItemControlTypeId:
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
219 tabs.push_back(GetElementName(element));
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
220 return TreeScope_Element;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
221 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
222 };
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
223
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
224 WalkElements(*tree_walker, parent, TreeScope_Subtree, 0, element_proc);
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
225 return true;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
226 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
227
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
228 /* ------------------------------------------------------------------------------------ */
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
229
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
230 bool GetWebBrowserInformation(const Window& window, web_browser_proc_t web_browser_proc) {
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
231 if (!web_browser_proc)
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
232 return false;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
233
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
234 if (!InitializeUIAutomation())
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
235 return false;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
236
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
237 ComInterface<Element> parent(GetElementFromHandle(hwnd));
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
238 if (!parent)
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
239 return false;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
240
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
241 const std::string title = ToUtf8String(GetElementName(*parent));
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
242 web_browser_proc({WebBrowserInformationType::Title, title});
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
243
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
244 std::wstring address;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
245 std::vector<std::wstring> tabs;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
246
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
247 if (!FindWebBrowserElements(*parent, address, tabs))
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
248 return false;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
249
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
250 web_browser_proc({WebBrowserInformationType::Address, ToUtf8String(address)});
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
251 for (const auto& tab : tabs)
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
252 web_browser_proc({WebBrowserInformationType::Tab, ToUtf8String(tab)});
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
253
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
254 return true;
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
255 }
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
256
74e2365326c6 dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff changeset
257 } // namespace animone::internal::win32