yew_virtual/core/virtual_item.rs
1use crate::core::virtual_key::VirtualKey;
2
3/// Metadata for a single virtualized item.
4///
5/// Represents the computed layout information that the virtualizer
6/// produces for each item within the visible range. Components use
7/// this metadata to position and render items in the scroll container.
8#[derive(Debug, Clone, PartialEq)]
9pub struct VirtualItem {
10 /// The index of this item in the full dataset.
11 pub index: usize,
12
13 /// The measured or estimated size of this item in pixels.
14 ///
15 /// Represents height for vertical lists or width for horizontal lists.
16 pub size: f64,
17
18 /// The offset in pixels from the start of the virtualized region.
19 ///
20 /// Represents the top offset for vertical lists or the left offset
21 /// for horizontal lists.
22 pub start: f64,
23
24 /// The end position in pixels (start + size).
25 pub end: f64,
26
27 /// A stable identity key for this item.
28 ///
29 /// When a custom `get_item_key` is provided, this key follows the
30 /// item across reorders. Otherwise it equals the index.
31 pub key: VirtualKey,
32
33 /// The lane index for grid layouts (0 for single-column lists).
34 pub lane: usize,
35}
36
37impl VirtualItem {
38 /// Creates a new virtual item with the given layout parameters.
39 ///
40 /// # Parameters
41 ///
42 /// - `index`: The item's position in the full dataset.
43 /// - `size`: The item's size in pixels along the scroll axis.
44 /// - `start`: The item's offset from the start of the virtual region.
45 pub fn new(index: usize, size: f64, start: f64) -> Self {
46 // Compute the end position from start and size.
47 let end = start + size;
48
49 // Construct the virtual item with the computed layout.
50 Self {
51 index,
52 size,
53 start,
54 end,
55 key: VirtualKey::Index(index),
56 lane: 0,
57 }
58 }
59
60 /// Creates a new virtual item with a specific key and lane.
61 ///
62 /// # Parameters
63 ///
64 /// - `index`: The item's position in the full dataset.
65 /// - `size`: The item's size in pixels along the scroll axis.
66 /// - `start`: The item's offset from the start of the virtual region.
67 /// - `key`: The stable identity key for this item.
68 /// - `lane`: The lane (column/row) index for grid layouts.
69 pub fn with_key_and_lane(
70 index: usize,
71 size: f64,
72 start: f64,
73 key: VirtualKey,
74 lane: usize,
75 ) -> Self {
76 // Compute the end position from start and size.
77 let end = start + size;
78
79 // Construct the virtual item with key and lane information.
80 Self {
81 index,
82 size,
83 start,
84 end,
85 key,
86 lane,
87 }
88 }
89}