Mercurial > minori
annotate dep/animone/src/a11y/win32.cc @ 342:adb79bdde329
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 | 052ec053ee37 |
children | 1faa72660932 |
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> |
342
adb79bdde329
dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents:
341
diff
changeset
|
4 #include <memory> |
340
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
5 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
6 #include <windows.h> |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
7 #include <uiautomation.h> |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
8 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
9 #include "animone/a11y.h" |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
10 #include "animone/a11y/win32.h" |
342
adb79bdde329
dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents:
341
diff
changeset
|
11 #include "animone/util/win32.h" |
340
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 namespace animone::internal::win32 { |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
14 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
15 // Windows Accessibility API reference: |
74e2365326c6
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 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
17 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
18 // Commonly used interfaces |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
19 using Element = IUIAutomationElement; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
20 using TreeWalker = IUIAutomationTreeWalker; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
21 using ValuePattern = IUIAutomationValuePattern; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
22 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
23 using element_proc_t = std::function<TreeScope(Element&)>; |
74e2365326c6
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>>; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
25 |
341
052ec053ee37
dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents:
340
diff
changeset
|
26 /* ------------------------------------------------------------------- */ |
052ec053ee37
dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents:
340
diff
changeset
|
27 |
052ec053ee37
dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents:
340
diff
changeset
|
28 template <typename T> |
052ec053ee37
dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents:
340
diff
changeset
|
29 struct ComInterfaceDeleter { |
342
adb79bdde329
dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents:
341
diff
changeset
|
30 static_assert(std::is_base_of<IUnknown, T>::value, "Invalid COM interface"); |
adb79bdde329
dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents:
341
diff
changeset
|
31 using pointer = T*; |
adb79bdde329
dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents:
341
diff
changeset
|
32 void operator()(pointer p) const { if (p) p->Release(); } |
341
052ec053ee37
dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents:
340
diff
changeset
|
33 }; |
052ec053ee37
dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents:
340
diff
changeset
|
34 |
052ec053ee37
dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents:
340
diff
changeset
|
35 template <typename T> |
052ec053ee37
dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents:
340
diff
changeset
|
36 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
|
37 |
052ec053ee37
dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents:
340
diff
changeset
|
38 /* ------------------------------------------------------------------- */ |
052ec053ee37
dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents:
340
diff
changeset
|
39 |
340
74e2365326c6
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 |
74e2365326c6
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. |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
42 static ComInterface<IUIAutomation> ui_automation; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
43 |
341
052ec053ee37
dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents:
340
diff
changeset
|
44 /* ------------------------------------------------------------------- */ |
340
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
45 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
46 static bool InitializeUIAutomation() { |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
47 if (ui_automation) |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
48 return true; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
49 |
74e2365326c6
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 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
51 // CoCreateInstance. |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
52 ::CoInitialize(nullptr); |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
53 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
54 IUIAutomation* ui_automation_interface = nullptr; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
55 const auto result = ::CoCreateInstance( |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
56 CLSID_CUIAutomation, nullptr, CLSCTX_INPROC_SERVER, IID_IUIAutomation, |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
57 reinterpret_cast<void**>(&ui_automation_interface)); |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
58 ui_automation.reset(ui_automation_interface); |
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 return SUCCEEDED(result); |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
61 } |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
62 |
341
052ec053ee37
dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents:
340
diff
changeset
|
63 /* ------------------------------------------------------------------- */ |
340
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
64 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
65 static Element* GetElementFromHandle(HWND hwnd) { |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
66 Element* element = nullptr; |
74e2365326c6
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); |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
68 return element; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
69 } |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
70 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
71 static std::wstring GetElementName(Element& element) { |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
72 std::wstring element_name; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
73 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
74 BSTR bstr = nullptr; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
75 if (SUCCEEDED(element.get_CurrentName(&bstr)) && bstr) { |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
76 element_name = bstr; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
77 ::SysFreeString(bstr); |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
78 } |
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 return element_name; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
81 } |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
82 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
83 static std::wstring GetElementValue(Element& element) { |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
84 std::wstring element_value; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
85 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
86 ValuePattern* value_pattern_interface = nullptr; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
87 element.GetCurrentPatternAs( |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
88 UIA_ValuePatternId, IID_PPV_ARGS(&value_pattern_interface)); |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
89 ComInterface<ValuePattern> value_pattern(value_pattern_interface); |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
90 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
91 if (value_pattern) { |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
92 BSTR bstr = nullptr; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
93 if (SUCCEEDED(value_pattern->get_CurrentValue(&bstr)) && bstr) { |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
94 element_value = bstr; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
95 ::SysFreeString(bstr); |
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 } |
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 return element_value; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
100 } |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
101 |
341
052ec053ee37
dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents:
340
diff
changeset
|
102 /* ------------------------------------------------------------------- */ |
340
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
103 |
74e2365326c6
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) { |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
105 VARIANT v = {}; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
106 for (const auto& pair : properties) { |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
107 if (FAILED(element.GetCurrentPropertyValue(pair.first, &v))) |
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 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
|
110 return false; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
111 } |
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 return true; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
114 } |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
115 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
116 static bool IsAddressBarElement(Element& element) { |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
117 static const properties_t properties = { |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
118 {UIA_IsEnabledPropertyId, true}, |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
119 {UIA_IsKeyboardFocusablePropertyId, true}, |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
120 {UIA_IsValuePatternAvailablePropertyId, true}, |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
121 {UIA_ValueIsReadOnlyPropertyId, false}, |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
122 }; |
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 return VerifyElementProperties(element, properties); |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
125 } |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
126 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
127 static bool IsTabsElement(Element& element) { |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
128 static const properties_t properties = { |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
129 {UIA_ValueIsReadOnlyPropertyId, true}, |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
130 }; |
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 return VerifyElementProperties(element, properties); |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
133 } |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
134 |
341
052ec053ee37
dep/animone: a11y/win32: fix missing ComInterface definition
Paper <paper@paper.us.eu.org>
parents:
340
diff
changeset
|
135 /* ------------------------------------------------------------------- */ |
340
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
136 |
74e2365326c6
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, |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
138 size_t depth, element_proc_t element_proc) { |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
139 constexpr size_t kMaxTreeDepth = 16; // arbitrary value |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
140 if (depth > kMaxTreeDepth) |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
141 return; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
142 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
143 if (scope & TreeScope_Element) |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
144 scope = element_proc(parent); |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
145 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
146 auto descend = [](TreeScope scope) { |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
147 return (scope & TreeScope_Children) || (scope & TreeScope_Descendants); |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
148 }; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
149 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
150 if (descend(scope)) { |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
151 Element* first_element = nullptr; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
152 tree_walker.GetFirstChildElement(&parent, &first_element); |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
153 ComInterface<Element> element(first_element); |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
154 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
155 while (element) { |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
156 scope = element_proc(*element); |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
157 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
158 if (descend(scope)) |
74e2365326c6
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); |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
160 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
161 Element* next_element = nullptr; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
162 tree_walker.GetNextSiblingElement(element.get(), &next_element); |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
163 element.reset(next_element); |
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 } |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
167 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
168 static bool FindWebBrowserElements(Element& parent, std::wstring& address, |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
169 std::vector<std::wstring>& tabs) { |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
170 TreeWalker* tree_walker_interface = nullptr; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
171 ui_automation->get_ControlViewWalker(&tree_walker_interface); |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
172 ComInterface<TreeWalker> tree_walker(tree_walker_interface); |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
173 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
174 if (!tree_walker) |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
175 return false; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
176 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
177 auto element_proc = [&](Element& element) -> TreeScope { |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
178 CONTROLTYPEID control_type_id = 0; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
179 element.get_CurrentControlType(&control_type_id); |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
180 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
181 switch (control_type_id) { |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
182 default: |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
183 // Are we done? |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
184 if (!address.empty() && !tabs.empty()) |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
185 return TreeScope_Element; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
186 // Otherwise continue descending the tree. |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
187 return TreeScope_Descendants; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
188 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
189 case UIA_DocumentControlTypeId: |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
190 case UIA_MenuBarControlTypeId: |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
191 case UIA_TitleBarControlTypeId: |
74e2365326c6
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 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
193 // documents dramatically improves our performance on worst case |
74e2365326c6
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 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
195 // than using FindFirst and FindAll methods. |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
196 return TreeScope_Element; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
197 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
198 case UIA_EditControlTypeId: |
74e2365326c6
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 |
74e2365326c6
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 |
74e2365326c6
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 |
74e2365326c6
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" |
74e2365326c6
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 |
74e2365326c6
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, |
74e2365326c6
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. |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
206 if (address.empty() && IsAddressBarElement(element)) { |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
207 address = GetElementValue(element); |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
208 return TreeScope_Element; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
209 } else { |
74e2365326c6
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 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
211 // control ("Address bar"). |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
212 return TreeScope_Descendants; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
213 } |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
214 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
215 case UIA_TabControlTypeId: |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
216 if (tabs.empty() && IsTabsElement(element)) |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
217 return TreeScope_Children; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
218 return TreeScope_Element; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
219 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
220 case UIA_TabItemControlTypeId: |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
221 tabs.push_back(GetElementName(element)); |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
222 return TreeScope_Element; |
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 }; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
225 |
74e2365326c6
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); |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
227 return true; |
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 /* ------------------------------------------------------------------------------------ */ |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
231 |
74e2365326c6
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) { |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
233 if (!web_browser_proc) |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
234 return false; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
235 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
236 if (!InitializeUIAutomation()) |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
237 return false; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
238 |
342
adb79bdde329
dep/animone: fix tons of issues
Paper <paper@paper.us.eu.org>
parents:
341
diff
changeset
|
239 ComInterface<Element> parent(GetElementFromHandle(reinterpret_cast<HWND>(window.id.win32))); |
340
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
240 if (!parent) |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
241 return false; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
242 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
243 const std::string title = ToUtf8String(GetElementName(*parent)); |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
244 web_browser_proc({WebBrowserInformationType::Title, title}); |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
245 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
246 std::wstring address; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
247 std::vector<std::wstring> tabs; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
248 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
249 if (!FindWebBrowserElements(*parent, address, tabs)) |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
250 return false; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
251 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
252 web_browser_proc({WebBrowserInformationType::Address, ToUtf8String(address)}); |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
253 for (const auto& tab : tabs) |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
254 web_browser_proc({WebBrowserInformationType::Tab, ToUtf8String(tab)}); |
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 return true; |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
257 } |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
258 |
74e2365326c6
dep/animone: add experimental accessibility strategy
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
259 } // namespace animone::internal::win32 |