3#include "common/config.h"
5#if defined(EVENGINE_WEBGPU) && defined(__EMSCRIPTEN__)
21#include <Poco/Dynamic/Var.h>
22#include <Poco/JSON/Array.h>
23#include <Poco/JSON/Object.h>
24#include <Poco/JSON/Stringifier.h>
83bool isClickable(
const UINode&
node) {
87 node.handlerClick != 0;
90const char* focusModeName(
FocusMode mode) {
139 std::ostringstream
out;
140 Poco::JSON::Stringifier::stringify(
value,
out, 0, 0);
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;
148 const UINode&
node = tree.nodes[
static_cast<size_t>(
index)];
149 out->set(
"type", nodeTypeName(
node.type));
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);
176 out->set(
"checked",
node.checked);
180 out->set(
"min",
node.minValue);
181 out->set(
"max",
node.maxValue);
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));
193 out->set(
"valueText",
node.valueText);
196 out->set(
"multiline",
node.multiline);
197 out->set(
"readOnly",
node.readOnly);
198 out->set(
"placeholder",
node.placeholder);
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);
204 Poco::JSON::Array::Ptr
children(
new Poco::JSON::Array());
206 if (
child >=
static_cast<int>(tree.nodes.size()))
break;
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));
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;
244std::vector<WidgetMatch> findWidgets(
const std::string& hostName,
const std::string& widgetId) {
245 std::vector<WidgetMatch>
result;
248 if (!
host || (!hostName.empty() &&
host->get().getName() != hostName))
continue;
249 auto node =
host->get().findById(widgetId);
251 const auto tree =
host->get().tree();
252 result.push_back({
handle,
static_cast<int>(&
node->get() - tree->nodes.data())});
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;
263 return "error: widget id is ambiguous; specify host: " +
widget;
268 std::string tree(
const std::string& hostName)
const override {
269 Poco::JSON::Array::Ptr items(
new Poco::JSON::Array());
272 if (!
host || (!hostName.empty() &&
host->get().getName() != hostName))
continue;
273 items->add(hostJson(
host->get()));
275 if (!hostName.empty() && items->size() == 0)
return "error: UI host not found: " + hostName;
277 Poco::JSON::Object::Ptr
out(
new Poco::JSON::Object());
278 out->set(
"hostCount",
static_cast<int>(items->size()));
279 out->set(
"hosts", items);
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());
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);
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());
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;
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;
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");
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());
335 static UIAutomationImpl
impl;
336 eve::cap::provide<eve::IUIAutomation>(&
impl);
std::set< std::string > hosts
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.
static std::vector< UIEvent > & pendingEvents()
Pending events.
Result< std::string > stringify(const eve::Value &value)
Serialize the canonical owning Value as deterministic compact JSON.
MouseFilter
Pointer event participation and propagation policy.
WidgetDesc child(std::string id, std::vector< WidgetDesc > children, float width, float height)
Scrollable child region with an explicit size.
NodeType
Widget node kinds understood by the UI renderer.
FocusMode
Keyboard/gamepad focus policy for an interactive control.
ThemePreset
Built-in theme override inherited by a retained UI subtree.
ecs::EntityHandle UIHostHandle
AccessibilityRole
Platform-neutral accessibility role exposed by controls.
void registerUIAutomationCapabilities()
Registers ui automation capabilities.