annotate src/a11y/win32.cc @ 25:60ded877339b

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