gpui-rhai 0.1.8

A Rhai-authored declarative UI runtime for GPUI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
use std::cell::RefCell;
use std::collections::{BTreeMap, BTreeSet};
use std::ops::Range;
use std::rc::Rc;
use std::sync::Arc;

use thiserror::Error;

use crate::{ComponentInstancePath, UiNode};

#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct VirtualCollectionId {
    pub component: ComponentInstancePath,
    pub key: String,
}

#[derive(Clone, Debug, PartialEq)]
pub struct VirtualCollectionNodeSpec {
    pub id: VirtualCollectionId,
    pub label: String,
    pub data: crate::VirtualCollectionData,
    pub realized: BTreeMap<usize, UiNode>,
    pub estimated_height: f64,
    /// Fixed logical-pixel viewport height, or `None` to fill the resolved
    /// height offered by the parent flex layout.
    pub height: Option<f64>,
    pub overdraw_pixels: f64,
    pub bottom_align: bool,
    pub follow_tail: bool,
    /// Stable data key to reveal when this controlled target changes.
    pub reveal_key: Option<String>,
    /// Item indices whose realized nodes act as top-pinned section headers.
    pub sticky_headers: Arc<BTreeSet<usize>>,
    /// Presentation-only motion group inherited from a surrounding
    /// `motion_group`; applied to items realized after the initial render.
    pub inherited_motion_group: Option<String>,
}

#[derive(Clone, Debug, Default)]
pub struct VirtualRequestRegistry {
    requests: Rc<RefCell<BTreeMap<VirtualCollectionId, BTreeSet<usize>>>>,
    metrics: Rc<RefCell<BTreeMap<VirtualCollectionId, VirtualCollectionMetrics>>>,
    wake: crate::async_runtime::AsyncWake,
}

#[derive(Clone, Debug, PartialEq)]
pub struct VirtualCollectionMetrics {
    pub id: VirtualCollectionId,
    pub item_count: usize,
    pub realized_count: usize,
    pub realized_range: Range<usize>,
    pub requested_count: usize,
    pub requested_range: Range<usize>,
    pub visible_range: Range<usize>,
    pub viewport_height: f64,
    pub scroll_item: usize,
    pub scroll_offset: f64,
    pub is_scrolled: bool,
    pub sticky_header: Option<usize>,
    pub bottom_align: bool,
    pub follow_tail: bool,
}

impl VirtualCollectionMetrics {
    fn new(id: VirtualCollectionId) -> Self {
        Self {
            id,
            item_count: 0,
            realized_count: 0,
            realized_range: 0..0,
            requested_count: 0,
            requested_range: 0..0,
            visible_range: 0..0,
            viewport_height: 0.0,
            scroll_item: 0,
            scroll_offset: 0.0,
            is_scrolled: false,
            sticky_header: None,
            bottom_align: false,
            follow_tail: false,
        }
    }
}

#[derive(Clone, Debug)]
pub(crate) struct VirtualRequestSnapshot {
    requests: BTreeMap<VirtualCollectionId, BTreeSet<usize>>,
    metrics: BTreeMap<VirtualCollectionId, VirtualCollectionMetrics>,
}

impl VirtualRequestRegistry {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    pub fn request(&self, id: VirtualCollectionId, indices: impl IntoIterator<Item = usize>) {
        let indices = indices.into_iter().collect::<Vec<_>>();
        let requested_count = {
            let mut requests = self.requests.borrow_mut();
            let requested = requests.entry(id.clone()).or_default();
            requested.extend(indices.iter().copied());
            requested.len()
        };
        let new_range = index_range(indices);
        let mut metrics = self.metrics.borrow_mut();
        let metrics = metrics
            .entry(id.clone())
            .or_insert_with(|| VirtualCollectionMetrics::new(id));
        metrics.requested_count = requested_count;
        if !new_range.is_empty() {
            metrics.requested_range = if metrics.requested_range.is_empty() {
                new_range
            } else {
                metrics.requested_range.start.min(new_range.start)
                    ..metrics.requested_range.end.max(new_range.end)
            };
        }
        self.wake.notify();
    }

    /// Replace one collection's pending request with a complete atomic target.
    ///
    /// Unlike [`Self::request`], this does not union partial layout callbacks.
    /// The virtual element calls it only after collecting a complete prepaint.
    pub(crate) fn request_target(
        &self,
        id: VirtualCollectionId,
        indices: impl IntoIterator<Item = usize>,
    ) {
        let indices = indices.into_iter().collect::<BTreeSet<_>>();
        let requested_count = indices.len();
        self.requests
            .borrow_mut()
            .insert(id.clone(), indices.clone());
        let requested_range = index_range(indices);
        let mut metrics = self.metrics.borrow_mut();
        let metrics = metrics
            .entry(id.clone())
            .or_insert_with(|| VirtualCollectionMetrics::new(id));
        metrics.requested_count = requested_count;
        metrics.requested_range = requested_range;
        self.wake.notify();
    }

    pub(crate) fn clear_target(&self, id: &VirtualCollectionId) {
        self.requests.borrow_mut().remove(id);
        if let Some(metrics) = self.metrics.borrow_mut().get_mut(id) {
            metrics.requested_count = 0;
            metrics.requested_range = 0..0;
        }
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.requests.borrow().is_empty()
    }

    #[must_use]
    pub(crate) fn has_scope(&self, root: &ComponentInstancePath) -> bool {
        self.requests
            .borrow()
            .keys()
            .any(|id| id.component.is_within(root))
    }

    pub(crate) fn drain(&self) -> BTreeMap<VirtualCollectionId, BTreeSet<usize>> {
        let requests = std::mem::take(&mut *self.requests.borrow_mut());
        for metrics in self.metrics.borrow_mut().values_mut() {
            metrics.requested_count = 0;
            metrics.requested_range = 0..0;
        }
        requests
    }

    pub(crate) fn wake(&self) -> crate::async_runtime::AsyncWake {
        self.wake.clone()
    }

    pub(crate) fn snapshot(&self) -> VirtualRequestSnapshot {
        VirtualRequestSnapshot {
            requests: self.requests.borrow().clone(),
            metrics: self.metrics.borrow().clone(),
        }
    }

    pub(crate) fn restore(&self, snapshot: VirtualRequestSnapshot) {
        *self.requests.borrow_mut() = snapshot.requests;
        *self.metrics.borrow_mut() = snapshot.metrics;
    }

    pub(crate) fn retain(&self, active: &BTreeSet<VirtualCollectionId>) {
        self.requests
            .borrow_mut()
            .retain(|id, _| active.contains(id));
        self.metrics
            .borrow_mut()
            .retain(|id, _| active.contains(id));
    }

    pub(crate) fn report_frame(
        &self,
        spec: &VirtualCollectionNodeSpec,
        viewport_height: f64,
        scroll_item: usize,
        scroll_offset: f64,
        measured_visible: Option<Range<usize>>,
    ) {
        let mut metrics = self.metrics.borrow_mut();
        let metrics = metrics
            .entry(spec.id.clone())
            .or_insert_with(|| VirtualCollectionMetrics::new(spec.id.clone()));
        metrics.item_count = spec.data.len();
        metrics.realized_count = spec.realized.len();
        metrics.realized_range = index_range(spec.realized.keys().copied());
        if let Some(visible_range) = measured_visible {
            metrics.visible_range = visible_range;
        } else if metrics.visible_range.is_empty() {
            metrics.visible_range = metrics.realized_range.clone();
        } else {
            metrics.visible_range = metrics.visible_range.start.min(spec.data.len())
                ..metrics.visible_range.end.min(spec.data.len());
        }
        metrics.viewport_height = viewport_height;
        metrics.scroll_item = scroll_item;
        metrics.scroll_offset = scroll_offset;
        metrics.sticky_header = spec
            .sticky_headers
            .range(..=scroll_item)
            .next_back()
            .copied();
        metrics.bottom_align = spec.bottom_align;
        metrics.follow_tail = spec.follow_tail;
    }

    pub(crate) fn report_scroll(
        &self,
        id: &VirtualCollectionId,
        visible_range: Range<usize>,
        is_scrolled: bool,
    ) {
        let mut metrics = self.metrics.borrow_mut();
        let metrics = metrics
            .entry(id.clone())
            .or_insert_with(|| VirtualCollectionMetrics::new(id.clone()));
        metrics.visible_range = visible_range;
        metrics.is_scrolled = is_scrolled;
    }

    #[must_use]
    pub fn inspect(&self) -> Vec<VirtualCollectionMetrics> {
        self.metrics.borrow().values().cloned().collect()
    }

    pub(crate) fn metrics(&self, id: &VirtualCollectionId) -> Option<VirtualCollectionMetrics> {
        self.metrics.borrow().get(id).cloned()
    }
}

fn index_range(indices: impl IntoIterator<Item = usize>) -> Range<usize> {
    let mut indices = indices.into_iter();
    let Some(first) = indices.next() else {
        return 0..0;
    };
    let (min, max) = indices.fold((first, first), |(min, max), index| {
        (min.min(index), max.max(index))
    });
    min..max.saturating_add(1)
}

fn validate_viewport(scroll_offset: f64, viewport_height: f64) -> Result<(), VirtualListError> {
    if !scroll_offset.is_finite() || scroll_offset < 0.0 {
        return Err(VirtualListError::InvalidScrollOffset(scroll_offset));
    }
    if !viewport_height.is_finite() || viewport_height < 0.0 {
        return Err(VirtualListError::InvalidViewport(viewport_height));
    }
    Ok(())
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct VariableListSpec {
    pub estimated_height: f64,
    pub overscan_pixels: f64,
}

impl VariableListSpec {
    /// Construct a variable-height one-dimensional virtualization policy.
    ///
    /// # Errors
    ///
    /// Returns for non-finite/non-positive estimates or negative overscan.
    pub fn new(estimated_height: f64, overscan_pixels: f64) -> Result<Self, VirtualListError> {
        if !estimated_height.is_finite() || estimated_height <= 0.0 {
            return Err(VirtualListError::InvalidEstimatedHeight(estimated_height));
        }
        if !overscan_pixels.is_finite() || overscan_pixels < 0.0 {
            return Err(VirtualListError::InvalidOverscan(overscan_pixels));
        }
        Ok(Self {
            estimated_height,
            overscan_pixels,
        })
    }
}

#[derive(Clone, Debug, Default)]
pub struct VariableListState {
    keys: Vec<String>,
    indices: BTreeMap<String, usize>,
    measured: BTreeMap<String, f64>,
    heights: Vec<f64>,
    prefix: FenwickTree,
}

impl VariableListState {
    /// Install ordered keys while preserving measurements by identity.
    ///
    /// # Errors
    ///
    /// Returns for invalid policy or duplicate keys.
    pub fn set_keys(
        &mut self,
        keys: Vec<String>,
        spec: VariableListSpec,
    ) -> Result<(), VirtualListError> {
        VariableListSpec::new(spec.estimated_height, spec.overscan_pixels)?;
        let mut unique = BTreeSet::new();
        if let Some(key) = keys.iter().find(|key| !unique.insert((*key).clone())) {
            return Err(VirtualListError::DuplicateKey(key.clone()));
        }
        self.measured.retain(|key, _| unique.contains(key));
        self.indices = keys
            .iter()
            .enumerate()
            .map(|(index, key)| (key.clone(), index))
            .collect();
        self.heights = keys
            .iter()
            .map(|key| {
                self.measured
                    .get(key)
                    .copied()
                    .unwrap_or(spec.estimated_height)
            })
            .collect();
        self.prefix = FenwickTree::from_values(&self.heights);
        self.keys = keys;
        Ok(())
    }

    /// Record a measured item height and return the scroll correction required
    /// to keep an anchor key at the same viewport position.
    ///
    /// # Errors
    ///
    /// Returns for unknown keys or invalid measurements.
    pub fn measure(
        &mut self,
        key: &str,
        height: f64,
        anchor: Option<&str>,
    ) -> Result<f64, VirtualListError> {
        if !height.is_finite() || height <= 0.0 {
            return Err(VirtualListError::InvalidMeasurement(height));
        }
        let index = *self
            .indices
            .get(key)
            .ok_or_else(|| VirtualListError::UnknownKey(key.to_owned()))?;
        let anchor_index = anchor
            .map(|anchor| {
                self.indices
                    .get(anchor)
                    .copied()
                    .ok_or_else(|| VirtualListError::UnknownKey(anchor.to_owned()))
            })
            .transpose()?;
        let before = anchor_index.map_or(0.0, |anchor| self.prefix.sum(anchor));
        let delta = height - self.heights[index];
        self.heights[index] = height;
        self.measured.insert(key.to_owned(), height);
        self.prefix.add(index, delta);
        let after = anchor_index.map_or(0.0, |anchor| self.prefix.sum(anchor));
        Ok(after - before)
    }

    /// Compute the bounded realization window and spacer geometry.
    ///
    /// # Errors
    ///
    /// Returns viewport validation errors.
    pub fn window(
        &self,
        spec: VariableListSpec,
        scroll_offset: f64,
        viewport_height: f64,
    ) -> Result<VariableListWindow, VirtualListError> {
        validate_viewport(scroll_offset, viewport_height)?;
        VariableListSpec::new(spec.estimated_height, spec.overscan_pixels)?;
        if self.keys.is_empty() {
            return Ok(VariableListWindow::default());
        }
        let start_offset = (scroll_offset - spec.overscan_pixels).max(0.0);
        let end_offset = scroll_offset + viewport_height + spec.overscan_pixels;
        let start = self.prefix.lower_bound(start_offset).min(self.keys.len());
        let end = self
            .prefix
            .lower_bound(end_offset)
            .saturating_add(1)
            .min(self.keys.len());
        Ok(VariableListWindow {
            range: start..end,
            leading: self.prefix.sum(start),
            trailing: (self.prefix.total() - self.prefix.sum(end)).max(0.0),
            total: self.prefix.total(),
        })
    }

    /// Return the positive visible offset that keeps the bottom aligned.
    #[must_use]
    pub fn tail_offset(&self, viewport_height: f64) -> f64 {
        (self.prefix.total() - viewport_height.max(0.0)).max(0.0)
    }

    #[must_use]
    pub fn measured_count(&self) -> usize {
        self.measured.len()
    }
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct VariableListWindow {
    pub range: Range<usize>,
    pub leading: f64,
    pub trailing: f64,
    pub total: f64,
}

#[derive(Clone, Debug, Default)]
struct FenwickTree {
    tree: Vec<f64>,
}

impl FenwickTree {
    fn from_values(values: &[f64]) -> Self {
        let mut tree = Self {
            tree: vec![0.0; values.len() + 1],
        };
        for (index, value) in values.iter().copied().enumerate() {
            tree.add(index, value);
        }
        tree
    }

    fn add(&mut self, index: usize, delta: f64) {
        let mut cursor = index + 1;
        while cursor < self.tree.len() {
            self.tree[cursor] += delta;
            cursor += cursor & cursor.wrapping_neg();
        }
    }

    fn sum(&self, end: usize) -> f64 {
        let mut cursor = end.min(self.tree.len().saturating_sub(1));
        let mut total = 0.0;
        while cursor > 0 {
            total += self.tree[cursor];
            cursor &= cursor - 1;
        }
        total
    }

    fn total(&self) -> f64 {
        self.sum(self.tree.len().saturating_sub(1))
    }

    fn lower_bound(&self, target: f64) -> usize {
        let mut index = 0usize;
        let mut accumulated = 0.0;
        let mut step = self.tree.len().next_power_of_two() / 2;
        while step > 0 {
            let next = index + step;
            if next < self.tree.len() && accumulated + self.tree[next] <= target {
                index = next;
                accumulated += self.tree[next];
            }
            step /= 2;
        }
        index.min(self.tree.len().saturating_sub(1))
    }
}

#[derive(Clone, Debug, Error, PartialEq)]
pub enum VirtualListError {
    #[error("scroll offset must be finite and non-negative, got {0}")]
    InvalidScrollOffset(f64),
    #[error("viewport height must be finite and non-negative, got {0}")]
    InvalidViewport(f64),
    #[error("virtual list key `{0}` is duplicated")]
    DuplicateKey(String),
    #[error("virtual list key `{0}` is unknown")]
    UnknownKey(String),
    #[error("estimated item height must be finite and positive, got {0}")]
    InvalidEstimatedHeight(f64),
    #[error("virtual-list overscan pixels must be finite and non-negative, got {0}")]
    InvalidOverscan(f64),
    #[error("measured item height must be finite and positive, got {0}")]
    InvalidMeasurement(f64),
}

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

    #[test]
    fn variable_height_window_is_bounded_and_measurement_preserves_anchor() {
        let spec = VariableListSpec::new(20.0, 80.0).unwrap();
        let mut list = VariableListState::default();
        list.set_keys(
            (0..10_000).map(|index| format!("row-{index}")).collect(),
            spec,
        )
        .unwrap();
        let window = list.window(spec, 50_000.0, 600.0).unwrap();
        assert!(window.range.len() <= 40, "{window:?}");
        let correction = list.measure("row-0", 50.0, Some("row-2500")).unwrap();
        assert!((correction - 30.0).abs() < f64::EPSILON);
        let corrected = list.window(spec, 50_030.0, 600.0).unwrap();
        assert_eq!(corrected.range, window.range);
        assert_eq!(list.measured_count(), 1);
    }

    #[test]
    fn variable_height_measurements_survive_reorder_and_tail_alignment() {
        let spec = VariableListSpec::new(20.0, 0.0).unwrap();
        let mut list = VariableListState::default();
        list.set_keys(vec!["a".into(), "b".into(), "c".into()], spec)
            .unwrap();
        list.measure("b", 60.0, None).unwrap();
        list.set_keys(vec!["c".into(), "b".into(), "a".into()], spec)
            .unwrap();
        assert_eq!(list.measured_count(), 1);
        assert!((list.tail_offset(50.0) - 50.0).abs() < f64::EPSILON);
    }

    #[test]
    fn virtual_collection_metrics_track_frame_scroll_requests_and_rollback() {
        let id = VirtualCollectionId {
            component: ComponentInstancePath::root("Chat", "main"),
            key: "messages".to_owned(),
        };
        let item = |key: &str| {
            UiValue::Map(BTreeMap::from([(
                "key".to_owned(),
                UiValue::String(key.to_owned()),
            )]))
        };
        let spec = VirtualCollectionNodeSpec {
            id: id.clone(),
            label: "Messages".to_owned(),
            data: (0..5).map(|index| item(&format!("row-{index}"))).collect(),
            realized: BTreeMap::from([(1, UiNode::text("one")), (2, UiNode::text("two"))]),
            estimated_height: 24.0,
            height: Some(120.0),
            overdraw_pixels: 48.0,
            bottom_align: true,
            follow_tail: true,
            reveal_key: None,
            sticky_headers: Arc::new(BTreeSet::new()),
            inherited_motion_group: None,
        };
        let registry = VirtualRequestRegistry::new();
        registry.report_frame(&spec, 120.0, 1, 3.5, Some(1..3));
        registry.request(id.clone(), [3, 4]);
        registry.report_scroll(&id, 1..4, true);
        let metrics = &registry.inspect()[0];
        assert_eq!(metrics.realized_range, 1..3);
        assert_eq!(metrics.requested_range, 3..5);
        assert_eq!(metrics.visible_range, 1..4);
        assert!(metrics.bottom_align && metrics.follow_tail && metrics.is_scrolled);
        assert_eq!(metrics.sticky_header, None);

        let snapshot = registry.snapshot();
        let _ = registry.drain();
        assert_eq!(registry.inspect()[0].requested_count, 0);
        registry.restore(snapshot);
        assert_eq!(registry.inspect()[0].requested_count, 2);
        registry.retain(&BTreeSet::new());
        assert!(registry.inspect().is_empty());
    }
}