use std::any::Any;
use std::fmt::{self, Debug};
use std::rc::Rc;
use std::time::Duration;
use waft_animation::Curves;
use waft_embedder::{Clip, Offset, Rect, Size};
use waft_foundation::{App, Handle, Listener, PRECISION_ERROR_TOLERANCE, Task};
use waft_painting::{Axis, AxisDirection, axis_direction_to_axis, transform_rect};
use crate::framework::{BuildContext, State};
use crate::widgets::actions::{Action, ActionData, ContextAction, Intent};
use crate::widgets::primary_scroll_controller::PrimaryScrollController;
use crate::widgets::scroll_context::ScrollContext;
use crate::widgets::scroll_controller::AnyScrollController;
use crate::widgets::scroll_metrics::ScrollMetrics;
use crate::widgets::scroll_physics::ScrollPhysicsRef;
use crate::widgets::scrollable::{Scrollable, ScrollableState};
#[derive(Clone)]
pub struct ScrollableDetails {
pub direction: AxisDirection,
pub controller: Option<AnyScrollController>,
pub physics: Option<ScrollPhysicsRef>,
pub decoration_clip_behavior: Option<Clip>,
}
impl ScrollableDetails {
pub fn new(direction: AxisDirection) -> ScrollableDetails {
ScrollableDetails {
direction,
controller: None,
physics: None,
decoration_clip_behavior: None,
}
}
pub fn vertical(reverse: bool) -> ScrollableDetails {
ScrollableDetails::new(if reverse {
AxisDirection::Up
} else {
AxisDirection::Down
})
}
pub fn horizontal(reverse: bool) -> ScrollableDetails {
ScrollableDetails::new(if reverse {
AxisDirection::Left
} else {
AxisDirection::Right
})
}
pub fn controller(mut self, controller: AnyScrollController) -> ScrollableDetails {
self.controller = Some(controller);
self
}
pub fn physics(mut self, physics: ScrollPhysicsRef) -> ScrollableDetails {
self.physics = Some(physics);
self
}
pub fn decoration_clip_behavior(mut self, decoration_clip_behavior: Clip) -> ScrollableDetails {
self.decoration_clip_behavior = Some(decoration_clip_behavior);
self
}
#[deprecated(
note = "Migrate to decoration_clip_behavior. This property was deprecated so that its \
application is clearer. This clip applies to decorators, and does not directly \
clip a scroll view. This feature was deprecated after v3.9.0-1.0.pre."
)]
pub fn clip_behavior(self, clip_behavior: Clip) -> ScrollableDetails {
self.decoration_clip_behavior(clip_behavior)
}
pub fn copy_with(&self) -> ScrollableDetails {
self.clone()
}
}
impl PartialEq for ScrollableDetails {
fn eq(&self, other: &ScrollableDetails) -> bool {
self.direction == other.direction
&& self.controller == other.controller
&& match (&self.physics, &other.physics) {
(None, None) => true,
(Some(physics), Some(other)) => Rc::ptr_eq(physics, other),
_ => false,
}
&& self.decoration_clip_behavior == other.decoration_clip_behavior
}
}
impl Debug for ScrollableDetails {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut description = vec![format!("axisDirection: {:?}", self.direction)];
if self.controller.is_some() {
description.push("scroll controller: attached".to_string());
}
if let Some(physics) = &self.physics {
description.push(format!("scroll physics: {physics:?}"));
}
if let Some(clip) = self.decoration_clip_behavior {
description.push(format!("decorationClipBehavior: {clip:?}"));
}
write!(f, "ScrollableDetails({})", description.join(", "))
}
}
pub struct EdgeDraggingAutoScroller {
pub scrollable: Handle<ScrollableState>,
pub on_scroll_view_scrolled: Option<Listener>,
pub velocity_scalar: f64,
drag_target_related_to_scroll_origin: Option<Rect>,
scrolling: bool,
}
impl EdgeDraggingAutoScroller {
pub fn new(
app: &mut App,
scrollable: Handle<ScrollableState>,
on_scroll_view_scrolled: Option<Listener>,
velocity_scalar: f64,
) -> Handle<EdgeDraggingAutoScroller> {
app.create(EdgeDraggingAutoScroller {
scrollable,
on_scroll_view_scrolled,
velocity_scalar,
drag_target_related_to_scroll_origin: None,
scrolling: false,
})
}
pub fn scrolling(self: Handle<Self>, app: &App) -> bool {
app.get(self).scrolling
}
fn offset_extent(offset: Offset, scroll_direction: Axis) -> f64 {
match scroll_direction {
Axis::Horizontal => offset.dx(),
Axis::Vertical => offset.dy(),
}
}
fn size_extent(size: Size, scroll_direction: Axis) -> f64 {
match scroll_direction {
Axis::Horizontal => size.width(),
Axis::Vertical => size.height(),
}
}
fn axis_direction(self: Handle<Self>, app: &App) -> AxisDirection {
app.get(self).scrollable.axis_direction(app)
}
fn scroll_direction(self: Handle<Self>, app: &App) -> Axis {
axis_direction_to_axis(self.axis_direction(app))
}
pub fn start_auto_scroll_if_necessary(self: Handle<Self>, app: &mut App, drag_target: Rect) {
let scrollable = app.get(self).scrollable;
let physics = scrollable.resolved_physics(app);
if let Some(physics) = physics
&& !physics.should_accept_user_offset(&scrollable.position(app).copy_with(app))
{
self.stop_auto_scroll(app);
return;
}
let delta_to_origin = scrollable.delta_to_scroll_origin(app);
app.get_mut(self).drag_target_related_to_scroll_origin =
Some(drag_target.translate(delta_to_origin.dx(), delta_to_origin.dy()));
if app.get(self).scrolling {
return;
}
debug_assert!(!app.get(self).scrolling);
drop(self.scroll(app));
}
pub fn stop_auto_scroll(self: Handle<Self>, app: &mut App) {
app.get_mut(self).scrolling = false;
}
fn scroll(self: Handle<Self>, app: &mut App) -> Task<()> {
let scrollable = app.get(self).scrollable;
let scroll_render_box = scrollable
.context(app)
.find_render_object(app)
.and_then(|object| object.as_box())
.expect("a Scrollable's render object is a RenderBox");
let transform = scroll_render_box.as_object().get_transform_to(app, None);
let size = scroll_render_box.size(app);
let global_rect = transform_rect(
&transform,
Rect::from_ltwh(0.0, 0.0, size.width(), size.height()),
);
let drag_target_related_to_scroll_origin = app
.get(self)
.drag_target_related_to_scroll_origin
.expect("startAutoScrollIfNecessary sets the drag target before a scroll");
let transformed_drag_target =
transform_rect(&transform, drag_target_related_to_scroll_origin);
debug_assert!(
(global_rect.size().width() + PRECISION_ERROR_TOLERANCE)
>= transformed_drag_target.size().width()
&& (global_rect.size().height() + PRECISION_ERROR_TOLERANCE)
>= transformed_drag_target.size().height(),
"Drag target size is larger than scrollable size, which may cause bouncing"
);
app.get_mut(self).scrolling = true;
let mut new_offset: Option<f64> = None;
const OVER_DRAG_MAX: f64 = 20.0;
let delta_to_origin = scrollable.delta_to_scroll_origin(app);
let viewport_origin = global_rect
.top_left()
.translate(delta_to_origin.dx(), delta_to_origin.dy());
let scroll_direction = self.scroll_direction(app);
let viewport_start = Self::offset_extent(viewport_origin, scroll_direction);
let viewport_end = viewport_start + Self::size_extent(global_rect.size(), scroll_direction);
let proxy_start = Self::offset_extent(
drag_target_related_to_scroll_origin.top_left(),
scroll_direction,
);
let proxy_end = Self::offset_extent(
drag_target_related_to_scroll_origin.bottom_right(),
scroll_direction,
);
let position = scrollable.position(app);
let pixels = position.pixels(app);
let (min_scroll_extent, max_scroll_extent) = (
position.min_scroll_extent(app),
position.max_scroll_extent(app),
);
match self.axis_direction(app) {
AxisDirection::Up | AxisDirection::Left => {
if proxy_end > viewport_end && pixels > min_scroll_extent {
let over_drag = (proxy_end - viewport_end).min(OVER_DRAG_MAX);
new_offset = Some(min_scroll_extent.max(pixels - over_drag));
} else if proxy_start < viewport_start && pixels < max_scroll_extent {
let over_drag = (viewport_start - proxy_start).min(OVER_DRAG_MAX);
new_offset = Some(max_scroll_extent.min(pixels + over_drag));
}
}
AxisDirection::Right | AxisDirection::Down => {
if proxy_start < viewport_start && pixels > min_scroll_extent {
let over_drag = (viewport_start - proxy_start).min(OVER_DRAG_MAX);
new_offset = Some(min_scroll_extent.max(pixels - over_drag));
} else if proxy_end > viewport_end && pixels < max_scroll_extent {
let over_drag = (proxy_end - viewport_end).min(OVER_DRAG_MAX);
new_offset = Some(max_scroll_extent.min(pixels + over_drag));
}
}
}
let Some(new_offset) = new_offset.filter(|new_offset| (new_offset - pixels).abs() >= 1.0)
else {
app.get_mut(self).scrolling = false;
return Task::ready(());
};
let velocity_scalar = app.get(self).velocity_scalar;
let duration = Duration::from_millis((1000.0 / velocity_scalar).round() as u64);
let animated = position.animate_to(app, new_offset, duration, Curves::linear());
let retained = app.retain(self);
app.spawn(async move |cx| {
let _retained = retained;
animated.await;
let scrolling = cx.update(|app| {
if let Some(on_scroll_view_scrolled) = app.get(self).on_scroll_view_scrolled.clone()
{
on_scroll_view_scrolled.call(app);
}
app.get(self).scrolling
});
if scrolling {
cx.update(|app| self.scroll(app)).await;
}
})
}
}
pub type ScrollIncrementCalculator = Rc<dyn Fn(&ScrollIncrementDetails) -> f64>;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ScrollIncrementType {
Line,
Page,
}
pub struct ScrollIncrementDetails {
pub r#type: ScrollIncrementType,
pub metrics: Rc<dyn ScrollMetrics>,
}
impl ScrollIncrementDetails {
pub fn new(
r#type: ScrollIncrementType,
metrics: Rc<dyn ScrollMetrics>,
) -> ScrollIncrementDetails {
ScrollIncrementDetails { r#type, metrics }
}
}
impl Debug for ScrollIncrementDetails {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ScrollIncrementDetails")
.field("type", &self.r#type)
.field("metrics", &self.metrics)
.finish()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ScrollIntent {
pub direction: AxisDirection,
pub r#type: ScrollIncrementType,
}
impl ScrollIntent {
pub const fn new(direction: AxisDirection) -> ScrollIntent {
ScrollIntent {
direction,
r#type: ScrollIncrementType::Line,
}
}
pub const fn r#type(mut self, r#type: ScrollIncrementType) -> ScrollIntent {
self.r#type = r#type;
self
}
}
impl Intent for ScrollIntent {
fn as_any(&self) -> &dyn Any {
self
}
}
pub struct ScrollAction {
action: ActionData,
}
impl ScrollAction {
pub fn new(app: &mut App) -> Handle<ScrollAction> {
app.create(ScrollAction {
action: ActionData::new(),
})
}
fn calculate_scroll_increment(
app: &mut App,
state: Handle<ScrollableState>,
r#type: ScrollIncrementType,
) -> f64 {
let position = state.position(app);
debug_assert!(position.has_pixels(app));
debug_assert!(
state
.resolved_physics(app)
.is_none_or(|physics| physics.should_accept_user_offset(&position.copy_with(app)))
);
if let Some(increment_calculator) = state.widget(app).increment_calculator.clone() {
let metrics: Rc<dyn ScrollMetrics> = Rc::new(position.copy_with(app));
return increment_calculator(&ScrollIncrementDetails::new(r#type, metrics));
}
match r#type {
ScrollIncrementType::Line => 50.0,
ScrollIncrementType::Page => 0.8 * position.viewport_dimension(app),
}
}
pub fn get_directional_increment(
app: &mut App,
state: Handle<ScrollableState>,
intent: &ScrollIntent,
) -> f64 {
if axis_direction_to_axis(intent.direction)
== axis_direction_to_axis(state.axis_direction(app))
{
let increment = ScrollAction::calculate_scroll_increment(app, state, intent.r#type);
return if intent.direction == state.axis_direction(app) {
increment
} else {
-increment
};
}
0.0
}
}
impl Action for ScrollAction {
type Intent = ScrollIntent;
crate::action_accessors!();
crate::context_action_overrides!();
}
impl ContextAction for ScrollAction {
fn is_enabled(
self: Handle<Self>,
app: &mut App,
_intent: &ScrollIntent,
context: Option<BuildContext>,
) -> bool {
let Some(context) = context else {
return false;
};
if Scrollable::maybe_of(app, context, None).is_some() {
return true;
}
PrimaryScrollController::maybe_of(app, context)
.is_some_and(|controller| controller.has_clients(app))
}
fn invoke(
self: Handle<Self>,
app: &mut App,
intent: &ScrollIntent,
context: Option<BuildContext>,
) -> Option<Rc<dyn Any>> {
let context = context.expect("Cannot scroll without a context.");
let mut state = Scrollable::maybe_of(app, context, None);
if state.is_none() {
let primary_scroll_controller = PrimaryScrollController::of(app, context);
debug_assert!(
primary_scroll_controller.positions(app).len() == 1,
"A ScrollAction was invoked with the PrimaryScrollController, but more than one \
ScrollPosition is attached. Only one ScrollPosition can be manipulated by a \
ScrollAction at a time."
);
let notification_context = primary_scroll_controller
.position(app)
.context(app)
.notification_context(app);
if let Some(notification_context) = notification_context {
state = Scrollable::maybe_of(app, notification_context, None);
}
}
let state = state?;
debug_assert!(
state.position(app).has_pixels(app),
"Scrollable must be laid out before it can be scrolled via a ScrollAction"
);
let position = state.position(app);
if let Some(physics) = state.resolved_physics(app)
&& !physics.should_accept_user_offset(&position.copy_with(app))
{
return None;
}
let increment = ScrollAction::get_directional_increment(app, state, intent);
if increment == 0.0 {
return None;
}
position.move_to(
app,
position.pixels(app) + increment,
Some(Duration::from_millis(100)),
Some(Curves::ease_in_out()),
None,
);
None
}
}
#[cfg(test)]
mod tests {
use std::any::TypeId;
use std::cell::Cell;
use std::collections::HashMap;
use waft_embedder::Offset;
use waft_foundation::{App, AppCell};
use waft_painting::Axis;
use waft_rendering::{
AnyRenderObject, BoxConstraints, RenderBox, RenderConstrainedBox, RenderHandle,
RenderSliver, RenderSliverToBoxAdapter, RenderViewport, RenderViewportBase,
};
use super::*;
use crate::framework::{
Element, IntoWidget, LeafRenderObjectWidget, RenderObjectWidget, WidgetRef,
};
use crate::test_harness::pump_every_view;
use crate::test_harness::{Harness, VIEW_HEIGHT};
use crate::widgets::actions::{Actions, AnyAction};
use crate::widgets::basic::Builder;
use crate::widgets::media_query::{MediaQuery, MediaQueryData};
use crate::widgets::scroll_physics::NeverScrollableScrollPhysics;
use crate::widgets::scrollable::ViewportBuilder;
use waft_rendering::AnyViewportOffset;
#[derive(Debug)]
struct TestViewport {
offset: AnyViewportOffset,
}
impl RenderObjectWidget for TestViewport {
type RenderObject = RenderViewport;
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
let content = RenderConstrainedBox::new(
app,
BoxConstraints::tight(waft_embedder::Size::new(300.0, 800.0)),
None,
);
let sliver = RenderSliverToBoxAdapter::new(app, Some(RenderBox::as_box(content)));
RenderViewport::new(
app,
AxisDirection::Right,
self.offset,
Some(vec![RenderSliver::as_sliver(sliver)]),
None,
)
.as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderViewport>,
) {
RenderViewportBase::set_offset(render_object, app, self.offset);
}
}
impl LeafRenderObjectWidget for TestViewport {}
fn mount_scroll_action(
app: &mut App,
configure: impl FnOnce(Scrollable) -> Scrollable,
) -> (Harness, Handle<ScrollableState>, BuildContext) {
let captured: Rc<Cell<Option<BuildContext>>> = Rc::default();
let sink = Rc::clone(&captured);
let viewport: ViewportBuilder = Rc::new(move |_app, _context, offset| {
let sink = Rc::clone(&sink);
let probe = Builder::new(move |_app, context| {
sink.set(Some(context));
TestViewport { offset }.into_widget()
});
probe.into_widget()
});
let action = ScrollAction::new(app);
let actions = HashMap::from([(
TypeId::of::<ScrollIntent>(),
Action::as_action(action) as AnyAction,
)]);
let scrollable = configure(Scrollable::new(viewport));
let tree: WidgetRef =
MediaQuery::new(MediaQueryData::new(), Actions::new(actions, scrollable)).into_widget();
let harness = Harness::mount(app, tree);
harness.pump(app);
let mut element = harness.root.as_element();
let state = loop {
element = element.children(app)[0];
if let Some(state) = element.state_handle::<ScrollableState>(app) {
break state;
}
};
let context = captured.get().expect("the viewport built");
(harness, state, context)
}
fn pump_frame_and_checkpoint(cell: &AppCell, at: Duration) {
pump_every_view(&mut cell.borrow_mut(), at);
cell.checkpoint();
}
struct AutoScrollDrag {
scroller: Handle<EdgeDraggingAutoScroller>,
scrolls: Rc<Cell<usize>>,
dragging: Rc<Cell<bool>>,
}
fn auto_scroll_drag(
app: &mut App,
state: Handle<ScrollableState>,
drag_target: Rect,
) -> AutoScrollDrag {
let scrolls = Rc::new(Cell::new(0));
let dragging = Rc::new(Cell::new(true));
let scroller_slot: Rc<Cell<Option<Handle<EdgeDraggingAutoScroller>>>> = Rc::default();
let counter = Rc::clone(&scrolls);
let in_progress = Rc::clone(&dragging);
let slot = Rc::clone(&scroller_slot);
let on_scrolled = Listener::new(move |app| {
counter.set(counter.get() + 1);
if !in_progress.get() {
return;
}
if let Some(scroller) = slot.get() {
scroller.start_auto_scroll_if_necessary(app, drag_target);
}
});
let scroller = EdgeDraggingAutoScroller::new(app, state, Some(on_scrolled), 50.0);
scroller_slot.set(Some(scroller));
AutoScrollDrag {
scroller,
scrolls,
dragging,
}
}
#[test]
fn an_auto_scroller_keeps_scrolling_while_the_drag_target_hangs_over_the_edge() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let (_harness, state, _context) = mount_scroll_action(&mut app, |scrollable| scrollable);
let drag_target = Rect::from_ltwh(0.0, VIEW_HEIGHT - 10.0, 50.0, 20.0);
let AutoScrollDrag {
scroller,
scrolls,
dragging,
} = auto_scroll_drag(&mut app, state, drag_target);
assert!(!scroller.scrolling(&app));
scroller.start_auto_scroll_if_necessary(&mut app, drag_target);
assert!(scroller.scrolling(&app));
assert_eq!(
state.position(&app).pixels(&app),
0.0,
"the animation has not ticked"
);
drop(app);
for frame in 0..=8 {
pump_frame_and_checkpoint(&cell, Duration::from_millis(20 * frame));
let app = cell.borrow();
assert!(
state.position(&app).pixels(&app) >= 10.0 * scrolls.get() as f64,
"every reported scroll moved 10 pixels"
);
}
let app = cell.borrow();
assert_eq!(scrolls.get(), 3);
assert!(scroller.scrolling(&app));
drop(app);
dragging.set(false);
scroller.stop_auto_scroll(&mut cell.borrow_mut());
assert!(!scroller.scrolling(&cell.borrow()));
for frame in 9..=16 {
pump_frame_and_checkpoint(&cell, Duration::from_millis(20 * frame));
}
let app = cell.borrow();
assert_eq!(scrolls.get(), 4);
assert_eq!(state.position(&app).pixels(&app), 40.0);
assert!(!scroller.scrolling(&app));
}
#[test]
fn an_auto_scroller_follows_the_axis_direction() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let (_harness, state, _context) = mount_scroll_action(&mut app, |scrollable| {
scrollable.axis_direction(AxisDirection::Up)
});
let over_the_top = Rect::from_ltwh(0.0, -10.0, 50.0, 20.0);
let AutoScrollDrag {
scroller, scrolls, ..
} = auto_scroll_drag(&mut app, state, over_the_top);
scroller.start_auto_scroll_if_necessary(&mut app, over_the_top);
assert!(scroller.scrolling(&app));
drop(app);
for frame in 0..=2 {
pump_frame_and_checkpoint(&cell, Duration::from_millis(20 * frame));
}
let app = cell.borrow();
assert_eq!(scrolls.get(), 1);
assert_eq!(state.position(&app).pixels(&app), 10.0);
}
#[test]
fn an_auto_scroller_stays_idle_for_a_target_inside_the_viewport_or_refusing_physics() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let (_harness, state, _context) = mount_scroll_action(&mut app, |scrollable| scrollable);
let inside = Rect::from_ltwh(0.0, 50.0, 50.0, 20.0);
let AutoScrollDrag {
scroller, scrolls, ..
} = auto_scroll_drag(&mut app, state, inside);
scroller.start_auto_scroll_if_necessary(&mut app, inside);
assert!(
!scroller.scrolling(&app),
"the drag should not trigger a scroll"
);
drop(app);
pump_frame_and_checkpoint(&cell, Duration::ZERO);
assert_eq!(scrolls.get(), 0);
let mut app = cell.borrow_mut();
let (_harness, state, _context) = mount_scroll_action(&mut app, |scrollable| {
scrollable.physics(Rc::new(NeverScrollableScrollPhysics::new()))
});
let over_the_edge = Rect::from_ltwh(0.0, VIEW_HEIGHT - 10.0, 50.0, 20.0);
let AutoScrollDrag { scroller, .. } = auto_scroll_drag(&mut app, state, over_the_edge);
scroller.start_auto_scroll_if_necessary(&mut app, over_the_edge);
assert!(!scroller.scrolling(&app));
assert_eq!(state.position(&app).pixels(&app), 0.0);
}
#[test]
fn a_scroll_intent_reaches_the_ambient_scrollable_through_actions() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let (_harness, state, context) = mount_scroll_action(&mut app, |scrollable| scrollable);
assert!(
ScrollContext::notification_context(&state, &mut app).is_some(),
"no notification context"
);
Actions::invoke(
&mut app,
context,
&ScrollIntent::new(AxisDirection::Down).r#type(ScrollIncrementType::Line),
);
for frame in 0..20 {
pump_every_view(&mut app, Duration::from_millis(16 * frame));
}
assert_eq!(state.position(&app).pixels(&app), 50.0);
Actions::invoke(
&mut app,
context,
&ScrollIntent::new(AxisDirection::Down).r#type(ScrollIncrementType::Page),
);
for frame in 20..40 {
pump_every_view(&mut app, Duration::from_millis(16 * frame));
}
assert_eq!(state.position(&app).pixels(&app), 50.0 + 0.8 * VIEW_HEIGHT);
Actions::invoke(
&mut app,
context,
&ScrollIntent::new(AxisDirection::Up).r#type(ScrollIncrementType::Line),
);
for frame in 40..60 {
pump_every_view(&mut app, Duration::from_millis(16 * frame));
}
assert_eq!(state.position(&app).pixels(&app), 0.8 * VIEW_HEIGHT);
}
#[test]
fn a_scroll_intent_across_the_axis_moves_nothing() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let (_harness, state, context) = mount_scroll_action(&mut app, |scrollable| scrollable);
assert_eq!(
ScrollAction::get_directional_increment(
&mut app,
state,
&ScrollIntent::new(AxisDirection::Right)
),
0.0
);
Actions::invoke(&mut app, context, &ScrollIntent::new(AxisDirection::Right));
for frame in 0..20 {
pump_every_view(&mut app, Duration::from_millis(16 * frame));
}
assert_eq!(state.position(&app).pixels(&app), 0.0);
}
#[test]
fn a_scroll_action_is_disabled_without_a_scrollable_and_refuses_physics_that_do_not_scroll() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let action = ScrollAction::new(&mut app);
let captured: Rc<Cell<Option<BuildContext>>> = Rc::default();
let sink = Rc::clone(&captured);
let harness = Harness::mount(
&mut app,
Builder::new(move |_app, context| {
sink.set(Some(context));
crate::widgets::basic::SizedBox::shrink().into_widget()
})
.into_widget(),
);
harness.pump(&mut app);
let context = captured.get().expect("the builder ran");
assert!(!ContextAction::is_enabled(
action,
&mut app,
&ScrollIntent::new(AxisDirection::Down),
Some(context)
));
let (_harness, state, context) = mount_scroll_action(&mut app, |scrollable| {
scrollable.physics(Rc::new(NeverScrollableScrollPhysics::new()))
});
Actions::invoke(&mut app, context, &ScrollIntent::new(AxisDirection::Down));
for frame in 0..20 {
pump_every_view(&mut app, Duration::from_millis(16 * frame));
}
assert_eq!(state.position(&app).pixels(&app), 0.0);
}
#[test]
fn scrollable_details_compare_their_axis_controller_physics_and_clip() {
let axis = ScrollableDetails::vertical(false);
assert_eq!(axis.direction, AxisDirection::Down);
assert_eq!(
ScrollableDetails::vertical(true).direction,
AxisDirection::Up
);
assert_eq!(
ScrollableDetails::horizontal(false).direction,
AxisDirection::Right
);
assert_eq!(
ScrollableDetails::horizontal(true).direction,
AxisDirection::Left
);
let physics: ScrollPhysicsRef = Rc::new(NeverScrollableScrollPhysics::new());
let with_physics = axis.copy_with().physics(Rc::clone(&physics));
assert_ne!(with_physics, axis);
assert_eq!(with_physics, axis.copy_with().physics(physics));
assert_ne!(
with_physics,
with_physics
.copy_with()
.decoration_clip_behavior(Clip::None)
);
assert!(format!("{with_physics:?}").contains("scroll physics"));
let _ = Axis::Vertical;
let _ = Offset::ZERO;
}
}