Skip to main content

mgui_core/
measure.rs

1//! 统一测量环境契约(0.8)。
2//!
3//! 组件层的可变行高实测表必须知道“这批实测值是为哪套测量条件算的”。
4//! [`MeasureKey`] 把这套条件收敛成一个可比较的值:宽度签名、字体样式
5//! generation、DPR、内容 revision。
6//!
7//! 四个维度都对应框架里真实存在的输入,不是为缓存臆造的标签:
8//! - 宽度进入 `mgui_text::shape_text` 的 `max_width`,决定软换行行数;
9//! - `TextRuntime::style_generation()` 进入 shaped-line key,字体栈/行高变化推进它;
10//! - `TextRuntime::dpr()` 决定栅格空间(`size * dpr`),逻辑高度虽除以 DPR,
11//!   但 shaping 在栅格空间进行,DPR 变化可能带来舍入差异,必须进 key 保证确定性;
12//! - revision 由应用推进,覆盖“内容变了但行数没变”的场景。
13//!
14//! 刻意**不包含** widget / builder 闭包指针:每帧重建 widget 树是常态,按指针
15//! 作废等于每帧重测一遍,白付一次 shaping 成本。
16
17use std::collections::hash_map::DefaultHasher;
18use std::hash::{Hash, Hasher};
19
20/// 一次组件层实测所依赖的环境与内容版本。
21#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
22pub struct MeasureKey {
23    width_signature: u64,
24    font_generation: u64,
25    dpr_bits: u32,
26    revision: u64,
27}
28
29impl MeasureKey {
30    /// - `width_signature`:所有参与测量的宽度折成的签名,见 [`width_signature`]。
31    /// - `font_generation`:`TextRuntime::style_generation()`。
32    /// - `dpr`:`TextRuntime::dpr()`。
33    /// - `revision`:应用推进的内容版本。
34    pub fn new(width_signature: u64, font_generation: u64, dpr: f32, revision: u64) -> Self {
35        Self { width_signature, font_generation, dpr_bits: normalize_dpr(dpr).to_bits(), revision }
36    }
37
38    pub fn width_signature(&self) -> u64 {
39        self.width_signature
40    }
41
42    pub fn font_generation(&self) -> u64 {
43        self.font_generation
44    }
45
46    pub fn dpr(&self) -> f32 {
47        f32::from_bits(self.dpr_bits)
48    }
49
50    pub fn revision(&self) -> u64 {
51        self.revision
52    }
53}
54
55/// 把一组参与测量的宽度(逻辑像素)折成稳定签名。
56///
57/// 顺序敏感:列宽数组不能先排序,否则列序交换后仍命中旧实测值。
58/// 非有限值 / 负值按 0 处理,避免 NaN 位模式让同一次测量被判成不同环境。
59pub fn width_signature(widths: impl IntoIterator<Item = f32>) -> u64 {
60    let mut h = DefaultHasher::new();
61    for w in widths {
62        let w = if w.is_finite() && w > 0.0 { w } else { 0.0 };
63        w.to_bits().hash(&mut h);
64    }
65    h.finish()
66}
67
68/// 单一测量宽度的签名(列表 / 树的交叉轴宽度)。
69pub fn single_width_signature(width: f32) -> u64 {
70    width_signature([width])
71}
72
73fn normalize_dpr(dpr: f32) -> f32 {
74    if dpr.is_finite() && dpr > 0.0 {
75        dpr
76    } else {
77        1.0
78    }
79}
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84
85    fn key(width: f32, gen: u64, dpr: f32, rev: u64) -> MeasureKey {
86        MeasureKey::new(single_width_signature(width), gen, dpr, rev)
87    }
88
89    #[test]
90    fn identical_environment_produces_identical_key() {
91        assert_eq!(key(200.0, 7, 1.5, 3), key(200.0, 7, 1.5, 3));
92    }
93
94    #[test]
95    fn each_dimension_invalidates_the_key() {
96        let base = key(200.0, 7, 1.5, 3);
97        assert_ne!(base, key(240.0, 7, 1.5, 3), "宽度变化必须重测");
98        assert_ne!(base, key(200.0, 8, 1.5, 3), "字体 generation 变化必须重测");
99        assert_ne!(base, key(200.0, 7, 2.0, 3), "DPR 变化必须重测");
100        assert_ne!(base, key(200.0, 7, 1.5, 4), "内容 revision 变化必须重测");
101    }
102
103    #[test]
104    fn non_finite_widths_are_normalized() {
105        assert_eq!(single_width_signature(f32::NAN), single_width_signature(0.0));
106        assert_eq!(single_width_signature(f32::INFINITY), single_width_signature(0.0));
107        assert_eq!(key(f32::NAN, 1, 1.0, 0), key(0.0, 1, 1.0, 0));
108    }
109
110    #[test]
111    fn invalid_dpr_falls_back_to_one() {
112        assert_eq!(key(200.0, 1, f32::NAN, 0), key(200.0, 1, 1.0, 0));
113    }
114
115    #[test]
116    fn width_signature_is_order_sensitive() {
117        assert_ne!(
118            width_signature([100.0, 200.0]),
119            width_signature([200.0, 100.0]),
120            "列序交换后可用宽度归属变了,不能命中旧实测值"
121        );
122    }
123}