lurq 0.41.2

A Rust UI toolkit with typed components, retained state, reactive signals, GPU rendering, and DevTools.
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
use std::{
  collections::{HashMap, HashSet},
  fmt::Display,
  marker::PhantomData,
  sync::Arc,
};

use parking_lot::Mutex;

use super::{Column, ScrollBoth, ScrollVertical, Spacer};
use crate::{
  app::{component::Component, ctx::Ctx, events::ScrollEvent},
  core::{ElementRef, Signal},
  layout::{
    Alignment,
    layout_kind::{FrameConstraints, ScrollState},
    scrollbar::ScrollBarStyle,
  },
  node::{Element, EventHandler, IntoScrollEventHandler, dimension::Dimension},
};

const DEFAULT_OVERSCAN_PX: f32 = 256.0;
const HEIGHT_EPSILON: f32 = 0.01;

/// `LURQ_VLIST_DEBUG=1` paints diagnostics over every virtualized list: row
/// wrappers are tinted by the window-build generation (a stale reused subtree
/// keeps its old tint), and a stats panel shows the live windowing state.
fn debug_overlay_enabled() -> bool {
  static ENABLED: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
  *ENABLED.get_or_init(|| {
    std::env::var("LURQ_VLIST_DEBUG")
      .map(|value| value != "0" && !value.is_empty())
      .unwrap_or(false)
  })
}

/// Distinct translucent tints, rotated per window build.
fn debug_build_tint(build_seq: u64) -> crate::node::color::Color {
  const PALETTE: [(u8, u8, u8); 6] = [
    (46, 204, 113),
    (52, 152, 219),
    (155, 89, 182),
    (241, 196, 15),
    (230, 126, 34),
    (231, 76, 60),
  ];
  let (r, g, b) = PALETTE[(build_seq % PALETTE.len() as u64) as usize];
  crate::node::color::Color::new(r, g, b, 36)
}
/// Upper bound on not-yet-measured rows mounted per frame while a pending
/// scroll anchor needs exact heights.
const MEASURE_CHUNK: usize = 256;
/// Rows rendered on the very first pass, before any height is known; their
/// measured heights seed the estimate that sizes the rest of the list.
const BOOTSTRAP_ROWS: usize = 64;

#[derive(Clone, Default)]
struct VirtualizedListOptions {
  frame: FrameConstraints,
  flex: Option<f32>,
  overscan_px: f32,
  scroll_state: Option<ScrollState>,
  scrollbar: Option<ScrollBarStyle>,
  scrollbar_hovered: Option<Arc<dyn Fn(ScrollBarStyle) -> ScrollBarStyle + Send + Sync>>,
  scroll_handlers: Vec<EventHandler<ScrollEvent>>,
  top_handlers: Vec<EventHandler<ScrollEvent>>,
  bottom_handlers: Vec<EventHandler<ScrollEvent>>,
  list_key: Option<String>,
  /// Scroll horizontally too (rows wider than the viewport pan; virtualization
  /// still works off the vertical axis only).
  horizontal: bool,
  /// A row key to scroll into view (`scrollIntoView` with `nearest` block
  /// behaviour): revealed once when this value changes, and only when the row
  /// is off-screen — an already-visible row does not move. Used to restore a
  /// selection after navigation without persisting scroll offsets.
  reveal_key: Option<String>,
}

pub struct VirtualizedList<'a, T> {
  ctx: &'a mut Ctx,
  items: Arc<Vec<T>>,
  options: VirtualizedListOptions,
}

impl<'a, T> VirtualizedList<'a, T> {
  pub fn new(ctx: &'a mut Ctx, items: impl IntoIterator<Item = T>) -> Self {
    Self::new_shared(ctx, Arc::new(items.into_iter().collect()))
  }

  /// Build the list from an already-shared item vector. Callers that memoize
  /// their (potentially huge) item list across re-renders pass the same `Arc`
  /// back in, so a re-render costs neither a rebuild nor a copy of the items.
  pub fn new_shared(ctx: &'a mut Ctx, items: Arc<Vec<T>>) -> Self {
    Self {
      ctx,
      items,
      options: VirtualizedListOptions {
        overscan_px: DEFAULT_OVERSCAN_PX,
        ..VirtualizedListOptions::default()
      },
    }
  }

  pub fn list_key(mut self, key: impl Into<String>) -> Self {
    self.options.list_key = Some(key.into());
    self
  }

  pub fn overscan_px(mut self, overscan_px: f32) -> Self {
    self.options.overscan_px = overscan_px.max(0.0);
    self
  }

  /// Also scroll horizontally: rows wider than the viewport pan sideways.
  /// Virtualization still windows on the vertical axis only.
  pub fn horizontal_scroll(mut self, enabled: bool) -> Self {
    self.options.horizontal = enabled;
    self
  }

  /// Scroll the row with this key into view (`scrollIntoView`, `nearest`): the
  /// reveal fires once each time the key changes, and only if the row is
  /// off-screen, so an already-visible target does not jump. Pass the current
  /// selection's row key to restore it after navigation without persisting the
  /// scroll offset.
  pub fn reveal_key(mut self, key: Option<String>) -> Self {
    self.options.reveal_key = key;
    self
  }

  pub fn width(mut self, width: impl Into<Dimension>) -> Self {
    self.options.frame.width = Some(width.into());
    self
  }

  pub fn height(mut self, height: impl Into<Dimension>) -> Self {
    self.options.frame.height = Some(height.into());
    self
  }

  pub fn size(mut self, width: impl Into<Dimension>, height: impl Into<Dimension>) -> Self {
    self.options.frame.width = Some(width.into());
    self.options.frame.height = Some(height.into());
    self
  }

  pub fn min_width(mut self, width: impl Into<Dimension>) -> Self {
    self.options.frame.min_width = Some(width.into());
    self
  }

  pub fn max_width(mut self, width: impl Into<Dimension>) -> Self {
    self.options.frame.max_width = Some(width.into());
    self
  }

  pub fn min_height(mut self, height: impl Into<Dimension>) -> Self {
    self.options.frame.min_height = Some(height.into());
    self
  }

  /// Cap the list height. Without a fixed `height` (or `flex`), the list
  /// sizes to its content up to this cap and scrolls beyond it.
  pub fn max_height(mut self, height: impl Into<Dimension>) -> Self {
    self.options.frame.max_height = Some(height.into());
    self
  }

  pub fn flex(mut self, flex: f32) -> Self {
    self.options.flex = Some(flex);
    self
  }

  pub fn scrollbar(mut self, style: ScrollBarStyle) -> Self {
    self.options.scrollbar = Some(style);
    self
  }

  pub fn scrollbar_hovered(mut self, f: impl Fn(ScrollBarStyle) -> ScrollBarStyle + Send + Sync + 'static) -> Self {
    self.options.scrollbar_hovered = Some(Arc::new(f));
    self
  }

  pub fn with_scroll_state(mut self, scroll_state: ScrollState) -> Self {
    self.options.scroll_state = Some(scroll_state);
    self
  }

  pub fn on_scroll(mut self, f: impl IntoScrollEventHandler) -> Self {
    self.options.scroll_handlers.push(f.into_event_handler());
    self
  }

  pub fn on_top_reached(mut self, f: impl Fn() + Send + Sync + 'static) -> Self {
    self.options.top_handlers.push(EventHandler::new(move |_| f()));
    self
  }

  pub fn on_scroll_reach_top(mut self, f: impl IntoScrollEventHandler) -> Self {
    self.options.top_handlers.push(f.into_event_handler());
    self
  }

  pub fn on_bottom_reached(mut self, f: impl Fn() + Send + Sync + 'static) -> Self {
    self.options.bottom_handlers.push(EventHandler::new(move |_| f()));
    self
  }

  pub fn on_scroll_reach_bottom(mut self, f: impl IntoScrollEventHandler) -> Self {
    self.options.bottom_handlers.push(f.into_event_handler());
    self
  }

  pub fn mount_keyed<C, K, KF, PF>(self, key_fn: KF, props_fn: PF) -> Element
  where
    C: Component,
    T: Clone + PartialEq + Send + Sync + 'static,
    K: Display + Send + Sync + 'static,
    KF: Fn(&T) -> K + Send + Sync + 'static,
    PF: Fn(&T) -> C::Props + Send + Sync + 'static,
  {
    let props = VirtualizedMountListProps::<T, KF, PF> {
      items: self.items,
      key_fn: Arc::new(key_fn),
      props_fn: Arc::new(props_fn),
      options: self.options.clone(),
    };

    match self.options.list_key.as_deref() {
      Some(key) => self
        .ctx
        .mount_keyed::<VirtualizedMountList<T, C, K, KF, PF>>(key, props),
      None => self.ctx.mount::<VirtualizedMountList<T, C, K, KF, PF>>(props),
    }
  }
}

struct VirtualizedMountListProps<T, KF, PF> {
  /// Shared, not cloned: steady-state scroll re-renders must not copy or
  /// deep-compare the item list.
  items: Arc<Vec<T>>,
  key_fn: Arc<KF>,
  props_fn: Arc<PF>,
  options: VirtualizedListOptions,
}

impl<T, KF, PF> PartialEq for VirtualizedMountListProps<T, KF, PF> {
  fn eq(&self, _other: &Self) -> bool {
    false
  }
}

#[cfg(feature = "devtools")]
impl<T, KF, PF> crate::app::component::DevtoolsInspectable for VirtualizedMountListProps<T, KF, PF> {
  fn inspect(&self, formatter: &mut crate::app::component::DevtoolsFormatter<'_>) {
    formatter.value("VirtualizedList", format!("len={}", self.items.len()));
  }
}

struct VirtualizedMountList<T, C, K, KF, PF>
where
  C: Component,
{
  scroll_state: ScrollState,
  active_scroll_state: Arc<Mutex<ScrollState>>,
  revision: Signal<u64>,
  runtime: Arc<Mutex<VirtualizedRuntime<T>>>,
  _marker: PhantomData<(C, K, KF, PF)>,
}

struct VirtualizedRuntime<T> {
  heights: HashMap<String, f32>,
  /// The item list as of the last change, for cheap change detection — the
  /// steady-state scroll path must not rebuild keys or maps per frame.
  /// Compared by pointer first, contents only when the pointer changed.
  items_snapshot: Arc<Vec<T>>,
  order: Vec<String>,
  /// Cumulative row heights (`prefix[i]` = top of row `i`). Rows that were
  /// never measured count as the average measured height, so the extent and
  /// windowing work without ever mounting off-screen rows. Rebuilt only when
  /// items or measured heights change; the per-scroll visible-range query is
  /// a binary search.
  prefix: Vec<f32>,
  prefix_dirty: bool,
  rendered_refs: HashMap<String, ElementRef>,
  pending_anchor: Option<ScrollAnchor>,
  /// A row key requested via `reveal_key` that still needs to be scrolled into
  /// view (deferred until every row above it is measured for an exact offset).
  pending_reveal: Option<String>,
  /// The last `reveal_key` acted on, so a reveal fires only when it changes.
  last_reveal: Option<String>,
  /// Scroll position at which the row window was last built; re-renders only
  /// happen once the position drifts past `rebuild_threshold`.
  last_render_scroll_y: f32,
  /// Half the overscan margin (set from the options in `rows`).
  rebuild_threshold: f32,
  last_viewport_height: f32,
  /// Diagnostics for the viewport-coverage probe: the index window and its
  /// prefix top from the last `rows()` build.
  last_window: (usize, usize),
  last_window_prefix_start: f32,
  /// Ring of recent window builds: (scroll_y, start, end, prefix_start,
  /// items_changed) — lets the coverage probe identify which historical
  /// build the on-screen geometry actually corresponds to.
  recent_builds: std::collections::VecDeque<(f32, usize, usize, f32, bool)>,
  /// Monotonic window-build counter (drives the debug overlay row tint).
  build_seq: u64,
  /// Last coverage-probe failure, shown in the debug overlay panel.
  last_hole: Option<String>,
}

struct ScrollAnchor {
  key: String,
  offset: f32,
}

impl<T> Default for VirtualizedRuntime<T> {
  fn default() -> Self {
    Self {
      heights: HashMap::new(),
      items_snapshot: Arc::new(Vec::new()),
      order: Vec::new(),
      prefix: vec![0.0],
      prefix_dirty: false,
      rendered_refs: HashMap::new(),
      pending_anchor: None,
      pending_reveal: None,
      last_reveal: None,
      last_render_scroll_y: 0.0,
      rebuild_threshold: DEFAULT_OVERSCAN_PX * 0.5,
      last_viewport_height: 0.0,
      last_window: (0, 0),
      last_window_prefix_start: 0.0,
      recent_builds: std::collections::VecDeque::new(),
      build_seq: 0,
      last_hole: None,
    }
  }
}

impl<T, C, K, KF, PF> Component for VirtualizedMountList<T, C, K, KF, PF>
where
  C: Component,
  T: Clone + PartialEq + Send + Sync + 'static,
  K: Display + Send + Sync + 'static,
  KF: Fn(&T) -> K + Send + Sync + 'static,
  PF: Fn(&T) -> C::Props + Send + Sync + 'static,
{
  type Props = VirtualizedMountListProps<T, KF, PF>;

  fn create(ctx: &mut Ctx) -> Self {
    let scroll_state = ScrollState::new();
    Self {
      scroll_state: scroll_state.clone(),
      active_scroll_state: Arc::new(Mutex::new(scroll_state)),
      revision: ctx.signal(0),
      runtime: Arc::new(Mutex::new(VirtualizedRuntime::default())),
      _marker: PhantomData,
    }
  }

  fn render(&self, ctx: &mut Ctx) -> impl Into<Element> {
    let (items, key_fn, props_fn, mut options) = {
      let props = ctx.props::<Self::Props>();
      (
        props.items.clone(),
        props.key_fn.clone(),
        props.props_fn.clone(),
        props.options.clone(),
      )
    };
    let _ = self.revision.get();
    // With the debug overlay the scroll gets wrapped in a Stack; the outer
    // frame/flex move onto the Stack while the scroll fills it.
    let outer_frame = options.frame;
    let outer_flex = options.flex;
    if debug_overlay_enabled() {
      options.frame = FrameConstraints {
        width: Some(Dimension::full()),
        height: Some(Dimension::full()),
        ..FrameConstraints::default()
      };
      options.flex = None;
    }
    let scroll_state = options
      .scroll_state
      .clone()
      .unwrap_or_else(|| self.scroll_state.clone());
    *self.active_scroll_state.lock() = scroll_state.clone();
    let rows = self.rows(ctx, &items, &*key_fn, &*props_fn, &options, &scroll_state);

    // Re-render only when the scroll position drifts past half the overscan
    // margin from where the window was last built — everything closer is
    // already materialized, so plain retained scrolling covers it. Without
    // this hysteresis every wheel tick rebuilds and re-lays the whole visible
    // window, which crawls on long lists.
    let rebuild_handler = {
      let revision = self.revision.clone();
      let runtime = self.runtime.clone();
      let handler_scroll_state = scroll_state.clone();
      let rebuild_threshold = (options.overscan_px * 0.5).max(1.0);
      move |event: ScrollEvent| {
        // Handlers run before the default scroll movement applies, so predict
        // the post-scroll position from the delta.
        let scroll_y = (handler_scroll_state.scroll_y() - event.delta_y).max(0.0);
        let drifted = (runtime.lock().last_render_scroll_y - scroll_y).abs() > rebuild_threshold;
        if drifted {
          revision.update(|value| *value = value.wrapping_add(1));
        }
      }
    };

    // The vertical and both-axes scroll containers are distinct types with
    // identical (macro-provided) builders, so the shared option chain is
    // applied through a macro.
    macro_rules! configure_scroll {
      ($scroll:expr) => {{
        let mut scroll = $scroll.with_scroll_state(scroll_state.clone());
        if let Some(style) = options.scrollbar.clone() {
          scroll = scroll.scrollbar(style);
        }
        if let Some(hovered) = options.scrollbar_hovered.clone() {
          scroll = scroll.scrollbar_hovered(move |style| hovered(style));
        }
        for handler in options.top_handlers.iter().cloned() {
          scroll = scroll.on_scroll_reach_top(handler);
        }
        for handler in options.bottom_handlers.iter().cloned() {
          scroll = scroll.on_scroll_reach_bottom(handler);
        }
        for handler in options.scroll_handlers.iter().cloned() {
          scroll = scroll.on_scroll(handler);
        }
        scroll = scroll.on_scroll(rebuild_handler.clone());
        if let Some(width) = options.frame.width {
          scroll = scroll.width(width);
        }
        if let Some(height) = options.frame.height {
          scroll = scroll.height(height);
        }
        if let Some(min_width) = options.frame.min_width {
          scroll = scroll.min_width(min_width);
        }
        if let Some(max_width) = options.frame.max_width {
          scroll = scroll.max_width(max_width);
        }
        if let Some(min_height) = options.frame.min_height {
          scroll = scroll.min_height(min_height);
        }
        if let Some(max_height) = options.frame.max_height {
          scroll = scroll.max_height(max_height);
        }
        if let Some(flex) = options.flex {
          scroll = scroll.flex(flex);
        }
        scroll
      }};
    }

    let scroll: Element = if options.horizontal {
      configure_scroll!(ScrollBoth::new(rows)).into()
    } else {
      configure_scroll!(ScrollVertical::new(rows)).into()
    };
    if !debug_overlay_enabled() {
      return scroll;
    }
    self.wrap_with_debug_overlay(scroll, &scroll_state, &outer_frame, outer_flex)
  }

  fn after_layout(&self) {
    let mut changed = false;
    {
      let mut runtime = self.runtime.lock();
      let rendered_refs: Vec<(String, ElementRef)> = runtime
        .rendered_refs
        .iter()
        .map(|(key, element_ref)| (key.clone(), element_ref.clone()))
        .collect();

      for (key, element_ref) in rendered_refs {
        if !element_ref.is_attached() {
          continue;
        }
        let height = element_ref.height().max(0.0);
        let previous = runtime.heights.insert(key, height);
        if previous.is_none_or(|old| (old - height).abs() > HEIGHT_EPSILON) {
          runtime.prefix_dirty = true;
          changed = true;
        }
      }

      let scroll_state = self.active_scroll_state.lock().clone();
      let scroll_y = scroll_state.scroll_y();
      let viewport_height = scroll_state.viewport_height();

      // Paint-time invariant probe: the rows this list just laid out must
      // cover the scroll viewport (in absolute window coords). A hole here
      // is a windowing/layout failure; a hole the user sees WITHOUT this
      // warning is a renderer failure.
      if viewport_height > 0.0 && !runtime.rendered_refs.is_empty() {
        let viewport_top = scroll_state.layout_viewport_y();
        let viewport_bottom = viewport_top + viewport_height;
        let mut spans: Vec<(f32, f32)> = runtime
          .rendered_refs
          .values()
          .filter(|element_ref| element_ref.is_attached())
          .map(|element_ref| (element_ref.y(), element_ref.y() + element_ref.height()))
          .filter(|(top, bottom)| *bottom > viewport_top && *top < viewport_bottom)
          .collect();
        spans.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));
        let content_bottom = viewport_top + (scroll_state.content_height() - scroll_y).max(0.0);
        let mut covered_to = if scroll_y <= 1.0 { viewport_top } else { f32::MIN };
        let mut hole = None;
        if let Some(first) = spans.first() {
          if covered_to == f32::MIN {
            covered_to = first.0;
          }
          if first.0 > viewport_top + 1.0 && scroll_y > 1.0 {
            hole = Some((viewport_top, first.0));
          }
          for (top, bottom) in &spans {
            if *top > covered_to + 1.0 {
              hole.get_or_insert((covered_to, *top));
            }
            covered_to = covered_to.max(*bottom);
          }
          if covered_to < viewport_bottom.min(content_bottom) - 1.0 {
            hole.get_or_insert((covered_to, viewport_bottom));
          }
        } else {
          hole = Some((viewport_top, viewport_bottom));
        }
        if let Some((from, to)) = hole {
          let all: Vec<(f32, f32)> = runtime
            .rendered_refs
            .values()
            .filter(|element_ref| element_ref.is_attached())
            .map(|element_ref| (element_ref.y(), element_ref.height()))
            .collect();
          let min_top = all.iter().map(|(y, _)| *y).fold(f32::MAX, f32::min);
          let max_bottom = all.iter().map(|(y, h)| y + h).fold(f32::MIN, f32::max);
          let zero_height = all.iter().filter(|(_, h)| *h < 0.5).count();
          let (first_key, first_top) = runtime
            .rendered_refs
            .iter()
            .filter(|(_, element_ref)| element_ref.is_attached())
            .min_by(|a, b| a.1.y().partial_cmp(&b.1.y()).unwrap_or(std::cmp::Ordering::Equal))
            .map(|(key, element_ref)| (key.clone(), element_ref.y()))
            .unwrap_or_default();
          // Where the window's first row SHOULD be, per the prefix the window
          // was built from — the measured/expected delta separates a stale
          // scroll translation from a stale spacer or poisoned heights.
          let expected_first_top = viewport_top + runtime.last_window_prefix_start - scroll_y;
          let mut sorted_heights: Vec<f32> = runtime.heights.values().copied().collect();
          sorted_heights.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
          let (height_min, height_median, height_max) = if sorted_heights.is_empty() {
            (0.0, 0.0, 0.0)
          } else {
            (
              sorted_heights[0],
              sorted_heights[sorted_heights.len() / 2],
              sorted_heights[sorted_heights.len() - 1],
            )
          };
          let small_heights = sorted_heights.iter().filter(|height| **height < 10.0).count();
          tracing::warn!(
            target: "lurq::vlist",
            "laid-out rows leave viewport hole y=[{from:.1}..{to:.1}] (viewport=[{viewport_top:.1}..{viewport_bottom:.1}] scroll_y={scroll_y:.1} window_built_at={:.1} rows={} rows_span=[{min_top:.1}..{max_bottom:.1}] zero_h={zero_height} first_row=({first_key}@{first_top:.1}) expected_first_top={expected_first_top:.1} window={:?} prefix_start={:.1} content_h={:.1} prefix_len={} heights={} h_min={height_min:.1} h_med={height_median:.1} h_max={height_max:.1} h_small={small_heights})",
            runtime.last_render_scroll_y,
            runtime.rendered_refs.len(),
            runtime.last_window,
            runtime.last_window_prefix_start,
            scroll_state.content_height(),
            runtime.prefix.len(),
            runtime.heights.len(),
          );
          tracing::warn!(
            target: "lurq::vlist",
            "recent builds (scroll, start, end, prefix_start, items_changed): {:?}",
            runtime.recent_builds
          );
          runtime.last_hole = Some(format!(
            "y=[{from:.0}..{to:.0}] rows@[{min_top:.0}..{max_bottom:.0}] exp {expected_first_top:.0}"
          ));
        } else {
          runtime.last_hole = None;
        }
      }
      // Same hysteresis as the scroll handler: rebuild only once the position
      // leaves the already-materialized overscan margin (or the viewport
      // itself resized).
      let drifted = (runtime.last_render_scroll_y - scroll_y).abs() > runtime.rebuild_threshold;
      if drifted && (runtime.last_render_scroll_y - scroll_y).abs() > runtime.rebuild_threshold * 2.0 {
        // The scroll handler should have re-windowed well before this point —
        // reaching here means scroll moved without the handlers firing.
        tracing::warn!(
          target: "lurq::vlist",
          "virtualized window healed after missed scroll events: scroll_y={:.1} window_built_at={:.1}",
          scroll_y,
          runtime.last_render_scroll_y
        );
      }
      if drifted || (runtime.last_viewport_height - viewport_height).abs() > HEIGHT_EPSILON {
        runtime.last_viewport_height = viewport_height;
        changed = true;
      }

      // Restore the anchored row's viewport position once every height is
      // exact (the bounded measure march in `rows` fills the gaps). An
      // anchor whose row VANISHED with the new items — a different dataset
      // swapped in under the same list, e.g. switching record categories —
      // is dropped immediately instead: chasing it forced the march across
      // every row of the new list (seconds of first-render jank on
      // multi-thousand-row tables) for a position that no longer means
      // anything there.
      if let Some(anchor) = runtime.pending_anchor.take() {
        if runtime.heights.len() >= runtime.order.len() {
          if let Some(anchor_index) = runtime.order.iter().position(|key| key == &anchor.key) {
            let new_top = prefix_height(&runtime.order[..anchor_index], &runtime.heights);
            scroll_state.set_scroll(0.0, new_top + anchor.offset);
            changed = true;
          }
        } else {
          runtime.pending_anchor = Some(anchor);
        }
      }

      // Reveal the requested row: when it is not already fully visible, scroll
      // it to the top of the viewport (`scrollIntoView`, `block: start`); an
      // already-visible row does not move, so clicking a visible row never
      // jumps. The target's offset comes from the same estimate prefix; the
      // reveal stays pending until the row itself has a real measurement, so
      // the position converges as the revealed window's heights land.
      if let Some(key) = runtime.pending_reveal.clone() {
        match runtime.order.iter().position(|k| k == &key) {
          Some(index) => {
            let row_top = runtime.prefix.get(index).copied().unwrap_or(0.0);
            let row_bottom = runtime.prefix.get(index + 1).copied().unwrap_or(row_top);
            let view_top = scroll_state.scroll_y();
            let view_bottom = view_top + scroll_state.viewport_height();
            let fully_visible = row_top >= view_top && row_bottom <= view_bottom;
            if fully_visible {
              if runtime.heights.contains_key(&key) {
                runtime.pending_reveal = None;
              }
            } else {
              scroll_state.set_scroll(0.0, row_top.max(0.0));
              changed = true;
            }
          }
          None => runtime.pending_reveal = None,
        }
      }
    }

    if changed {
      self.revision.update(|value| *value = value.wrapping_add(1));
    }
  }
}

impl<T, C, K, KF, PF> VirtualizedMountList<T, C, K, KF, PF>
where
  C: Component,
  T: Clone + PartialEq + Send + Sync + 'static,
  K: Display + Send + Sync + 'static,
  KF: Fn(&T) -> K + Send + Sync + 'static,
  PF: Fn(&T) -> C::Props + Send + Sync + 'static,
{
  /// Debug overlay (`LURQ_VLIST_DEBUG=1`): wraps the scroll in a Stack with a
  /// live stats panel pinned top-right. The outer frame/flex land on the
  /// Stack; the scroll fills it.
  fn wrap_with_debug_overlay(
    &self,
    scroll: Element,
    scroll_state: &ScrollState,
    outer_frame: &FrameConstraints,
    outer_flex: Option<f32>,
  ) -> Element {
    use super::{Stack, Text};
    use crate::{
      layout::text_style::TextStyle,
      node::{color::Color, padding::Padding},
    };

    let runtime = self.runtime.lock();
    let max_scroll = (scroll_state.content_height() - scroll_state.viewport_height()).max(0.0);
    let percent = if max_scroll > 0.0 {
      (scroll_state.scroll_y() / max_scroll * 100.0).clamp(0.0, 100.0)
    } else {
      0.0
    };
    let lines = vec![
      format!("build #{}", runtime.build_seq),
      format!(
        "scroll {:.0}/{max_scroll:.0} ({percent:.0}%)  row {}/{}",
        scroll_state.scroll_y(),
        runtime.last_window.0,
        runtime.order.len()
      ),
      format!(
        "scroll {:.1}  built@ {:.1}",
        scroll_state.scroll_y(),
        runtime.last_render_scroll_y
      ),
      format!(
        "window {}..{}  rows {}",
        runtime.last_window.0,
        runtime.last_window.1,
        runtime.rendered_refs.len()
      ),
      format!("prefix_start {:.1}", runtime.last_window_prefix_start),
      format!(
        "content {:.1}  viewport {:.1}",
        scroll_state.content_height(),
        scroll_state.viewport_height()
      ),
      format!("heights {}/{}", runtime.heights.len(), runtime.order.len()),
    ];
    let hole = runtime.last_hole.clone();
    drop(runtime);

    let line_style = |color: Color| TextStyle {
      font_size: 11.0,
      line_height: 1.25,
      color,
      ..TextStyle::default()
    };
    let mut panel = Column::new()
      .spacing(1.0)
      .padding(Padding::new().top(6.0).bottom(6.0).left(9.0).right(9.0))
      .background(Color::new(8, 10, 14, 216));
    for line in &lines {
      panel = panel.child(Text::styled(line, line_style(Color::new(150, 235, 170, 255))));
    }
    if let Some(hole) = hole {
      panel = panel.child(Text::styled(
        &format!("HOLE {hole}"),
        line_style(Color::new(255, 110, 110, 255)),
      ));
    }
    let overlay = Column::new()
      .width(Dimension::full())
      .align_items(Alignment::End)
      .padding(Padding::new().top(44.0).right(18.0))
      .child(panel);

    let mut stack = Stack::new().child(scroll).child(overlay);
    if let Some(width) = outer_frame.width {
      stack = stack.width(width);
    }
    if let Some(height) = outer_frame.height {
      stack = stack.height(height);
    }
    if let Some(min_width) = outer_frame.min_width {
      stack = stack.min_width(min_width);
    }
    if let Some(max_width) = outer_frame.max_width {
      stack = stack.max_width(max_width);
    }
    if let Some(min_height) = outer_frame.min_height {
      stack = stack.min_height(min_height);
    }
    if let Some(max_height) = outer_frame.max_height {
      stack = stack.max_height(max_height);
    }
    if let Some(flex) = outer_flex {
      stack = stack.flex(flex);
    }
    stack.into()
  }

  fn rows(
    &self,
    ctx: &mut Ctx,
    items: &Arc<Vec<T>>,
    key_fn: &KF,
    props_fn: &PF,
    options: &VirtualizedListOptions,
    scroll_state: &ScrollState,
  ) -> Column {
    let mut runtime = self.runtime.lock();

    // Steady-state scroll ticks must be free of O(n) work: the shared item
    // list is compared by pointer, and contents only when the pointer changed
    // (a parent re-render building an identical list).
    let items_changed =
      !Arc::ptr_eq(&runtime.items_snapshot, items) && runtime.items_snapshot.as_slice() != items.as_slice();
    if !items_changed && !Arc::ptr_eq(&runtime.items_snapshot, items) {
      // Same contents, new allocation — adopt it so the next compare is O(1).
      runtime.items_snapshot = items.clone();
    }
    if items_changed {
      let keys: Vec<String> = items.iter().map(|item| format!("{}", key_fn(item))).collect();
      tracing::warn!(
        target: "lurq::vlist",
        "items changed in place: old_len={} new_len={} keys_equal={} scroll_y={:.1}",
        runtime.order.len(),
        keys.len(),
        runtime.order == keys,
        scroll_state.scroll_y(),
      );
      let current_keys: HashSet<&str> = keys.iter().map(String::as_str).collect();
      let survivors = runtime
        .order
        .iter()
        .filter(|key| current_keys.contains(key.as_str()))
        .count();
      if !runtime.order.is_empty() && survivors == 0 {
        // No key survived: a DIFFERENT dataset swapped in under the same
        // list (e.g. switching record categories). There is no position to
        // restore and every retained height is meaningless — chasing an
        // anchor here forced a measure march across the whole new list
        // (seconds of first-render jank on multi-thousand-row tables).
        // Reset to a fresh bootstrap at the top instead.
        runtime.heights.clear();
        runtime.pending_anchor = None;
        scroll_state.set_scroll(0.0, 0.0);
      } else {
        if runtime.order != keys && runtime.pending_anchor.is_none() && !runtime.order.is_empty() {
          // Anchor the first (partially) visible row using the pre-change
          // prefix, so the viewport is restored once the new heights settle.
          let scroll_y = scroll_state.scroll_y();
          let index = runtime.prefix[1..runtime.order.len() + 1]
            .partition_point(|&bottom| bottom <= scroll_y)
            .min(runtime.order.len() - 1);
          runtime.pending_anchor = Some(ScrollAnchor {
            key: runtime.order[index].clone(),
            offset: scroll_y - runtime.prefix[index],
          });
        }

        // Evict heights only for keys that disappeared. Keys that survive an
        // item change keep their old measurement as a stale-but-usable
        // estimate: rendered rows re-measure on the very next layout, and
        // dropping every height at once would collapse the prefix to the
        // bootstrap window — teleporting the scroll (clamped against the
        // collapsed content) and blanking the viewport when the same-shaped
        // list is swapped in place (e.g. a preview re-decoded into a new
        // allocation while scrolled deep).
        runtime.heights.retain(|key, _| current_keys.contains(key.as_str()));
      }
      drop(current_keys);
      runtime.order = keys;
      runtime.items_snapshot = items.clone();
      runtime.prefix_dirty = true;
    }

    // A new `reveal_key` (e.g. a selection restored by navigation) queues a
    // scroll-into-view, applied once heights above the row are known. Fires
    // only on change, so it never fights the user's scrolling.
    if options.reveal_key != runtime.last_reveal {
      runtime.last_reveal = options.reveal_key.clone();
      runtime.pending_reveal = options.reveal_key.clone();
    }

    // Cumulative heights (`prefix[i]` = top of row `i`), rebuilt only when
    // items or measured heights changed. Rows that were never measured count
    // as the average measured height — off-screen rows are never mounted just
    // to size them; the extent refines as real heights come in. Row offsets
    // and the visible range then cost O(1)/O(log n) per scroll tick.
    if runtime.prefix_dirty {
      let mut measured_sum = 0.0f64;
      let mut measured_count = 0usize;
      let row_heights: Vec<Option<f32>> = runtime
        .order
        .iter()
        .map(|key| {
          let height = runtime.heights.get(key).copied();
          if let Some(height) = height {
            measured_sum += f64::from(height);
            measured_count += 1;
          }
          height
        })
        .collect();
      let estimate = if measured_count > 0 {
        (measured_sum / measured_count as f64) as f32
      } else {
        0.0
      };
      let mut prefix = Vec::with_capacity(row_heights.len() + 1);
      let mut sum = 0.0f32;
      prefix.push(0.0);
      for height in row_heights {
        sum += height.unwrap_or(estimate);
        prefix.push(sum);
      }
      runtime.prefix = prefix;
      runtime.prefix_dirty = false;
    }

    let count = runtime.order.len();
    let has_measurements = !runtime.heights.is_empty();

    let visible_range = if has_measurements {
      visible_range_from_prefix(
        &runtime.prefix,
        scroll_state.scroll_y(),
        scroll_state.viewport_height(),
        options.overscan_px,
      )
    } else {
      // Nothing measured yet: render a small seed batch; its measured heights
      // become the estimate that sizes everything else.
      (0, count.min(BOOTSTRAP_ROWS))
    };
    let mut rendered_indices: Vec<usize> = (visible_range.0..visible_range.1).collect();
    // A pending anchor (same rows re-shaped in place) restores an exact
    // position, so the not-yet-measured remainder is measured in bounded
    // chunks per frame. Reveals never march: they apply from the estimate
    // prefix in `after_layout` and converge as the revealed window measures.
    if runtime.pending_anchor.is_some() && runtime.heights.len() < count {
      rendered_indices.extend(
        runtime
          .order
          .iter()
          .enumerate()
          .filter_map(|(index, key)| (!runtime.heights.contains_key(key)).then_some(index))
          .take(MEASURE_CHUNK),
      );
      rendered_indices.sort_unstable();
      rendered_indices.dedup();
    }
    let total_height = has_measurements.then(|| runtime.prefix[count]);
    // Only the handful of rendered rows need owned keys/offsets.
    let rendered: Vec<(usize, String, f32, f32)> = rendered_indices
      .into_iter()
      .map(|index| {
        (
          index,
          runtime.order[index].clone(),
          runtime.prefix[index],
          runtime.prefix[index + 1] - runtime.prefix[index],
        )
      })
      .collect();
    runtime.last_render_scroll_y = scroll_state.scroll_y();
    runtime.rebuild_threshold = (options.overscan_px * 0.5).max(1.0);
    runtime.last_window = visible_range;
    let window_prefix_start = runtime.prefix.get(visible_range.0).copied().unwrap_or(0.0);
    runtime.last_window_prefix_start = window_prefix_start;
    runtime.recent_builds.push_back((
      scroll_state.scroll_y(),
      visible_range.0,
      visible_range.1,
      window_prefix_start,
      items_changed,
    ));
    if runtime.recent_builds.len() > 8 {
      runtime.recent_builds.pop_front();
    }
    runtime.build_seq = runtime.build_seq.wrapping_add(1);
    let build_seq = runtime.build_seq;
    runtime.rendered_refs.clear();
    drop(runtime);

    let debug_tint = debug_overlay_enabled().then(|| debug_build_tint(build_seq));
    let mut column = Column::new().spacing(0.0).align_items(Alignment::Start);
    let mut cursor_y = 0.0;

    for (index, key, row_y, row_height) in rendered {
      if row_y > cursor_y {
        column = column.child(Spacer::new().height(row_y - cursor_y));
      }
      let row_ref = ctx.element_ref();
      self.runtime.lock().rendered_refs.insert(key.clone(), row_ref.clone());
      let row = ctx.mount_keyed::<C>(&key, props_fn(&items[index]));
      // Key the wrapper too: it is the row's direct sibling in the column, so
      // keying it lets node-id reconciliation re-align wrappers by key when the
      // window shifts — keeping hover/focus glued to the row the pointer is
      // actually over instead of the position it used to occupy.
      let mut wrapper = Column::new()
        .spacing(0.0)
        .key(key.as_str())
        .ref_element(row_ref)
        .child(row);
      if let Some(tint) = debug_tint {
        wrapper = wrapper.background(tint);
      }
      column = column.child(wrapper);
      cursor_y = row_y + row_height;
    }

    if let Some(total_height) = total_height
      && total_height > cursor_y
    {
      column = column.child(Spacer::new().height(total_height - cursor_y));
    }

    column
  }
}

fn prefix_height(keys: &[String], heights: &HashMap<String, f32>) -> f32 {
  keys.iter().map(|key| heights.get(key).copied().unwrap_or(0.0)).sum()
}

/// Visible index window from the cumulative-height prefix (see
/// `VirtualizedRuntime::prefix`): two binary searches instead of an O(n) walk.
fn visible_range_from_prefix(prefix: &[f32], scroll_y: f32, viewport_height: f32, overscan_px: f32) -> (usize, usize) {
  let count = prefix.len().saturating_sub(1);
  if count == 0 {
    return (0, 0);
  }
  let start_y = (scroll_y - overscan_px).max(0.0);
  let end_y = scroll_y + viewport_height + overscan_px;

  // First row whose bottom edge is past `start_y`.
  let start = prefix[1..].partition_point(|&bottom| bottom <= start_y).min(count);
  // One past the last row whose top edge is before `end_y`.
  let end = prefix[..count].partition_point(|&top| top < end_y).max(start);
  (start, end)
}