载入中...
搜索中...
未找到
UIAutomationCapabilities.cpp
浏览该文件的文档.
2
3#include "common/config.h"
4
5#if defined(EVENGINE_WEBGPU) && defined(__EMSCRIPTEN__)
6
7namespace eve::ui {
8
10
11} // namespace eve::ui
12
13#else
14
15#include "common/Capability.h"
16#include "common/ECS.h"
17#include "common/UIAutomation.h"
18#include "ui/UIHost.h"
19#include "ui/UISystem.h"
20
21#include <Poco/Dynamic/Var.h>
22#include <Poco/JSON/Array.h>
23#include <Poco/JSON/Object.h>
24#include <Poco/JSON/Stringifier.h>
25
26#include <sstream>
27#include <string>
28#include <vector>
29
30namespace eve::ui {
31namespace {
32
33const char* nodeTypeName(NodeType type) {
34 switch (type) {
35 case NodeType::Window: return "window";
36 case NodeType::Text: return "text";
37 case NodeType::Link: return "link";
38 case NodeType::Button: return "button";
39 case NodeType::SameLine: return "sameLine";
40 case NodeType::Group: return "group";
41 case NodeType::Separator: return "separator";
42 case NodeType::Checkbox: return "checkbox";
43 case NodeType::Slider: return "slider";
44 case NodeType::FilledImage: return "filled-image";
45 case NodeType::RadialFill: return "radial-fill";
46 case NodeType::Progress: return "progress";
47 case NodeType::InputText: return "inputText";
48 case NodeType::CollapsingHeader: return "collapsingHeader";
49 case NodeType::Child: return "child";
50 case NodeType::Flex: return "flex";
51 case NodeType::Grid: return "grid";
52 case NodeType::Spacer: return "spacer";
53 case NodeType::Image: return "image";
54 case NodeType::ImageButton: return "imageButton";
55 case NodeType::Combo: return "combo";
56 case NodeType::ScrollList: return "scrollList";
57 case NodeType::Viewport: return "viewport";
58 case NodeType::SearchField: return "searchField";
59 case NodeType::Switch: return "switch";
60 case NodeType::Badge: return "badge";
61 case NodeType::Card: return "card";
62 case NodeType::NinePatchPanel: return "ninePatchPanel";
63 case NodeType::ColorPalette: return "colorPalette";
64 case NodeType::SectionHeader: return "sectionHeader";
65 case NodeType::MenuBar: return "menuBar";
66 case NodeType::Menu: return "menu";
67 case NodeType::MenuItem: return "menuItem";
68 case NodeType::Toolbar: return "toolbar";
69 case NodeType::Toolbox: return "toolbox";
70 case NodeType::Sidebar: return "sidebar";
71 case NodeType::StatusBar: return "statusBar";
72 case NodeType::SplitPane: return "splitPane";
73 case NodeType::Tab: return "tab";
74 case NodeType::TabBar: return "tabBar";
75 case NodeType::TabContainer: return "tabContainer";
76 case NodeType::PanelFrame: return "panelFrame";
77 case NodeType::DockArea: return "dockArea";
78 case NodeType::AppFrame: return "appFrame";
79 }
80 return "unknown";
81}
82
83bool isClickable(const UINode& node) {
84 if (!node.enabled || node.mouseFilter == MouseFilter::Ignore) return false;
85 return node.type == NodeType::Link || node.type == NodeType::Button || node.type == NodeType::ImageButton ||
86 node.type == NodeType::Switch || node.type == NodeType::MenuItem || node.type == NodeType::Tab ||
87 node.handlerClick != 0;
88}
89
90const char* focusModeName(FocusMode mode) {
91 switch (mode) {
92 case FocusMode::None: return "none";
93 case FocusMode::Click: return "click";
94 case FocusMode::All: return "all";
95 }
96 return "none";
97}
98
99const char* mouseFilterName(MouseFilter filter) {
100 switch (filter) {
101 case MouseFilter::Stop: return "stop";
102 case MouseFilter::Pass: return "pass";
103 case MouseFilter::Ignore: return "ignore";
104 }
105 return "ignore";
106}
107
108const char* themePresetName(ThemePreset preset) {
109 switch (preset) {
110 case ThemePreset::Inherit: return "inherit";
111 case ThemePreset::Dark: return "dark";
112 case ThemePreset::Light: return "light";
113 }
114 return "inherit";
115}
116
117const char* accessibilityRoleName(AccessibilityRole role) {
118 switch (role) {
119 case AccessibilityRole::Auto: return "auto";
120 case AccessibilityRole::Button: return "button";
121 case AccessibilityRole::Checkbox: return "checkbox";
122 case AccessibilityRole::Slider: return "slider";
123 case AccessibilityRole::Text: return "text";
124 case AccessibilityRole::Link: return "link";
125 case AccessibilityRole::TextInput: return "textInput";
126 case AccessibilityRole::List: return "list";
127 case AccessibilityRole::ListItem: return "listItem";
128 case AccessibilityRole::Menu: return "menu";
129 case AccessibilityRole::MenuItem: return "menuItem";
130 case AccessibilityRole::Progress: return "progress";
131 case AccessibilityRole::Region: return "region";
132 case AccessibilityRole::Tab: return "tab";
133 case AccessibilityRole::Window: return "window";
134 }
135 return "auto";
136}
137
138std::string stringify(const Poco::Dynamic::Var& value) {
139 std::ostringstream out;
140 Poco::JSON::Stringifier::stringify(value, out, 0, 0);
141 return out.str();
142}
143
144Poco::JSON::Object::Ptr nodeJson(const UIHost::Tree& tree, int index) {
145 Poco::JSON::Object::Ptr out(new Poco::JSON::Object());
146 if (index < 0 || index >= static_cast<int>(tree.nodes.size())) return out;
147
148 const UINode& node = tree.nodes[static_cast<size_t>(index)];
149 out->set("type", nodeTypeName(node.type));
150 out->set("id", node.id);
151 if (!node.key.empty()) out->set("key", node.key);
152 out->set("text", node.text);
153 out->set("visible", node.visible);
154 out->set("enabled", node.enabled);
155 out->set("clickable", isClickable(node));
156 out->set("focusMode", focusModeName(node.focusMode));
157 out->set("focused", node.focused);
158 out->set("tabIndex", node.tabIndex);
159 out->set("mouseFilter", mouseFilterName(node.mouseFilter));
160 out->set("theme", themePresetName(node.themePreset));
161 if (!node.focusPrevious.empty()) out->set("focusPrevious", node.focusPrevious);
162 if (!node.focusNext.empty()) out->set("focusNext", node.focusNext);
163 if (!node.focusLeft.empty()) out->set("focusLeft", node.focusLeft);
164 if (!node.focusRight.empty()) out->set("focusRight", node.focusRight);
165 if (!node.focusUp.empty()) out->set("focusUp", node.focusUp);
166 if (!node.focusDown.empty()) out->set("focusDown", node.focusDown);
167 Poco::JSON::Object::Ptr accessibility(new Poco::JSON::Object());
168 accessibility->set("role", accessibilityRoleName(node.accessibilityRole));
169 accessibility->set("name", node.accessibilityName.empty() ? node.text
170 : node.accessibilityName);
171 if (!node.accessibilityDescription.empty())
172 accessibility->set("description", node.accessibilityDescription);
173 out->set("accessibility", accessibility);
174 if (node.type == NodeType::Checkbox || node.type == NodeType::Switch ||
175 node.type == NodeType::MenuItem)
176 out->set("checked", node.checked);
177 if (node.type == NodeType::Slider || node.type == NodeType::Progress || node.type == NodeType::FilledImage ||
179 out->set("value", node.value);
180 out->set("min", node.minValue);
181 out->set("max", node.maxValue);
182 }
183 if (node.type == NodeType::ColorPalette) {
184 Poco::JSON::Array::Ptr color(new Poco::JSON::Array());
185 color->add(static_cast<double>(node.tintR));
186 color->add(static_cast<double>(node.tintG));
187 color->add(static_cast<double>(node.tintB));
188 color->add(static_cast<double>(node.tintA));
189 out->set("color", color);
190 }
191 if (node.type == NodeType::InputText || node.type == NodeType::SearchField ||
192 node.type == NodeType::Combo || node.type == NodeType::MenuItem)
193 out->set("valueText", node.valueText);
194 if (node.type == NodeType::Link) out->set("target", node.valueText);
195 if (node.type == NodeType::InputText) {
196 out->set("multiline", node.multiline);
197 out->set("readOnly", node.readOnly);
198 out->set("placeholder", node.placeholder);
199 }
200 if (!node.helpText.empty()) out->set("helpText", node.helpText);
201 if (!node.disabledReason.empty()) out->set("disabledReason", node.disabledReason);
202 if (!node.tooltip.empty()) out->set("tooltip", node.tooltip);
203
204 Poco::JSON::Array::Ptr children(new Poco::JSON::Array());
205 for (int child = node.firstChild; child >= 0; child = tree.nodes[static_cast<size_t>(child)].nextSibling) {
206 if (child >= static_cast<int>(tree.nodes.size())) break;
207 children->add(nodeJson(tree, child));
208 }
209 if (children->size() != 0) out->set("children", children);
210 return out;
211}
212
213Poco::JSON::Object::Ptr hostJson(UIHost& host) {
214 Poco::JSON::Object::Ptr out(new Poco::JSON::Object());
215 const auto meta = host.meta();
216 const auto tree = host.tree();
217 out->set("name", meta->name);
218 out->set("visible", meta->visible);
219 out->set("layer", meta->layer);
220 out->set("modal", meta->modal);
221 out->set("overlay", meta->overlay);
222 out->set("nodeCount", static_cast<int>(tree->nodes.size()));
223 if (tree->root >= 0) out->set("root", nodeJson(*tree, tree->root));
224 return out;
225}
226
227std::vector<UIHostHandle> hosts() {
228 std::vector<UIHostHandle> result;
229 if (ecs::current()->getManager<UIHost>() == nullptr) return result;
230 auto view = ecs::View<UIHost, UIHost::Meta, UIHost::Tree>();
231 for (auto it = view.begin(); it != view.end(); ++it) {
232 auto [meta, tree] = *it;
233 (void)tree;
234 if (UIHost::resolve(meta->entity)) result.push_back(meta->entity);
235 }
236 return result;
237}
238
239struct WidgetMatch {
241 int nodeIndex = -1;
242};
243
244std::vector<WidgetMatch> findWidgets(const std::string& hostName, const std::string& widgetId) {
245 std::vector<WidgetMatch> result;
246 for (UIHostHandle handle : hosts()) {
248 if (!host || (!hostName.empty() && host->get().getName() != hostName)) continue;
249 auto node = host->get().findById(widgetId);
250 if (!node) continue;
251 const auto tree = host->get().tree();
252 result.push_back({handle, static_cast<int>(&node->get() - tree->nodes.data())});
253 }
254 return result;
255}
256
257std::string matchError(const std::string& host, const std::string& widget, size_t matchCount) {
258 if (widget.empty()) return "error: missing widget";
259 if (matchCount == 0) {
260 if (!host.empty()) return "error: widget not found: " + host + "/" + widget;
261 return "error: widget not found: " + widget;
262 }
263 return "error: widget id is ambiguous; specify host: " + widget;
264}
265
266class UIAutomationImpl final : public eve::IUIAutomation {
267public:
268 std::string tree(const std::string& hostName) const override {
269 Poco::JSON::Array::Ptr items(new Poco::JSON::Array());
270 for (UIHostHandle handle : hosts()) {
272 if (!host || (!hostName.empty() && host->get().getName() != hostName)) continue;
273 items->add(hostJson(host->get()));
274 }
275 if (!hostName.empty() && items->size() == 0) return "error: UI host not found: " + hostName;
276
277 Poco::JSON::Object::Ptr out(new Poco::JSON::Object());
278 out->set("hostCount", static_cast<int>(items->size()));
279 out->set("hosts", items);
280 return stringify(Poco::Dynamic::Var(out));
281 }
282
283 std::string get(const std::string& hostName, const std::string& widgetId) const override {
284 const std::vector<WidgetMatch> matches = findWidgets(hostName, widgetId);
285 if (matches.size() != 1) return matchError(hostName, widgetId, matches.size());
286 auto host = UIHost::resolve(matches[0].host);
287 if (!host) return "error: UI host became stale";
288 Poco::JSON::Object::Ptr out = nodeJson(*host->get().tree(), matches[0].nodeIndex);
289 out->set("host", host->get().getName());
290 out->set("hostVisible", host->get().meta()->visible);
291 return stringify(Poco::Dynamic::Var(out));
292 }
293
294 std::string click(const std::string& hostName, const std::string& widgetId) override {
295 const std::vector<WidgetMatch> matches = findWidgets(hostName, widgetId);
296 if (matches.size() != 1) return matchError(hostName, widgetId, matches.size());
297 auto host = UIHost::resolve(matches[0].host);
298 if (!host) return "error: UI host became stale";
299 UIHost& resolvedHost = host->get();
300 const auto tree = resolvedHost.tree();
301 if (matches[0].nodeIndex < 0 || matches[0].nodeIndex >= static_cast<int>(tree->nodes.size()))
302 return "error: UI widget became stale";
303 UINode& node = tree->nodes[static_cast<std::size_t>(matches[0].nodeIndex)];
304 if (!resolvedHost.meta()->visible || !node.visible)
305 return "error: widget is not visible: " + resolvedHost.getName() + "/" + widgetId;
306 if (!isClickable(node)) return "error: widget is not clickable: " + resolvedHost.getName() + "/" + widgetId;
307
308 UIEvent event;
309 event.host = matches[0].host;
310 event.hostName = resolvedHost.getName();
311 event.nodeId = node.id;
312 event.nodeIndex = matches[0].nodeIndex;
313 event.kind = "click";
314 event.handlerIndex = node.handlerClick;
315 UISystem::pendingEvents().push_back(std::move(event));
316
317 Poco::JSON::Object::Ptr out(new Poco::JSON::Object());
318 out->set("queued", true);
319 out->set("host", resolvedHost.getName());
320 out->set("widget", widgetId);
321 out->set("event", "click");
322 return stringify(Poco::Dynamic::Var(out));
323 }
324
325private:
326 static int nodeIndex(const UIHost::Tree& tree, const UINode* node) {
327 if (!node || tree.nodes.empty()) return -1;
328 return static_cast<int>(node - tree.nodes.data());
329 }
330};
331
332} // namespace
333
335 static UIAutomationImpl impl;
336 eve::cap::provide<eve::IUIAutomation>(&impl);
337}
338
339} // namespace eve::ui
340
341#endif
void * impl
Poco::JSON::Object::Ptr result
OceanSprayView view
PrimitiveHandle handle
std::set< std::string > hosts
Object::Ptr value
const RoadNode * node
std::string filter
ScriptStructure out
int children
Definition TreeMesh.cpp:432
UIHostHandle host
uint32_t index
Semantic retained-UI automation provided by the ui module.
static eve::OptionalRef< UIHost > resolve(UIHostHandle handle) noexcept
Resolves a UI host handle with ECS generation checking.
Definition UIHost.cpp:62
static std::vector< UIEvent > & pendingEvents()
Pending events.
Result< std::string > stringify(const eve::Value &value)
Serialize the canonical owning Value as deterministic compact JSON.
Definition Json.cpp:605
MouseFilter
Pointer event participation and propagation policy.
Definition UIHost.h:107
WidgetDesc child(std::string id, std::vector< WidgetDesc > children, float width, float height)
Scrollable child region with an explicit size.
Definition Widget.cpp:826
NodeType
Widget node kinds understood by the UI renderer.
Definition UIHost.h:32
FocusMode
Keyboard/gamepad focus policy for an interactive control.
Definition UIHost.h:101
ThemePreset
Built-in theme override inherited by a retained UI subtree.
Definition UIHost.h:110
ecs::EntityHandle UIHostHandle
Definition UIHost.h:20
AccessibilityRole
Platform-neutral accessibility role exposed by controls.
Definition UIHost.h:129
void registerUIAutomationCapabilities()
Registers ui automation capabilities.
std::string widget
glm::vec4 color