pub struct PaginatorState {
pub total_items: usize,
pub per_page: usize,
pub page: usize,
pub style: PaginatorStyle,
}Expand description
Standalone pagination state, decoupled from any list or table.
Owns a page index over an arbitrary item count, so you can paginate a
wizard, slide deck, onboarding flow, carousel, or any non-table data. Pass a
mutable reference to Context::paginator each
frame; Left/h/PageUp move to the previous page and Right/l/PageDown move
to the next page when the widget is focused.
§Example
use slt::{PaginatorState, PaginatorStyle};
let mut state = PaginatorState::new(42, 10); // 42 items, 10 per page
state.style = PaginatorStyle::Arabic;
assert_eq!(state.total_pages(), 5);
let (start, end) = state.page_bounds(); // slice your own data with these
assert_eq!((start, end), (0, 10));Fields§
§total_items: usizeTotal number of items being paged over.
per_page: usizeItems per page (clamped to >= 1 internally).
page: usizeCurrent page (0-based).
style: PaginatorStyleRendering style.
Implementations§
Source§impl PaginatorState
impl PaginatorState
Sourcepub fn new(total_items: usize, per_page: usize) -> Self
pub fn new(total_items: usize, per_page: usize) -> Self
Create a paginator over total_items with per_page items per page.
per_page is clamped to at least 1 internally (so a 0 argument is
treated as 1, avoiding division by zero). The current page starts at
0 and the style defaults to PaginatorStyle::Dots.
§Example
use slt::PaginatorState;
let state = PaginatorState::new(30, 0); // 0 per_page -> clamped to 1
assert_eq!(state.per_page, 1);
assert_eq!(state.total_pages(), 30);Sourcepub fn total_pages(&self) -> usize
pub fn total_pages(&self) -> usize
Total number of pages; always >= 1 (returns 1 when there are no items).
§Example
use slt::PaginatorState;
assert_eq!(PaginatorState::new(0, 5).total_pages(), 1);
assert_eq!(PaginatorState::new(10, 3).total_pages(), 4);
assert_eq!(PaginatorState::new(9, 3).total_pages(), 3);Sourcepub fn page_bounds(&self) -> (usize, usize)
pub fn page_bounds(&self) -> (usize, usize)
Inclusive-start / exclusive-end item indices for the current page.
end is clamped to total_items, so callers can slice their own data
with &items[start..end] without bounds-checking the tail page.
§Example
use slt::PaginatorState;
let mut state = PaginatorState::new(10, 3);
assert_eq!(state.page_bounds(), (0, 3));
state.set_page(3); // last (partial) page
assert_eq!(state.page_bounds(), (9, 10));Sourcepub fn next_page(&mut self)
pub fn next_page(&mut self)
Advance one page, clamped to the last page (no wrap).
§Example
use slt::PaginatorState;
let mut state = PaginatorState::new(6, 3); // 2 pages
state.next_page();
assert_eq!(state.page, 1);
state.next_page(); // already last page -> clamped
assert_eq!(state.page, 1);Sourcepub fn prev_page(&mut self)
pub fn prev_page(&mut self)
Go back one page, clamped to 0 (no wrap).
§Example
use slt::PaginatorState;
let mut state = PaginatorState::new(6, 3);
state.prev_page(); // already page 0 -> clamped
assert_eq!(state.page, 0);Sourcepub fn set_page(&mut self, page: usize)
pub fn set_page(&mut self, page: usize)
Jump to a specific page, clamped into [0, total_pages() - 1].
§Example
use slt::PaginatorState;
let mut state = PaginatorState::new(10, 3); // 4 pages
state.set_page(99);
assert_eq!(state.page, 3);Sourcepub fn set_total_items(&mut self, total: usize)
pub fn set_total_items(&mut self, total: usize)
Update the item count and re-clamp the current page into range.
§Example
use slt::PaginatorState;
let mut state = PaginatorState::new(10, 3);
state.set_page(3); // last page
state.set_total_items(3); // now only 1 page
assert_eq!(state.page, 0);Sourcepub fn set_per_page(&mut self, per_page: usize)
pub fn set_per_page(&mut self, per_page: usize)
Update items-per-page (clamped to >= 1) and re-clamp the current page.
§Example
use slt::PaginatorState;
let mut state = PaginatorState::new(10, 3); // 4 pages
state.set_page(3);
state.set_per_page(10); // now only 1 page
assert_eq!(state.per_page, 10);
assert_eq!(state.page, 0);Trait Implementations§
Source§impl Clone for PaginatorState
impl Clone for PaginatorState
Source§fn clone(&self) -> PaginatorState
fn clone(&self) -> PaginatorState
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more