use cranpose_foundation::{ScrollId, lazy::LazyListState};
use cranpose_ui_layout::Axis;
use crate::{
draggable::{DraggableState, ScrollableState},
nested_scroll::NestedScrollSource,
scroll::{ScrollSettlePolicy, ScrollState},
};
pub(super) trait ScrollTarget: Clone {
fn apply_delta(&self, delta: f32) -> f32;
fn apply_wheel_delta(&self, delta: f32) -> f32 {
self.apply_delta(delta)
}
fn apply_fling_delta(&self, delta: f32) -> f32;
fn invalidate(&self);
fn current_offset(&self) -> f32;
fn can_scroll(&self) -> bool {
true
}
fn can_consume(&self, gesture_delta: f32) -> bool {
let _ = gesture_delta;
self.can_scroll()
}
fn settle_policy(&self) -> Option<ScrollSettlePolicy> {
None
}
fn allows_fling(&self) -> bool {
true
}
fn set_dragging(&self, dragging: bool) {
let _ = dragging;
}
fn stop_animation(&self) -> bool {
false
}
fn pre_fling(&self, velocity: f32) -> f32 {
let _ = velocity;
0.0
}
fn start_scroll(&self, _stop: impl FnOnce() + 'static) -> Option<ScrollId> {
None
}
fn end_scroll(&self, _id: ScrollId) {}
}
#[derive(Clone)]
pub(super) struct NestedTarget<S> {
pub(super) target: S,
pub(super) parent: crate::nested_scroll::NestedScrollParent,
pub(super) axis: Axis,
}
impl<S: ScrollTarget> ScrollTarget for NestedTarget<S> {
fn apply_delta(&self, delta: f32) -> f32 {
self.parent
.dispatch(self.axis, delta, NestedScrollSource::Drag, |left| {
self.target.apply_delta(left)
})
}
fn apply_wheel_delta(&self, delta: f32) -> f32 {
self.parent
.dispatch(self.axis, delta, NestedScrollSource::Wheel, |left| {
self.target.apply_wheel_delta(left)
})
}
fn apply_fling_delta(&self, delta: f32) -> f32 {
-self
.parent
.dispatch(self.axis, -delta, NestedScrollSource::Fling, |left| {
-self.target.apply_fling_delta(-left)
})
}
fn invalidate(&self) {
self.target.invalidate();
}
fn current_offset(&self) -> f32 {
self.target.current_offset()
}
fn can_scroll(&self) -> bool {
self.target.can_scroll()
}
fn can_consume(&self, gesture_delta: f32) -> bool {
self.target.can_consume(gesture_delta)
}
fn settle_policy(&self) -> Option<ScrollSettlePolicy> {
self.target.settle_policy()
}
fn allows_fling(&self) -> bool {
self.target.allows_fling()
}
fn set_dragging(&self, dragging: bool) {
self.target.set_dragging(dragging);
}
fn stop_animation(&self) -> bool {
self.target.stop_animation()
}
fn pre_fling(&self, velocity: f32) -> f32 {
self.parent.pre_fling(self.axis, velocity)
}
fn start_scroll(&self, stop: impl FnOnce() + 'static) -> Option<ScrollId> {
self.target.start_scroll(stop)
}
fn end_scroll(&self, id: ScrollId) {
self.target.end_scroll(id);
}
}
impl ScrollTarget for ScrollState {
fn apply_delta(&self, delta: f32) -> f32 {
-self.dispatch_raw_delta(-delta)
}
fn apply_fling_delta(&self, delta: f32) -> f32 {
self.dispatch_raw_delta(delta)
}
fn invalidate(&self) {}
fn current_offset(&self) -> f32 {
self.value()
}
fn can_scroll(&self) -> bool {
self.max_value() > 0.0
}
fn can_consume(&self, gesture_delta: f32) -> bool {
let raw = -gesture_delta;
if raw > 0.0 {
self.value_non_reactive() < self.max_value()
} else {
self.value_non_reactive() > 0.0
}
}
fn settle_policy(&self) -> Option<ScrollSettlePolicy> {
Self::settle_policy(self)
}
fn stop_animation(&self) -> bool {
self.stop_scroll_animation()
}
fn start_scroll(&self, stop: impl FnOnce() + 'static) -> Option<ScrollId> {
Some(self.start_scroll_animation(stop))
}
fn end_scroll(&self, id: ScrollId) {
self.end_scroll_animation(id);
}
}
impl ScrollTarget for LazyListState {
fn apply_delta(&self, delta: f32) -> f32 {
self.dispatch_scroll_delta(delta)
}
fn apply_wheel_delta(&self, delta: f32) -> f32 {
if delta.abs() <= 0.001 {
0.0
} else {
self.dispatch_scroll_delta(delta)
}
}
fn apply_fling_delta(&self, delta: f32) -> f32 {
-self.dispatch_scroll_delta(-delta)
}
fn invalidate(&self) {}
fn current_offset(&self) -> f32 {
self.first_visible_item_scroll_offset()
}
fn can_scroll(&self) -> bool {
self.total_items_count() == 0
|| self.can_scroll_forward_non_reactive()
|| self.can_scroll_backward_non_reactive()
}
fn can_consume(&self, gesture_delta: f32) -> bool {
if self.total_items_count() == 0 {
return true;
}
if gesture_delta < 0.0 {
self.can_scroll_forward_non_reactive()
} else {
self.can_scroll_backward_non_reactive()
}
}
fn settle_policy(&self) -> Option<ScrollSettlePolicy> {
Self::settle_policy(self)
}
fn set_dragging(&self, dragging: bool) {
self.set_dragged(dragging);
}
fn stop_animation(&self) -> bool {
self.stop_scroll_animation()
}
fn start_scroll(&self, stop: impl FnOnce() + 'static) -> Option<ScrollId> {
self.start_scroll_animation(stop)
}
fn end_scroll(&self, id: ScrollId) {
self.end_scroll_animation(id);
}
}
impl ScrollTarget for DraggableState {
fn apply_delta(&self, delta: f32) -> f32 {
self.drag_by(delta);
delta
}
fn apply_fling_delta(&self, delta: f32) -> f32 {
self.drag_by(delta);
delta
}
fn invalidate(&self) {}
fn current_offset(&self) -> f32 {
self.offset()
}
fn allows_fling(&self) -> bool {
false
}
fn set_dragging(&self, dragging: bool) {
Self::set_dragging(self, dragging);
}
}
impl ScrollTarget for ScrollableState {
fn apply_delta(&self, delta: f32) -> f32 {
self.dispatch_raw_delta(delta)
}
fn apply_fling_delta(&self, delta: f32) -> f32 {
-self.dispatch_raw_delta(-delta)
}
fn invalidate(&self) {}
fn current_offset(&self) -> f32 {
-self.offset()
}
fn set_dragging(&self, dragging: bool) {
Self::set_dragging(self, dragging);
}
}