1use std::fmt::{self, Debug};
4use std::hash::Hash;
5use std::rc::Rc;
6use std::time::Duration;
7
8use waft_animation::{Curve, Curves};
9use waft_foundation::{
10 App, ChangeNotifier, ChangeNotifierData, CompleterFuture, Handle, HandleId, ListenableHandle,
11 ListenableObject, Listener, Ref, Retained, bare_type_name, describe_identity,
12};
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
16pub enum ScrollDirection {
17 Idle,
19
20 Forward,
22
23 Reverse,
25}
26
27pub fn flip_scroll_direction(direction: ScrollDirection) -> ScrollDirection {
31 match direction {
32 ScrollDirection::Idle => ScrollDirection::Idle,
33 ScrollDirection::Forward => ScrollDirection::Reverse,
34 ScrollDirection::Reverse => ScrollDirection::Forward,
35 }
36}
37
38pub trait ViewportOffset: ChangeNotifier + Sized {
51 fn pixels(self: Handle<Self>, app: &App) -> f64;
61
62 fn has_pixels(self: Handle<Self>, app: &App) -> bool;
64
65 fn apply_viewport_dimension(self: Handle<Self>, app: &mut App, viewport_dimension: f64)
83 -> bool;
84
85 fn apply_content_dimensions(
105 self: Handle<Self>,
106 app: &mut App,
107 min_scroll_extent: f64,
108 max_scroll_extent: f64,
109 ) -> bool;
110
111 fn correct_by(self: Handle<Self>, app: &mut App, correction: f64);
124
125 fn jump_to(self: Handle<Self>, app: &mut App, pixels: f64);
133
134 fn animate_to(
142 self: Handle<Self>,
143 app: &mut App,
144 to: f64,
145 duration: Duration,
146 curve: Rc<dyn Curve>,
147 ) -> CompleterFuture<()>;
148
149 fn move_to(
157 self: Handle<Self>,
158 app: &mut App,
159 to: f64,
160 duration: Option<Duration>,
161 curve: Option<Rc<dyn Curve>>,
162 clamp: Option<bool>,
163 ) -> CompleterFuture<()> {
164 ViewportOffsetBase::move_to(self, app, to, duration, curve, clamp)
165 }
166
167 fn user_scroll_direction(self: Handle<Self>, app: &App) -> ScrollDirection;
178
179 fn allow_implicit_scrolling(self: Handle<Self>, app: &App) -> bool;
187
188 fn debug_fill_description(self: Handle<Self>, app: &App, description: &mut Vec<String>) {
193 if self.has_pixels(app) {
194 description.push(format!("offset: {:.1}", self.pixels(app)));
195 }
196 }
197
198 fn as_viewport_offset(self: Handle<Self>) -> AnyViewportOffset {
201 AnyViewportOffset {
202 id: self.id(),
203 vtable: const { &ViewportOffsetVTable::of::<Self>() },
204 }
205 }
206}
207
208pub struct ViewportOffsetBase;
210
211impl ViewportOffsetBase {
212 pub fn move_to<T: ViewportOffset>(
214 this: Handle<T>,
215 app: &mut App,
216 to: f64,
217 duration: Option<Duration>,
218 curve: Option<Rc<dyn Curve>>,
219 clamp: Option<bool>,
220 ) -> CompleterFuture<()> {
221 let _ = clamp;
222 let Some(duration) = duration.filter(|duration| !duration.is_zero()) else {
223 this.jump_to(app, to);
224 return CompleterFuture::ready(());
225 };
226 this.animate_to(app, to, duration, curve.unwrap_or(Curves::ease()))
227 }
228}
229
230type AnimateToFn = fn(&mut App, HandleId, f64, Duration, Rc<dyn Curve>) -> CompleterFuture<()>;
232
233type MoveToFn = fn(
235 &mut App,
236 HandleId,
237 f64,
238 Option<Duration>,
239 Option<Rc<dyn Curve>>,
240 Option<bool>,
241) -> CompleterFuture<()>;
242
243struct ViewportOffsetVTable {
246 add_listener: fn(&mut App, HandleId, Listener),
247 remove_listener: fn(&mut App, HandleId, &Listener),
248 pixels: fn(&App, HandleId) -> f64,
249 has_pixels: fn(&App, HandleId) -> bool,
250 apply_viewport_dimension: fn(&mut App, HandleId, f64) -> bool,
251 apply_content_dimensions: fn(&mut App, HandleId, f64, f64) -> bool,
252 correct_by: fn(&mut App, HandleId, f64),
253 jump_to: fn(&mut App, HandleId, f64),
254 animate_to: AnimateToFn,
255 move_to: MoveToFn,
256 user_scroll_direction: fn(&App, HandleId) -> ScrollDirection,
257 allow_implicit_scrolling: fn(&App, HandleId) -> bool,
258 debug_fill_description: fn(&App, HandleId, &mut Vec<String>),
259 type_name: fn() -> &'static str,
261}
262
263fn resolve<T: 'static>(id: HandleId) -> Handle<T> {
265 Handle::from_id(id)
266}
267
268impl ViewportOffsetVTable {
269 const fn of<T: ViewportOffset>() -> ViewportOffsetVTable {
270 ViewportOffsetVTable {
271 add_listener: |app, id, listener| {
272 ListenableObject::add_listener(resolve::<T>(id), app, listener)
273 },
274 remove_listener: |app, id, listener| {
275 ListenableObject::remove_listener(resolve::<T>(id), app, listener)
276 },
277 pixels: |app, id| T::pixels(resolve(id), app),
278 has_pixels: |app, id| T::has_pixels(resolve(id), app),
279 apply_viewport_dimension: |app, id, dimension| {
280 T::apply_viewport_dimension(resolve(id), app, dimension)
281 },
282 apply_content_dimensions: |app, id, min, max| {
283 T::apply_content_dimensions(resolve(id), app, min, max)
284 },
285 correct_by: |app, id, correction| T::correct_by(resolve(id), app, correction),
286 jump_to: |app, id, pixels| T::jump_to(resolve(id), app, pixels),
287 animate_to: |app, id, to, duration, curve| {
288 T::animate_to(resolve(id), app, to, duration, curve)
289 },
290 move_to: |app, id, to, duration, curve, clamp| {
291 T::move_to(resolve(id), app, to, duration, curve, clamp)
292 },
293 user_scroll_direction: |app, id| T::user_scroll_direction(resolve(id), app),
294 allow_implicit_scrolling: |app, id| T::allow_implicit_scrolling(resolve(id), app),
295 debug_fill_description: |app, id, description| {
296 T::debug_fill_description(resolve(id), app, description)
297 },
298 type_name: std::any::type_name::<T>,
299 }
300 }
301}
302
303#[derive(Clone, Copy)]
307pub struct AnyViewportOffset {
308 id: HandleId,
309 vtable: &'static ViewportOffsetVTable,
310}
311
312impl AnyViewportOffset {
313 pub fn to_string_short(self) -> String {
316 describe_identity(
317 bare_type_name((self.vtable.type_name)()),
318 self.id.identity_hash(),
319 )
320 }
321
322 pub fn to_string_with_app(self, app: &App) -> String {
325 let mut description = Vec::new();
326 (self.vtable.debug_fill_description)(app, self.id, &mut description);
327 format!("{}({})", self.to_string_short(), description.join(", "))
328 }
329
330 pub fn add_listener(self, app: &mut App, listener: Listener) {
332 (self.vtable.add_listener)(app, self.id, listener)
333 }
334
335 pub fn remove_listener(self, app: &mut App, listener: &Listener) {
337 (self.vtable.remove_listener)(app, self.id, listener)
338 }
339
340 pub fn pixels(self, app: &App) -> f64 {
342 (self.vtable.pixels)(app, self.id)
343 }
344
345 pub fn has_pixels(self, app: &App) -> bool {
347 (self.vtable.has_pixels)(app, self.id)
348 }
349
350 pub fn apply_viewport_dimension(self, app: &mut App, viewport_dimension: f64) -> bool {
352 (self.vtable.apply_viewport_dimension)(app, self.id, viewport_dimension)
353 }
354
355 pub fn apply_content_dimensions(
357 self,
358 app: &mut App,
359 min_scroll_extent: f64,
360 max_scroll_extent: f64,
361 ) -> bool {
362 (self.vtable.apply_content_dimensions)(app, self.id, min_scroll_extent, max_scroll_extent)
363 }
364
365 pub fn correct_by(self, app: &mut App, correction: f64) {
367 (self.vtable.correct_by)(app, self.id, correction)
368 }
369
370 pub fn jump_to(self, app: &mut App, pixels: f64) {
372 (self.vtable.jump_to)(app, self.id, pixels)
373 }
374
375 pub fn animate_to(
377 self,
378 app: &mut App,
379 to: f64,
380 duration: Duration,
381 curve: Rc<dyn Curve>,
382 ) -> CompleterFuture<()> {
383 (self.vtable.animate_to)(app, self.id, to, duration, curve)
384 }
385
386 pub fn move_to(
388 self,
389 app: &mut App,
390 to: f64,
391 duration: Option<Duration>,
392 curve: Option<Rc<dyn Curve>>,
393 clamp: Option<bool>,
394 ) -> CompleterFuture<()> {
395 (self.vtable.move_to)(app, self.id, to, duration, curve, clamp)
396 }
397
398 pub fn user_scroll_direction(self, app: &App) -> ScrollDirection {
400 (self.vtable.user_scroll_direction)(app, self.id)
401 }
402
403 pub fn allow_implicit_scrolling(self, app: &App) -> bool {
405 (self.vtable.allow_implicit_scrolling)(app, self.id)
406 }
407}
408
409impl Debug for AnyViewportOffset {
410 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
411 write!(f, "AnyViewportOffset({:?})", self.id)
412 }
413}
414
415impl ListenableHandle for AnyViewportOffset {
416 fn add_listener_to(self, app: &mut App, listener: Listener) {
417 (self.vtable.add_listener)(app, self.id, listener)
418 }
419
420 fn remove_listener_from(self, app: &mut App, listener: &Listener) {
421 (self.vtable.remove_listener)(app, self.id, listener)
422 }
423}
424
425pub type ViewportOffsetRef = Retained<AnyViewportOffset>;
427
428waft_foundation::erased_handle!(AnyViewportOffset: ViewportOffset => ViewportOffset::as_viewport_offset);
429
430#[derive(Debug, Default)]
436pub struct FixedViewportOffset {
437 change_notifier: ChangeNotifierData,
438 pixels: f64,
439}
440
441impl FixedViewportOffset {
442 pub fn new(app: &mut App, value: f64) -> Ref<FixedViewportOffset> {
446 app.create(FixedViewportOffset {
447 change_notifier: ChangeNotifierData::new(),
448 pixels: value,
449 })
450 }
451
452 pub fn zero(app: &mut App) -> Ref<FixedViewportOffset> {
456 FixedViewportOffset::new(app, 0.0)
457 }
458}
459
460impl ChangeNotifier for FixedViewportOffset {
461 fn change_notifier_data(&self) -> &ChangeNotifierData {
462 &self.change_notifier
463 }
464
465 fn change_notifier_data_mut(&mut self) -> &mut ChangeNotifierData {
466 &mut self.change_notifier
467 }
468}
469
470impl ViewportOffset for FixedViewportOffset {
471 fn pixels(self: Handle<Self>, app: &App) -> f64 {
472 app.get(self).pixels
473 }
474
475 fn has_pixels(self: Handle<Self>, _app: &App) -> bool {
476 true
477 }
478
479 fn apply_viewport_dimension(
480 self: Handle<Self>,
481 _app: &mut App,
482 _viewport_dimension: f64,
483 ) -> bool {
484 true
485 }
486
487 fn apply_content_dimensions(
488 self: Handle<Self>,
489 _app: &mut App,
490 _min_scroll_extent: f64,
491 _max_scroll_extent: f64,
492 ) -> bool {
493 true
494 }
495
496 fn correct_by(self: Handle<Self>, app: &mut App, correction: f64) {
497 app.get_mut(self).pixels += correction;
498 }
499
500 fn jump_to(self: Handle<Self>, _app: &mut App, _pixels: f64) {
501 }
503
504 fn animate_to(
505 self: Handle<Self>,
506 _app: &mut App,
507 _to: f64,
508 _duration: Duration,
509 _curve: Rc<dyn Curve>,
510 ) -> CompleterFuture<()> {
511 CompleterFuture::ready(())
512 }
513
514 fn user_scroll_direction(self: Handle<Self>, _app: &App) -> ScrollDirection {
515 ScrollDirection::Idle
516 }
517
518 fn allow_implicit_scrolling(self: Handle<Self>, _app: &App) -> bool {
519 false
520 }
521}
522
523#[cfg(test)]
524mod tests {
525 use super::*;
526 use waft_foundation::AppCell;
527
528 #[test]
529 fn a_fixed_offset_only_moves_by_correction() {
530 let cell = AppCell::new();
531 let mut app = cell.borrow_mut();
532 let offset = FixedViewportOffset::new(&mut app, 12.0);
533 assert_eq!(offset.pixels(&app), 12.0);
534 assert!(offset.has_pixels(&app));
535 offset.jump_to(&mut app, 40.0);
536 assert_eq!(offset.pixels(&app), 12.0);
537 offset.correct_by(&mut app, -5.0);
538 assert_eq!(offset.pixels(&app), 7.0);
539 assert_eq!(offset.user_scroll_direction(&app), ScrollDirection::Idle);
540 assert!(!offset.allow_implicit_scrolling(&app));
541 }
542
543 #[test]
544 fn the_erased_edge_dispatches_to_the_concrete_offset() {
545 let cell = AppCell::new();
546 let mut app = cell.borrow_mut();
547 let offset = FixedViewportOffset::zero(&mut app).as_viewport_offset();
548 assert_eq!(offset.pixels(&app), 0.0);
549 assert!(offset.apply_viewport_dimension(&mut app, 100.0));
550 assert!(offset.apply_content_dimensions(&mut app, 0.0, 200.0));
551 let moved = offset.move_to(&mut app, 30.0, None, None, None);
552 assert_eq!(offset.pixels(&app), 0.0);
553 assert!(
554 moved.is_completed(),
555 "a jump hands back an already completed future"
556 );
557 let animated = offset.animate_to(&mut app, 30.0, Duration::from_millis(10), Curves::ease());
558 assert!(
559 animated.is_completed(),
560 "a fixed offset's animation is over before it starts"
561 );
562 }
563}