1use crate::{LayoutNode, Rect, ScrollLayout};
2use std::{ops::Range, sync::Arc};
3use xrack_runtime::{ListMetrics, ListSection, Node, RowHeights, RowKey};
4
5#[derive(Clone, Debug, PartialEq)]
7pub struct ListSectionHeader {
8 pub first_row: usize,
9 pub row_count: usize,
10 pub key: RowKey,
11 pub label: Arc<str>,
12 pub bounds: Rect,
13 pub pinned: bool,
14 pub(crate) content: Option<Box<(Node, LayoutNode)>>,
15}
16
17impl ListSectionHeader {
18 pub fn content(&self) -> Option<(&Node, &LayoutNode)> {
19 self.content.as_deref().map(|(node, item)| (node, item))
20 }
21}
22
23pub(crate) fn list_body_clip(clip: Rect, headers: &[ListSectionHeader]) -> Rect {
24 let Some(header) = headers.first().filter(|header| header.pinned) else {
25 return clip;
26 };
27 let y = (header.bounds.y + header.bounds.height).clamp(clip.y, clip.y + clip.height);
28 Rect::new(clip.x, y, clip.width, clip.y + clip.height - y)
29}
30
31#[derive(Clone, Debug)]
33pub(crate) struct ViewCells {
34 pub(crate) columns: usize,
35 height: f32,
36 width: f32,
37 spacing: f32,
38 row_heights: Option<Arc<RowHeights>>,
39 sections: Option<Arc<[ListSection]>>,
40 section_height: f32,
41 sticky_sections: bool,
42}
43
44impl ViewCells {
45 pub(crate) fn new(node: &Node, viewport_width: f32) -> Self {
46 if node.kind != "GridView" {
47 return Self {
48 columns: 1,
49 height: ListMetrics::row_height(node),
50 width: viewport_width,
51 spacing: 0.0,
52 row_heights: node.row_heights.clone(),
53 sections: node
54 .list_sections
55 .as_ref()
56 .filter(|sections| !sections.is_empty())
57 .cloned(),
58 section_height: node
59 .length("section-height")
60 .filter(|height| height.is_finite())
61 .unwrap_or(28.0)
62 .clamp(16.0, 128.0),
63 sticky_sections: node.boolean("sticky-section-headers", false),
64 };
65 }
66 let width = node
67 .length("cell-width")
68 .unwrap_or(144.0)
69 .clamp(16.0, 1024.0);
70 let height = node
71 .length("cell-height")
72 .unwrap_or(96.0)
73 .clamp(12.0, 512.0);
74 let spacing = node.length("spacing").unwrap_or(0.0).clamp(0.0, 128.0);
75 let columns = ((viewport_width + spacing) / (width + spacing)).floor() as usize;
76 Self {
77 columns: columns.max(1),
78 height,
79 width: width.min(viewport_width),
80 spacing,
81 row_heights: None,
82 sections: None,
83 section_height: 0.0,
84 sticky_sections: false,
85 }
86 }
87
88 fn pitch(&self) -> f64 {
89 f64::from(self.height) + f64::from(self.spacing)
90 }
91
92 pub(crate) fn top(&self, index: usize) -> f64 {
93 self.base_top(index)
94 + self.section_at(index).map_or(0.0, |section| {
95 section.heading_count as f64 * f64::from(self.section_height)
96 })
97 }
98
99 fn base_top(&self, index: usize) -> f64 {
100 self.row_heights.as_ref().map_or_else(
101 || (index / self.columns) as f64 * self.pitch(),
102 |heights| heights.top(index),
103 )
104 }
105
106 fn section_at(&self, index: usize) -> Option<&ListSection> {
107 let sections = self.sections.as_deref()?;
108 sections
109 .partition_point(|section| section.index <= index)
110 .checked_sub(1)
111 .and_then(|index| sections.get(index))
112 }
113
114 pub(crate) fn leading_clearance(&self, index: usize, viewport: f32) -> f32 {
115 if self.sticky_sections
116 && self.section_height < viewport
117 && self
118 .section_at(index)
119 .is_some_and(|section| !section.label.is_empty())
120 {
121 self.section_height
122 } else {
123 0.0
124 }
125 }
126
127 pub(crate) fn reveal(&self, scroll: ScrollLayout, index: usize, viewport: f32) -> ScrollLayout {
128 let leading = self.leading_clearance(index, viewport);
129 let positioned = ScrollLayout::new(scroll.offset + leading, scroll.max_offset + leading)
130 .revealing(
131 self.top(index) as f32,
132 self.height(index),
133 viewport - leading,
134 );
135 ScrollLayout::new(positioned.offset - leading, scroll.max_offset)
136 }
137
138 pub(crate) fn headers(
139 &self,
140 viewport: Rect,
141 offset: f32,
142 row_count: usize,
143 ) -> Option<Box<[ListSectionHeader]>> {
144 let sections = self.sections.as_deref()?;
145 let h = f64::from(self.section_height);
146 let offset = f64::from(offset);
147 let end = offset + f64::from(viewport.height);
148 let start = |section: &ListSection| {
149 self.base_top(section.index)
150 + (section.heading_count - usize::from(!section.label.is_empty())) as f64 * h
151 };
152 let first = sections
153 .partition_point(|section| start(section) <= offset)
154 .saturating_sub(1);
155 let mut headers = Vec::new();
156 for (index, section) in sections.iter().enumerate().skip(first) {
157 let natural = start(section);
158 if natural >= end {
159 break;
160 }
161 if section.label.is_empty() {
162 continue;
163 }
164 let pinned = index == first
165 && natural < offset
166 && self.sticky_sections
167 && self.section_height < viewport.height;
168 let top = if pinned {
169 sections
170 .get(index + 1)
171 .map_or(offset, |next| offset.min(start(next) - h))
172 } else {
173 natural
174 };
175 if top + h > offset {
176 headers.push(ListSectionHeader {
177 first_row: section.index,
178 row_count: sections
179 .get(index + 1)
180 .map_or(row_count, |next| next.index)
181 .saturating_sub(section.index),
182 key: section.key,
183 label: section.label.clone(),
184 bounds: Rect::new(
185 viewport.x,
186 viewport.y + (top - offset) as f32,
187 viewport.width,
188 self.section_height,
189 ),
190 pinned,
191 content: None,
192 });
193 }
194 }
195 (!headers.is_empty()).then(|| headers.into_boxed_slice())
196 }
197
198 pub(crate) fn height(&self, index: usize) -> f32 {
199 self.row_heights
200 .as_ref()
201 .map_or(self.height, |heights| heights.height(index))
202 }
203
204 pub(crate) fn extent(&self, count: usize) -> f32 {
205 count.checked_sub(1).map_or(0.0, |last| {
206 (self.top(last) + f64::from(self.height(last))) as f32
207 })
208 }
209
210 pub(crate) fn bounds(&self, index: usize, viewport: Rect, offset: f32) -> Rect {
211 let x = (index % self.columns) as f32 * (self.width + self.spacing);
212 let y = (self.top(index) - f64::from(offset)) as f32;
213 Rect::new(
214 viewport.x + x,
215 viewport.y + y,
216 self.width,
217 self.height(index),
218 )
219 }
220
221 pub(crate) fn visible(&self, top: f64, bottom: f64, count: usize) -> Range<usize> {
222 if self.row_heights.is_some() || self.sections.is_some() {
223 let first = self.row_at(top, count);
224 let end = boundary(count, |index| self.top(index) < bottom);
225 return first..end;
226 }
227 let first = ((top + f64::from(self.spacing)) / self.pitch()).floor() as usize;
228 let end = (bottom / self.pitch()).ceil() as usize;
229 first.saturating_mul(self.columns).min(count)..end.saturating_mul(self.columns).min(count)
230 }
231
232 fn row_at(&self, y: f64, count: usize) -> usize {
233 boundary(count, |index| {
234 self.top(index) + f64::from(self.height(index)) <= y
235 })
236 }
237
238 pub(crate) fn page(&self, row: usize, viewport: f32, count: usize, forward: bool) -> usize {
239 if self.row_heights.is_some() || self.sections.is_some() {
240 let height = f64::from(viewport);
241 let y = self.top(row) + if forward { height } else { -height };
242 let next = self.row_at(y, count);
243 return if forward {
244 next.max(row.saturating_add(1)).min(count - 1)
245 } else {
246 next.min(row.saturating_sub(1))
247 };
248 }
249 let step = ((f64::from(viewport) / self.pitch()).floor() as usize)
250 .max(1)
251 .saturating_mul(self.columns);
252 if forward {
253 row.saturating_add(step).min(count - 1)
254 } else {
255 row.saturating_sub(step)
256 }
257 }
258}
259
260fn boundary(count: usize, before: impl Fn(usize) -> bool) -> usize {
261 let mut first = 0;
262 let mut end = count;
263 while first < end {
264 let middle = first + (end - first) / 2;
265 if before(middle) {
266 first = middle + 1;
267 } else {
268 end = middle;
269 }
270 }
271 first
272}