mgui-core 0.8.0

mgui 框架核心:Element/RenderObject/Widget 三树、布局与绘制协议、帧管线与响应式原语
Documentation
//! 统一测量环境契约(0.8)。
//!
//! 组件层的可变行高实测表必须知道“这批实测值是为哪套测量条件算的”。
//! [`MeasureKey`] 把这套条件收敛成一个可比较的值:宽度签名、字体样式
//! generation、DPR、内容 revision。
//!
//! 四个维度都对应框架里真实存在的输入,不是为缓存臆造的标签:
//! - 宽度进入 `mgui_text::shape_text` 的 `max_width`,决定软换行行数;
//! - `TextRuntime::style_generation()` 进入 shaped-line key,字体栈/行高变化推进它;
//! - `TextRuntime::dpr()` 决定栅格空间(`size * dpr`),逻辑高度虽除以 DPR,
//!   但 shaping 在栅格空间进行,DPR 变化可能带来舍入差异,必须进 key 保证确定性;
//! - revision 由应用推进,覆盖“内容变了但行数没变”的场景。
//!
//! 刻意**不包含** widget / builder 闭包指针:每帧重建 widget 树是常态,按指针
//! 作废等于每帧重测一遍,白付一次 shaping 成本。

use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

/// 一次组件层实测所依赖的环境与内容版本。
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct MeasureKey {
    width_signature: u64,
    font_generation: u64,
    dpr_bits: u32,
    revision: u64,
}

impl MeasureKey {
    /// - `width_signature`:所有参与测量的宽度折成的签名,见 [`width_signature`]。
    /// - `font_generation`:`TextRuntime::style_generation()`。
    /// - `dpr`:`TextRuntime::dpr()`。
    /// - `revision`:应用推进的内容版本。
    pub fn new(width_signature: u64, font_generation: u64, dpr: f32, revision: u64) -> Self {
        Self { width_signature, font_generation, dpr_bits: normalize_dpr(dpr).to_bits(), revision }
    }

    pub fn width_signature(&self) -> u64 {
        self.width_signature
    }

    pub fn font_generation(&self) -> u64 {
        self.font_generation
    }

    pub fn dpr(&self) -> f32 {
        f32::from_bits(self.dpr_bits)
    }

    pub fn revision(&self) -> u64 {
        self.revision
    }
}

/// 把一组参与测量的宽度(逻辑像素)折成稳定签名。
///
/// 顺序敏感:列宽数组不能先排序,否则列序交换后仍命中旧实测值。
/// 非有限值 / 负值按 0 处理,避免 NaN 位模式让同一次测量被判成不同环境。
pub fn width_signature(widths: impl IntoIterator<Item = f32>) -> u64 {
    let mut h = DefaultHasher::new();
    for w in widths {
        let w = if w.is_finite() && w > 0.0 { w } else { 0.0 };
        w.to_bits().hash(&mut h);
    }
    h.finish()
}

/// 单一测量宽度的签名(列表 / 树的交叉轴宽度)。
pub fn single_width_signature(width: f32) -> u64 {
    width_signature([width])
}

fn normalize_dpr(dpr: f32) -> f32 {
    if dpr.is_finite() && dpr > 0.0 {
        dpr
    } else {
        1.0
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn key(width: f32, gen: u64, dpr: f32, rev: u64) -> MeasureKey {
        MeasureKey::new(single_width_signature(width), gen, dpr, rev)
    }

    #[test]
    fn identical_environment_produces_identical_key() {
        assert_eq!(key(200.0, 7, 1.5, 3), key(200.0, 7, 1.5, 3));
    }

    #[test]
    fn each_dimension_invalidates_the_key() {
        let base = key(200.0, 7, 1.5, 3);
        assert_ne!(base, key(240.0, 7, 1.5, 3), "宽度变化必须重测");
        assert_ne!(base, key(200.0, 8, 1.5, 3), "字体 generation 变化必须重测");
        assert_ne!(base, key(200.0, 7, 2.0, 3), "DPR 变化必须重测");
        assert_ne!(base, key(200.0, 7, 1.5, 4), "内容 revision 变化必须重测");
    }

    #[test]
    fn non_finite_widths_are_normalized() {
        assert_eq!(single_width_signature(f32::NAN), single_width_signature(0.0));
        assert_eq!(single_width_signature(f32::INFINITY), single_width_signature(0.0));
        assert_eq!(key(f32::NAN, 1, 1.0, 0), key(0.0, 1, 1.0, 0));
    }

    #[test]
    fn invalid_dpr_falls_back_to_one() {
        assert_eq!(key(200.0, 1, f32::NAN, 0), key(200.0, 1, 1.0, 0));
    }

    #[test]
    fn width_signature_is_order_sensitive() {
        assert_ne!(
            width_signature([100.0, 200.0]),
            width_signature([200.0, 100.0]),
            "列序交换后可用宽度归属变了,不能命中旧实测值"
        );
    }
}