Module stats

Source
Expand description

Measured, bounded label/value lists in logical UI units.

Values and units belong to the caller: borrow already formatted strings (a reusable String works), including explicit unavailable copy. No parsing, rounding, unit conversion, ellipsis or font scaling occurs here. Wrapping uses WrappedText and the existing bitmap painter: ASCII is uppercased, unsupported Unicode glyphs paint as ?, and wrap-boundary whitespace follows that API. Inspection retains the supplied UTF-8 copy.

A runnable result-summary recipe with actual retained UI and inspection. The same content selects columns at 640 logical pixels and stacks at 320; the card accepts only a complete list. A caller could instead scroll, expand, or show an explicit hidden-row message using the returned report.

use ferravex_render::ui::{ButtonStyle, Ui, UiError, UiLabel, UiRect, Viewport};
use ferravex_render::ui::stats::{StatRow, StatRowsLayout, StatRowsMode, StatRowsStyle};
use std::fmt::Write;
let mut ui = Ui::new(); // Retain across frames.
ui.set_inspection_enabled(true);
let mut time = String::with_capacity(32);
let mut delta = String::with_capacity(32);
let mut count = String::with_capacity(32);
// Caller owns time rollover, signs, precision and units.
let millis = 60_001_u64;
write!(time, "{}:{:02}.{:03}", millis / 60_000, millis / 1_000 % 60, millis % 1_000).unwrap();
write!(delta, "{:+.3} s", -1.25_f64).unwrap();
write!(count, "{}", u64::MAX).unwrap();
let rows = [
    StatRow { label: "TIME", value: &time, description: None },
    StatRow { label: "DELTA", value: &delta, description: None },
    StatRow { label: "DELIVERIES", value: &count, description: Some("Accepted cargo only.") },
    StatRow { label: "BEST", value: "UNAVAILABLE", description: None },
];
for (width, mode) in [(640., StatRowsMode::Columns), (320., StatRowsMode::Stacked)] {
    ui.begin(Viewport::responsive([width, 440.], [0.; 4])?);
    let bounds = UiRect { min: [16., 16.], size: [width - 32., 340.] };
    let layout = StatRowsLayout::new(bounds, &rows, StatRowsStyle::default())?;
    assert_eq!(layout.report().mode, mode);
    let submitted = ui.compose(|card| {
        card.panel(UiRect { min: [8., 8.], size: [width - 16., 420.] }, ferravex_render::ui::PANEL)?;
        let report = layout.submit(card)?;
        if report.hidden_rows != 0 { return Err(UiError::Capacity); }
        card.button(901, UiRect { min: [16., 374.], size: [width - 32., 44.] },
                    "CONTINUE", true, ButtonStyle::default())?;
        Ok(report)
    })?;
    assert_eq!((submitted.visible_rows, submitted.hidden_rows), (4, 0));
    // These are the actual retained label/value rectangles, shared with paint.
    let mut inspection = ui.inspect().unwrap().skip(1); // Panel.
    for (row, geometry) in rows.iter().zip(layout.rows()) {
        for (copy, rect) in [(row.label, geometry.label), (row.value, geometry.value)] {
            let element = inspection.next().unwrap();
            assert_eq!(element.label, UiLabel::Full(copy));
            assert_eq!(element.painted_label, copy);
            assert_eq!(element.bounds, rect);
            assert_eq!(element.visible_bounds, Some(rect));
        }
        if let Some(copy) = row.description {
            let element = inspection.next().unwrap();
            assert_eq!(element.label, UiLabel::Full(copy));
            assert_eq!(element.bounds, geometry.description.unwrap());
        }
    }
    ui.paint(); // Headless retained paint, not a device readability test.
}

Structs§

StatRow
Borrowed caller-authored copy. An empty value stays empty; supply a distinct string such as "UNAVAILABLE" when that is the metric’s meaning.
StatRowGeometry
Exact full-line-slot rectangles used for accepted text commands. The row includes its description but excludes the gap before the next row.
StatRowsLayout
Immutable borrowed layout with fixed stack storage for at most 64 row geometries, no heap allocation or retained cache. Rebuild for changed copy, styles or bounds; construction scans all supplied text, including hidden rows. Source lengths and scanning work are caller-owned, not capped here. Submission copies visible source into the existing bounded UI arena.
StatRowsReport
Measurements before viewport projection, ancestor clipping or occlusion. Visible rows form a complete prefix; no partial row is ever submitted.
StatRowsStyle
Fixed text sizes and nonnegative gaps, all in logical units. Each block honors color/alignment/shadow; wrap is ignored and always enabled. Defaults use size 18 for every block. No automatic font-size/accessibility policy is imposed; choose a readable size and responsive viewport in the consumer.

Enums§

StatRowsMode
One mode is selected for the whole list using every supplied label/value, including rows hidden by height. Descriptions do not affect column choice.

Constants§

MAX_STAT_ROWS
Hard bound on measured rows. Larger slices return UiError::Capacity before scanning any text; callers can paginate their source explicitly.