Skip to main content

repose_material/material3/
carousel.rs

1#![allow(non_snake_case)]
2
3use std::rc::Rc;
4
5use repose_core::*;
6use repose_ui::LazyRowState;
7use repose_ui::lazy::LazyRow;
8use repose_ui::lazy_states::LazyRowConfig;
9
10
11/// M3 Carousel - a horizontally scrolling container with peek edges.
12///
13/// Uses a `LazyRow` internally. The first and last items are partially visible
14/// (peek) to indicate there is more scrollable content.
15/// Configuration for [`Carousel`].
16#[derive(Clone, Debug)]
17pub struct CarouselConfig {
18    pub modifier: Modifier,
19}
20
21impl Default for CarouselConfig {
22    fn default() -> Self {
23        Self {
24            modifier: Modifier::new(),
25        }
26    }
27}
28
29/// M3 Carousel - a horizontally scrolling container with peek edges.
30///
31/// Uses a `LazyRow` internally. The first and last items are partially visible
32/// (peek) to indicate there is more scrollable content.
33pub fn Carousel<T, F>(
34    items: Vec<T>,
35    item_width: f32,
36    peek_amount: f32,
37    state: Rc<LazyRowState>,
38    item_builder: F,
39    config: CarouselConfig,
40) -> View
41where
42    T: Clone + 'static,
43    F: Fn(T, usize) -> View + 'static,
44{
45    let padded_modifier = config.modifier.padding_values(PaddingValues {
46        left: peek_amount,
47        right: peek_amount,
48        top: 0.0,
49        bottom: 0.0,
50    });
51
52    LazyRow(
53        items,
54        item_width,
55        item_builder,
56        LazyRowConfig {
57            state,
58            modifier: padded_modifier,
59            ..Default::default()
60        },
61    )
62}