diff --git a/crates/behavior/src/wheel.rs b/crates/behavior/src/wheel.rs index 917cb4df77..a5826e2826 100644 --- a/crates/behavior/src/wheel.rs +++ b/crates/behavior/src/wheel.rs @@ -95,7 +95,7 @@ pub struct Wheel { /// The eased motion of a canvas's line wheel: notches gather into owed motion that later passes pay out. #[derive(Clone, Copy, Debug, Default, PartialEq)] pub struct WheelEase { - owed: Vec2, + owed: kurbo::Vec2, burst: u32, last_notch: f64, last_sign: Vec2, @@ -158,7 +158,7 @@ impl WheelEase { if !self.owes() { self.last_paid = now - FIRST_PASS; } - self.owed = Vec2::new(owe(self.owed.x, pan.x * boost), owe(self.owed.y, pan.y * boost)); + self.owed = kurbo::Vec2::new(owe(self.owed.x, f64::from(pan.x * boost)), owe(self.owed.y, f64::from(pan.y * boost))); } /// Drops the owed motion on the axes marked. @@ -175,7 +175,7 @@ impl WheelEase { pub fn step(&mut self, now: f64) -> Vec2 { if !now.is_finite() || now < self.last_paid { // A clock that runs backwards or breaks cannot tell how much to pay: the tail goes. - self.owed = Vec2::ZERO; + self.owed = kurbo::Vec2::ZERO; self.last_paid = if now.is_finite() { now } else { self.last_paid }; return Vec2::ZERO; } @@ -186,26 +186,33 @@ impl WheelEase { } let fraction = 1.0 - 0.1_f64.powf(dt / 0.1); let paid = Vec2::new(pay(self.owed.x, fraction), pay(self.owed.y, fraction)); - self.owed -= paid; + self.owed -= kurbo::Vec2::new(f64::from(paid.x), f64::from(paid.y)); + // A tail below normal f32 precision cannot move a view and must not keep scheduling frames. + if self.owed.x.abs() < f64::from(f32::MIN_POSITIVE) { + self.owed.x = 0.0; + } + if self.owed.y.abs() < f64::from(f32::MIN_POSITIVE) { + self.owed.y = 0.0; + } paid } /// Whether motion is still owed. pub fn owes(&self) -> bool { - self.owed != Vec2::ZERO + self.owed != kurbo::Vec2::ZERO } } /// `owed` with `more` added on one axis: motion the other way drops what was owed, and the sum stays within /// `MAX_OWED`. -fn owe(owed: f32, more: f32) -> f32 { +fn owe(owed: f64, more: f64) -> f64 { let owed = if more * owed < 0.0 { 0.0 } else { owed }; - (owed + more).clamp(-MAX_OWED, MAX_OWED) + (owed + more).clamp(-f64::from(MAX_OWED), f64::from(MAX_OWED)) } /// The part of `owed` one pass pays, `fraction` of it; a remainder under `SETTLE` is paid whole. -fn pay(owed: f32, fraction: f64) -> f32 { - if owed.abs() <= SETTLE { owed } else { (f64::from(owed) * fraction) as f32 } +fn pay(owed: f64, fraction: f64) -> f32 { + if owed.abs() <= f64::from(SETTLE) { owed as f32 } else { (owed * fraction) as f32 } } impl Wheel { @@ -772,11 +779,11 @@ mod tests { #[test] fn fast_notches_accelerate_to_a_cap_and_reset_on_reversal_or_idle_but_a_lone_notch_does_not() { - const NOTCH: f32 = 40.0; + const NOTCH: f64 = 40.0; // What one notch of `y` points adds to the owed motion. - let notch = |ease: &mut WheelEase, y: f32, now: f64| { + let notch = |ease: &mut WheelEase, y: f64, now: f64| { let before = ease.owed.y; - ease.feed(Vec2::new(0.0, y), now); + ease.feed(Vec2::new(0.0, y as f32), now); ease.owed.y - before }; let mut ease = WheelEase::default(); @@ -788,9 +795,9 @@ mod tests { gains.push(notch(&mut ease, NOTCH, 1.5 + f64::from(i) * 0.02)); } // The owed sum rounds in its last bits, so per-notch gains compare within a hundredth of a point. - let near = |a: f32, b: f32| (a - b).abs() < 0.01; + let near = |a: f64, b: f64| (a - b).abs() < 0.01; assert!(gains.windows(2).all(|pair| pair[1] > pair[0] - 0.01), "a fast run never pays less per notch: {gains:?}"); - let fastest = gains.iter().copied().fold(0.0, f32::max); + let fastest = gains.iter().copied().fold(0.0, f64::max); assert!(fastest > 2.0 * NOTCH, "a fast run accelerates: {gains:?}"); assert!(fastest < 6.0 * NOTCH + 0.01, "the acceleration is capped: {gains:?}"); assert!(gains.last().is_some_and(|&last| near(last, 6.0 * NOTCH)), "a long run reaches the cap and stays there: {gains:?}"); diff --git a/crates/pages/src/ui/view.rs b/crates/pages/src/ui/view.rs index 9b0fc2a310..fe37146b95 100644 --- a/crates/pages/src/ui/view.rs +++ b/crates/pages/src/ui/view.rs @@ -712,7 +712,11 @@ fn run_pass( // The canvas was given the view the pass started with, so its change of it is the pan; the state's offset // may have moved on since (a scale change anchors the point at the centre), and the pan adds to that. let panned = drawn_frame.offset_of(target.center, vw, vh); - let given_at = drawn_frame.offset_of(given.center, vw, vh); + // A new layout can put the old offset outside its document. The canvas clamps that view before + // reporting it; that correction is not a pan to add to the anchor computed above. + let mut baseline = given; + CanvasScrollbars::clamp(&mut baseline, DocRect::from_origin_size(Point::ZERO, drawn_frame.document(&drawn))); + let given_at = drawn_frame.offset_of(baseline.center, vw, vh); let (along_x, along_y) = (panned.0 - given_at.0, panned.1 - given_at.1); if along_x.abs() > OFFSET_EPSILON || along_y.abs() > OFFSET_EPSILON { want = (state.offset.0 + along_x, state.offset.1 + along_y); diff --git a/crates/pages/tests/it/thumbnails.rs b/crates/pages/tests/it/thumbnails.rs index 12c8ac465c..ae1a8302b1 100644 --- a/crates/pages/tests/it/thumbnails.rs +++ b/crates/pages/tests/it/thumbnails.rs @@ -273,6 +273,8 @@ fn every_open_of_the_grid_reveals_the_asked_page() { let open = Rc::new(std::cell::Cell::new(true)); let reveal = Rc::new(std::cell::Cell::new(Some(30_usize))); let current = Rc::new(std::cell::Cell::new(30_usize)); + let navigated = Rc::new(std::cell::Cell::new(false)); + let observed = Rc::clone(&navigated); let picked: Rc>> = Rc::default(); let (shown, asked, at, held) = (Rc::clone(&open), Rc::clone(&reveal), Rc::clone(¤t), Rc::clone(&picked)); let pages = DemoPages::new(60); @@ -287,6 +289,7 @@ fn every_open_of_the_grid_reveals_the_asked_page() { .show(ui); for event in out.events { if let ThumbnailEvent::Go(page) = event { + observed.set(true); at.set(page); } } @@ -294,6 +297,7 @@ fn every_open_of_the_grid_reveals_the_asked_page() { }); fixture.room(vec2(600.0, 600.0)); fixture.settle(); + assert!(!navigated.get(), "revealing the current page does not navigate the document"); let in_view = |fixture: &Fixture, n: usize| { let grid = fixture.by_role(Role::LIST_BOX).first().map(|node| node.rect).expect("the grid"); fixture.by_label(&format!("Page {n}")).is_some_and(|node| grid.contains_rect(node.rect)) diff --git a/crates/pages/tests/it/views.rs b/crates/pages/tests/it/views.rs index a8e9ecdd40..61ab2c033e 100644 --- a/crates/pages/tests/it/views.rs +++ b/crates/pages/tests/it/views.rs @@ -235,6 +235,21 @@ fn a_changed_zoom_keeps_the_point_at_the_centre_and_a_new_room_the_current_pages assert!((after - before).abs() < 1.0, "the current page's top stays put: {before} then {after}"); } +#[trial] +fn resizing_back_keeps_the_last_pages_top() { + let mut view = rig(3, state_with(|s| s.fit = Fit::Width)); + view.state.borrow_mut().go_to(2); + view.fixture.idle(4); + let top = |out: &Out| out.rects[2].y0 - out.offset.1; + let before = top(&view.out.borrow()); + view.fixture.room(vec2(1200.0, 600.0)); + view.fixture.idle(4); + view.fixture.room(vec2(900.0, 600.0)); + view.fixture.idle(4); + let after = top(&view.out.borrow()); + assert!((before - after).abs() < 1.0, "the last page stays at its top: {before} then {after}"); +} + /// Turning the page gaps off keeps the scroll offset, as production did, so the pages below the top close up toward /// it; also when the gaps change while the view is hidden (a page grid shown in its place) and it shows again. #[trial] @@ -291,6 +306,29 @@ fn wheel(fixture: &mut Fixture, lines: f32) { }]); } +#[trial] +fn an_eased_notch_settles_at_the_same_page_position_as_an_immediate_notch() { + let run = |eased| { + let mut navigation = NavigationOptions::default(); + navigation.wheel_line = 100.0; + navigation.eased_wheel = eased; + let start = state_with(|s| { + s.fit = Fit::None; + s.zoom = 1.0; + }); + let mut view = rig_over(DemoPages::new(3), start, navigation, true); + wheel(&mut view.fixture, -1.0); + view.fixture.idle(100); + let state = view.state.borrow(); + let out = view.out.borrow(); + (state.offset.1, out.pages[0].rect.min.y) + }; + let immediate = run(false); + let eased = run(true); + assert!((immediate.0 - eased.0).abs() < 0.0000001, "both notches settle at the same offset: {immediate:?} {eased:?}"); + assert!((immediate.1 - eased.1).abs() < 0.5, "both notches place the page alike: {immediate:?} {eased:?}"); +} + /// A scroll of `dy` points over the middle of the room from `source`, in device phase `phase`. fn swipe(fixture: &mut Fixture, dy: f32, phase: egui::TouchPhase, source: egui::MouseWheelSource) { fixture.frame(vec![egui::Event::MouseWheel { @@ -339,6 +377,28 @@ fn half_notches_past_the_bottom_turn_once_they_add_up_within_one_gesture_and_nev assert_eq!(state.borrow().current, 2, "a whole notch turns"); } +#[trial] +fn the_preview_takes_page_keys_when_first_opened() { + let at = Rc::new(RefCell::new(1_usize)); + let kept = Rc::clone(&at); + let pages = DemoPages::new(5); + let mut open = false; + let mut fixture = Fixture::new(move |ui| { + if craft_ui::contract::clicked(&craft_ui::buttons::Button::new("Preview").show(ui)) { + open = true; + } + if open { + PagePreview::new(&pages, &[0, 1, 2, 3, 4], &mut kept.borrow_mut(), PAPER).show(ui); + } + }); + fixture.room(vec2(900.0, 600.0)); + fixture.settle(); + let button = fixture.by_label("Preview").expect("preview button"); + fixture.click(&button).settle(); + fixture.key(Key::PageDown, Modifiers::NONE); + assert_eq!(*at.borrow(), 2, "Page Down steps the newly opened preview"); +} + #[trial] fn the_preview_steps_with_the_arrow_and_page_keys_from_a_button_of_its_row() { let at = Rc::new(RefCell::new(1_usize)); diff --git a/crates/ui-contract/src/contract/items/reveal.rs b/crates/ui-contract/src/contract/items/reveal.rs index fb119d55d0..ff83d1f530 100644 --- a/crates/ui-contract/src/contract/items/reveal.rs +++ b/crates/ui-contract/src/contract/items/reveal.rs @@ -3,7 +3,6 @@ use super::measure::RowMeasure; use super::{ItemsHooks, rows}; use crate::contract::Collection; -use crate::contract::collection::LeadSource; use crate::runtime::env::{Env, EnvView}; use crate::runtime::memory::MemoryStage; use craft_behavior::{ItemLayout, Proposal, ScrollPos}; @@ -15,7 +14,12 @@ impl ItemsHooks<'_, '_, C> { /// moves to it and it scrolls into view. pub(super) fn requested_reveal(&mut self, comp: &C, layout: &C::ItemLayout) { if let Some(at) = comp.reveal().and_then(|key| self.index_of(key)).filter(|&at| !self.source.section_stop(at)) { - self.move_lead(at, LeadSource::Keys); + let key = self.source.key(at); + if self.lead.key.as_ref() != Some(&key) { + self.lead.rested = None; + } + self.lead.key = Some(key); + self.lead.at = at; self.reveal_with(layout, at); } }