repose-ui 0.17.4

UI widgets and libs for Repose
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
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
use crate::ViewExt;
use crate::anim::animate_f32_from;
use crate::scroll::ScrollPhysics;
use repose_core::*;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;

pub trait ItemHeight<T> {
    fn get(&self, item: &T) -> f32;
}

impl<T> ItemHeight<T> for f32 {
    fn get(&self, _item: &T) -> f32 {
        *self
    }
}

impl<T, F: Fn(&T) -> f32> ItemHeight<T> for F {
    fn get(&self, item: &T) -> f32 {
        (self)(item)
    }
}

pub struct LazyColumnState {
    scroll_offset: Signal<f32>,   // px
    viewport_height: Signal<f32>, // px
    content_height: Signal<f32>,  // px, actual measured height from layout

    physics: RefCell<ScrollPhysics>,
}

impl Default for LazyColumnState {
    fn default() -> Self {
        Self::new()
    }
}

impl LazyColumnState {
    pub fn new() -> Self {
        Self {
            scroll_offset: signal(0.0),
            viewport_height: signal(600.0),
            content_height: signal(0.0),
            physics: RefCell::new(ScrollPhysics::new(0.90, 5.0, 10.0)),
        }
    }

    pub fn set_offset(&self, off: f32, content_height: f32) {
        let vh = self.viewport_height.get();
        let max_off = (content_height - vh).max(0.0);
        self.scroll_offset.set(off.clamp(0.0, max_off));
    }

    /// Consume delta in px. Returns leftover in px (for nested scroll).
    pub fn scroll_immediate(&self, delta_px: f32, content_height_px: f32) -> f32 {
        let before = self.scroll_offset.get();
        let viewport = self.viewport_height.get();
        let max_offset = (content_height_px - viewport).max(0.0);

        let new_offset = (before + delta_px).clamp(0.0, max_offset);
        self.scroll_offset.set(new_offset);

        let consumed = new_offset - before;

        self.physics.borrow_mut().record_input(consumed);

        delta_px - consumed
    }

    /// Advance inertia one tick; returns true if animating.
    pub fn tick(&self, content_height_px: f32) -> bool {
        let viewport = self.viewport_height.get();
        let max_offset = (content_height_px - viewport).max(0.0);

        let mut p = self.physics.borrow_mut();
        if let Some(new_off) = p.tick_integrate(self.scroll_offset.get(), 0.0, max_offset) {
            drop(p);
            self.scroll_offset.set(new_off);
            true
        } else {
            false
        }
    }
}

struct AnimState<T> {
    prev_keys: Vec<u64>,
    exiting: Vec<(u64, usize, T, u64)>,
    item_cache: HashMap<u64, T>,
}

/// Virtualized list - only renders visible items.
///
/// `item_height` may be a uniform `f32` (dp) or a per-item closure. For
/// heterogeneous heights, pass `|item| item.height_dp` to compute each
/// item's height from its data.
///
/// Optionally animates item enter/exit when items are added or removed.
/// Provide `get_key` for stable item identity and `animate_spec` to enable
/// fade-in for new items and fade-out for removed items.
///
/// When `animate_spec` is `None`, behavior is identical to the original
/// (no item animations).
#[allow(non_snake_case)]
pub fn LazyColumn<T, F, K, H>(
    items: Vec<T>,
    item_height: H,
    state: Rc<LazyColumnState>,
    modifier: Modifier,
    get_key: K,
    animate_spec: Option<AnimationSpec>,
    item_builder: F,
) -> View
where
    T: Clone + 'static,
    F: Fn(T, usize) -> View + 'static,
    K: Fn(&T) -> u64 + 'static,
    H: ItemHeight<T>,
{
    let heights_dp: Vec<f32> = items
        .iter()
        .map(|it| item_height.get(it).max(1.0))
        .collect();
    let cumulative_px: Vec<f32> = {
        let mut cum = Vec::with_capacity(heights_dp.len() + 1);
        cum.push(0.0);
        let mut acc = 0.0_f32;
        for h in &heights_dp {
            acc += dp_to_px(*h);
            cum.push(acc);
        }
        cum
    };
    let content_height_px = *cumulative_px.last().unwrap_or(&0.0);

    let scroll_offset_px = state.scroll_offset.get();
    let viewport_height_px = state.viewport_height.get();

    let actual_content_h_px = if state.content_height.get() > 0.0 {
        state.content_height.get()
    } else {
        content_height_px
    };
    state.tick(actual_content_h_px);

    let first_visible = match cumulative_px.binary_search_by(|p| {
        p.partial_cmp(&scroll_offset_px)
            .unwrap_or(std::cmp::Ordering::Equal)
    }) {
        Ok(i) => i,
        Err(i) => i.saturating_sub(1),
    };
    let viewport_end_px = scroll_offset_px + viewport_height_px;
    let last_visible = match cumulative_px.binary_search_by(|p| {
        p.partial_cmp(&viewport_end_px)
            .unwrap_or(std::cmp::Ordering::Equal)
    }) {
        Ok(i) => (i + 1).min(items.len()),
        Err(i) => i.min(items.len()),
    };

    let buffer = 2usize;
    let first_with_buffer = first_visible.saturating_sub(buffer);
    let last_with_buffer = (last_visible + buffer).min(items.len());

    let mut combined_children: Vec<View> = Vec::new();

    if first_with_buffer > 0 {
        let top_spacer_px = cumulative_px[first_with_buffer];
        if top_spacer_px > 0.0 {
            combined_children.push(crate::Box(
                Modifier::new().size(1.0, px_to_dp(top_spacer_px).max(0.0)),
            ));
        }
    }

    let total_slots: usize;
    if let Some(spec) = animate_spec {
        // Stable per-call-site ID for animation key namespacing.
        let inst = remember(|| std::cell::Cell::new(0u64));
        if inst.get() == 0 {
            static CTR: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
            inst.set(CTR.fetch_add(1, std::sync::atomic::Ordering::Relaxed));
        }
        let aid = inst.get();

        let state_slot: Rc<RefCell<AnimState<T>>> = remember(|| {
            RefCell::new(AnimState {
                prev_keys: Vec::new(),
                exiting: Vec::new(),
                item_cache: HashMap::new(),
            })
        });

        let mut s = state_slot.borrow_mut();

        let curr_keys: Vec<u64> = items.iter().map(&get_key).collect();

        // Find added and removed keys
        let added: Vec<u64> = curr_keys
            .iter()
            .filter(|k| !s.prev_keys.contains(k))
            .copied()
            .collect();
        let removed: Vec<(usize, u64)> = s
            .prev_keys
            .iter()
            .enumerate()
            .filter(|(_, k)| !curr_keys.contains(k))
            .map(|(i, k)| (i, *k))
            .collect();

        let had_prev = !s.prev_keys.is_empty();

        // Add removed items to exiting list
        if had_prev && !removed.is_empty() {
            for (old_idx, key) in &removed {
                if let Some(old_item) = s.item_cache.get(key) {
                    let v = s.exiting.len() as u64;
                    let cloned = old_item.clone();
                    s.exiting.push((*key, *old_idx, cloned, v));
                }
            }
        }

        // Update item cache with current items
        for item in &items {
            s.item_cache.insert(get_key(item), item.clone());
        }

        // Process exiting items - only keep those still fading
        let mut still_exiting: Vec<(u64, usize, T, u64)> = Vec::new();
        for (key, old_idx, old_item, version) in s.exiting.iter() {
            let exit_key = format!("_lz_x:{aid}:{key}:v{version}");
            let alpha = animate_f32_from(exit_key, 1.0, 0.0, spec);
            if alpha > 0.005 {
                still_exiting.push((*key, *old_idx, old_item.clone(), *version));
            }
        }

        // Extend visible range to cover exiting items' positions
        let max_exit_slot = still_exiting
            .iter()
            .map(|(_, i, _, _)| *i)
            .max()
            .unwrap_or(0);
        let vis_end = last_with_buffer.max(max_exit_slot + 1 + buffer);
        total_slots = items.len().max(max_exit_slot + 1);

        // Build combined children: interleave exiting items at their old indices
        // with normal items filling remaining slots in order
        let mut normal_ptr = 0usize;
        for visual_i in first_with_buffer..vis_end {
            let entry = still_exiting.iter().find(|(_, oi, _, _)| *oi == visual_i);
            if let Some((key, old_idx, old_item, version)) = entry {
                let ek = format!("_lz_x:{aid}:{key}:v{version}");
                let alpha = animate_f32_from(ek, 1.0, 0.0, spec);
                let exit_top_px = cumulative_px
                    .get(*old_idx)
                    .copied()
                    .unwrap_or(*old_idx as f32 * 1.0);
                let exit_h_dp = heights_dp.get(*old_idx).copied().unwrap_or(1.0);
                let exit_bottom_px = exit_top_px + dp_to_px(exit_h_dp);
                let in_view = exit_bottom_px > scroll_offset_px
                    && exit_top_px < scroll_offset_px + viewport_height_px;
                if in_view {
                    let exit_view = item_builder(old_item.clone(), *old_idx);
                    combined_children.push(
                        crate::Box(
                            Modifier::new()
                                .fill_max_width()
                                .height(exit_h_dp)
                                .alpha(alpha),
                        )
                        .child(exit_view),
                    );
                }
            } else if let Some(item) = items.get(normal_ptr) {
                let key = get_key(item);
                if had_prev && added.contains(&key) {
                    let enter_key = format!("_lz_n:{aid}:{key}");
                    let alpha = animate_f32_from(enter_key, 0.0, 1.0, spec);
                    combined_children.push(
                        crate::Box(Modifier::new().fill_max_width().alpha(alpha))
                            .child(item_builder(item.clone(), normal_ptr)),
                    );
                } else {
                    combined_children.push(item_builder(item.clone(), normal_ptr));
                }
                normal_ptr += 1;
            }
        }

        s.exiting = still_exiting;
        s.prev_keys = curr_keys;
    } else {
        // No animation: render items normally
        for i in first_with_buffer..last_with_buffer {
            if let Some(item) = items.get(i) {
                let h_dp = item_height.get(item).max(1.0);
                combined_children.push(
                    crate::Box(Modifier::new().fill_max_width().height(h_dp))
                        .child(item_builder(item.clone(), i)),
                );
            }
        }
        total_slots = items.len();
    }

    // Bottom spacer (dp; converted by layout)
    let has_top = first_with_buffer > 0;
    let rendered_items = combined_children.len() - if has_top { 1 } else { 0 };
    if first_with_buffer + rendered_items < total_slots {
        let end_px = cumulative_px
            .get(first_with_buffer + rendered_items)
            .copied()
            .unwrap_or(content_height_px);
        let remaining_px = (content_height_px - end_px).max(0.0);
        if remaining_px > 0.0 {
            combined_children.push(crate::Box(
                Modifier::new().size(1.0, px_to_dp(remaining_px).max(0.0)),
            ));
        }
    }

    let content = crate::View::new(0, ViewKind::Column).with_children(combined_children);

    // Scroll callbacks (px)
    let on_scroll = {
        let st = state.clone();
        Rc::new(move |d: repose_core::Vec2| -> repose_core::Vec2 {
            let ch = st.content_height.get();
            let ch = if ch > 0.0 { ch } else { content_height_px };
            let leftover_y_px = st.scroll_immediate(d.y, ch);
            repose_core::Vec2 {
                x: d.x,
                y: leftover_y_px,
            }
        })
    };

    let set_viewport = {
        let st = state.clone();
        Rc::new(move |h_px: f32| st.viewport_height.set(h_px.max(0.0)))
    };

    let get_scroll = {
        let st = state.clone();
        Rc::new(move || -> f32 { st.scroll_offset.get() })
    };

    let set_scroll = {
        let st = state.clone();
        Rc::new(move |off_px: f32| {
            let ch = st.content_height.get();
            let ch = if ch > 0.0 { ch } else { content_height_px };
            st.set_offset(off_px, ch);
        })
    };

    let measured_h_px = {
        let st = state.clone();
        Rc::new(move |h_px: f32| {
            st.content_height.set(h_px);
            st.set_offset(st.scroll_offset.get(), h_px);
        })
    };

    repose_core::View::new(
        0,
        repose_core::ViewKind::ScrollV {
            on_scroll: Some(on_scroll),
            set_viewport_height: Some(set_viewport),
            set_content_height: Some(Rc::new(move |h| measured_h_px(h))),
            get_scroll_offset: Some(get_scroll),
            set_scroll_offset: Some(set_scroll),
            show_scrollbar: true,
        },
    )
    .modifier(modifier)
    .with_children(vec![content])
}

/// List without virtualization (for small lists)
#[allow(non_snake_case)]
pub fn SimpleList<T: Clone + 'static>(
    items: Vec<T>,
    modifier: Modifier,
    item_builder: Rc<dyn Fn(T, usize) -> View>,
) -> View {
    let children: Vec<View> = items
        .into_iter()
        .enumerate()
        .map(|(i, item)| item_builder(item, i))
        .collect();

    crate::Column(modifier).with_children(children)
}

/// State for a virtualized scrolling grid.
pub struct LazyGridState {
    scroll_offset: Signal<f32>,
    viewport_height: Signal<f32>,
    content_height: Signal<f32>,
    physics: RefCell<ScrollPhysics>,
}

impl Default for LazyGridState {
    fn default() -> Self {
        Self::new()
    }
}

impl LazyGridState {
    pub fn new() -> Self {
        Self {
            scroll_offset: signal(0.0),
            viewport_height: signal(600.0),
            content_height: signal(0.0),
            physics: RefCell::new(ScrollPhysics::new(0.90, 5.0, 10.0)),
        }
    }

    pub fn set_offset(&self, off: f32, content_height: f32) {
        let vh = self.viewport_height.get();
        let max_off = (content_height - vh).max(0.0);
        self.scroll_offset.set(off.clamp(0.0, max_off));
    }

    pub fn scroll_immediate(&self, delta_px: f32, content_height_px: f32) -> f32 {
        let before = self.scroll_offset.get();
        let viewport = self.viewport_height.get();
        let max_offset = (content_height_px - viewport).max(0.0);
        let new_offset = (before + delta_px).clamp(0.0, max_offset);
        self.scroll_offset.set(new_offset);
        let consumed = new_offset - before;
        self.physics.borrow_mut().record_input(consumed);
        delta_px - consumed
    }

    pub fn tick(&self, content_height_px: f32) -> bool {
        let viewport = self.viewport_height.get();
        let max_offset = (content_height_px - viewport).max(0.0);
        let mut p = self.physics.borrow_mut();
        if let Some(new_off) = p.tick_integrate(self.scroll_offset.get(), 0.0, max_offset) {
            drop(p);
            self.scroll_offset.set(new_off);
            true
        } else {
            false
        }
    }
}

/// Virtualized scrolling grid with a fixed number of columns.
///
/// Items are arranged left-to-right, top-to-bottom. Only items visible in the
/// viewport (plus a buffer) are rendered. Each item has the same height (`item_height_dp`).
///
/// Supply row/column gaps via modifier methods on the `modifier` parameter:
/// `.row_gap(v)`, `.column_gap(v)`, or `.gap(v)` to set both.
///
/// # Example
/// ```ignore
/// let state = LazyGridState::new();
/// let state_rc = Rc::new(state);
/// LazyVerticalGrid(
///     3,                          // columns
///     items,                      // Vec<MyItem>
///     120.0,                      // item height in dp
///     state_rc.clone(),
///     Modifier::new().fill_max_size().gap(8.0),
///     |item, index| Card(Modifier::new().fill_max_width(), Text(format!("Item {index}"))),
/// )
/// ```
#[allow(non_snake_case)]
pub fn LazyVerticalGrid<T, F>(
    columns: usize,
    items: Vec<T>,
    item_height_dp: f32,
    state: Rc<LazyGridState>,
    modifier: Modifier,
    item_builder: F,
) -> View
where
    T: Clone + 'static,
    F: Fn(T, usize) -> View + 'static,
{
    let columns = columns.max(1);
    let item_h_px = dp_to_px(item_height_dp).max(1.0);
    let total_items = items.len();
    let total_rows = total_items.div_ceil(columns);
    let content_height_px = total_rows as f32 * item_h_px;

    let scroll_offset_px = state.scroll_offset.get();
    let viewport_height_px = state.viewport_height.get();

    let actual_content_h_px = if state.content_height.get() > 0.0 {
        state.content_height.get()
    } else {
        content_height_px
    };
    state.tick(actual_content_h_px);

    let buffer_rows = 2usize;
    let first_row = ((scroll_offset_px / item_h_px).floor().max(0.0)) as usize;
    let first_row = first_row.saturating_sub(buffer_rows);
    let last_row = (((scroll_offset_px + viewport_height_px) / item_h_px).ceil() as usize
        + buffer_rows)
        .min(total_rows);

    let first_item = first_row * columns;
    let last_item = (last_row * columns).min(total_items);

    let mut children: Vec<View> = Vec::new();

    // Top spacer
    if first_row > 0 {
        children.push(crate::Box(
            Modifier::new().size(1.0, first_row as f32 * item_height_dp),
        ));
    }

    // Visible items arranged in a Taffy CSS grid
    if first_item < last_item {
        let visible_items: Vec<View> = (first_item..last_item)
            .map(|i| item_builder(items[i].clone(), i))
            .collect();

        // Extract gap settings from the parent modifier so the inner grid uses them
        let rg = modifier.row_gap.or(modifier.gap).unwrap_or(0.0);
        let cg = modifier.column_gap.or(modifier.gap).unwrap_or(0.0);
        let grid_mod = Modifier::new().grid(columns, rg, cg).fill_max_width();
        children.push(crate::Column(grid_mod).with_children(visible_items));
    }

    // Bottom spacer
    if last_row < total_rows {
        children.push(crate::Box(
            Modifier::new().size(1.0, (total_rows - last_row) as f32 * item_height_dp),
        ));
    }

    let on_scroll = {
        let st = state.clone();
        Rc::new(move |d: Vec2| -> Vec2 {
            let ch = st.content_height.get();
            let ch = if ch > 0.0 { ch } else { content_height_px };
            Vec2 {
                x: d.x,
                y: st.scroll_immediate(d.y, ch),
            }
        })
    };

    let set_viewport = {
        let st = state.clone();
        Rc::new(move |h: f32| st.viewport_height.set(h.max(0.0)))
    };

    let get_scroll = {
        let st = state.clone();
        Rc::new(move || -> f32 { st.scroll_offset.get() })
    };

    let set_scroll = {
        let st = state.clone();
        Rc::new(move |off: f32| {
            let ch = st.content_height.get();
            st.set_offset(off, if ch > 0.0 { ch } else { content_height_px });
        })
    };

    let measured_h = {
        let st = state.clone();
        Rc::new(move |h: f32| {
            st.content_height.set(h);
            st.set_offset(st.scroll_offset.get(), h);
        })
    };

    let content = crate::Column(Modifier::new().fill_max_width()).with_children(children);

    View::new(
        0,
        ViewKind::ScrollV {
            on_scroll: Some(on_scroll),
            set_viewport_height: Some(set_viewport),
            set_content_height: Some(Rc::new(move |h| measured_h(h))),
            get_scroll_offset: Some(get_scroll),
            set_scroll_offset: Some(set_scroll),
            show_scrollbar: true,
        },
    )
    .modifier(modifier)
    .with_children(vec![content])
}

/// State for a horizontal lazy list (`LazyRow`).
///
/// Tracks scroll offset, viewport width, content width, and inertial physics.
pub struct LazyRowState {
    scroll_offset: Signal<f32>,
    viewport_width: Signal<f32>,
    content_width: Signal<f32>,
    physics: RefCell<ScrollPhysics>,
}

impl Default for LazyRowState {
    fn default() -> Self {
        Self::new()
    }
}

impl LazyRowState {
    pub fn new() -> Self {
        Self {
            scroll_offset: signal(0.0),
            viewport_width: signal(600.0),
            content_width: signal(0.0),
            physics: RefCell::new(ScrollPhysics::new(0.90, 5.0, 10.0)),
        }
    }

    pub fn set_offset(&self, off: f32, content_width: f32) {
        let vw = self.viewport_width.get();
        let max_off = (content_width - vw).max(0.0);
        self.scroll_offset.set(off.clamp(0.0, max_off));
    }

    pub fn scroll_immediate(&self, delta_px: f32, content_width_px: f32) -> f32 {
        let before = self.scroll_offset.get();
        let viewport = self.viewport_width.get();
        let max_offset = (content_width_px - viewport).max(0.0);
        let new_offset = (before + delta_px).clamp(0.0, max_offset);
        self.scroll_offset.set(new_offset);
        let consumed = new_offset - before;
        self.physics.borrow_mut().record_input(consumed);
        delta_px - consumed
    }

    pub fn tick(&self, content_width_px: f32) -> bool {
        let viewport = self.viewport_width.get();
        let max_offset = (content_width_px - viewport).max(0.0);
        let mut p = self.physics.borrow_mut();
        if let Some(new_off) = p.tick_integrate(self.scroll_offset.get(), 0.0, max_offset) {
            drop(p);
            self.scroll_offset.set(new_off);
            true
        } else {
            false
        }
    }
}

/// Virtualized horizontal list - only renders visible items.
///
/// Items are arranged left-to-right. Only items within the viewport
/// (plus a buffer) are rendered. Each item has the same width (`item_width_dp`).
///
/// # Example
/// ```ignore
/// let state = Rc::new(LazyRowState::new());
/// LazyRow(
///     items,                  // Vec<MyItem>
///     100.0,                  // item width in dp
///     state,
///     Modifier::new().fill_max_width().height(120.0),
///     |item, index| Text(format!("Item {index}")),
/// )
/// ```
#[allow(non_snake_case)]
pub fn LazyRow<T, F>(
    items: Vec<T>,
    item_width_dp: f32,
    state: Rc<LazyRowState>,
    modifier: Modifier,
    item_builder: F,
) -> View
where
    T: Clone + 'static,
    F: Fn(T, usize) -> View + 'static,
{
    let item_w_px = dp_to_px(item_width_dp).max(1.0);
    let content_width_px = items.len() as f32 * item_w_px;

    let scroll_offset_px = state.scroll_offset.get();
    let viewport_width_px = state.viewport_width.get();

    let actual_content_w_px = if state.content_width.get() > 0.0 {
        state.content_width.get()
    } else {
        content_width_px
    };
    state.tick(actual_content_w_px);

    let first_visible = (scroll_offset_px / item_w_px).floor().max(0.0) as usize;
    let last_visible = ((scroll_offset_px + viewport_width_px) / item_w_px).ceil() as usize + 2;

    let buffer = 2usize;
    let first_with_buffer = first_visible.saturating_sub(buffer);

    let mut children = Vec::new();

    if first_with_buffer > 0 {
        children.push(crate::Box(
            Modifier::new().size(first_with_buffer as f32 * item_width_dp, 1.0),
        ));
    }

    for i in first_with_buffer..last_visible {
        if let Some(item) = items.get(i) {
            children.push(item_builder(item.clone(), i));
        }
    }

    if last_visible < items.len() {
        let remaining = items.len() - last_visible;
        children.push(crate::Box(
            Modifier::new().size(remaining as f32 * item_width_dp, 1.0),
        ));
    }

    let on_scroll = {
        let st = state.clone();
        Rc::new(move |d: Vec2| -> Vec2 {
            let cw = st.content_width.get();
            let cw = if cw > 0.0 { cw } else { content_width_px };
            Vec2 {
                x: st.scroll_immediate(d.x, cw),
                y: d.y,
            }
        })
    };

    let set_viewport_w = {
        let st = state.clone();
        Rc::new(move |w: f32| st.viewport_width.set(w.max(0.0)))
    };

    let set_content_w = {
        let st = state.clone();
        Rc::new(move |w: f32| {
            st.content_width.set(w);
            st.set_offset(st.scroll_offset.get(), w);
        })
    };

    let get_scroll = {
        let st = state.clone();
        Rc::new(move || -> (f32, f32) { (st.scroll_offset.get(), 0.0) })
    };

    let set_scroll = {
        let st = state.clone();
        Rc::new(move |x: f32, _y: f32| {
            let cw = st.content_width.get();
            st.set_offset(x, if cw > 0.0 { cw } else { content_width_px });
        })
    };

    let content = crate::Row(Modifier::new().flex_shrink(0.0)).with_children(children);

    View::new(
        0,
        ViewKind::ScrollXY {
            on_scroll: Some(on_scroll),
            set_viewport_width: Some(set_viewport_w),
            set_viewport_height: None,
            set_content_width: Some(set_content_w),
            set_content_height: None,
            get_scroll_offset_xy: Some(get_scroll),
            set_scroll_offset_xy: Some(set_scroll),
            show_scrollbar: true,
        },
    )
    .modifier(modifier)
    .with_children(vec![content])
}

/// State for a virtualized staggered scrolling grid.
pub struct LazyVerticalStaggeredGridState {
    scroll_offset: Signal<f32>,
    viewport_height: Signal<f32>,
    content_height: Signal<f32>,
    physics: RefCell<ScrollPhysics>,
}

impl Default for LazyVerticalStaggeredGridState {
    fn default() -> Self {
        Self::new()
    }
}

impl LazyVerticalStaggeredGridState {
    pub fn new() -> Self {
        Self {
            scroll_offset: signal(0.0),
            viewport_height: signal(600.0),
            content_height: signal(0.0),
            physics: RefCell::new(ScrollPhysics::new(0.90, 5.0, 10.0)),
        }
    }

    pub fn set_offset(&self, off: f32, content_height: f32) {
        let vh = self.viewport_height.get();
        let max_off = (content_height - vh).max(0.0);
        self.scroll_offset.set(off.clamp(0.0, max_off));
    }

    pub fn scroll_immediate(&self, delta_px: f32, content_height_px: f32) -> f32 {
        let before = self.scroll_offset.get();
        let viewport = self.viewport_height.get();
        let max_offset = (content_height_px - viewport).max(0.0);
        let new_offset = (before + delta_px).clamp(0.0, max_offset);
        self.scroll_offset.set(new_offset);
        let consumed = new_offset - before;
        self.physics.borrow_mut().record_input(consumed);
        delta_px - consumed
    }

    pub fn tick(&self, content_height_px: f32) -> bool {
        let viewport = self.viewport_height.get();
        let max_offset = (content_height_px - viewport).max(0.0);
        let mut p = self.physics.borrow_mut();
        if let Some(new_off) = p.tick_integrate(self.scroll_offset.get(), 0.0, max_offset) {
            drop(p);
            self.scroll_offset.set(new_off);
            true
        } else {
            false
        }
    }
}

/// Pre-computed placement for an item in a staggered grid.
struct StaggeredPlacement {
    /// Which column this item occupies (0..columns).
    col: usize,
    /// Vertical offset from the top of the grid content (px).
    y_px: f32,
    /// Height of this item (px).
    h_px: f32,
}

/// Compute staggered grid placements for all items.
/// Uses the "shortest column" algorithm to balance items across columns.
fn compute_staggered_placements(
    heights_px: &[f32],
    columns: usize,
    gap_px: f32,
) -> Vec<StaggeredPlacement> {
    let mut placements = Vec::with_capacity(heights_px.len());
    let mut col_heights = vec![0.0_f32; columns];
    for (i, h) in heights_px.iter().enumerate() {
        // Find shortest column
        let col = col_heights
            .iter()
            .enumerate()
            .min_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .map(|(idx, _)| idx)
            .unwrap_or(i % columns);
        let y = col_heights[col];
        placements.push(StaggeredPlacement {
            col,
            y_px: y,
            h_px: *h,
        });
        col_heights[col] = y + h + gap_px;
    }
    placements
}

/// Virtualized staggered grid (Pinterest-style).
///
/// Items are arranged in a fixed number of columns. Each item can have a
/// different height. Items are placed in the column with the least accumulated
/// height, creating a staggered visual effect.
///
/// Only items visible in the viewport (plus a buffer) are rendered.
///
/// # Example
/// ```ignore
/// let state = Rc::new(LazyVerticalStaggeredGridState::new());
/// LazyVerticalStaggeredGrid(
///     2,                          // columns
///     items,                      // Vec<MyItem>
///     |item: &MyItem| item.height_dp,  // per-item height
///     state,
///     Modifier::new().fill_max_size().gap(8.0),
///     |item: &MyItem, index: usize| { /* ... */ },
/// )
/// ```
#[allow(non_snake_case)]
pub fn LazyVerticalStaggeredGrid<T, F, K>(
    columns: usize,
    items: Vec<T>,
    item_height_dp: K,
    state: Rc<LazyVerticalStaggeredGridState>,
    modifier: Modifier,
    item_builder: F,
) -> View
where
    T: Clone + 'static,
    F: Fn(T, usize) -> View + 'static,
    K: Fn(&T) -> f32 + 'static,
{
    let columns = columns.max(1);
    let gap_dp = modifier.row_gap.or(modifier.gap).unwrap_or(0.0);
    let gap_px = dp_to_px(gap_dp);

    // Compute heights in px
    let heights_px: Vec<f32> = items
        .iter()
        .map(|it| dp_to_px(item_height_dp(it).max(1.0)))
        .collect();
    let placements = compute_staggered_placements(&heights_px, columns, gap_px);

    // Compute total content height
    let total_content_height_px = placements
        .iter()
        .map(|p| p.y_px + p.h_px)
        .fold(0.0_f32, f32::max);

    let scroll_offset_px = state.scroll_offset.get();
    let viewport_height_px = state.viewport_height.get();

    state.tick(state.content_height.get().max(viewport_height_px));

    let buffer = 2;
    let mut first_visible = usize::MAX;
    let mut last_visible = 0usize;

    for (i, p) in placements.iter().enumerate() {
        let item_top = p.y_px;
        let item_bot = p.y_px + p.h_px;
        if item_bot > scroll_offset_px && item_top < scroll_offset_px + viewport_height_px {
            if i < first_visible {
                first_visible = i;
            }
            if i > last_visible {
                last_visible = i;
            }
        }
    }

    if first_visible == usize::MAX {
        first_visible = 0;
    }
    if last_visible == 0 && !items.is_empty() {
        last_visible = items.len().saturating_sub(1);
    }

    let first_idx = first_visible.saturating_sub(buffer);
    let last_idx = (last_visible + buffer).min(items.len());

    // Build per-column children with spacers for staggered positioning.
    // Every item in [first_idx, last_idx) gets a placeholder so column heights
    // match total_content_height_px - prevents scroll boundary from jumping.
    let mut col_children: Vec<Vec<View>> = (0..columns).map(|_| Vec::new()).collect();
    for col in 0..columns {
        let mut prev_y = 0.0_f32;
        for (i, p) in placements.iter().enumerate() {
            if p.col != col || i < first_idx || i >= last_idx {
                continue;
            }
            let spacer_y = p.y_px - prev_y;
            if spacer_y > 0.0 {
                col_children[col].push(crate::Box(
                    Modifier::new().size(1.0, px_to_dp(spacer_y).max(0.0)),
                ));
            }
            if let Some(item) = items.get(i) {
                let h_dp = item_height_dp(item).max(1.0);
                let vis_top = p.y_px;
                let vis_bot = vis_top + p.h_px;
                let in_view =
                    vis_bot > scroll_offset_px && vis_top < scroll_offset_px + viewport_height_px;
                if in_view {
                    col_children[col].push(
                        crate::Box(Modifier::new().fill_max_width().height(h_dp))
                            .child(item_builder(item.clone(), i)),
                    );
                } else {
                    // Placeholder to maintain column height
                    col_children[col].push(crate::Box(Modifier::new().size(1.0, h_dp)));
                }
            }
            prev_y = p.y_px + p.h_px;
        }
        let remaining = total_content_height_px - prev_y;
        if remaining > 0.0 {
            col_children[col].push(crate::Box(
                Modifier::new().size(1.0, px_to_dp(remaining).max(0.0)),
            ));
        }
    }

    let col_views: Vec<View> = col_children
        .into_iter()
        .map(|children| {
            crate::Column(Modifier::new().flex_grow(1.0).flex_basis(0.0)).with_children(children)
        })
        .collect();

    let on_scroll = {
        let st = state.clone();
        Rc::new(move |d: Vec2| -> Vec2 {
            let ch = st.content_height.get().max(st.viewport_height.get());
            Vec2 {
                x: d.x,
                y: st.scroll_immediate(d.y, ch),
            }
        })
    };

    let set_viewport = {
        let st = state.clone();
        Rc::new(move |h: f32| st.viewport_height.set(h.max(0.0)))
    };

    let get_scroll = {
        let st = state.clone();
        Rc::new(move || -> f32 { st.scroll_offset.get() })
    };

    let set_scroll = {
        let st = state.clone();
        Rc::new(move |off: f32| {
            let ch = st.content_height.get().max(st.viewport_height.get());
            st.set_offset(off, ch);
        })
    };

    let measured_h = {
        let st = state.clone();
        Rc::new(move |h: f32| {
            st.content_height.set(h);
            st.set_offset(st.scroll_offset.get(), h);
        })
    };

    let content = crate::Row(Modifier::new().fill_max_width().gap(gap_dp)).with_children(col_views);

    View::new(
        0,
        ViewKind::ScrollV {
            on_scroll: Some(on_scroll),
            set_viewport_height: Some(set_viewport),
            set_content_height: Some(Rc::new(move |h| measured_h(h))),
            get_scroll_offset: Some(get_scroll),
            set_scroll_offset: Some(set_scroll),
            show_scrollbar: true,
        },
    )
    .modifier(modifier)
    .with_children(vec![content])
}

fn builder(_item: i32, _idx: usize) -> View {
    crate::Box(Modifier::new().size(10.0, 10.0))
}

#[test]
fn test_item_height_uniform_f32() {
    let f: f32 = 50.0;
    let item = 7;
    assert_eq!(f.get(&item), 50.0);
}

#[test]
fn test_item_height_per_item_closure() {
    let items: Vec<i32> = vec![1, 2, 3, 4, 5];
    let h = |i: &i32| 30.0 + (*i as f32) * 10.0;
    let sum_dp: f32 = items.iter().map(|i| h.get(i)).sum();
    let expected_sum: f32 = items.iter().map(|i| 30.0 + (*i as f32) * 10.0).sum();
    assert!((sum_dp - expected_sum).abs() < 0.001);
}

#[test]
fn test_lazy_column_uniform_height_compiles() {
    let state = Rc::new(LazyColumnState::new());
    let v = LazyColumn(
        vec![1, 2, 3],
        48.0_f32,
        state,
        Modifier::new().size(200.0, 400.0),
        |it: &i32| *it as u64,
        None,
        builder,
    );
    let _ = v;
}

#[test]
fn test_lazy_column_heterogeneous_heights_compiles() {
    let state = Rc::new(LazyColumnState::new());
    let v = LazyColumn(
        vec![1, 2, 3, 4, 5],
        |it: &i32| 30.0 + (*it as f32) * 12.0,
        state,
        Modifier::new().size(200.0, 400.0),
        |it: &i32| *it as u64,
        None,
        builder,
    );
    let _ = v;
}