1use crate::*;
2
3#[derive(Clone, Copy, Debug)]
5pub struct PaginationOptions {
6 pub boundary_count: usize,
7 pub sibling_count: usize,
8 pub show_first_last: bool,
9}
10
11impl Default for PaginationOptions {
12 fn default() -> Self {
13 Self {
14 boundary_count: 1,
15 sibling_count: 1,
16 show_first_last: false,
17 }
18 }
19}
20
21#[derive(Clone, Copy, Debug)]
23pub struct PageIndicatorOptions {
24 pub dot_size: f32,
25 pub gap: f32,
26 pub active_color: Option<Color>,
27 pub inactive_color: Option<Color>,
28 pub interactive: bool,
29}
30
31impl Default for PageIndicatorOptions {
32 fn default() -> Self {
33 Self {
34 dot_size: 6.0,
35 gap: 12.0,
36 active_color: None,
37 inactive_color: None,
38 interactive: false,
39 }
40 }
41}
42
43impl Frame<'_> {
44 pub fn pagination(&mut self, id: u64, page: &mut usize, count: usize, enabled: bool) {
46 self.pagination_with_options(id, page, count, enabled, PaginationOptions::default());
47 }
48
49 pub fn pagination_with_options(
51 &mut self,
52 id: u64,
53 page: &mut usize,
54 count: usize,
55 enabled: bool,
56 options: PaginationOptions,
57 ) {
58 let count = count.max(1);
59 *page = (*page).clamp(1, count);
60 let boundary = options.boundary_count.max(1).min(count);
61 let sibling = options.sibling_count;
62 let mut pages = Vec::new();
63 for p in 1..=boundary {
64 pages.push(p);
65 }
66 for p in count.saturating_sub(boundary - 1)..=count {
67 pages.push(p);
68 }
69 if count <= boundary * 2 + sibling * 2 + 3 {
70 pages.extend(1..=count);
71 }
72 for p in page.saturating_sub(sibling).max(1)..=page.saturating_add(sibling).min(count) {
73 pages.push(p);
74 }
75 pages.sort_unstable();
76 pages.dedup();
77 let mut entries = Vec::with_capacity(pages.len() + 2);
78 for p in pages {
79 if let Some(Some(previous)) = entries.last().copied() {
80 if p.saturating_sub(previous) == 2 {
81 entries.push(Some(previous + 1));
82 } else if p.saturating_sub(previous) > 2 {
83 entries.push(None);
84 }
85 }
86 entries.push(Some(p));
87 }
88 let extra = if options.show_first_last { 4 } else { 2 };
89 let row = self.reserve(40.0);
90 let total = entries.len() + extra;
91 let width = (row.width / total as f32).min(40.0);
92 for i in 0..total {
93 let bounds = Rect {
94 x: row.x + i as f32 * width,
95 width,
96 ..row
97 };
98 let first = options.show_first_last && i == 0;
99 let previous = if options.show_first_last { i == 1 } else { i == 0 };
100 let next = if options.show_first_last { i + 2 == total } else { i + 1 == total };
101 let last = options.show_first_last && i + 1 == total;
102 let entry_offset = if options.show_first_last { 2 } else { 1 };
103 let target = if first {
104 Some(1)
105 } else if previous {
106 Some(page.saturating_sub(1).max(1))
107 } else if next {
108 Some(page.saturating_add(1).min(count))
109 } else if last {
110 Some(count)
111 } else {
112 entries[i - entry_offset]
113 };
114 let Some(target) = target else {
115 self.styled_text(
116 bounds,
117 "…",
118 self.ui.theme.text,
119 TextStyle {
120 size: 14.0,
121 align: 1,
122 ..TextStyle::default()
123 },
124 );
125 continue;
126 };
127 let is_nav = first || previous || next || last;
128 let selected = !is_nav && target == *page;
129 let available = enabled
130 && (!(first || previous) || *page > 1)
131 && (!(next || last) || *page < count);
132 let r = self.interact(id + i as u64, bounds, available);
133 let label = if first {
134 "First page".to_owned()
135 } else if previous {
136 "Previous page".to_owned()
137 } else if next {
138 "Next page".to_owned()
139 } else if last {
140 "Last page".to_owned()
141 } else {
142 format!("Page {target}")
143 };
144 if selected {
145 self.paint_rect(
146 bounds.inset(4.0),
147 (width - 8.0).max(0.0) / 2.0,
148 self.ui.theme.text.opacity(0.08),
149 );
150 }
151 self.state_layer(r, width / 2.0, self.ui.theme.text);
152 self.focus_ring(r);
153 let color = self
154 .ui
155 .theme
156 .text
157 .opacity(if r.enabled { 1.0 } else { 0.38 });
158 if is_nav {
159 let cx = bounds.x + bounds.width / 2.0;
160 let cy = bounds.y + bounds.height / 2.0;
161 let direction = if first || previous { -1.0 } else { 1.0 };
162 let tip = [cx + direction * 2.65, cy];
163 self.paint_line([cx - direction * 2.1, cy - 4.6], tip, 1.67, color);
164 self.paint_line(tip, [cx - direction * 2.1, cy + 4.6], 1.67, color);
165 if first || last {
166 let bar_x = cx + direction * 5.2;
167 self.paint_line([bar_x, cy - 4.8], [bar_x, cy + 4.8], 1.67, color);
168 }
169 } else {
170 self.styled_text(
171 bounds,
172 &target.to_string(),
173 color,
174 TextStyle {
175 size: 14.0,
176 align: 1,
177 ..TextStyle::default()
178 },
179 );
180 }
181 self.semantic(r, Role::Button, &label, selected, None, None, None);
182 if r.clicked {
183 *page = target;
184 }
185 }
186 }
187
188 pub fn paginator(
191 &mut self,
192 id: u64,
193 page: &mut usize,
194 page_size: usize,
195 total_items: usize,
196 enabled: bool,
197 ) -> bool {
198 let mut size = page_size.max(1);
199 self.paginator_with_page_size(id, page, &mut size, &[], total_items, enabled)
200 }
201
202 pub fn paginator_with_page_size(
206 &mut self,
207 id: u64,
208 page: &mut usize,
209 page_size: &mut usize,
210 page_size_options: &[usize],
211 total_items: usize,
212 enabled: bool,
213 ) -> bool {
214 *page_size = (*page_size).max(1);
215 let size = *page_size;
216 let pages = total_items.div_ceil(size).max(1);
217 *page = (*page).min(pages - 1);
218 let start = if total_items == 0 { 0 } else { *page * size + 1 };
219 let end = ((*page + 1) * size).min(total_items);
220 let range_label = format!("{start} – {end} of {total_items}");
221
222 let t = self.theme();
223 let bar = self.reserve(56.0);
224 let mut changed = false;
225 let mut left_used = 0.0;
226 if !page_size_options.is_empty() {
227 let sz_text = format!("Items per page: {size}");
228 let btn_w = (self.text_width(&sz_text, TextStyle { size: 13.0, ..TextStyle::default() }) + 24.0)
229 .clamp(96.0, (bar.width * 0.5).max(96.0));
230 let sz_rect = Rect { x: bar.x + 8.0, y: bar.y + 8.0, width: btn_w, height: 40.0 };
231 let r = self.target(id.wrapping_add(10), sz_rect, &sz_text, enabled, Role::Button);
232 let fg = if enabled { t.on_surface_variant } else { t.text.opacity(0.38) };
233 self.state_layer(r, 8.0, fg);
234 self.focus_ring(r);
235 self.styled_text(
236 sz_rect,
237 &sz_text,
238 fg,
239 TextStyle { size: 13.0, align: 1, ..TextStyle::default() },
240 );
241 if r.clicked {
242 let pos = page_size_options.iter().position(|&v| v == size).unwrap_or(0);
243 *page_size = page_size_options[(pos + 1) % page_size_options.len()].max(1);
244 *page = 0;
245 changed = true;
246 }
247 left_used = btn_w + 16.0;
248 }
249
250 let btn_w = 40.0;
251 let next_rect = Rect { x: bar.x + bar.width - btn_w, y: bar.y + 8.0, width: btn_w, height: btn_w };
252 let prev_rect = Rect { x: next_rect.x - btn_w - 4.0, y: bar.y + 8.0, width: btn_w, height: btn_w };
253 let label_rect = Rect {
254 x: bar.x + left_used + 8.0,
255 y: bar.y,
256 width: (prev_rect.x - (bar.x + left_used) - 16.0).max(0.0),
257 height: bar.height,
258 };
259 self.styled_text(
260 label_rect,
261 &range_label,
262 if enabled { t.on_surface_variant } else { t.text.opacity(0.38) },
263 TextStyle { size: 13.0, align: 2, ..TextStyle::default() },
264 );
265 for (i, (rect, can, name, dir)) in [
266 (prev_rect, enabled && *page > 0, "Previous page", -1.0_f32),
267 (next_rect, enabled && *page + 1 < pages, "Next page", 1.0_f32),
268 ]
269 .into_iter()
270 .enumerate()
271 {
272 let r = self.target(id.wrapping_add(i as u64), rect, name, can, Role::Button);
273 let fg = if can { t.on_surface_variant } else { t.text.opacity(0.38) };
274 self.state_layer(r, 20.0, fg);
275 self.focus_ring(r);
276 let cx = rect.x + rect.width * 0.5;
277 let cy = rect.y + rect.height * 0.5;
278 self.paint_line([cx - dir * 2.5, cy - 4.5], [cx + dir * 2.0, cy], 1.8, fg);
279 self.paint_line([cx + dir * 2.0, cy], [cx - dir * 2.5, cy + 4.5], 1.8, fg);
280 if r.clicked {
281 if i == 0 {
282 *page = page.saturating_sub(1);
283 } else {
284 *page = (*page + 1).min(pages - 1);
285 }
286 changed = true;
287 }
288 }
289 changed
290 }
291
292 pub fn page_indicator(
295 &mut self,
296 id: u64,
297 bounds: Rect,
298 page_count: usize,
299 current: f32,
300 options: PageIndicatorOptions,
301 ) -> Option<usize> {
302 if page_count <= 1 {
303 return None;
304 }
305 let t = self.theme();
306 let dot = options.dot_size.max(2.0);
307 let gap = options.gap.max(dot + 2.0);
308 let total_w = (page_count - 1) as f32 * gap + dot;
309 let start_x = bounds.x + (bounds.width - total_w) * 0.5;
310 let dot_y = bounds.y + (bounds.height - dot) * 0.5;
311 let active_c = options.active_color.unwrap_or(t.primary);
312 let inactive_c = options.inactive_color.unwrap_or(t.on_surface_variant.opacity(0.45));
313
314 let mut next = None;
315 if options.interactive {
316 let r = self.target(id, bounds, "Switch page", true, Role::Button);
317 if r.clicked {
318 let cur = (current.round().max(0.0) as usize).min(page_count - 1);
319 next = Some((cur + 1) % page_count);
320 }
321 }
322 for i in 0..page_count {
323 let active = (current - i as f32).abs() < 0.5;
324 self.paint_rect(
325 Rect { x: start_x + i as f32 * gap, y: dot_y, width: dot, height: dot },
326 dot * 0.5,
327 if active { active_c } else { inactive_c },
328 );
329 }
330 next
331 }
332}
333
334#[cfg(test)]
335#[path = "pagination.spec.rs"]
336mod tests;