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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//! Flutter counterpart: `widgets/scroll_metrics.dart`.
use std::fmt::{self, Debug};
use waft_embedder::clamp_double;
use waft_painting::{Axis, AxisDirection, axis_direction_to_axis};
/// A description of a `Scrollable`'s contents, useful for modeling the state
/// of its viewport.
///
/// This trait defines a current position, [`pixels`](Self::pixels), and a range of values
/// considered "in bounds" for that position. The range has a minimum value at
/// [`min_scroll_extent`](Self::min_scroll_extent) and a maximum value at
/// [`max_scroll_extent`](Self::max_scroll_extent) (inclusive). The viewport scrolls in the
/// direction and axis described by [`axis_direction`](Self::axis_direction) and
/// [`axis`](Self::axis).
///
/// The [`out_of_range`](Self::out_of_range) getter will return true if
/// [`pixels`](Self::pixels) is outside this defined range. The [`at_edge`](Self::at_edge)
/// getter will return true if the [`pixels`](Self::pixels) position equals either the
/// [`min_scroll_extent`](Self::min_scroll_extent) or the
/// [`max_scroll_extent`](Self::max_scroll_extent).
///
/// The dimensions of the viewport in the given [`axis`](Self::axis) are described by
/// [`viewport_dimension`](Self::viewport_dimension).
///
/// The above values are also exposed in terms of [`extent_before`](Self::extent_before),
/// [`extent_inside`](Self::extent_inside), and [`extent_after`](Self::extent_after), which
/// may be more useful for use cases such as scroll bars.
///
/// See also:
///
/// * [`FixedScrollMetrics`], which is an immutable object that implements this
/// interface.
pub trait ScrollMetrics: Debug {
/// Creates a [`ScrollMetrics`] that has the same properties as this object.
///
/// This is useful if this object is mutable, but you want to get a snapshot
/// of the current state.
///
/// The `with_*` setters on the result allow the values to be adjusted in the process.
/// This is useful to examine hypothetical situations, for example "would applying this
/// delta unmodified take the position [`out_of_range`](Self::out_of_range)?".
fn copy_with(&self) -> FixedScrollMetrics {
FixedScrollMetrics::new(
self.has_content_dimensions()
.then(|| self.min_scroll_extent()),
self.has_content_dimensions()
.then(|| self.max_scroll_extent()),
self.has_pixels().then(|| self.pixels()),
self.has_viewport_dimension()
.then(|| self.viewport_dimension()),
self.axis_direction(),
self.device_pixel_ratio(),
)
}
/// The minimum in-range value for [`pixels`](Self::pixels).
///
/// The actual [`pixels`](Self::pixels) value might be
/// [`out_of_range`](Self::out_of_range).
///
/// This value is typically less than or equal to
/// [`max_scroll_extent`](Self::max_scroll_extent). It can be negative infinity, if the
/// scroll is unbounded.
fn min_scroll_extent(&self) -> f64;
/// The maximum in-range value for [`pixels`](Self::pixels).
///
/// The actual [`pixels`](Self::pixels) value might be
/// [`out_of_range`](Self::out_of_range).
///
/// This value is typically greater than or equal to
/// [`min_scroll_extent`](Self::min_scroll_extent). It can be infinity, if the scroll is
/// unbounded.
///
/// For scrollables that lazily construct their contents, such as
/// `ListView.builder`, this value can be an estimate that changes as more
/// children are laid out.
fn max_scroll_extent(&self) -> f64;
/// Whether the [`min_scroll_extent`](Self::min_scroll_extent) and the
/// [`max_scroll_extent`](Self::max_scroll_extent) properties are available.
fn has_content_dimensions(&self) -> bool;
/// The current scroll position, in logical pixels along the
/// [`axis_direction`](Self::axis_direction).
fn pixels(&self) -> f64;
/// Whether the [`pixels`](Self::pixels) property is available.
fn has_pixels(&self) -> bool;
/// The extent of the viewport along the [`axis_direction`](Self::axis_direction).
fn viewport_dimension(&self) -> f64;
/// Whether the [`viewport_dimension`](Self::viewport_dimension) property is available.
fn has_viewport_dimension(&self) -> bool;
/// The direction in which the scroll view scrolls.
fn axis_direction(&self) -> AxisDirection;
/// The axis in which the scroll view scrolls.
fn axis(&self) -> Axis {
axis_direction_to_axis(self.axis_direction())
}
/// Whether the [`pixels`](Self::pixels) value is outside the
/// [`min_scroll_extent`](Self::min_scroll_extent) and
/// [`max_scroll_extent`](Self::max_scroll_extent).
fn out_of_range(&self) -> bool {
self.pixels() < self.min_scroll_extent() || self.pixels() > self.max_scroll_extent()
}
/// Whether the [`pixels`](Self::pixels) value is exactly at the
/// [`min_scroll_extent`](Self::min_scroll_extent) or the
/// [`max_scroll_extent`](Self::max_scroll_extent).
fn at_edge(&self) -> bool {
self.pixels() == self.min_scroll_extent() || self.pixels() == self.max_scroll_extent()
}
/// The quantity of content conceptually "above" the viewport in the scrollable.
/// This is the content above the content described by
/// [`extent_inside`](Self::extent_inside).
fn extent_before(&self) -> f64 {
(self.pixels() - self.min_scroll_extent()).max(0.0)
}
/// The quantity of content conceptually "inside" the viewport in the
/// scrollable (including empty space if the total amount of content is less
/// than the [`viewport_dimension`](Self::viewport_dimension)).
///
/// The value is typically the extent of the viewport
/// ([`viewport_dimension`](Self::viewport_dimension)) when
/// [`out_of_range`](Self::out_of_range) is false. It can be less when overscrolling.
///
/// The value is always non-negative, and less than or equal to
/// [`viewport_dimension`](Self::viewport_dimension).
fn extent_inside(&self) -> f64 {
debug_assert!(self.min_scroll_extent() <= self.max_scroll_extent());
self.viewport_dimension()
// "above" overscroll value
- clamp_double(
self.min_scroll_extent() - self.pixels(),
0.0,
self.viewport_dimension(),
)
// "below" overscroll value
- clamp_double(
self.pixels() - self.max_scroll_extent(),
0.0,
self.viewport_dimension(),
)
}
/// The quantity of content conceptually "below" the viewport in the scrollable.
/// This is the content below the content described by
/// [`extent_inside`](Self::extent_inside).
fn extent_after(&self) -> f64 {
(self.max_scroll_extent() - self.pixels()).max(0.0)
}
/// The total quantity of content available.
///
/// This is the sum of [`extent_before`](Self::extent_before),
/// [`extent_inside`](Self::extent_inside), and [`extent_after`](Self::extent_after),
/// modulo any rounding errors.
fn extent_total(&self) -> f64 {
self.max_scroll_extent() - self.min_scroll_extent() + self.viewport_dimension()
}
/// The `FlutterView.devicePixelRatio` of the view that the `Scrollable`
/// associated with this metrics object is drawn into.
fn device_pixel_ratio(&self) -> f64;
}
/// An immutable snapshot of values associated with a `Scrollable` viewport.
///
/// For details, see [`ScrollMetrics`], which defines this object's interfaces.
#[derive(Clone, Copy)]
pub struct FixedScrollMetrics {
min_scroll_extent: Option<f64>,
max_scroll_extent: Option<f64>,
pixels: Option<f64>,
viewport_dimension: Option<f64>,
axis_direction: AxisDirection,
device_pixel_ratio: f64,
}
impl FixedScrollMetrics {
/// Creates an immutable snapshot of values associated with a `Scrollable` viewport.
pub fn new(
min_scroll_extent: Option<f64>,
max_scroll_extent: Option<f64>,
pixels: Option<f64>,
viewport_dimension: Option<f64>,
axis_direction: AxisDirection,
device_pixel_ratio: f64,
) -> FixedScrollMetrics {
FixedScrollMetrics {
min_scroll_extent,
max_scroll_extent,
pixels,
viewport_dimension,
axis_direction,
device_pixel_ratio,
}
}
/// Dart `copyWith(minScrollExtent:)`.
pub fn with_min_scroll_extent(mut self, min_scroll_extent: f64) -> FixedScrollMetrics {
self.min_scroll_extent = Some(min_scroll_extent);
self
}
/// Dart `copyWith(maxScrollExtent:)`.
pub fn with_max_scroll_extent(mut self, max_scroll_extent: f64) -> FixedScrollMetrics {
self.max_scroll_extent = Some(max_scroll_extent);
self
}
/// Dart `copyWith(pixels:)`.
pub fn with_pixels(mut self, pixels: f64) -> FixedScrollMetrics {
self.pixels = Some(pixels);
self
}
/// Dart `copyWith(viewportDimension:)`.
pub fn with_viewport_dimension(mut self, viewport_dimension: f64) -> FixedScrollMetrics {
self.viewport_dimension = Some(viewport_dimension);
self
}
/// Dart `copyWith(axisDirection:)`.
pub fn with_axis_direction(mut self, axis_direction: AxisDirection) -> FixedScrollMetrics {
self.axis_direction = axis_direction;
self
}
/// Dart `copyWith(devicePixelRatio:)`.
pub fn with_device_pixel_ratio(mut self, device_pixel_ratio: f64) -> FixedScrollMetrics {
self.device_pixel_ratio = device_pixel_ratio;
self
}
}
impl ScrollMetrics for FixedScrollMetrics {
fn min_scroll_extent(&self) -> f64 {
self.min_scroll_extent
.expect("FixedScrollMetrics.minScrollExtent was read without content dimensions")
}
fn max_scroll_extent(&self) -> f64 {
self.max_scroll_extent
.expect("FixedScrollMetrics.maxScrollExtent was read without content dimensions")
}
fn has_content_dimensions(&self) -> bool {
self.min_scroll_extent.is_some() && self.max_scroll_extent.is_some()
}
fn pixels(&self) -> f64 {
self.pixels
.expect("FixedScrollMetrics.pixels was read before it was available")
}
fn has_pixels(&self) -> bool {
self.pixels.is_some()
}
fn viewport_dimension(&self) -> f64 {
self.viewport_dimension
.expect("FixedScrollMetrics.viewportDimension was read before it was available")
}
fn has_viewport_dimension(&self) -> bool {
self.viewport_dimension.is_some()
}
fn axis_direction(&self) -> AxisDirection {
self.axis_direction
}
fn device_pixel_ratio(&self) -> f64 {
self.device_pixel_ratio
}
}
impl Debug for FixedScrollMetrics {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"FixedScrollMetrics({:.1}..[{:.1}]..{:.1})",
self.extent_before(),
self.extent_inside(),
self.extent_after()
)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn overscrolled() -> FixedScrollMetrics {
FixedScrollMetrics::new(
Some(0.0),
Some(400.0),
Some(-20.0),
Some(600.0),
AxisDirection::Down,
3.0,
)
}
#[test]
fn out_of_range_and_at_edge_read_the_pixels_against_the_extents() {
let metrics = overscrolled();
assert!(metrics.out_of_range());
assert!(!metrics.at_edge());
let at_min = metrics.copy_with().with_pixels(0.0);
assert!(!at_min.out_of_range());
assert!(at_min.at_edge());
let at_max = metrics.copy_with().with_pixels(400.0);
assert!(!at_max.out_of_range());
assert!(at_max.at_edge());
let inside = metrics.copy_with().with_pixels(100.0);
assert!(!inside.out_of_range());
assert!(!inside.at_edge());
}
#[test]
fn the_extents_split_the_content_around_the_viewport() {
let metrics = overscrolled();
assert_eq!(metrics.extent_before(), 0.0);
assert_eq!(metrics.extent_inside(), 580.0);
assert_eq!(metrics.extent_after(), 420.0);
assert_eq!(metrics.extent_total(), 1000.0);
assert_eq!(metrics.axis(), Axis::Vertical);
assert_eq!(
format!("{metrics:?}"),
"FixedScrollMetrics(0.0..[580.0]..420.0)"
);
let inside = metrics.copy_with().with_pixels(100.0);
assert_eq!(inside.extent_before(), 100.0);
assert_eq!(inside.extent_inside(), 600.0);
assert_eq!(inside.extent_after(), 300.0);
}
#[test]
fn copy_with_carries_the_absent_values_across() {
let empty = FixedScrollMetrics::new(None, None, None, None, AxisDirection::Right, 2.0);
assert!(!empty.has_content_dimensions());
assert!(!empty.has_pixels());
assert!(!empty.has_viewport_dimension());
assert_eq!(empty.axis(), Axis::Horizontal);
let copy = empty.copy_with().with_pixels(7.0);
assert!(!copy.has_content_dimensions());
assert_eq!(copy.pixels(), 7.0);
assert_eq!(copy.device_pixel_ratio(), 2.0);
let full = overscrolled().copy_with();
assert!(full.has_content_dimensions());
assert!(full.has_pixels());
assert!(full.has_viewport_dimension());
}
}