2.3 控件的可访问性
自绘控件想被读屏软件与键盘用户正确使用,必须补齐角色、可读标签与焦点顺序。归一化函数给缺失字段兜底;焦点顺序按 tabIndex 升序,0 保持文档顺序,-1 表示不参与 Tab。属性说明文本的 id 也要有稳定约定,aria-describedby 才能指向真实节点。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 自定义控件 | 《视图与子编辑器》 第 5 章 | 卷三做控件渲染与数据绑定;本节补可访问性契约 |
| UI 框架 | 《架构内核》 | 内核管布局与焦点模型;本节处理控件的 ARIA 语义 |
| 全卷脉络 | 本卷目录 | 本节是自定义 Inspector 章的收口 |
从零实现:控件的可访问性
归一化只做三件事:补默认角色、标签回退到 name、tabIndex 缺失按 0 处理。Tab 顺序先过滤掉 -1,再按 tabIndex 升序排序,相等时用原始下标保持文档顺序。说明文本 id 把非法字符替换成横线,多个 id 用空格拼接;空列表返回 undefined,避免输出空字符串属性。
对标控件接口
Godot Control 提供焦点邻居以及前后焦点配置。helio 示例只整理控件语义字段与 Tab 顺序。可访问名称、焦点可见性和键盘操作的实际效果仍需在宿主界面验证。 Godot Control
可运行产物:6 条用例
下面是 src/accessibility.ts,层级 L1,完整可编译。运行命令 node --test test/02-3-widget-accessibility.test.ts:
// file: src/accessibility.ts
// tier: L1
// verify: node --test test/02-3-widget-accessibility.test.ts
//
// 卷四(第二章):控件的可访问性。
// 自绘控件想被读屏软件与键盘用户正确使用,必须补齐三样东西:角色
// (role)、可读标签(label)、焦点顺序(tabIndex)。归一化函数给缺失
// 字段兜底;焦点顺序按 tabIndex 升序,0 保持文档顺序,-1 表示不参与 Tab。
export interface A11yInput {
name?: string;
label?: string;
role?: string;
tabIndex?: number;
}
export interface A11yNode {
role: string;
label: string;
tabIndex: number;
}
export function normalizeA11y(node: A11yInput): A11yNode {
return {
role: node.role ?? 'widget',
label: node.label ?? node.name ?? '未命名控件',
tabIndex: node.tabIndex ?? 0,
};
}
/** 计算 Tab 焦点顺序;tabIndex 为 -1 的控件被排除。 */
export function tabOrder(nodes: A11yInput[]): string[] {
return nodes
.map((node, index) => ({ node: normalizeA11y(node), index }))
.filter((entry) => entry.node.tabIndex >= 0)
.sort((a, b) =>
a.node.tabIndex === b.node.tabIndex
? a.index - b.index
: a.node.tabIndex - b.node.tabIndex,
)
.map((entry) => entry.node.label);
}
/** 属性名到说明文本 id 的约定,保证 aria-describedby 指向真实节点。 */
export function describedById(propertyName: string): string {
return `${propertyName.replace(/[^a-z0-9_-]/gi, '-')}-desc`;
}
export function describedByList(names: string[]): string | undefined {
return names.length === 0 ? undefined : names.map(describedById).join(' ');
}
焦点顺序的稳定性来自「tabIndex 相同时按文档顺序」这条规则;没有它,排序实现一变,用户按 Tab 的路径就跟着变。说明文本 id 规范化后仍要保证唯一,因此调用方应以属性名而不是显示文本作为输入。
下面是 test/02-3-widget-accessibility.test.ts,层级 L1:
// file: test/02-3-widget-accessibility.test.ts
// tier: L1
// verify: node --test test/02-3-widget-accessibility.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { normalizeA11y, tabOrder, describedById, describedByList } from '../src/accessibility.ts';
test('缺失字段补默认角色与标签', () => {
assert.deepEqual(normalizeA11y({}), { role: 'widget', label: '未命名控件', tabIndex: 0 });
});
test('label 优先于 name', () => {
const node = normalizeA11y({ name: '内部名', label: '显示名', role: 'slider' });
assert.equal(node.label, '显示名');
assert.equal(node.role, 'slider');
});
test('Tab 顺序按 tabIndex 升序', () => {
const order = tabOrder([
{ label: 'c', tabIndex: 2 },
{ label: 'a', tabIndex: 1 },
{ label: 'b', tabIndex: 0 },
]);
assert.deepEqual(order, ['b', 'a', 'c']);
});
test('tabIndex 为 0 时保持文档顺序', () => {
const order = tabOrder([{ label: 'x' }, { label: 'y' }, { label: 'z' }]);
assert.deepEqual(order, ['x', 'y', 'z']);
});
test('tabIndex 为 -1 的控件不参与 Tab', () => {
const order = tabOrder([{ label: 'skip', tabIndex: -1 }, { label: 'keep', tabIndex: 0 }]);
assert.deepEqual(order, ['keep']);
});
test('说明文本 id 规范化并拼接', () => {
assert.equal(describedById('max/min'), 'max-min-desc');
assert.equal(describedByList(['a', 'b']), 'a-desc b-desc');
assert.equal(describedByList([]), undefined);
});
本机实测(Node v26.5.0,node --test test/02-3-widget-accessibility.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。
前两条钉默认值与标签优先级;第三、四条钉 tabIndex 排序与文档顺序;第五条钉 -1 排除;第六条钉说明 id 规范化。
小结
- 可访问性的最小集:角色、标签、焦点顺序。
- tabIndex 为 0 时保持文档顺序,排序必须稳定。
- tabIndex 为 -1 的控件从 Tab 序列中移除,但仍可被程序聚焦。
- 说明文本 id 需要稳定规则,读屏才能找到对应节点。
阅读导航:上一节:2.2 自定义资产 Inspector · 下一节:3.1 声明自定义资产类型 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。