Skip to main content

mgui_core/
id.rs

1use slotmap::new_key_type;
2
3new_key_type! {
4    /// Element 句柄
5    pub struct ElementId;
6    /// RenderObject 句柄
7    pub struct RenderObjectId;
8    /// Layer 句柄
9    pub struct LayerId;
10}
11
12impl ElementId {
13    /// 无障碍树的合成根节点 id(不属于任何 Element)。
14    ///
15    /// AccessKit 要求单根,而主树与各 overlay 是平行森林,因此 core 在
16    /// `build_accessibility_tree` 里插入这个稳定 id 作为容器根。
17    /// 选 `u64::MAX`:slotmap 的实际分配永远达不到该索引,不会与真实 Element 冲突。
18    pub const A11Y_ROOT: Self = Self(slotmap::KeyData::from_ffi(u64::MAX));
19
20    /// 从 FFI u64 构造(供 runtime 测试 + accesskit NodeId 桥接)
21    pub fn from_ffi(data: u64) -> Self {
22        // slotmap 1.1 的 new_key_type 生成 tuple struct,KeyData::from_ffi 可用
23        #[allow(clippy::let_and_return)]
24        Self(slotmap::KeyData::from_ffi(data))
25    }
26
27    /// 导出为 FFI u64(和 NodeId 互转)
28    pub fn to_ffi(self) -> u64 {
29        <Self as slotmap::Key>::data(&self).as_ffi()
30    }
31}