-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathuiwalker.cpp
187 lines (152 loc) · 5.35 KB
/
uiwalker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "uiwalker.hpp"
#include "core/errlogger.hpp"
#include "container.hpp"
#include "cuia.hpp"
#include "debug.hpp"
#include "def.hpp"
#include "rect.hpp"
#include "screen_metrics.hpp"
#include "uia.hpp"
#include "winwrap.hpp"
#include <initializer_list>
#include <stdexcept>
#include <unordered_set>
#include <vector>
namespace vind
{
namespace util
{
struct UIWalker::Impl {
SmartCacheReq cache_request_ = \
CUIA::get_instance().create_cache_request() ;
std::unordered_set<PROPERTYID> properties_{} ;
} ;
UIWalker::UIWalker()
: pimpl(std::make_unique<Impl>())
{
setup_cache_request(pimpl->cache_request_) ;
}
UIWalker::UIWalker(PROPERTYID id)
: UIWalker()
{
add_property(pimpl->cache_request_, id) ;
pimpl->properties_.insert(id) ;
}
UIWalker::UIWalker(const std::initializer_list<PROPERTYID>& ids)
: UIWalker()
{
for(const auto& id : ids) {
add_property(pimpl->cache_request_, id) ;
pimpl->properties_.insert(id) ;
}
}
UIWalker::UIWalker(std::initializer_list<PROPERTYID>&& ids)
: UIWalker()
{
for(auto&& id : ids) {
add_property(pimpl->cache_request_, id) ;
pimpl->properties_.insert(id) ;
}
}
UIWalker::~UIWalker() noexcept = default ;
UIWalker::UIWalker(UIWalker&&) = default ;
UIWalker& UIWalker::operator=(UIWalker&&) = default ;
void UIWalker::enable_fullcontrol() {
switch_mode(pimpl->cache_request_, true) ;
}
void UIWalker::setup_cache_request(SmartCacheReq req) {
add_property(req, UIA_IsEnabledPropertyId) ;
add_property(req, UIA_IsOffscreenPropertyId) ;
add_property(req, UIA_BoundingRectanglePropertyId) ;
// All property getters is only available for cache.
switch_mode(req, false) ;
change_scope(req, TreeScope::TreeScope_Subtree) ;
}
const std::unordered_set<PROPERTYID>& UIWalker::get_properties() const noexcept {
return pimpl->properties_ ;
}
bool UIWalker::filter_element(const SmartElement&) {
return true ; // Pass all elements
}
bool UIWalker::pinpoint_element(const SmartElement&) {
return false ; // Block all elements
}
bool UIWalker::filter_root_element(const SmartElement& elem) {
if(!is_enabled(elem)) {
return false ;
}
if(is_offscreen(elem)) {
return false ;
}
return true ;
}
bool UIWalker::append_candidate(
SmartElement elem,
std::vector<SmartElement>& elements) {
if(filter_element(elem)) {
if(pinpoint_element(elem)) {
elements.clear() ;
elements.push_back(std::move(elem)) ;
return false ; // Found the pinpoint element
}
elements.push_back(std::move(elem)) ;
}
return true ;
}
bool UIWalker::scan_childrens(
const std::vector<SmartElement>& parents,
std::vector<SmartElement>& elements) {
for(const auto& elem : parents) {
if(!is_enabled(elem)) {
continue ;
}
std::vector<SmartElement> children ;
get_children(elem, children) ;
if(!children.empty()) {
auto before_size = elements.size() ;
//recursive calling
if(!scan_childrens(children, elements)) {
return false ;
}
// Detected children as target
if(elements.size() > before_size) {
continue ;
}
}
if(!append_candidate(elem, elements)) {
return false ;
}
}
return true ; // continue
}
bool UIWalker::scan_element_subtree(
const SmartElement& elem,
std::vector<SmartElement>& elements) {
std::vector<SmartElement> children ;
get_children(elem, children) ;
if(children.empty()) {
return append_candidate(elem, elements) ;
}
auto before_size = elements.size() ;
if(!scan_childrens(children, elements)) {
return false ;
}
if(elements.size() == before_size) {
return append_candidate(elem, elements) ;
}
return true ;
}
void UIWalker::scan(
HWND hwnd,
std::vector<SmartElement>& elements) {
auto root = CUIA::get_instance().get_root_element(hwnd) ;
root = update_element(root, pimpl->cache_request_) ;
scan_element_subtree(root, elements) ;
}
void UIWalker::scan(
const SmartElement& root,
std::vector<SmartElement>& elements) {
scan_element_subtree(root, elements) ;
}
}
}