use std::rc::Rc;
use std::time::Duration;
use waft_animation::{AnimationBehavior, AnimationController, Curve};
use waft_embedder::PointerDeviceKind;
use waft_foundation::{
App, Completer, CompleterFuture, Handle, HandleId, ListenableObject, Listener,
PRECISION_ERROR_TOLERANCE,
};
use waft_gestures::{DragEndDetails, DragObject, DragStartDetails, DragUpdateDetails};
use waft_painting::{AxisDirection, axis_direction_is_reversed};
use waft_physics::Simulation;
use waft_scheduler::TickerProvider;
use crate::framework::{BuildContext, Notification};
use crate::widgets::scroll_metrics::ScrollMetrics;
use crate::widgets::scroll_notification::{
OverscrollNotification, ScrollEndNotification, ScrollStartNotification,
ScrollUpdateNotification,
};
pub trait ScrollActivityDelegate: Sized + 'static {
fn axis_direction(self: Handle<Self>, app: &App) -> AxisDirection;
fn set_pixels(self: Handle<Self>, app: &mut App, pixels: f64) -> f64;
fn apply_user_offset(self: Handle<Self>, app: &mut App, delta: f64);
fn go_idle(self: Handle<Self>, app: &mut App);
fn go_ballistic(self: Handle<Self>, app: &mut App, velocity: f64);
fn as_scroll_activity_delegate(self: Handle<Self>) -> AnyScrollActivityDelegate {
AnyScrollActivityDelegate {
id: self.id(),
vtable: const { &ScrollActivityDelegateVTable::of::<Self>() },
}
}
}
struct ScrollActivityDelegateVTable {
axis_direction: fn(&App, HandleId) -> AxisDirection,
set_pixels: fn(&mut App, HandleId, f64) -> f64,
apply_user_offset: fn(&mut App, HandleId, f64),
go_idle: fn(&mut App, HandleId),
go_ballistic: fn(&mut App, HandleId, f64),
}
fn resolve<T: 'static>(id: HandleId) -> Handle<T> {
Handle::from_id(id)
}
impl ScrollActivityDelegateVTable {
const fn of<D: ScrollActivityDelegate>() -> ScrollActivityDelegateVTable {
ScrollActivityDelegateVTable {
axis_direction: |app, id| D::axis_direction(resolve(id), app),
set_pixels: |app, id, pixels| D::set_pixels(resolve(id), app, pixels),
apply_user_offset: |app, id, delta| D::apply_user_offset(resolve(id), app, delta),
go_idle: |app, id| D::go_idle(resolve(id), app),
go_ballistic: |app, id, velocity| D::go_ballistic(resolve(id), app, velocity),
}
}
}
#[derive(Clone, Copy)]
pub struct AnyScrollActivityDelegate {
id: HandleId,
vtable: &'static ScrollActivityDelegateVTable,
}
impl AnyScrollActivityDelegate {
pub fn axis_direction(self, app: &App) -> AxisDirection {
(self.vtable.axis_direction)(app, self.id)
}
pub fn set_pixels(self, app: &mut App, pixels: f64) -> f64 {
(self.vtable.set_pixels)(app, self.id, pixels)
}
pub fn apply_user_offset(self, app: &mut App, delta: f64) {
(self.vtable.apply_user_offset)(app, self.id, delta);
}
pub fn go_idle(self, app: &mut App) {
(self.vtable.go_idle)(app, self.id);
}
pub fn go_ballistic(self, app: &mut App, velocity: f64) {
(self.vtable.go_ballistic)(app, self.id, velocity);
}
}
impl PartialEq for AnyScrollActivityDelegate {
fn eq(&self, other: &AnyScrollActivityDelegate) -> bool {
self.id == other.id
}
}
impl Eq for AnyScrollActivityDelegate {}
pub struct ScrollActivityData {
delegate: AnyScrollActivityDelegate,
is_disposed: bool,
}
impl ScrollActivityData {
pub fn new(delegate: AnyScrollActivityDelegate) -> ScrollActivityData {
ScrollActivityData {
delegate,
is_disposed: false,
}
}
}
#[macro_export]
macro_rules! scroll_activity_accessors {
() => {
fn scroll_activity_data(
self: ::waft_foundation::Handle<Self>,
app: &::waft_foundation::App,
) -> &$crate::ScrollActivityData {
&app.get(self).scroll_activity
}
fn scroll_activity_data_mut(
self: ::waft_foundation::Handle<Self>,
app: &mut ::waft_foundation::App,
) -> &mut $crate::ScrollActivityData {
&mut app.get_mut(self).scroll_activity
}
};
}
pub trait ScrollActivity: Sized + 'static {
fn scroll_activity_data(self: Handle<Self>, app: &App) -> &ScrollActivityData;
fn scroll_activity_data_mut(self: Handle<Self>, app: &mut App) -> &mut ScrollActivityData;
fn delegate(self: Handle<Self>, app: &App) -> AnyScrollActivityDelegate {
self.scroll_activity_data(app).delegate
}
fn is_disposed(self: Handle<Self>, app: &App) -> bool {
self.scroll_activity_data(app).is_disposed
}
fn update_delegate(self: Handle<Self>, app: &mut App, value: AnyScrollActivityDelegate) {
debug_assert!(self.delegate(app) != value);
self.scroll_activity_data_mut(app).delegate = value;
}
fn reset_activity(self: Handle<Self>, app: &mut App) {
let _ = (self, app);
}
fn dispatch_scroll_start_notification(
self: Handle<Self>,
app: &mut App,
metrics: Rc<dyn ScrollMetrics>,
context: Option<BuildContext>,
) {
ScrollActivityBase::dispatch_scroll_start_notification(app, metrics, context);
}
fn dispatch_scroll_update_notification(
self: Handle<Self>,
app: &mut App,
metrics: Rc<dyn ScrollMetrics>,
context: BuildContext,
scroll_delta: f64,
) {
ScrollActivityBase::dispatch_scroll_update_notification(
app,
metrics,
context,
scroll_delta,
);
}
fn dispatch_overscroll_notification(
self: Handle<Self>,
app: &mut App,
metrics: Rc<dyn ScrollMetrics>,
context: BuildContext,
overscroll: f64,
) {
ScrollActivityBase::dispatch_overscroll_notification(app, metrics, context, overscroll);
}
fn dispatch_scroll_end_notification(
self: Handle<Self>,
app: &mut App,
metrics: Rc<dyn ScrollMetrics>,
context: BuildContext,
) {
ScrollActivityBase::dispatch_scroll_end_notification(app, metrics, context);
}
fn apply_new_dimensions(self: Handle<Self>, app: &mut App) {
let _ = (self, app);
}
fn should_ignore_pointer(self: Handle<Self>, app: &App) -> bool;
fn is_scrolling(self: Handle<Self>, app: &App) -> bool;
fn velocity(self: Handle<Self>, app: &mut App) -> f64;
fn dispose(self: Handle<Self>, app: &mut App) {
ScrollActivityBase::dispose(self, app);
}
fn describe(self: Handle<Self>, app: &App) -> String {
let _ = app;
describe_identity::<Self>(self.id())
}
fn as_hold_controller(self: Handle<Self>) -> Option<Rc<dyn ScrollHoldController>> {
let _ = self;
None
}
fn as_activity(self: Handle<Self>) -> AnyScrollActivity {
AnyScrollActivity {
id: self.id(),
vtable: const { &ScrollActivityVTable::of::<Self>() },
}
}
}
fn describe_identity<T: ?Sized>(id: HandleId) -> String {
let name = std::any::type_name::<T>();
let short = name.rsplit("::").next().unwrap_or(name);
format!("{short}#{id:?}")
}
pub struct ScrollActivityBase;
impl ScrollActivityBase {
pub fn dispatch_scroll_start_notification(
app: &mut App,
metrics: Rc<dyn ScrollMetrics>,
context: Option<BuildContext>,
) {
ScrollStartNotification::new(metrics, context).dispatch(app, context);
}
pub fn dispatch_scroll_update_notification(
app: &mut App,
metrics: Rc<dyn ScrollMetrics>,
context: BuildContext,
scroll_delta: f64,
) {
ScrollUpdateNotification::new(metrics, context)
.scroll_delta(scroll_delta)
.dispatch(app, Some(context));
}
pub fn dispatch_overscroll_notification(
app: &mut App,
metrics: Rc<dyn ScrollMetrics>,
context: BuildContext,
overscroll: f64,
) {
OverscrollNotification::new(metrics, context, overscroll).dispatch(app, Some(context));
}
pub fn dispatch_scroll_end_notification(
app: &mut App,
metrics: Rc<dyn ScrollMetrics>,
context: BuildContext,
) {
ScrollEndNotification::new(metrics, context).dispatch(app, Some(context));
}
pub fn dispose<A: ScrollActivity>(this: Handle<A>, app: &mut App) {
this.scroll_activity_data_mut(app).is_disposed = true;
}
}
type DispatchDeltaFn = fn(&mut App, HandleId, Rc<dyn ScrollMetrics>, BuildContext, f64);
struct ScrollActivityVTable {
delegate: fn(&App, HandleId) -> AnyScrollActivityDelegate,
is_disposed: fn(&App, HandleId) -> bool,
update_delegate: fn(&mut App, HandleId, AnyScrollActivityDelegate),
reset_activity: fn(&mut App, HandleId),
dispatch_scroll_start_notification:
fn(&mut App, HandleId, Rc<dyn ScrollMetrics>, Option<BuildContext>),
dispatch_scroll_update_notification: DispatchDeltaFn,
dispatch_overscroll_notification: DispatchDeltaFn,
dispatch_scroll_end_notification: fn(&mut App, HandleId, Rc<dyn ScrollMetrics>, BuildContext),
apply_new_dimensions: fn(&mut App, HandleId),
should_ignore_pointer: fn(&App, HandleId) -> bool,
is_scrolling: fn(&App, HandleId) -> bool,
velocity: fn(&mut App, HandleId) -> f64,
dispose: fn(&mut App, HandleId),
describe: fn(&App, HandleId) -> String,
as_hold_controller: fn(HandleId) -> Option<Rc<dyn ScrollHoldController>>,
}
impl ScrollActivityVTable {
const fn of<A: ScrollActivity>() -> ScrollActivityVTable {
ScrollActivityVTable {
delegate: |app, id| A::delegate(resolve(id), app),
is_disposed: |app, id| A::is_disposed(resolve(id), app),
update_delegate: |app, id, value| A::update_delegate(resolve(id), app, value),
reset_activity: |app, id| A::reset_activity(resolve(id), app),
dispatch_scroll_start_notification: |app, id, metrics, context| {
A::dispatch_scroll_start_notification(resolve(id), app, metrics, context);
},
dispatch_scroll_update_notification: |app, id, metrics, context, delta| {
A::dispatch_scroll_update_notification(resolve(id), app, metrics, context, delta);
},
dispatch_overscroll_notification: |app, id, metrics, context, overscroll| {
A::dispatch_overscroll_notification(resolve(id), app, metrics, context, overscroll);
},
dispatch_scroll_end_notification: |app, id, metrics, context| {
A::dispatch_scroll_end_notification(resolve(id), app, metrics, context);
},
apply_new_dimensions: |app, id| A::apply_new_dimensions(resolve(id), app),
should_ignore_pointer: |app, id| A::should_ignore_pointer(resolve(id), app),
is_scrolling: |app, id| A::is_scrolling(resolve(id), app),
velocity: |app, id| A::velocity(resolve(id), app),
dispose: |app, id| A::dispose(resolve(id), app),
describe: |app, id| A::describe(resolve(id), app),
as_hold_controller: |id| A::as_hold_controller(resolve(id)),
}
}
}
#[derive(Clone, Copy)]
pub struct AnyScrollActivity {
id: HandleId,
vtable: &'static ScrollActivityVTable,
}
impl AnyScrollActivity {
pub fn delegate(self, app: &App) -> AnyScrollActivityDelegate {
(self.vtable.delegate)(app, self.id)
}
pub fn is_disposed(self, app: &App) -> bool {
(self.vtable.is_disposed)(app, self.id)
}
pub fn update_delegate(self, app: &mut App, value: AnyScrollActivityDelegate) {
(self.vtable.update_delegate)(app, self.id, value);
}
pub fn reset_activity(self, app: &mut App) {
(self.vtable.reset_activity)(app, self.id);
}
pub fn dispatch_scroll_start_notification(
self,
app: &mut App,
metrics: Rc<dyn ScrollMetrics>,
context: Option<BuildContext>,
) {
(self.vtable.dispatch_scroll_start_notification)(app, self.id, metrics, context);
}
pub fn dispatch_scroll_update_notification(
self,
app: &mut App,
metrics: Rc<dyn ScrollMetrics>,
context: BuildContext,
scroll_delta: f64,
) {
(self.vtable.dispatch_scroll_update_notification)(
app,
self.id,
metrics,
context,
scroll_delta,
);
}
pub fn dispatch_overscroll_notification(
self,
app: &mut App,
metrics: Rc<dyn ScrollMetrics>,
context: BuildContext,
overscroll: f64,
) {
(self.vtable.dispatch_overscroll_notification)(app, self.id, metrics, context, overscroll);
}
pub fn dispatch_scroll_end_notification(
self,
app: &mut App,
metrics: Rc<dyn ScrollMetrics>,
context: BuildContext,
) {
(self.vtable.dispatch_scroll_end_notification)(app, self.id, metrics, context);
}
pub fn apply_new_dimensions(self, app: &mut App) {
(self.vtable.apply_new_dimensions)(app, self.id);
}
pub fn should_ignore_pointer(self, app: &App) -> bool {
(self.vtable.should_ignore_pointer)(app, self.id)
}
pub fn is_scrolling(self, app: &App) -> bool {
(self.vtable.is_scrolling)(app, self.id)
}
pub fn velocity(self, app: &mut App) -> f64 {
(self.vtable.velocity)(app, self.id)
}
pub fn dispose(self, app: &mut App) {
(self.vtable.dispose)(app, self.id);
}
pub fn describe(self, app: &App) -> String {
(self.vtable.describe)(app, self.id)
}
pub fn as_hold_controller(self) -> Option<Rc<dyn ScrollHoldController>> {
(self.vtable.as_hold_controller)(self.id)
}
}
impl PartialEq for AnyScrollActivity {
fn eq(&self, other: &AnyScrollActivity) -> bool {
self.id == other.id
}
}
impl Eq for AnyScrollActivity {}
pub struct IdleScrollActivity {
scroll_activity: ScrollActivityData,
}
impl IdleScrollActivity {
pub fn new(app: &mut App, delegate: AnyScrollActivityDelegate) -> Handle<IdleScrollActivity> {
app.create(IdleScrollActivity {
scroll_activity: ScrollActivityData::new(delegate),
})
}
}
impl ScrollActivity for IdleScrollActivity {
crate::scroll_activity_accessors!();
fn apply_new_dimensions(self: Handle<Self>, app: &mut App) {
self.delegate(app).go_ballistic(app, 0.0);
}
fn should_ignore_pointer(self: Handle<Self>, _app: &App) -> bool {
false
}
fn is_scrolling(self: Handle<Self>, _app: &App) -> bool {
false
}
fn velocity(self: Handle<Self>, _app: &mut App) -> f64 {
0.0
}
}
pub trait ScrollHoldController {
fn cancel(&self, app: &mut App);
}
pub trait ScrollHoldControllerObject: Sized + 'static {
fn cancel(self: Handle<Self>, app: &mut App);
}
impl<T: ScrollHoldControllerObject> ScrollHoldController for Handle<T> {
fn cancel(&self, app: &mut App) {
T::cancel(*self, app);
}
}
pub struct HoldScrollActivity {
scroll_activity: ScrollActivityData,
pub on_hold_canceled: Option<Listener>,
}
impl HoldScrollActivity {
pub fn new(
app: &mut App,
delegate: AnyScrollActivityDelegate,
on_hold_canceled: Option<Listener>,
) -> Handle<HoldScrollActivity> {
app.create(HoldScrollActivity {
scroll_activity: ScrollActivityData::new(delegate),
on_hold_canceled,
})
}
}
impl ScrollActivity for HoldScrollActivity {
crate::scroll_activity_accessors!();
fn should_ignore_pointer(self: Handle<Self>, _app: &App) -> bool {
false
}
fn is_scrolling(self: Handle<Self>, _app: &App) -> bool {
false
}
fn velocity(self: Handle<Self>, _app: &mut App) -> f64 {
0.0
}
fn dispose(self: Handle<Self>, app: &mut App) {
if let Some(on_hold_canceled) = app.get(self).on_hold_canceled.clone() {
on_hold_canceled.call(app);
}
ScrollActivityBase::dispose(self, app);
}
fn as_hold_controller(self: Handle<Self>) -> Option<Rc<dyn ScrollHoldController>> {
Some(Rc::new(self))
}
}
impl ScrollHoldControllerObject for HoldScrollActivity {
fn cancel(self: Handle<Self>, app: &mut App) {
self.delegate(app).go_ballistic(app, 0.0);
}
}
#[derive(Clone)]
pub enum LastDragDetails {
Start(DragStartDetails),
Update(DragUpdateDetails),
End(DragEndDetails),
}
pub struct ScrollDragController {
delegate: AnyScrollActivityDelegate,
pub on_drag_canceled: Option<Listener>,
pub carried_velocity: Option<f64>,
pub motion_start_distance_threshold: Option<f64>,
last_non_stationary_timestamp: Option<Duration>,
retain_momentum: bool,
offset_since_last_stop: Option<f64>,
kind: Option<PointerDeviceKind>,
last_details: Option<LastDragDetails>,
}
impl ScrollDragController {
pub const MOMENTUM_RETAIN_STATIONARY_DURATION_THRESHOLD: Duration = Duration::from_millis(20);
pub const MOMENTUM_RETAIN_VELOCITY_THRESHOLD_FACTOR: f64 = 0.5;
pub const MOTION_STOPPED_DURATION_THRESHOLD: Duration = Duration::from_millis(50);
const BIG_THRESHOLD_BREAK_DISTANCE: f64 = 24.0;
pub fn new(
app: &mut App,
delegate: AnyScrollActivityDelegate,
details: DragStartDetails,
on_drag_canceled: Option<Listener>,
carried_velocity: Option<f64>,
motion_start_distance_threshold: Option<f64>,
) -> Handle<ScrollDragController> {
debug_assert!(
motion_start_distance_threshold.is_none_or(|threshold| threshold > 0.0),
"motionStartDistanceThreshold must be a positive number or null"
);
app.create(ScrollDragController {
delegate,
on_drag_canceled,
carried_velocity,
motion_start_distance_threshold,
last_non_stationary_timestamp: details.source_time_stamp,
retain_momentum: carried_velocity.is_some_and(|velocity| velocity != 0.0),
offset_since_last_stop: motion_start_distance_threshold.map(|_| 0.0),
kind: details.kind,
last_details: Some(LastDragDetails::Start(details)),
})
}
pub fn delegate(self: Handle<Self>, app: &App) -> AnyScrollActivityDelegate {
app.get(self).delegate
}
pub fn kind(self: Handle<Self>, app: &App) -> Option<PointerDeviceKind> {
app.get(self).kind
}
pub fn last_details(self: Handle<Self>, app: &App) -> Option<&LastDragDetails> {
app.get(self).last_details.as_ref()
}
pub fn update_delegate(self: Handle<Self>, app: &mut App, value: AnyScrollActivityDelegate) {
debug_assert!(app.get(self).delegate != value);
app.get_mut(self).delegate = value;
}
pub fn dispose(self: Handle<Self>, app: &mut App) {
app.get_mut(self).last_details = None;
if let Some(on_drag_canceled) = app.get(self).on_drag_canceled.clone() {
on_drag_canceled.call(app);
}
}
pub fn describe(self: Handle<Self>) -> String {
describe_identity::<ScrollDragController>(self.id())
}
fn reversed(self: Handle<Self>, app: &App) -> bool {
axis_direction_is_reversed(self.delegate(app).axis_direction(app))
}
fn maybe_lose_momentum(
self: Handle<Self>,
app: &mut App,
offset: f64,
timestamp: Option<Duration>,
) {
let controller = app.get(self);
if controller.retain_momentum
&& offset == 0.0
&& timestamp.is_none_or(|timestamp| {
timestamp.saturating_sub(
controller
.last_non_stationary_timestamp
.expect("a retained momentum carries a timestamp"),
) > Self::MOMENTUM_RETAIN_STATIONARY_DURATION_THRESHOLD
})
{
app.get_mut(self).retain_momentum = false;
}
}
fn adjust_for_scroll_start_threshold(
self: Handle<Self>,
app: &mut App,
offset: f64,
timestamp: Option<Duration>,
) -> f64 {
let Some(timestamp) = timestamp else {
return offset;
};
let controller = app.get(self);
let threshold = controller.motion_start_distance_threshold;
if offset == 0.0 {
if threshold.is_some()
&& controller.offset_since_last_stop.is_none()
&& timestamp.saturating_sub(
controller
.last_non_stationary_timestamp
.expect("a stationary update follows a timestamped one"),
) > Self::MOTION_STOPPED_DURATION_THRESHOLD
{
app.get_mut(self).offset_since_last_stop = Some(0.0);
}
0.0
} else {
let Some(offset_since_last_stop) = controller.offset_since_last_stop else {
return offset;
};
let offset_since_last_stop = offset_since_last_stop + offset;
app.get_mut(self).offset_since_last_stop = Some(offset_since_last_stop);
let threshold = threshold.expect("an offset since the last stop implies a threshold");
if offset_since_last_stop.abs() > threshold {
app.get_mut(self).offset_since_last_stop = None;
if offset.abs() > Self::BIG_THRESHOLD_BREAK_DISTANCE {
offset
} else {
(threshold / 3.0).min(offset.abs()) * offset.signum()
}
} else {
0.0
}
}
}
}
impl DragObject for ScrollDragController {
fn update(self: Handle<Self>, app: &mut App, details: DragUpdateDetails) {
debug_assert!(details.primary_delta.is_some());
let mut offset = details.primary_delta.expect("a primary delta");
let source_time_stamp = details.source_time_stamp;
app.get_mut(self).last_details = Some(LastDragDetails::Update(details));
if offset != 0.0 {
app.get_mut(self).last_non_stationary_timestamp = source_time_stamp;
}
self.maybe_lose_momentum(app, offset, source_time_stamp);
offset = self.adjust_for_scroll_start_threshold(app, offset, source_time_stamp);
if offset == 0.0 {
return;
}
if self.reversed(app) {
offset = -offset;
}
self.delegate(app).apply_user_offset(app, offset);
}
fn end(self: Handle<Self>, app: &mut App, details: DragEndDetails) {
debug_assert!(details.primary_velocity.is_some());
let mut velocity = -details.primary_velocity.expect("a primary velocity");
if self.reversed(app) {
velocity = -velocity;
}
app.get_mut(self).last_details = Some(LastDragDetails::End(details));
let controller = app.get(self);
if controller.retain_momentum {
let carried_velocity = controller
.carried_velocity
.expect("a retained momentum carries a velocity");
let is_flinging_in_same_direction = velocity.signum() == carried_velocity.signum();
let is_velocity_not_substantially_less_than_carried_momentum = velocity.abs()
> carried_velocity.abs() * Self::MOMENTUM_RETAIN_VELOCITY_THRESHOLD_FACTOR;
if is_flinging_in_same_direction
&& is_velocity_not_substantially_less_than_carried_momentum
{
velocity += carried_velocity;
}
}
self.delegate(app).go_ballistic(app, velocity);
}
fn cancel(self: Handle<Self>, app: &mut App) {
self.delegate(app).go_ballistic(app, 0.0);
}
}
pub struct DragScrollActivity {
scroll_activity: ScrollActivityData,
controller: Option<Handle<ScrollDragController>>,
}
impl DragScrollActivity {
pub fn new(
app: &mut App,
delegate: AnyScrollActivityDelegate,
controller: Handle<ScrollDragController>,
) -> Handle<DragScrollActivity> {
app.create(DragScrollActivity {
scroll_activity: ScrollActivityData::new(delegate),
controller: Some(controller),
})
}
fn controller(self: Handle<Self>, app: &App) -> Handle<ScrollDragController> {
app.get(self)
.controller
.expect("the drag activity outlives its controller")
}
}
impl ScrollActivity for DragScrollActivity {
crate::scroll_activity_accessors!();
fn dispatch_scroll_start_notification(
self: Handle<Self>,
app: &mut App,
metrics: Rc<dyn ScrollMetrics>,
context: Option<BuildContext>,
) {
let last_details = self.controller(app).last_details(app).cloned();
let Some(LastDragDetails::Start(drag_details)) = last_details else {
unreachable!("a drag activity starts with its start details");
};
ScrollStartNotification::new(metrics, context)
.drag_details(drag_details)
.dispatch(app, context);
}
fn dispatch_scroll_update_notification(
self: Handle<Self>,
app: &mut App,
metrics: Rc<dyn ScrollMetrics>,
context: BuildContext,
scroll_delta: f64,
) {
let last_details = self.controller(app).last_details(app).cloned();
let Some(LastDragDetails::Update(drag_details)) = last_details else {
unreachable!("a drag update carries its update details");
};
ScrollUpdateNotification::new(metrics, context)
.scroll_delta(scroll_delta)
.drag_details(drag_details)
.dispatch(app, Some(context));
}
fn dispatch_overscroll_notification(
self: Handle<Self>,
app: &mut App,
metrics: Rc<dyn ScrollMetrics>,
context: BuildContext,
overscroll: f64,
) {
let last_details = self.controller(app).last_details(app).cloned();
let Some(LastDragDetails::Update(drag_details)) = last_details else {
unreachable!("an overscroll follows an update");
};
OverscrollNotification::new(metrics, context, overscroll)
.drag_details(drag_details)
.dispatch(app, Some(context));
}
fn dispatch_scroll_end_notification(
self: Handle<Self>,
app: &mut App,
metrics: Rc<dyn ScrollMetrics>,
context: BuildContext,
) {
let last_details = self.controller(app).last_details(app).cloned();
let mut notification = ScrollEndNotification::new(metrics, context);
if let Some(LastDragDetails::End(drag_details)) = last_details {
notification = notification.drag_details(drag_details);
}
notification.dispatch(app, Some(context));
}
fn should_ignore_pointer(self: Handle<Self>, app: &App) -> bool {
app.get(self)
.controller
.and_then(|controller| controller.kind(app))
!= Some(PointerDeviceKind::Trackpad)
}
fn is_scrolling(self: Handle<Self>, _app: &App) -> bool {
true
}
fn velocity(self: Handle<Self>, _app: &mut App) -> f64 {
0.0
}
fn dispose(self: Handle<Self>, app: &mut App) {
app.get_mut(self).controller = None;
ScrollActivityBase::dispose(self, app);
}
fn describe(self: Handle<Self>, app: &App) -> String {
let controller = app.get(self).controller;
let described =
controller.map_or_else(|| "null".to_string(), ScrollDragController::describe);
format!(
"{}({described})",
describe_identity::<DragScrollActivity>(self.id())
)
}
}
pub struct BallisticScrollActivity {
scroll_activity: ScrollActivityData,
controller: Handle<AnimationController>,
should_ignore_pointer: bool,
}
impl BallisticScrollActivity {
pub fn new(
app: &mut App,
delegate: AnyScrollActivityDelegate,
simulation: Box<dyn Simulation>,
vsync: impl TickerProvider,
should_ignore_pointer: bool,
) -> Handle<BallisticScrollActivity> {
let controller = AnimationController::create_unbounded(
app,
0.0,
None,
None,
AnimationBehavior::Preserve,
vsync,
);
let this = app.create(BallisticScrollActivity {
scroll_activity: ScrollActivityData::new(delegate),
controller,
should_ignore_pointer,
});
controller.add_listener(app, Listener::handle_method(this, Self::tick));
let done = controller.animate_with(app, simulation);
done.when_complete(app, Listener::handle_method(this, Self::end));
this
}
fn tick(self: Handle<Self>, app: &mut App) {
let value = app.get(self).controller.value(app);
if !self.apply_move_to(app, value) {
self.delegate(app).go_idle(app);
}
}
pub fn apply_move_to(self: Handle<Self>, app: &mut App, value: f64) -> bool {
self.delegate(app).set_pixels(app, value).abs() < PRECISION_ERROR_TOLERANCE
}
fn end(self: Handle<Self>, app: &mut App) {
if !self.is_disposed(app) {
self.delegate(app).go_ballistic(app, 0.0);
}
}
}
impl ScrollActivity for BallisticScrollActivity {
crate::scroll_activity_accessors!();
fn reset_activity(self: Handle<Self>, app: &mut App) {
let velocity = self.velocity(app);
self.delegate(app).go_ballistic(app, velocity);
}
fn apply_new_dimensions(self: Handle<Self>, app: &mut App) {
let velocity = self.velocity(app);
self.delegate(app).go_ballistic(app, velocity);
}
fn dispatch_overscroll_notification(
self: Handle<Self>,
app: &mut App,
metrics: Rc<dyn ScrollMetrics>,
context: BuildContext,
overscroll: f64,
) {
let velocity = self.velocity(app);
OverscrollNotification::new(metrics, context, overscroll)
.velocity(velocity)
.dispatch(app, Some(context));
}
fn should_ignore_pointer(self: Handle<Self>, app: &App) -> bool {
app.get(self).should_ignore_pointer
}
fn is_scrolling(self: Handle<Self>, _app: &App) -> bool {
true
}
fn velocity(self: Handle<Self>, app: &mut App) -> f64 {
app.get(self).controller.velocity(app)
}
fn dispose(self: Handle<Self>, app: &mut App) {
app.get(self).controller.dispose(app);
ScrollActivityBase::dispose(self, app);
}
fn describe(self: Handle<Self>, app: &App) -> String {
format!(
"{}({:?})",
describe_identity::<BallisticScrollActivity>(self.id()),
app.get(self).controller.id()
)
}
}
pub struct DrivenScrollActivity {
scroll_activity: ScrollActivityData,
completer: Completer<()>,
controller: Handle<AnimationController>,
}
impl DrivenScrollActivity {
pub fn new(
app: &mut App,
delegate: AnyScrollActivityDelegate,
from: f64,
to: f64,
duration: Duration,
curve: Rc<dyn Curve>,
vsync: impl TickerProvider,
) -> Handle<DrivenScrollActivity> {
debug_assert!(duration > Duration::ZERO);
let completer = Completer::new();
let controller = AnimationController::create_unbounded(
app,
from,
None,
None,
AnimationBehavior::Preserve,
vsync,
);
let this = app.create(DrivenScrollActivity {
scroll_activity: ScrollActivityData::new(delegate),
completer,
controller,
});
controller.add_listener(app, Listener::handle_method(this, Self::tick));
let done = controller.animate_to(app, to, Some(duration), curve);
done.when_complete(app, Listener::handle_method(this, Self::end));
this
}
pub fn simulation(
app: &mut App,
delegate: AnyScrollActivityDelegate,
simulation: Box<dyn Simulation>,
vsync: impl TickerProvider,
) -> Handle<DrivenScrollActivity> {
let completer = Completer::new();
let controller = AnimationController::create_unbounded(
app,
0.0,
None,
None,
AnimationBehavior::Preserve,
vsync,
);
let this = app.create(DrivenScrollActivity {
scroll_activity: ScrollActivityData::new(delegate),
completer,
controller,
});
controller.add_listener(app, Listener::handle_method(this, Self::tick));
let done = controller.animate_with(app, simulation);
done.when_complete(app, Listener::handle_method(this, Self::end));
this
}
pub fn done(self: Handle<Self>, app: &App) -> CompleterFuture<()> {
app.get(self).completer.future()
}
fn tick(self: Handle<Self>, app: &mut App) {
let value = app.get(self).controller.value(app);
if !self.apply_move_to(app, value) {
self.delegate(app).go_idle(app);
}
}
pub fn apply_move_to(self: Handle<Self>, app: &mut App, value: f64) -> bool {
self.delegate(app).set_pixels(app, value).abs() < PRECISION_ERROR_TOLERANCE
}
fn end(self: Handle<Self>, app: &mut App) {
if !self.is_disposed(app) {
let velocity = self.velocity(app);
self.delegate(app).go_ballistic(app, velocity);
}
}
}
impl ScrollActivity for DrivenScrollActivity {
crate::scroll_activity_accessors!();
fn dispatch_overscroll_notification(
self: Handle<Self>,
app: &mut App,
metrics: Rc<dyn ScrollMetrics>,
context: BuildContext,
overscroll: f64,
) {
let velocity = self.velocity(app);
OverscrollNotification::new(metrics, context, overscroll)
.velocity(velocity)
.dispatch(app, Some(context));
}
fn should_ignore_pointer(self: Handle<Self>, _app: &App) -> bool {
true
}
fn is_scrolling(self: Handle<Self>, _app: &App) -> bool {
true
}
fn velocity(self: Handle<Self>, app: &mut App) -> f64 {
app.get(self).controller.velocity(app)
}
fn dispose(self: Handle<Self>, app: &mut App) {
let completer = app.get(self).completer.clone();
completer.complete(app, ());
app.get(self).controller.dispose(app);
ScrollActivityBase::dispose(self, app);
}
fn describe(self: Handle<Self>, app: &App) -> String {
format!(
"{}({:?})",
describe_identity::<DrivenScrollActivity>(self.id()),
app.get(self).controller.id()
)
}
}