Skip to content

Commit 56d14e6

Browse files
committed
Virtual items: remount keyed absolute windows
1 parent 77dd35f commit 56d14e6

25 files changed

Lines changed: 774 additions & 18 deletions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- `El::virtual_items(Rc<dyn VirtualItems>)` mounts application-selected, absolutely positioned keyed windows without re-running the view when only the window changes. Staying subtrees retain their bindings, memos and focus; entering items mount settled within a per-frame priority budget, leaving items retire immediately, and focused items remain mounted until focus leaves. Indexed virtual lists can share the tree, and partial window commits now refresh live glyphs while preserving their authored placeholder measurements.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//! Paired headless measurements. Run with the perf profile; no GPU upload or presentation.
2+
use rune::{Color, Cx, El, Env, Live, Runtime, VirtualItems, col, el, text};
3+
use std::{cell::Cell, rc::Rc, time::Instant};
4+
5+
struct Source {
6+
catalog: Vec<String>,
7+
offset: Cell<usize>,
8+
revision: Cell<u64>,
9+
builds: Cell<usize>,
10+
complex: bool,
11+
opacity: Live<f32>,
12+
}
13+
impl VirtualItems for Source {
14+
fn version(&self) -> u64 {
15+
self.revision.get()
16+
}
17+
fn keys(&self) -> Vec<String> {
18+
(0..50).map(|i| self.catalog[(self.offset.get() + i) % self.catalog.len()].clone()).collect()
19+
}
20+
fn build(&self, key: &str) -> El {
21+
self.builds.set(self.builds.get() + 1);
22+
let i = key[1..].parse::<usize>().unwrap();
23+
let item = col().absolute((i % 10) as f32 * 60.0, (i % 5) as f32 * 60.0).size(54.0, 54.0);
24+
if !self.complex {
25+
return item.fill(Color::WHITE);
26+
}
27+
item.live_opacity(&self.opacity).children((0..19).map(|j| {
28+
let p =
29+
if j % 4 == 0 { text("Canvas node").h(5.0) } else { el().size(8.0, 2.0).fill(Color::WHITE) };
30+
if j % 3 == 0 { p.live_opacity(&self.opacity) } else { p }
31+
}))
32+
}
33+
}
34+
struct App {
35+
source: Rc<Source>,
36+
views: Cell<usize>,
37+
}
38+
fn view(s: &App, _: &mut Cx<App>) -> El {
39+
s.views.set(s.views.get() + 1);
40+
el().size(640.0, 480.0).virtual_items(s.source.clone())
41+
}
42+
43+
fn main() {
44+
assert!(!cfg!(debug_assertions), "benchmarks require --profile perf");
45+
println!("BENCH,kind,total,enter,full,round,mean_ms,p95_ms,max_ms,views,builds");
46+
for complex in [false, true] {
47+
for total in [100, 10_000, 50_000] {
48+
for enter in [1, 4, 16] {
49+
for round in 0..5 {
50+
for full in if round % 2 == 0 { [false, true] } else { [true, false] } {
51+
let source = Rc::new(Source {
52+
catalog: (0..total).map(|i| format!("n{i}")).collect(),
53+
offset: Cell::new(0),
54+
revision: Cell::new(0),
55+
builds: Cell::new(0),
56+
complex,
57+
opacity: Live::new(0.8),
58+
});
59+
let mut rt = Runtime::new(
60+
App { source: source.clone(), views: Cell::new(0) },
61+
view,
62+
(640.0, 480.0),
63+
Env::new(true, 2.0),
64+
);
65+
rt.frame(0.0);
66+
let mut samples = Vec::new();
67+
for frame in 0..420 {
68+
let start = Instant::now();
69+
source.offset.set((source.offset.get() + enter) % total);
70+
source.revision.set(source.revision.get() + 1);
71+
let views = rt.state.views.get();
72+
let builds = source.builds.get();
73+
if full {
74+
rt.invalidate();
75+
}
76+
rt.frame(1.0 / 120.0);
77+
let ms = start.elapsed().as_secs_f64() * 1000.0;
78+
assert_eq!(rt.state.views.get() - views, usize::from(full));
79+
assert_eq!(source.builds.get() - builds, if full { 50 } else { enter });
80+
assert_eq!(
81+
rt.elements().iter().filter(|n| n.path.matches('/').count() == 1).count(),
82+
50
83+
);
84+
if frame >= 120 {
85+
samples.push(ms);
86+
}
87+
}
88+
samples.sort_by(f64::total_cmp);
89+
let mean = samples.iter().sum::<f64>() / samples.len() as f64;
90+
println!(
91+
"BENCH,{},{total},{enter},{full},{round},{mean:.6},{:.6},{:.6},{},{}",
92+
if complex { "complex" } else { "simple" },
93+
samples[284],
94+
samples[299],
95+
if full { 300 } else { 0 },
96+
if full { 15_000 } else { enter * 300 }
97+
);
98+
}
99+
}
100+
}
101+
}
102+
}
103+
}

‎crates/rune/src/el.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ pub fn text(content: impl Into<String>) -> El {
100100
}
101101
mod text_spec;
102102
mod tx;
103+
#[path = "el/virtual/items.rs"]
104+
pub(crate) mod virtual_items;
105+
#[path = "el/virtual/list.rs"]
103106
pub(crate) mod virtual_list;
107+
#[path = "el/virtual/rows.rs"]
104108
pub(crate) mod virtual_rows;
105109
mod visual;
106110
mod visuals;

‎crates/rune/src/el/rare.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ pub(crate) struct ElCold {
3232
pub(crate) scroll_edges: ScrollEdges,
3333
pub(crate) growth: Option<Box<GrowthSpec>>,
3434
pub(crate) virtual_estimate: Option<f32>,
35+
/// The application-selected window, built at layout time.
36+
pub(crate) virtual_items: Option<Rc<dyn crate::VirtualItems>>,
3537
/// Rows `El::virtual_rows` builds at layout time, in place of `children`.
3638
pub(crate) virtual_rows: Option<Rc<crate::el::virtual_rows::RowSource>>,
3739
/// [`El::virtual_rows_version`](crate::El::virtual_rows_version), hashed.
@@ -76,6 +78,7 @@ impl Empty for ElCold {
7678
scroll_edges: ScrollEdges { fade: 0.0, fade_style: crate::el::FadeStyle::SMOOTH, margin: 0.0, anchor: false },
7779
growth: None,
7880
virtual_estimate: None,
81+
virtual_items: None,
7982
virtual_rows: None,
8083
virtual_rows_version: None,
8184
glide: None,
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//! Application-selected, absolutely positioned virtual windows.
2+
use std::rc::Rc;
3+
4+
use crate::{El, el::ElFlags};
5+
6+
/// The UI-thread source of an application-selected virtual window.
7+
///
8+
/// Hold shared application data in the source (for example an `Rc<RefCell<_>>`), borrowing
9+
/// only within each method. Rune retains the source until its container leaves the tree.
10+
/// Publish changes and request a frame through the application's poll/input/wake mechanism;
11+
/// this source does not wake an idle host by itself.
12+
pub trait VirtualItems: 'static {
13+
/// Cheap revision read, checked after poll and input, before drawing each frame.
14+
/// Bump for changes to membership, stacking order or mount priority. To replace an
15+
/// already mounted item's content without a root commit, give it a new key.
16+
fn version(&self) -> u64;
17+
/// Unique stable keys in back-to-front stacking order, queried only when the revision
18+
/// changes. Return just the window's keys, not the entire scene. Keys must not contain `/`.
19+
fn keys(&self) -> Vec<String>;
20+
/// Build a keyed item's subtree. Rune sets its key and absolute positioning.
21+
fn build(&self, key: &str) -> El;
22+
/// Maximum newly mounted items per frame, including the initial frame; at least one.
23+
fn mount_budget(&self) -> usize {
24+
usize::MAX
25+
}
26+
/// Lower values mount first (for example squared distance from the viewport center).
27+
/// Ties follow stacking order; mounting never changes stacking order.
28+
fn mount_priority(&self, _key: &str) -> u64 {
29+
0
30+
}
31+
}
32+
33+
impl El {
34+
/// Mount only the absolutely positioned children selected by `source`.
35+
///
36+
/// The container takes no authored children and must not also use `virtual_rows` or
37+
/// `virtualize`. Give it an explicit size: its children do not determine its size.
38+
/// Window changes alone retain existing subtrees, Live bindings, memos, clocks and focus
39+
/// without running the root view. New items mount settled, with no entrance replay;
40+
/// leaving items unmount immediately without exits. A focused item's subtree stays
41+
/// mounted outside the window until focus leaves it (the source must still build its key).
42+
/// Hit testing, accessibility and Tab order see mounted children only, including that pin.
43+
///
44+
/// Mounting is spread across frames in priority order, with drawing in `keys` order.
45+
/// Root commits use the same window and budget but may rebuild its mounted children.
46+
/// Nested virtual containers, non-indexed virtual lists, changed state/layout reads,
47+
/// resizing, hot reload and other unsupported retained-layout cases use a full commit.
48+
/// All of these still build the selected window, including focus pins and the budget.
49+
pub fn virtual_items(mut self, source: Rc<dyn VirtualItems>) -> Self {
50+
assert!(self.cold.virtual_estimate.is_none(), "virtual_items cannot also virtualize rows");
51+
self.cold_mut().virtual_items = Some(source);
52+
self.flags.set(ElFlags::HAS_VIRTUAL, true);
53+
self
54+
}
55+
}

‎crates/rune/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ pub use el::row;
135135
pub use el::spacer;
136136
pub use el::text;
137137
pub use el::text_input;
138+
pub use el::virtual_items::VirtualItems;
138139
pub use file_dialog::FileDialogError;
139140
pub use file_dialog::FileDialogKind;
140141
pub use file_dialog::FileDialogOptions;

‎crates/rune/src/runtime.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ pub(crate) fn handler_state_ref<S: 'static>(state: &S) -> &dyn Any {
7878
#[cfg(debug_assertions)]
7979
#[path = "runtime/commit/layout_diagnostics.rs"]
8080
mod layout_diagnostics;
81+
#[path = "runtime/virtual/items.rs"]
82+
mod items;
8183
#[path = "runtime/paint/blend.rs"]
8284
mod blend;
8385
#[path = "runtime/commit/watch.rs"]
@@ -551,6 +553,7 @@ pub struct Runtime<S: 'static> {
551553
touch: touch_input::Touch,
552554
grows: HashMap<NodeId, GrowTrack>,
553555
stacks: HashMap<NodeId, Stack>,
556+
pub(crate) items: items::Windows,
554557
pub(crate) virtuals: HashMap<String, crate::el::virtual_list::VirtualList>,
555558
/// The recorder `Tx::record_rewind` attached (`runtime/motion/rewind.rs`).
556559
rewind: Option<crate::rewind::Rewind>,
@@ -950,6 +953,7 @@ impl<S: 'static> Runtime<S> {
950953
touch: Default::default(),
951954
grows: HashMap::default(),
952955
stacks: HashMap::default(),
956+
items: items::Windows::default(),
953957
virtuals: HashMap::default(),
954958
rewind: None,
955959
virtual_culled: HashSet::default(),

‎crates/rune/src/runtime/commit.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub(super) struct Measurement<'a, S: 'static> {
3737
pub(super) memo: &'a mut crate::memo::MemoCache,
3838
pub(super) next_id: &'a mut u64,
3939
pub(super) scrolls: &'a mut HashMap<NodeId, ScrollState>,
40+
pub(super) items: &'a mut items::Windows,
4041
pub(super) virtuals: &'a mut HashMap<String, crate::el::virtual_list::VirtualList>,
4142
pub(super) virtual_culled: &'a mut HashSet<String>,
4243
pub(super) virtual_inputs: &'a mut HashMap<String, Box<InputSpec>>,

0 commit comments

Comments
 (0)