use super::{HeadlessSurface, Tree};
use crate::{
app::{App, events::ScrollPhase},
components::{Column, ScrollVertical, Slider, Spacer},
core::Signal,
};
fn list_under_a_slider() -> (Tree, App) {
let mut app = App::new();
let mut tree = Tree::new();
tree.set_scale_factor(2.0);
tree.resize(400, 400);
tree.set_root(
Column::new()
.child(Slider::new(Signal::new(0)).range(0, 10).width(100.0).height(20.0))
.child(
ScrollVertical::new(Spacer::new().width(100.0).height(1000.0))
.width(100.0)
.height(180.0),
),
);
tree.pass(&mut app, &HeadlessSurface);
(tree, app)
}
#[test]
fn a_finger_scrolls_over_a_list_with_more_content_and_nowhere_else() {
let (mut tree, _app) = list_under_a_slider();
assert!(tree.touch_scrolls_at(100.0, 200.0), "over the list");
assert!(!tree.touch_scrolls_at(100.0, 20.0), "over the slider");
assert!(!tree.touch_scrolls_at(300.0, 200.0), "beside the list");
}
#[test]
fn a_list_that_shows_all_its_content_does_not_scroll() {
let mut app = App::new();
let mut tree = Tree::new();
tree.resize(400, 400);
tree.set_root(ScrollVertical::new(Spacer::new().width(100.0).height(50.0)).size(100.0, 200.0));
tree.pass(&mut app, &HeadlessSurface);
assert!(!tree.touch_scrolls_at(50.0, 100.0));
}
#[test]
fn the_content_moves_as_far_as_the_finger() {
let mut app = App::new();
let mut tree = Tree::new();
tree.set_scale_factor(3.0);
tree.resize(600, 600);
let state = crate::layout::layout_kind::ScrollState::new();
tree.set_root(
ScrollVertical::new(Spacer::new().width(100.0).height(1000.0))
.with_scroll_state(state.clone())
.size(100.0, 100.0),
);
tree.pass(&mut app, &HeadlessSurface);
tree.touch_scroll_begin(150.0, 150.0, 0.0, -90.0);
tree.touch_scroll(150.0, 150.0, 0.0, -90.0, ScrollPhase::Scroll, false);
assert_eq!(state.scroll_y(), 30.0);
}
#[test]
fn momentum_tells_whether_the_list_moved() {
let (mut tree, _app) = list_under_a_slider();
tree.touch_scroll_begin(100.0, 200.0, 0.0, -100.0);
assert!(tree.touch_scroll(100.0, 200.0, 0.0, -100.0, ScrollPhase::Scroll, true));
assert!(tree.touch_scroll(100.0, 200.0, 0.0, -100_000.0, ScrollPhase::Scroll, true));
assert!(
!tree.touch_scroll(100.0, 200.0, 0.0, -100.0, ScrollPhase::Scroll, true),
"at its end"
);
tree.touch_scroll_begin(300.0, 200.0, 0.0, -100.0);
assert!(
!tree.touch_scroll(300.0, 200.0, 0.0, -100.0, ScrollPhase::Scroll, true),
"beside it"
);
}
#[test]
fn a_gesture_keeps_scrolling_what_it_started_on() {
use crate::layout::layout_kind::ScrollState;
let mut app = App::new();
let mut tree = Tree::new();
tree.resize(200, 200);
let (page, inner) = (ScrollState::new(), ScrollState::new());
tree.set_root(
ScrollVertical::new(
Column::new()
.child(Spacer::new().width(200.0).height(300.0))
.child(
ScrollVertical::new(Spacer::new().width(200.0).height(2000.0))
.with_scroll_state(inner.clone())
.size(200.0, 300.0),
)
.child(Spacer::new().width(200.0).height(2000.0)),
)
.with_scroll_state(page.clone())
.size(200.0, 200.0),
);
tree.pass(&mut app, &HeadlessSurface);
tree.touch_scroll_begin(100.0, 100.0, 0.0, -250.0);
tree.touch_scroll(100.0, 100.0, 0.0, -250.0, ScrollPhase::Scroll, false);
tree.pass(&mut app, &HeadlessSurface);
tree.touch_scroll(100.0, 100.0, 0.0, -100.0, ScrollPhase::Scroll, false);
assert_eq!((page.scroll_y(), inner.scroll_y()), (350.0, 0.0));
tree.pass(&mut app, &HeadlessSurface);
tree.touch_scroll_begin(100.0, 100.0, 0.0, -40.0);
tree.touch_scroll(100.0, 100.0, 0.0, -40.0, ScrollPhase::Scroll, false);
assert_eq!((page.scroll_y(), inner.scroll_y()), (350.0, 40.0));
tree.touch_scroll(100.0, 100.0, 0.0, -5000.0, ScrollPhase::Scroll, false);
assert_eq!((page.scroll_y(), inner.scroll_y()), (350.0, 1700.0));
tree.pass(&mut app, &HeadlessSurface);
tree.touch_scroll_begin(100.0, 100.0, 0.0, -30.0);
tree.touch_scroll(100.0, 100.0, 0.0, -30.0, ScrollPhase::Scroll, false);
assert_eq!((page.scroll_y(), inner.scroll_y()), (380.0, 1700.0));
tree.pass(&mut app, &HeadlessSurface);
tree.touch_scroll_begin(100.0, 100.0, 0.0, 30.0);
tree.touch_scroll(100.0, 100.0, 0.0, 30.0, ScrollPhase::Scroll, false);
assert_eq!((page.scroll_y(), inner.scroll_y()), (380.0, 1670.0));
}
#[test]
fn the_travels_main_axis_picks_between_a_strip_and_the_page() {
use crate::{components::ScrollHorizontal, layout::layout_kind::ScrollState};
let mut app = App::new();
let mut tree = Tree::new();
tree.resize(200, 200);
let (page, strip) = (ScrollState::new(), ScrollState::new());
tree.set_root(
ScrollVertical::new(
Column::new()
.child(
ScrollHorizontal::new(Spacer::new().width(2000.0).height(100.0))
.with_scroll_state(strip.clone())
.size(200.0, 100.0),
)
.child(Spacer::new().width(200.0).height(2000.0)),
)
.with_scroll_state(page.clone())
.size(200.0, 200.0),
);
tree.pass(&mut app, &HeadlessSurface);
tree.touch_scroll_begin(100.0, 50.0, -40.0, -10.0);
tree.touch_scroll(100.0, 50.0, -40.0, -10.0, ScrollPhase::Scroll, false);
assert_eq!((strip.scroll_x(), page.scroll_y()), (40.0, 0.0));
tree.pass(&mut app, &HeadlessSurface);
tree.touch_scroll_begin(100.0, 50.0, -10.0, -40.0);
tree.touch_scroll(100.0, 50.0, -10.0, -40.0, ScrollPhase::Scroll, false);
assert_eq!((strip.scroll_x(), page.scroll_y()), (40.0, 40.0));
}
#[test]
fn two_taps_a_few_points_apart_are_a_double_click() {
use std::sync::{Arc, Mutex};
use crate::{app::events::MouseButton, components::Rect};
let mut app = App::new();
let mut tree = Tree::new();
tree.set_scale_factor(3.0);
tree.resize(600, 600);
tree.set_double_click_distance(40.0 * 3.0);
let log = Arc::new(Mutex::new(Vec::new()));
let (downs, ups, clicks, doubles) = (log.clone(), log.clone(), log.clone(), log.clone());
tree.set_root(
Rect::new(200.0, 200.0)
.on_mouse_down(move |_| downs.lock().unwrap().push("down"))
.on_mouse_up(move |_| ups.lock().unwrap().push("up"))
.on_click(move |_| clicks.lock().unwrap().push("click"))
.on_dblclick(move |_| doubles.lock().unwrap().push("double")),
);
tree.pass(&mut app, &HeadlessSurface);
for (x, y) in [(300.0, 300.0), (324.0, 318.0)] {
tree.mouse_down(x, y, MouseButton::Left);
tree.mouse_up(x, y, MouseButton::Left);
}
assert_eq!(*log.lock().unwrap(), ["down", "up", "down", "double", "up"]);
}
fn list() -> (Tree, App, crate::layout::layout_kind::ScrollState) {
let mut app = App::new();
let mut tree = Tree::new();
tree.resize(100, 100);
let state = crate::layout::layout_kind::ScrollState::new();
tree.set_root(
ScrollVertical::new(Spacer::new().width(100.0).height(1000.0))
.with_scroll_state(state.clone())
.size(100.0, 100.0),
);
tree.pass(&mut app, &HeadlessSurface);
(tree, app, state)
}
#[test]
fn a_finger_pulls_a_list_past_its_start_less_than_it_travels_and_lets_it_spring_back() {
use std::time::{Duration, Instant};
let (mut tree, _app, state) = list();
tree.touch_scroll_begin(50.0, 50.0, 0.0, 40.0);
assert!(tree.touch_scroll(50.0, 50.0, 0.0, 40.0, ScrollPhase::Scroll, false));
let pulled = state.overscroll().1;
assert!(
(-40.0..-10.0).contains(&pulled),
"drawn before the start, by less than 40: {pulled}"
);
assert_eq!(state.scroll_y(), 0.0);
tree.touch_scroll(50.0, 50.0, 0.0, 40.0, ScrollPhase::Scroll, false);
let further = state.overscroll().1;
assert!(
further < pulled && pulled - further < -pulled,
"{pulled} then {further}"
);
tree.touch_scroll(50.0, 50.0, 0.0, -40.0, ScrollPhase::Scroll, false);
assert_eq!((state.overscroll().1, state.scroll_y()), (pulled, 0.0));
tree.touch_scroll(50.0, 50.0, 0.0, 0.0, ScrollPhase::End, false);
assert!(tree.touch_animating());
tree.touch_scroll(50.0, 50.0, 0.0, 0.0, ScrollPhase::Start, false);
assert_eq!(state.overscroll().1, pulled);
let lifted = Instant::now();
assert!(tree.touch_tick(lifted + Duration::from_millis(50)));
let halfway = state.overscroll().1;
assert!(pulled < halfway && halfway < 0.0, "{pulled} then {halfway}");
assert!(!tree.touch_tick(lifted + Duration::from_secs(2)));
assert_eq!(state.overscroll(), (0.0, 0.0));
assert!(!tree.touch_animating());
}
#[test]
fn travel_past_the_end_and_back_scrolls_only_once_the_pull_is_let_go() {
let (mut tree, _app, state) = list();
tree.touch_scroll_begin(50.0, 50.0, 0.0, 30.0);
tree.touch_scroll(50.0, 50.0, 0.0, 30.0, ScrollPhase::Scroll, false);
tree.touch_scroll(50.0, 50.0, 0.0, -50.0, ScrollPhase::Scroll, false);
assert_eq!((state.overscroll().1, state.scroll_y()), (0.0, 20.0));
}
#[test]
fn momentum_that_reaches_the_end_carries_the_list_past_it_and_back() {
use std::time::{Duration, Instant};
let (mut tree, _app, state) = list();
tree.touch_scroll_begin(50.0, 50.0, 0.0, -40.0);
tree.touch_scroll(50.0, 50.0, 0.0, -100_000.0, ScrollPhase::Scroll, true);
assert_eq!(
(state.scroll_y(), state.overscroll().1),
(900.0, 0.0),
"momentum itself pulls nothing"
);
assert!(!tree.touch_scroll(50.0, 50.0, 0.0, -10.0, ScrollPhase::Scroll, true));
let reached = Instant::now();
tree.touch_scroll_bounce(0.0, -1500.0, reached);
assert!(tree.touch_tick(reached + Duration::from_millis(60)));
let out = state.overscroll().1;
assert!(out > 10.0, "carried beyond the end: {out}");
assert!(!tree.touch_tick(reached + Duration::from_secs(2)));
assert_eq!((state.scroll_y(), state.overscroll().1), (900.0, 0.0));
}
#[test]
fn momentum_that_stops_short_of_an_end_does_not_bounce() {
let (mut tree, _app, state) = list();
tree.touch_scroll_begin(50.0, 50.0, 0.0, -40.0);
tree.touch_scroll(50.0, 50.0, 0.0, -40.0, ScrollPhase::Scroll, true);
tree.touch_scroll_bounce(0.0, -1500.0, std::time::Instant::now());
assert!(!tree.touch_animating());
assert_eq!(state.overscroll(), (0.0, 0.0));
}
#[test]
fn a_list_that_shows_all_its_content_is_not_pulled() {
let mut app = App::new();
let mut tree = Tree::new();
tree.resize(200, 200);
let state = crate::layout::layout_kind::ScrollState::new();
tree.set_root(
ScrollVertical::new(Spacer::new().width(100.0).height(50.0))
.with_scroll_state(state.clone())
.size(100.0, 100.0),
);
tree.pass(&mut app, &HeadlessSurface);
tree.touch_scroll_begin(50.0, 50.0, 0.0, 40.0);
tree.touch_scroll(50.0, 50.0, 0.0, 40.0, ScrollPhase::Scroll, false);
assert_eq!(state.overscroll(), (0.0, 0.0));
}
#[test]
fn overscrolled_content_is_laid_out_past_its_end() {
let (mut tree, mut app, state) = list();
tree.touch_scroll_begin(50.0, 50.0, 0.0, 40.0);
tree.touch_scroll(50.0, 50.0, 0.0, 40.0, ScrollPhase::Scroll, false);
assert!(tree.needs_redraw());
tree.pass(&mut app, &HeadlessSurface);
let gap = -state.overscroll().1;
assert!(gap > 10.0);
let layout = tree.last_layout.as_ref().expect("a layout");
let content = layout.children.first().expect("the list's content");
assert_eq!(content.offset.y, gap.round());
}
#[test]
fn a_finger_catches_a_list_that_is_springing_back_and_lets_it_go_again() {
use std::time::{Duration, Instant};
let (mut tree, _app, state) = list();
tree.touch_scroll_begin(50.0, 50.0, 0.0, 60.0);
tree.touch_scroll(50.0, 50.0, 0.0, 60.0, ScrollPhase::Scroll, false);
tree.touch_scroll(50.0, 50.0, 0.0, 0.0, ScrollPhase::End, false);
tree.touch_tick(Instant::now() + Duration::from_millis(40));
let caught_at = state.overscroll().1;
assert!(caught_at < 0.0);
assert!(tree.touch_scroll_catch());
assert!(!tree.touch_animating());
assert!(!tree.touch_tick(Instant::now() + Duration::from_secs(1)));
assert_eq!(state.overscroll().1, caught_at);
assert!(!tree.touch_scroll_catch(), "nothing is springing any more");
tree.touch_scroll_settle();
assert!(tree.touch_animating());
assert!(!tree.touch_tick(Instant::now() + Duration::from_secs(2)));
assert_eq!(state.overscroll(), (0.0, 0.0));
}
#[test]
fn a_tap_on_the_status_bar_scrolls_the_page_to_its_top() {
use std::time::{Duration, Instant};
let (mut tree, _app, state) = list();
let now = Instant::now();
assert!(!tree.touch_scroll_to_top(now), "already at its top");
state.set_scroll(0.0, 400.0);
assert!(tree.touch_scroll_to_top(now));
assert!(tree.touch_animating());
assert!(tree.touch_tick(now + Duration::from_millis(100)));
let on_its_way = state.scroll_y();
assert!(
0.0 < on_its_way && on_its_way < 200.0,
"most of the way after a third of the time: {on_its_way}"
);
assert!(!tree.touch_tick(now + Duration::from_millis(400)));
assert_eq!(state.scroll_y(), 0.0);
assert!(!tree.touch_animating());
}
#[test]
fn a_finger_stops_a_page_on_its_way_to_its_top() {
use std::time::{Duration, Instant};
let (mut tree, _app, state) = list();
let now = Instant::now();
state.set_scroll(0.0, 400.0);
tree.touch_scroll_to_top(now);
tree.touch_tick(now + Duration::from_millis(100));
let stopped_at = state.scroll_y();
assert!(tree.touch_scroll_catch());
assert!(!tree.touch_tick(now + Duration::from_millis(400)));
assert_eq!(state.scroll_y(), stopped_at);
}
#[test]
fn the_scroll_indicator_shortens_against_the_end_the_list_is_pulled_past() {
use crate::layout::{layout_kind::ScrollAxis, scrollbar::ScrollBarStyle};
let (mut tree, mut app, state) = list();
let style = ScrollBarStyle::default();
let thumb = |state: &crate::layout::layout_kind::ScrollState| {
let bar = state
.scrollbar_geometry_for_axis(ScrollAxis::Vertical, &style)
.expect("a bar");
(bar.thumb_y, bar.thumb_height)
};
let at_rest = thumb(&state);
tree.touch_scroll_begin(50.0, 50.0, 0.0, 30.0);
tree.touch_scroll(50.0, 50.0, 0.0, 30.0, ScrollPhase::Scroll, false);
tree.pass(&mut app, &HeadlessSurface);
let pulled = thumb(&state);
assert_eq!(pulled.0, at_rest.0);
assert!(
(pulled.1 - (at_rest.1 + state.overscroll().1)).abs() < 0.01,
"{at_rest:?} then {pulled:?}"
);
tree.touch_scroll(50.0, 50.0, 0.0, 0.0, ScrollPhase::End, false);
tree.touch_tick(std::time::Instant::now() + std::time::Duration::from_secs(2));
state.set_scroll(0.0, 900.0);
tree.pass(&mut app, &HeadlessSurface);
let at_end = thumb(&state);
tree.touch_scroll_begin(50.0, 50.0, 0.0, -30.0);
tree.touch_scroll(50.0, 50.0, 0.0, -30.0, ScrollPhase::Scroll, false);
tree.pass(&mut app, &HeadlessSurface);
let pulled = thumb(&state);
assert!(pulled.1 < at_end.1);
assert!(
(pulled.0 + pulled.1 - (at_end.0 + at_end.1)).abs() < 0.01,
"{at_end:?} then {pulled:?}"
);
}
#[test]
fn a_list_let_go_while_the_finger_still_pulls_goes_on_outward_before_it_springs_back() {
use std::time::{Duration, Instant};
let (mut tree, _app, state) = list();
tree.touch_scroll_begin(50.0, 50.0, 0.0, 40.0);
tree.touch_scroll(50.0, 50.0, 0.0, 40.0, ScrollPhase::Scroll, false);
tree.touch_scroll(50.0, 50.0, 0.0, 0.0, ScrollPhase::End, false);
let let_go_at = state.overscroll().1;
let now = Instant::now();
tree.touch_scroll_bounce(0.0, 1200.0, now);
tree.touch_tick(now + Duration::from_millis(30));
let further = state.overscroll().1;
assert!(
further < let_go_at,
"further out than where it was let go: {let_go_at} then {further}"
);
assert!(!tree.touch_tick(now + Duration::from_secs(3)));
assert_eq!(state.overscroll(), (0.0, 0.0));
}
#[test]
fn a_list_that_does_not_scroll_to_top_leaves_the_status_bars_tap_alone() {
let (mut tree, _app, state) = list();
state.set_scroll(0.0, 400.0);
state.set_scrolls_to_top(false);
assert!(!tree.touch_scroll_to_top(std::time::Instant::now()));
assert_eq!(state.scroll_y(), 400.0);
}
#[test]
fn a_finger_scrolls_from_a_scroll_indicator_and_drags_a_bar_that_is_always_there() {
use crate::layout::scrollbar::{ScrollBarStyle, ScrollBarVisibility};
let list_with = |style: ScrollBarStyle| {
let mut app = App::new();
let mut tree = Tree::new();
tree.resize(200, 200);
tree.set_root(
ScrollVertical::new(Spacer::new().width(100.0).height(1000.0))
.scrollbar(style)
.size(100.0, 100.0),
);
tree.pass(&mut app, &HeadlessSurface);
tree.touch_scroll_begin(50.0, 50.0, 0.0, -10.0);
tree.touch_scroll(50.0, 50.0, 0.0, -10.0, ScrollPhase::Scroll, false);
tree.pass(&mut app, &HeadlessSurface);
tree.painted_quads();
tree
};
let mut bar = list_with(ScrollBarStyle {
width: 12.0,
padding: 0.0,
visible: ScrollBarVisibility::Auto,
..ScrollBarStyle::thin()
});
assert!(!bar.touch_scrolls_at(96.0, 10.0), "a bar is dragged");
let mut indicator = list_with(ScrollBarStyle {
width: 12.0,
padding: 0.0,
end_inset: None,
..ScrollBarStyle::indicator()
});
assert!(
indicator.touch_scrolls_at(96.0, 10.0),
"an indicator is content to a moving finger"
);
}
fn input_over_a_list() -> (Tree, App, crate::layout::layout_kind::ScrollState) {
use crate::components::TextInput;
let state = crate::layout::layout_kind::ScrollState::new();
let mut app = App::new();
let mut tree = Tree::new();
tree.resize(200, 400);
tree.set_root(
Column::new()
.child(TextInput::new(Signal::new(String::new())).width(200.0).height(40.0))
.child(
ScrollVertical::new(Spacer::new().width(200.0).height(1000.0))
.with_scroll_state(state.clone())
.width(200.0)
.height(300.0),
),
);
tree.pass(&mut app, &HeadlessSurface);
(tree, app, state)
}
#[test]
fn a_tap_outside_the_text_input_being_typed_into_lets_it_go() {
let (mut tree, mut app, _state) = input_over_a_list();
tree.touch_press(100.0, 20.0);
tree.touch_release(100.0, 20.0);
tree.pass(&mut app, &HeadlessSurface);
assert!(tree.focused_text_input(), "a tap on the input gives it the focus");
tree.touch_scroll_begin(100.0, 200.0, 0.0, -10.0);
assert!(
tree.focused_text_input(),
"a finger that scrolls leaves the keyboard up"
);
tree.touch_press(100.0, 200.0);
tree.touch_release(100.0, 200.0);
assert!(!tree.focused_text_input(), "a tap outside takes the keyboard down");
}
#[test]
fn scrolling_a_list_that_dismisses_the_keyboard_takes_the_focus_from_the_text_input() {
let (mut tree, mut app, state) = input_over_a_list();
tree.touch_press(100.0, 20.0);
tree.touch_release(100.0, 20.0);
tree.pass(&mut app, &HeadlessSurface);
tree.touch_scroll_begin(100.0, 200.0, 0.0, -10.0);
assert!(tree.focused_text_input(), "a list not set to keeps the keyboard up");
state.set_dismisses_keyboard(true);
tree.touch_scroll_begin(100.0, 200.0, 0.0, -10.0);
assert!(!tree.focused_text_input());
}
#[test]
fn a_list_the_keyboard_follows_a_finger_over_keeps_the_focus_until_the_platform_takes_the_keyboard() {
use crate::layout::layout_kind::KeyboardDismissal;
let (mut tree, mut app, state) = input_over_a_list();
assert!(!tree.touch_keyboard_follows_at(100.0, 200.0), "nothing is typed into");
tree.touch_press(100.0, 20.0);
tree.touch_release(100.0, 20.0);
tree.pass(&mut app, &HeadlessSurface);
assert!(!tree.touch_keyboard_follows_at(100.0, 200.0), "the list does not ask");
state.set_keyboard_dismissal(KeyboardDismissal::Interactive);
assert!(tree.touch_keyboard_follows_at(100.0, 200.0));
tree.touch_scroll_begin(100.0, 200.0, 0.0, -10.0);
assert!(tree.focused_text_input(), "scrolling alone leaves the keyboard up");
tree.keyboard_was_put_away();
assert!(!tree.focused_text_input(), "the finger took the keyboard down");
}
fn pages() -> (Tree, App, crate::layout::layout_kind::ScrollState) {
use crate::components::ScrollHorizontal;
let mut app = App::new();
let mut tree = Tree::new();
tree.resize(200, 200);
let state = crate::layout::layout_kind::ScrollState::new();
state.set_paging(true);
tree.set_root(
ScrollHorizontal::new(Spacer::new().width(500.0).height(100.0))
.with_scroll_state(state.clone())
.size(100.0, 100.0),
);
tree.pass(&mut app, &HeadlessSurface);
(tree, app, state)
}
fn drag_pages(tree: &mut Tree, travel: f32, velocity: f32) {
use std::time::{Duration, Instant};
tree.touch_scroll_begin(50.0, 50.0, travel.signum(), 0.0);
tree.touch_scroll(50.0, 50.0, travel, 0.0, ScrollPhase::Scroll, false);
tree.touch_scroll(50.0, 50.0, 0.0, 0.0, ScrollPhase::End, false);
assert!(tree.touch_scroll_pages());
let lifted = Instant::now();
tree.touch_scroll_page(velocity, 0.0, lifted);
tree.touch_tick(lifted + Duration::from_secs(3));
assert!(!tree.touch_animating());
}
#[test]
fn a_list_that_scrolls_by_pages_goes_to_the_nearest_when_let_go() {
let (mut tree, _app, state) = pages();
drag_pages(&mut tree, -40.0, 0.0);
assert_eq!(state.scroll_x(), 0.0, "less than half a page: back");
drag_pages(&mut tree, -60.0, 0.0);
assert_eq!(state.scroll_x(), 100.0, "more than half: on");
assert_eq!(state.page(), (1, 0));
}
#[test]
fn a_list_that_scrolls_by_pages_goes_to_the_next_when_thrown_and_no_further() {
let (mut tree, _app, state) = pages();
drag_pages(&mut tree, -10.0, -900.0);
assert_eq!(state.scroll_x(), 100.0);
drag_pages(&mut tree, -30.0, -20_000.0);
assert_eq!(state.scroll_x(), 200.0);
drag_pages(&mut tree, 10.0, 900.0);
assert_eq!(state.scroll_x(), 100.0);
drag_pages(&mut tree, -30.0, 900.0);
assert_eq!(state.scroll_x(), 100.0);
}
#[test]
fn a_list_on_its_way_to_a_page_moves_there_over_time_and_is_caught_by_a_finger() {
use std::time::{Duration, Instant};
let (mut tree, _app, state) = pages();
tree.touch_scroll_begin(50.0, 50.0, -1.0, 0.0);
tree.touch_scroll(50.0, 50.0, -60.0, 0.0, ScrollPhase::Scroll, false);
tree.touch_scroll(50.0, 50.0, 0.0, 0.0, ScrollPhase::End, false);
let lifted = Instant::now();
tree.touch_scroll_page(0.0, 0.0, lifted);
assert!(tree.touch_tick(lifted + Duration::from_millis(50)), "still on its way");
let between = state.scroll_x();
assert!(between > 60.0 && between < 100.0, "between the two: {between}");
assert!(tree.touch_scroll_catch());
assert!(!tree.touch_animating());
assert_eq!(state.scroll_x(), between);
tree.touch_scroll_settle();
tree.touch_tick(Instant::now() + Duration::from_secs(3));
assert_eq!(state.scroll_x(), 100.0);
}
#[test]
fn a_list_that_does_not_scroll_by_pages_is_left_where_the_finger_left_it() {
let (mut tree, _app, state) = pages();
state.set_paging(false);
tree.touch_scroll_begin(50.0, 50.0, -1.0, 0.0);
tree.touch_scroll(50.0, 50.0, -40.0, 0.0, ScrollPhase::Scroll, false);
tree.touch_scroll(50.0, 50.0, 0.0, 0.0, ScrollPhase::End, false);
assert!(!tree.touch_scroll_pages());
tree.touch_scroll_page(-900.0, 0.0, std::time::Instant::now());
assert!(!tree.touch_animating());
assert_eq!(state.scroll_x(), 40.0);
}
#[test]
fn pages_inside_a_page_that_scrolls_go_to_the_next_as_a_shell_sends_a_throw() {
use std::time::{Duration, Instant};
use crate::components::ScrollHorizontal;
let mut app = App::new();
let mut tree = Tree::new();
tree.set_scale_factor(3.0);
tree.resize(600, 900);
let state = crate::layout::layout_kind::ScrollState::new();
state.set_paging(true);
tree.set_root(
ScrollVertical::new(
Column::new()
.child(Spacer::new().width(200.0).height(40.0))
.child(
ScrollHorizontal::new(Spacer::new().width(1000.0).height(100.0))
.with_scroll_state(state.clone())
.width(200.0),
)
.child(Spacer::new().width(200.0).height(2000.0)),
)
.size(200.0, 300.0),
);
tree.pass(&mut app, &HeadlessSurface);
let (x, y) = (300.0, 270.0);
tree.touch_scroll_begin(x, y, -12.0, 1.0);
tree.touch_scroll(x, y, 0.0, 0.0, ScrollPhase::Start, false);
for _ in 0..10 {
tree.touch_scroll(x, y, -12.0, 0.0, ScrollPhase::Scroll, false);
tree.pass(&mut app, &HeadlessSurface);
}
assert_eq!(state.scroll_x(), 40.0, "the strip follows the finger");
tree.touch_scroll(x, y, 0.0, 0.0, ScrollPhase::End, false);
tree.touch_scroll(x, y, 0.0, 0.0, ScrollPhase::Start, false);
assert!(tree.touch_scroll_pages());
tree.touch_scroll(x, y, 0.0, 0.0, ScrollPhase::End, false);
let lifted = Instant::now();
tree.touch_scroll_page(-2400.0, 30.0, lifted);
let mut frames = 0;
while tree.touch_animating() {
frames += 1;
assert!(frames < 400, "the strip never came to rest: at {}", state.scroll_x());
tree.touch_tick(lifted + Duration::from_millis(8 * frames));
tree.pass(&mut app, &HeadlessSurface);
}
assert_eq!(state.scroll_x(), 200.0, "the next page");
assert!(frames > 5, "over some frames, not at once: {frames}");
}