1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//! Pagers: Compose's `HorizontalPager` and `VerticalPager`. The pages fill
//! the pager or take a fixed length, compose lazily as a lazy row or
//! column's items do, and a drag or a fling the user lets go of comes to
//! rest at the start of a page.
use std::rc::Rc;
use cranpose_foundation::lazy::{
LazyItems, LazyListIntervalContent, LazyListScope, LazyListState, LazySettlePolicy,
rememberLazyListStateWithPosition,
};
use cranpose_ui_layout::LinearArrangement;
use crate::{
AnimateScroll, composable,
modifier::Modifier,
widgets::{
Box, BoxSpec, BoxWithConstraints, BoxWithConstraintsScope, LazyColumn, LazyColumnSpec,
LazyRow, LazyRowSpec, PaddingValues,
},
};
/// A release faster than this, in logical pixels per second, turns the page
/// toward the fling however little the drag moved it.
const PAGE_FLING_VELOCITY: f32 = 400.0;
/// How long a pager's pages are along its axis — Compose's `PageSize`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum PageSize {
/// Each page is as long as the pager.
Fill,
/// Each page is this many logical pixels long, so a shorter page shows
/// the next one beside it.
Fixed(f32),
}
/// Options for a pager, Compose's pager arguments beside its state and its
/// page count.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PagerSpec {
/// How long each page is along the pager's axis.
pub page_size: PageSize,
/// Logical pixels between two pages.
pub page_spacing: f32,
/// Compose's `contentPadding`. A page comes to rest after the start
/// padding, a `PageSize::Fill` page is as long as the pager minus the
/// padding at both ends, and the neighbouring pages show in the padding.
pub content_padding: PaddingValues,
/// Pages composed and laid out beyond the ones in view on each side:
/// Compose's `beyondViewportPageCount`.
pub beyond_viewport_page_count: usize,
}
impl Default for PagerSpec {
fn default() -> Self {
Self {
page_size: PageSize::Fill,
page_spacing: 0.0,
content_padding: PaddingValues::default(),
beyond_viewport_page_count: 2,
}
}
}
/// Where a pager is — Compose's `PagerState`. It is `Copy`, so it moves into
/// any number of closures.
#[derive(Clone, Copy, PartialEq)]
pub struct PagerState {
list: LazyListState,
}
/// A [`PagerState`] that starts on `initial_page`.
#[composable]
pub fn rememberPagerState(initial_page: usize) -> PagerState {
PagerState {
list: rememberLazyListStateWithPosition(initial_page, 0.0),
}
}
impl PagerState {
/// A pager's state on `initial_page`, for code that remembers the state
/// itself. Call it inside a `remember`, as [`LazyListState::new`].
pub fn new(initial_page: usize) -> Self {
Self {
list: LazyListState::new(initial_page, 0.0),
}
}
/// The first page in view, or `None` once the slot that held this state
/// was released.
pub fn try_first_page(&self) -> Option<usize> {
self.list.try_position().map(|(page, _)| page)
}
/// The page that fills most of the pager. A composable that reads it
/// recomposes while the pager scrolls.
pub fn current_page(&self) -> usize {
self.page_at(
self.list.first_visible_item_index(),
self.list.first_visible_item_scroll_offset(),
)
}
/// The page the pager rests on: Compose's `settledPage`. While a drag,
/// a fling or an animation moves the pager, it stays the page that
/// scroll began on, so a composable that reads it recomposes when a
/// scroll starts and when it ends, not on the frames between.
pub fn settled_page(&self) -> usize {
let (first, offset) = match self.list.position_before_scroll() {
Some(start) if self.list.is_scroll_in_progress() => start,
// At rest the offset changes only with the page, so the page
// alone subscribes.
_ => (
self.list.first_visible_item_index(),
self.list.first_visible_item_scroll_offset_non_reactive(),
),
};
self.page_at(first, offset)
}
/// Whether a drag, a fling or an animation moves the pager now:
/// Compose's `isScrollInProgress`.
pub fn is_scroll_in_progress(&self) -> bool {
self.list.is_scroll_in_progress()
}
/// How far the pager stands from [`Self::current_page`], as a fraction of
/// a page from -0.5 to 0.5 — Compose's `currentPageOffsetFraction`.
pub fn current_page_offset_fraction(&self) -> f32 {
let page = self.page_extent();
if page <= 0.0 {
return 0.0;
}
let fraction = self.list.first_visible_item_scroll_offset() / page;
if fraction > 0.5 {
fraction - 1.0
} else {
fraction
}
}
/// Shows `page` at once.
pub fn scroll_to_page(&self, page: usize) {
self.list.scroll_to_item(page, 0.0);
}
/// Scrolls to `page` with a spring — Compose's `animateScrollToPage`. A
/// press on the pager stops it, and letting go settles on a page.
pub fn animate_scroll_to_page(&self, page: usize) {
self.list.animate_scroll_to_item(page, 0.0);
}
/// The state of the lazy row or column the pages sit in, for a scroll
/// indicator.
pub fn lazy_list_state(&self) -> LazyListState {
self.list
}
/// The page that fills most of the pager when page `first` is the first
/// in view, scrolled by `offset`.
fn page_at(&self, first: usize, offset: f32) -> usize {
if offset > self.page_extent() / 2.0 {
first + 1
} else {
first
}
}
/// How far the start of a page lies from the start of the next one.
fn page_extent(&self) -> f32 {
self.list
.with_layout_info(|info| {
info.visible_items_info
.first()
.map_or(info.viewport_size, |page| {
page.size + info.main_axis_item_spacing
})
})
.unwrap_or_default()
}
}
/// Where a scroll the user lets go of comes to rest: the start of the first
/// page it shows or of the page after, the way the release goes when it is a
/// fling, else the one closer to where it would stop.
fn page_settle(state: PagerState) -> LazySettlePolicy {
Rc::new(move |proposed, velocity| {
let page = state.page_extent();
if page <= 0.0 {
return proposed;
}
let forward = if velocity.abs() >= PAGE_FLING_VELOCITY {
velocity > 0.0
} else {
proposed > page / 2.0
};
if forward { page } else { 0.0 }
})
}
/// Pages side by side, `page_count` of them, each as high as the pager
/// and as wide as `spec` says — Compose's `HorizontalPager`. The pager
/// needs a bounded size.
#[composable(no_skip)]
pub fn HorizontalPager<F>(
modifier: Modifier,
state: PagerState,
spec: PagerSpec,
page_count: usize,
page_content: F,
) where
F: Fn(usize) + 'static,
{
Pager(
modifier,
state,
spec,
page_count,
true,
Rc::new(page_content),
);
}
/// Pages stacked from top to bottom — Compose's `VerticalPager`.
#[composable(no_skip)]
pub fn VerticalPager<F>(
modifier: Modifier,
state: PagerState,
spec: PagerSpec,
page_count: usize,
page_content: F,
) where
F: Fn(usize) + 'static,
{
Pager(
modifier,
state,
spec,
page_count,
false,
Rc::new(page_content),
);
}
#[composable(no_skip)]
fn Pager(
modifier: Modifier,
state: PagerState,
spec: PagerSpec,
page_count: usize,
horizontal: bool,
page_content: Rc<dyn Fn(usize)>,
) {
cranpose_core::DisposableEffect(state, move |scope| {
state.list.set_settle_policy(Some(page_settle(state)));
scope.on_dispose(move || state.list.set_settle_policy(None))
});
BoxWithConstraints(modifier, move |scope| {
let constraints = scope.constraints();
let padding = spec.content_padding;
let width = constraints.max_width - padding.start - padding.end;
let height = constraints.max_height - padding.top - padding.bottom;
let (width, height) = match (spec.page_size, horizontal) {
(PageSize::Fill, _) => (width, height),
(PageSize::Fixed(length), true) => (length, height),
(PageSize::Fixed(length), false) => (width, length),
};
let content = Rc::clone(&page_content);
let pages = move |pages: &mut LazyListIntervalContent| {
pages.items(
LazyItems::new(page_count).key(|page| page as u64),
move |page| {
let content = Rc::clone(&content);
Box(
Modifier::empty().size_points(width, height),
BoxSpec::default(),
move || content(page),
);
},
);
};
let fill = Modifier::empty().fill_max_size();
let spacing = LinearArrangement::SpacedBy(spec.page_spacing);
if horizontal {
let row = LazyRowSpec {
horizontal_arrangement: spacing,
content_padding: padding,
beyond_bounds_item_count: spec.beyond_viewport_page_count,
..LazyRowSpec::default()
};
LazyRow(fill, state.list, row, pages);
} else {
let column = LazyColumnSpec {
vertical_arrangement: spacing,
content_padding: padding,
beyond_bounds_item_count: spec.beyond_viewport_page_count,
..LazyColumnSpec::default()
};
LazyColumn(fill, state.list, column, pages);
}
});
}