Skip to main content

PaginatorState

Struct PaginatorState 

Source
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: usize

Total number of items being paged over.

§per_page: usize

Items per page (clamped to >= 1 internally).

§page: usize

Current page (0-based).

§style: PaginatorStyle

Rendering style.

Implementations§

Source§

impl PaginatorState

Source

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);
Source

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);
Source

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));
Source

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);
Source

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);
Source

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);
Source

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);
Source

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

Source§

fn clone(&self) -> PaginatorState

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PaginatorState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.