velox-engine 0.2.0

Velox core UI engine: arena, pod state, 1D fixed-point layout solver, damage tracker, SDF scanline rasterizer, and cross-platform GPU/software backends
Documentation
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! 无障碍语义树(Accessibility Semantic Tree,兼容 AccessKit / AT-SPI / UI Automation 层次与增量 Diff 模型)。
//!
//! 核心特性:
//! - **100% `#![no_std]` 零堆分配**:从当前帧 `HitRegion` 与 `DrawOp` 自动推导带父子归属的层次无障碍树;
//! - **跨帧增量更新(AccessKit `TreeUpdate` 1:1 映射)**:通过 [`snapshot_a11y_tree`] 与 [`diff_a11y_snapshot`] /
//!   [`diff_a11y_trees`] 计算最小变更集(`updated_nodes`、`removed_ids`、`focus_id`),静息帧输出空更新(零屏幕阅读器 IPC 噪声)。

use crate::arena::{SmallVec, StrRef};
use crate::motion::{DrawOp, DrawPayload};
use crate::solver::{HitRegion, Region};

/// 无障碍语义角色分类(对应 AccessKit / WAI-ARIA / AT-SPI 核心 Role)。
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum A11yRole {
    /// 根视口或顶层窗口。
    Window,
    /// 通用分组容器。
    Container,
    /// 可点击按钮。
    Button,
    /// 单行或多行文本输入框。
    TextInput,
    /// 可拖拽滑块或数值调节器。
    Slider,
    /// 可滚动视口区域。
    ScrollRegion,
    /// 静态文本标签。
    Text,
    /// 图像或图标。
    Image,
}

/// 单个无障碍语义节点快照(带父节点归属与状态属性,兼容 AccessKit `Node` 模型)。
#[derive(Copy, Clone, Debug)]
pub struct A11yNode<'a> {
    /// 节点稳定无障碍 ID(`0` 固定保留为根 `Window`)。
    pub id: u32,
    /// 所属父级语义节点 ID(根 `Window` 为 `None`,其余节点均精确指向其最内层容器或根 `Some(0)`)。
    pub parent_id: Option<u32>,
    /// 节点语义角色。
    pub role: A11yRole,
    /// 屏幕边界矩形。
    pub bounds: Region,
    /// 可选的可读文本标签(Accessible Name,如按钮文字或静态文本)。
    pub label: Option<StrRef<'a>>,
    /// 可选的控件当前值文本(如 `TextInput` 的编辑内容或 `Slider` 数值)。
    pub value_text: Option<StrRef<'a>>,
    /// 是否可接收键盘/旋钮焦点。
    pub focusable: bool,
    /// 当前是否处于键盘/旋钮聚焦态。
    pub focused: bool,
    /// 节点当前是否处于禁用态。
    pub disabled: bool,
    /// 可选勾选/开关状态(`Some(true/false)` 对应复选框/开关/单选态)。
    pub checked: Option<bool>,
    /// 若为滚动容器,包含 `(scroll_id, max_scroll, horizontal)`。
    pub scroll_info: Option<(u32, i16, bool)>,
}

#[inline]
fn hash_opt_str(s: Option<StrRef<'_>>) -> u64 {
    let Some(sr) = s else {
        return 0;
    };
    let mut h: u64 = 0xcbf2_9ce4_8422_2325;
    for &b in sr.as_str().as_bytes() {
        h ^= b as u64;
        h = h.wrapping_mul(0x0100_0000_01b3);
    }
    h | 1
}

impl<'a> A11yNode<'a> {
    /// 跨生命周期按内容比较两个无障碍节点是否完全一致(忽略底层 `ScratchArena` 指针地址差异)。
    #[inline]
    pub fn content_eq(&self, other: &A11yNode<'_>) -> bool {
        self.id == other.id
            && self.parent_id == other.parent_id
            && self.role == other.role
            && self.bounds == other.bounds
            && self.label.map(|s| s.as_str()) == other.label.map(|s| s.as_str())
            && self.value_text.map(|s| s.as_str()) == other.value_text.map(|s| s.as_str())
            && self.focusable == other.focusable
            && self.focused == other.focused
            && self.disabled == other.disabled
            && self.checked == other.checked
            && self.scroll_info == other.scroll_info
    }

    /// 转换为无生命周期依赖的跨帧指纹快照 [`A11yNodeSnapshot`]。
    #[inline]
    pub fn to_snapshot(&self) -> A11yNodeSnapshot {
        A11yNodeSnapshot {
            id: self.id,
            parent_id: self.parent_id,
            role: self.role,
            bounds: self.bounds,
            label_hash: hash_opt_str(self.label),
            value_hash: hash_opt_str(self.value_text),
            focusable: self.focusable,
            focused: self.focused,
            disabled: self.disabled,
            checked: self.checked,
            scroll_info: self.scroll_info,
        }
    }
}

impl PartialEq for A11yNode<'_> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.content_eq(other)
    }
}

impl Eq for A11yNode<'_> {}

/// 无生命周期借用的跨帧无障碍节点指纹(`Copy`,可跨越 `ScratchArena::rewind()` 持久化存储以驱动增量 Diff)。
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct A11yNodeSnapshot {
    pub id: u32,
    pub parent_id: Option<u32>,
    pub role: A11yRole,
    pub bounds: Region,
    pub label_hash: u64,
    pub value_hash: u64,
    pub focusable: bool,
    pub focused: bool,
    pub disabled: bool,
    pub checked: Option<bool>,
    pub scroll_info: Option<(u32, i16, bool)>,
}

/// 与 AccessKit `TreeUpdate` 1:1 映射的 `#![no_std]` 零堆增量无障碍树更新补丁。
#[derive(Debug)]
pub struct A11yTreeUpdate<'a, const CAP: usize> {
    /// 根节点 ID(固定为 `0` 对应 [`A11yRole::Window`])。
    pub root_id: u32,
    /// 本帧当前拥有焦点的语义节点 ID(`None` 表示焦点停留在窗口根)。
    pub focus_id: Option<u32>,
    /// 本帧焦点是否相较上一帧发生转移。
    pub focus_changed: bool,
    /// 本帧新增或属性/位置/父关系发生变更的语义节点集合。
    pub updated_nodes: SmallVec<A11yNode<'a>, CAP>,
    /// 本帧从语义树中移除的旧节点 ID 列表。
    pub removed_ids: SmallVec<u32, CAP>,
}

impl<'a, const CAP: usize> A11yTreeUpdate<'a, CAP> {
    /// 当节点无任何新增、变更、删除且焦点未变化时返回 `true`(平台桥接层可直接跳过 IPC 推送)。
    #[inline]
    pub fn is_empty(&self) -> bool {
        !self.focus_changed && self.updated_nodes.is_empty() && self.removed_ids.is_empty()
    }
}

#[inline]
fn region_contains(outer: Region, inner: Region) -> bool {
    !outer.is_empty()
        && inner.x >= outer.x
        && inner.y >= outer.y
        && inner.right() <= outer.right()
        && inner.bottom() <= outer.bottom()
}

#[inline]
fn region_area(r: Region) -> i32 {
    (r.w.max(0) as i32) * (r.h.max(0) as i32)
}

/// 在已收集的语义节点中选择包含 `bounds` 的最内层合法父节点 ID(若无更小容器则回退为根窗口 `0`)。
fn find_best_parent_id(nodes: &[A11yNode<'_>], bounds: Region, self_id: u32, allow_leaf_parents: bool) -> u32 {
    let mut best_id = 0u32;
    let mut best_area = i32::MAX;
    for n in nodes.iter().skip(1) {
        if n.id == self_id {
            continue;
        }
        let can_be_parent = match n.role {
            A11yRole::Window | A11yRole::Container | A11yRole::ScrollRegion => true,
            A11yRole::Button | A11yRole::TextInput | A11yRole::Slider => allow_leaf_parents,
            A11yRole::Text | A11yRole::Image => false,
        };
        if !can_be_parent {
            continue;
        }
        if region_contains(n.bounds, bounds) {
            let area = region_area(n.bounds);
            if area <= best_area {
                best_area = area;
                best_id = n.id;
            }
        }
    }
    best_id
}

/// 从当前帧缓存的 `HitRegion` 与 `DrawOp` 列表构建层次化无障碍语义树节点序列(含 `parent_id` 归属关系)。
pub fn collect_a11y_nodes<'a, const CAP: usize>(
    viewport: Region,
    hits: &[HitRegion<'a>],
    ops: &[DrawOp<'a>],
    focused_index: Option<u16>,
) -> SmallVec<A11yNode<'a>, CAP> {
    let mut out = SmallVec::new();
    let _ = out.push(A11yNode {
        id: 0,
        parent_id: None,
        role: A11yRole::Window,
        bounds: viewport,
        label: None,
        value_text: None,
        focusable: false,
        focused: false,
        disabled: false,
        checked: None,
        scroll_info: None,
    });

    for (idx, hit) in hits.iter().enumerate() {
        let has_key_input = hit
            .interact
            .and_then(|i| i.rare)
            .and_then(|r| r.on_key_input)
            .is_some();
        let has_drag = hit
            .interact
            .and_then(|i| i.rare)
            .and_then(|r| r.on_drag)
            .is_some();
        let has_click = hit.interact.and_then(|i| i.on_click).is_some();

        let role = if has_key_input {
            A11yRole::TextInput
        } else if has_drag {
            A11yRole::Slider
        } else if has_click {
            A11yRole::Button
        } else if hit.scroll_target.is_some() {
            A11yRole::ScrollRegion
        } else {
            A11yRole::Container
        };

        // 查找落在该交互区域内的第一段文本作为无障碍标签(Accessible Name)
        let label = ops.iter().find_map(|op| match op.payload {
            DrawPayload::TextRun { text, .. }
                if !op.bounds.intersect(hit.bounds).is_empty() =>
            {
                Some(text)
            }
            _ => None,
        });
        let value_text = if role == A11yRole::TextInput {
            label
        } else {
            None
        };

        let id = hit.id.unwrap_or((idx as u32) + 1);
        let parent_id = Some(find_best_parent_id(out.as_slice(), hit.bounds, id, false));
        let focusable = hit.focus_index.is_some();
        let focused = hit.focus_index.is_some() && hit.focus_index == focused_index;
        let scroll_info = hit
            .scroll_target
            .map(|(sid, max_s)| (sid, max_s, hit.scroll_horizontal));

        let _ = out.push(A11yNode {
            id,
            parent_id,
            role,
            bounds: hit.bounds,
            label,
            value_text,
            focusable,
            focused,
            disabled: false,
            checked: None,
            scroll_info,
        });
    }

    for op in ops.iter() {
        match op.payload {
            DrawPayload::TextRun { text, .. } => {
                let parent = find_best_parent_id(out.as_slice(), op.bounds, u32::MAX, true);
                let ordinal = out
                    .as_slice()
                    .iter()
                    .filter(|n| n.role == A11yRole::Text && n.parent_id == Some(parent))
                    .count() as u32;
                let id = op
                    .id
                    .unwrap_or_else(|| crate::pod::derive_scoped_id(parent, 0xA117_7E57, ordinal));
                let _ = out.push(A11yNode {
                    id,
                    parent_id: Some(parent),
                    role: A11yRole::Text,
                    bounds: op.bounds,
                    label: Some(text),
                    value_text: None,
                    focusable: false,
                    focused: false,
                    disabled: false,
                    checked: None,
                    scroll_info: None,
                });
            }
            DrawPayload::Sprite(_) => {
                let parent = find_best_parent_id(out.as_slice(), op.bounds, u32::MAX, true);
                let ordinal = out
                    .as_slice()
                    .iter()
                    .filter(|n| n.role == A11yRole::Image && n.parent_id == Some(parent))
                    .count() as u32;
                let id = op
                    .id
                    .unwrap_or_else(|| crate::pod::derive_scoped_id(parent, 0xA117_16A9, ordinal));
                let _ = out.push(A11yNode {
                    id,
                    parent_id: Some(parent),
                    role: A11yRole::Image,
                    bounds: op.bounds,
                    label: None,
                    value_text: None,
                    focusable: false,
                    focused: false,
                    disabled: false,
                    checked: None,
                    scroll_info: None,
                });
            }
            DrawPayload::Quad { .. }
            | DrawPayload::StyledQuad(_)
            | DrawPayload::PushLayer(_)
            | DrawPayload::PopLayer
            | DrawPayload::ExternalTexture(_) => {}
        }
    }

    out
}

/// 将一帧无障碍节点列表压缩为无生命周期借用的 [`A11yNodeSnapshot`] 列表,供跨帧增量 Diff 使用。
pub fn snapshot_a11y_tree<const CAP: usize>(
    nodes: &[A11yNode<'_>],
) -> SmallVec<A11yNodeSnapshot, CAP> {
    let mut out = SmallVec::new();
    for n in nodes {
        let _ = out.push(n.to_snapshot());
    }
    out
}

/// 迭代查询指定 `parent_id` 的全部直接子节点(便于 AccessKit 桥接层填充 `Node::children`)。
#[inline]
pub fn children_of<'a, 'b>(
    nodes: &'b [A11yNode<'a>],
    parent_id: u32,
) -> impl Iterator<Item = &'b A11yNode<'a>> {
    nodes.iter().filter(move |n| n.parent_id == Some(parent_id))
}

/// 对比上一帧无生命周期快照 `prev` 与当前帧 `curr`,生成最小增量 [`A11yTreeUpdate`]。
pub fn diff_a11y_snapshot<'a, const CAP: usize>(
    prev: &[A11yNodeSnapshot],
    curr: &[A11yNode<'a>],
) -> A11yTreeUpdate<'a, CAP> {
    let mut updated_nodes = SmallVec::new();
    let mut removed_ids = SmallVec::new();

    let prev_focus = prev.iter().find(|n| n.focused).map(|n| n.id);
    let curr_focus = curr.iter().find(|n| n.focused).map(|n| n.id);

    for c in curr {
        let snap = c.to_snapshot();
        let unchanged = prev.iter().any(|p| p.id == snap.id && *p == snap);
        if !unchanged {
            let _ = updated_nodes.push(*c);
        }
    }

    for p in prev {
        if !curr.iter().any(|c| c.id == p.id) {
            let _ = removed_ids.push(p.id);
        }
    }

    A11yTreeUpdate {
        root_id: 0,
        focus_id: curr_focus,
        focus_changed: prev_focus != curr_focus,
        updated_nodes,
        removed_ids,
    }
}

/// 直接对比两帧无障碍节点序列 `prev` 与 `curr`,生成最小增量 [`A11yTreeUpdate`]。
pub fn diff_a11y_trees<'a, const CAP: usize>(
    prev: &[A11yNode<'_>],
    curr: &[A11yNode<'a>],
) -> A11yTreeUpdate<'a, CAP> {
    let mut updated_nodes = SmallVec::new();
    let mut removed_ids = SmallVec::new();

    let prev_focus = prev.iter().find(|n| n.focused).map(|n| n.id);
    let curr_focus = curr.iter().find(|n| n.focused).map(|n| n.id);

    for c in curr {
        let unchanged = prev.iter().any(|p| p.id == c.id && c.content_eq(p));
        if !unchanged {
            let _ = updated_nodes.push(*c);
        }
    }

    for p in prev {
        if !curr.iter().any(|c| c.id == p.id) {
            let _ = removed_ids.push(p.id);
        }
    }

    A11yTreeUpdate {
        root_id: 0,
        focus_id: curr_focus,
        focus_changed: prev_focus != curr_focus,
        updated_nodes,
        removed_ids,
    }
}