Skip to main content

virtual_list/
virtual_list.rs

1//! virtual_list — exercises the virtualized list primitive.
2//!
3//! 10,000 rows of fixed height in a small viewport, scrolled into the
4//! middle of the list. The bundle artifacts should show that only the
5//! ~handful of rows whose rect intersects the viewport are realized
6//! — `tree.txt` lists rows in the visible window, not all 10k.
7//!
8//! Run: `cargo run -p aetna-core --example virtual_list`
9
10use aetna_core::prelude::*;
11// This headless artifact example seeds scroll state before rendering,
12// so it opts into the explicit advanced state/layout modules.
13use aetna_core::{UiState, layout};
14
15const ROW_COUNT: usize = 10_000;
16const ROW_HEIGHT: f32 = 44.0;
17
18fn build_row(i: usize) -> El {
19    let badge_el = match i % 5 {
20        0 => badge("info").muted(),
21        1 => badge("warn").warning(),
22        2 => badge("ok").success(),
23        3 => badge("err").destructive(),
24        _ => spacer(),
25    };
26    row([
27        text(format!("#{i:05}")).mono(),
28        spacer(),
29        text(format!("entry {i}")),
30        spacer(),
31        badge_el,
32    ])
33    .key(format!("row-{i}"))
34    .gap(tokens::SPACE_3)
35    .padding(Sides::xy(tokens::SPACE_3, tokens::SPACE_2))
36    .height(Size::Fixed(ROW_HEIGHT))
37}
38
39fn fixture() -> El {
40    column([
41        h1("Virtualized list"),
42        paragraph(format!(
43            "{ROW_COUNT} rows × {ROW_HEIGHT}px in a windowed viewport. \
44             Only the rows intersecting the viewport are realized — see \
45             `tree.txt` for proof."
46        ))
47        .muted(),
48        virtual_list(ROW_COUNT, ROW_HEIGHT, build_row)
49            .key("entries")
50            .height(Size::Fill(1.0)),
51    ])
52    .gap(tokens::SPACE_4)
53    .padding(tokens::SPACE_7)
54}
55
56fn find_id(node: &El, key: &str) -> Option<String> {
57    if node.key.as_deref() == Some(key) {
58        return Some(node.computed_id.clone());
59    }
60    for c in &node.children {
61        if let Some(id) = find_id(c, key) {
62            return Some(id);
63        }
64    }
65    None
66}
67
68fn main() -> std::io::Result<()> {
69    let mut root = fixture();
70    layout::assign_ids(&mut root);
71    let list_id = find_id(&root, "entries").expect("virtual_list id");
72
73    let mut ui_state = UiState::new();
74    // Scroll so the realized window is near row 5000.
75    ui_state.set_scroll_offset(list_id, 5000.0 * ROW_HEIGHT);
76
77    let viewport = Rect::new(0.0, 0.0, 540.0, 540.0);
78    let bundle = render_bundle_with(&mut root, &mut ui_state, viewport);
79
80    let out_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("out");
81    let written = write_bundle(&bundle, &out_dir, "virtual_list")?;
82    for p in &written {
83        println!("wrote {}", p.display());
84    }
85
86    if !bundle.lint.findings.is_empty() {
87        eprintln!("\nlint findings ({}):", bundle.lint.findings.len());
88        eprint!("{}", bundle.lint.text());
89    }
90
91    Ok(())
92}