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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Ply Accessibility Plugin — hidden DOM for screen readers
// Uses aria-activedescendant pattern: the canvas keeps keyboard focus,
// and the hidden DOM tree is used for screen reader announcements.
var a11y_root = null;
var nodes = {};
var a11y_viewport_width = 1;
var a11y_viewport_height = 1;
function applyA11yNodeStyle(el) {
el.style.cssText =
"position:fixed;pointer-events:none;overflow:hidden;" +
"margin:0;padding:0;border:0;background:transparent;" +
"color:transparent;opacity:0.001;box-sizing:border-box;" +
"white-space:nowrap;";
}
function setA11yNodeBounds(el, x, y, width, height) {
var rect = canvas.getBoundingClientRect();
var scale_x = a11y_viewport_width > 0 ? rect.width / a11y_viewport_width : 1;
var scale_y = a11y_viewport_height > 0 ? rect.height / a11y_viewport_height : 1;
var left = rect.left + x * scale_x;
var top = rect.top + y * scale_y;
var css_width = Math.max(1, width * scale_x);
var css_height = Math.max(1, height * scale_y);
el.style.left = left + "px";
el.style.top = top + "px";
el.style.width = css_width + "px";
el.style.height = css_height + "px";
}
function roleUsesTextContent(role) {
return (
role === "none" ||
role === "heading" ||
role === "note" ||
role === "button" ||
role === "link" ||
role === "menuitem" ||
role === "tab" ||
role === "listitem"
);
}
miniquad_add_plugin({
register_plugin: function (imp) {
// Initialise: set up the canvas for aria-activedescendant and create
// the hidden DOM container.
imp.env.ply_a11y_init = function () {
if (!a11y_root) {
// Make the canvas a proper application landmark
canvas.setAttribute("tabindex", "0");
canvas.setAttribute("role", "application");
canvas.setAttribute("aria-label", "Application");
// Create hidden container for screen reader elements
a11y_root = document.createElement("div");
a11y_root.id = "ply-a11y-root";
a11y_root.style.cssText =
"position:fixed;left:0;top:0;width:0;height:0;overflow:visible;" +
"pointer-events:none;z-index:2147483647;";
document.body.appendChild(a11y_root);
// Tell screen readers the canvas "owns" the hidden tree
canvas.setAttribute("aria-owns", "ply-a11y-root");
}
};
// Create or update a hidden DOM node for a given element id.
// Hidden elements are NOT focusable — the canvas stays focused,
// and aria-activedescendant points to the current element.
imp.env.ply_a11y_upsert_node = function (
id,
role_ptr,
role_len,
label_ptr,
label_len,
_tab_index,
) {
var role = UTF8ToString(role_ptr, role_len);
var label = UTF8ToString(label_ptr, label_len);
var el = nodes[id];
if (!el) {
el = document.createElement("div");
el.id = "ply-a11y-" + id;
applyA11yNodeStyle(el);
a11y_root.appendChild(el);
nodes[id] = el;
}
if (role && role !== "none") el.setAttribute("role", role);
else el.removeAttribute("role");
if (label) {
if (roleUsesTextContent(role)) {
el.removeAttribute("aria-label");
if (el.textContent !== label) el.textContent = label;
} else {
el.setAttribute("aria-label", label);
if (el.textContent !== "") el.textContent = "";
}
} else {
el.removeAttribute("aria-label");
if (el.textContent !== "") el.textContent = "";
}
};
// Set heading level (aria-level)
imp.env.ply_a11y_set_heading_level = function (id, level) {
var el = nodes[id];
if (el && level >= 1 && level <= 6)
el.setAttribute("aria-level", level);
};
// Set checked state
imp.env.ply_a11y_set_checked = function (id, checked) {
var el = nodes[id];
if (el)
el.setAttribute("aria-checked", checked ? "true" : "false");
};
// Set value + optional min/max (for sliders, progress bars)
imp.env.ply_a11y_set_value = function (
id,
value_ptr,
value_len,
min,
max,
) {
var el = nodes[id];
if (!el) return;
var value = UTF8ToString(value_ptr, value_len);
if (value) el.setAttribute("aria-valuenow", value);
// NaN !== NaN, so this skips NaN values
if (min === min) el.setAttribute("aria-valuemin", min);
if (max === max) el.setAttribute("aria-valuemax", max);
};
// Set live-region mode (0 = off, 1 = polite, 2 = assertive)
imp.env.ply_a11y_set_live = function (id, mode) {
var el = nodes[id];
if (!el) return;
if (mode === 1) el.setAttribute("aria-live", "polite");
else if (mode === 2) el.setAttribute("aria-live", "assertive");
else el.removeAttribute("aria-live");
};
// Remove a node from the hidden tree
imp.env.ply_a11y_remove_node = function (id) {
var el = nodes[id];
if (el) {
el.remove();
delete nodes[id];
}
};
// Update aria-activedescendant on the canvas to point to the
// given element. This tells screen readers which element is
// active without moving browser focus away from the canvas.
imp.env.ply_a11y_set_focus = function (id) {
if (id === 0) {
canvas.removeAttribute("aria-activedescendant");
} else {
canvas.setAttribute("aria-activedescendant", "ply-a11y-" + id);
}
};
// Remove all nodes (for full rebuild)
imp.env.ply_a11y_clear = function () {
if (a11y_root) a11y_root.innerHTML = "";
nodes = {};
canvas.removeAttribute("aria-activedescendant");
};
// Set text content (for announcements via live regions)
imp.env.ply_a11y_announce = function (id, text_ptr, text_len) {
var el = nodes[id];
if (el) el.textContent = UTF8ToString(text_ptr, text_len);
};
// Set aria-description
imp.env.ply_a11y_set_description = function (
id,
desc_ptr,
desc_len,
) {
var el = nodes[id];
if (el) {
var desc = UTF8ToString(desc_ptr, desc_len);
if (desc) el.setAttribute("aria-description", desc);
else el.removeAttribute("aria-description");
}
};
// Reorder DOM children to match layout order.
// ids_ptr points to a u32 array of element IDs in desired order.
// We use Int32Array because WASM passes u32 as i32 to JS, and
// nodes[] keys are the signed interpretation of those values.
imp.env.ply_a11y_reorder = function (ids_ptr, count) {
if (!a11y_root || count === 0) return;
var ids = new Int32Array(wasm_memory.buffer, ids_ptr, count);
for (var i = 0; i < count; i++) {
var el = nodes[ids[i]];
if (el) a11y_root.appendChild(el);
}
};
// Position a DOM node so screen-reader focus overlays match the canvas.
imp.env.ply_a11y_set_bounds = function (id, x, y, width, height) {
var el = nodes[id];
if (!el) return;
setA11yNodeBounds(el, x, y, width, height);
};
imp.env.ply_a11y_set_viewport = function (width, height) {
a11y_viewport_width = width > 0 ? width : 1;
a11y_viewport_height = height > 0 ? height : 1;
};
// Virtual keyboard support for mobile web.
// Creates a hidden textarea that triggers the on-screen keyboard
// when focused, and forwards input events to the canvas.
var vk_input = null;
imp.env.ply_show_virtual_keyboard = function (show) {
if (show) {
if (!vk_input) {
vk_input = document.createElement("textarea");
vk_input.id = "ply-virtual-keyboard";
vk_input.autocapitalize = "off";
vk_input.autocomplete = "off";
vk_input.spellcheck = false;
vk_input.style.cssText =
"position:fixed;left:0;top:0;width:1px;height:1px;" +
"opacity:0;z-index:-1;pointer-events:none;";
document.body.appendChild(vk_input);
// Forward character input back to the canvas
vk_input.addEventListener("input", function () {
var text = vk_input.value;
vk_input.value = "";
for (var i = 0; i < text.length; i++) {
var code = text.charCodeAt(i);
// Skip control characters
if (code >= 32) {
canvas.dispatchEvent(
new KeyboardEvent("keypress", {
charCode: code,
keyCode: code,
bubbles: true,
})
);
}
}
});
// Forward control keys (Backspace, Enter, arrows, etc.)
// Only forward the initial press (not browser-generated
// repeats) — Rust has its own repeat logic.
// Also forward all Ctrl/Meta combos (undo, redo, copy,
// paste, cut, select-all) so they reach the canvas.
vk_input.addEventListener("keydown", function (e) {
var forward = [
"Backspace", "Delete", "Enter", "Tab", "Escape",
"ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown",
"Home", "End",
];
var shouldForward =
forward.indexOf(e.key) !== -1 ||
e.ctrlKey || e.metaKey;
if (shouldForward) {
if (!e.repeat) {
// For Ctrl/Meta combos, map e.key to the correct
// code so non-QWERTY layouts work correctly.
var fwdCode = e.code;
if ((e.ctrlKey || e.metaKey) && e.key && e.key.length === 1) {
var upper = e.key.toUpperCase();
if (upper >= 'A' && upper <= 'Z') {
fwdCode = 'Key' + upper;
}
}
canvas.dispatchEvent(
new KeyboardEvent("keydown", {
key: e.key,
code: fwdCode,
keyCode: e.keyCode,
ctrlKey: e.ctrlKey,
shiftKey: e.shiftKey,
altKey: e.altKey,
metaKey: e.metaKey,
bubbles: true,
})
);
}
e.preventDefault();
}
});
// Forward keyup so the Rust key-repeat tracker knows
// the key was released and stops repeating.
vk_input.addEventListener("keyup", function (e) {
var fwdCode = e.code;
if ((e.ctrlKey || e.metaKey) && e.key && e.key.length === 1) {
var upper = e.key.toUpperCase();
if (upper >= 'A' && upper <= 'Z') {
fwdCode = 'Key' + upper;
}
}
canvas.dispatchEvent(
new KeyboardEvent("keyup", {
key: e.key,
code: fwdCode,
keyCode: e.keyCode,
ctrlKey: e.ctrlKey,
shiftKey: e.shiftKey,
altKey: e.altKey,
metaKey: e.metaKey,
bubbles: true,
})
);
});
}
vk_input.focus({ preventScroll: true });
} else {
if (vk_input) {
vk_input.blur();
}
canvas.focus();
}
};
},
on_init: function () {},
version: 1,
name: "ply_accessibility",
});