Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

M3: Accelerate coarse wheel scrolling (#1094)

Chiranjeevi U4db6ac4ae244
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index b5ca6abab..ad7483465 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -482,6 +482,9 @@ pub struct DocView {
/// (#759). The window outlasts a single frame so egui's internal easing tail is suppressed as
/// it decays, instead of adding on top of the 1:1 motion.
precise_scroll_until: f64,
+ /// Per-axis line-wheel state; horizontal input cannot extend a vertical burst.
+ wheel_boost_x: crate::wheel_boost::WheelBoost,
+ wheel_boost_y: crate::wheel_boost::WheelBoost,
/// Touchpad momentum after the fingers lift (#531).
fling: crate::fling::Fling,
pub(crate) auto_scroll: crate::autoscroll::AutoScroll,
@@ -756,6 +759,8 @@ impl DocView {
shown: false,
wheel: Default::default(),
precise_scroll_until: 0.0,
+ wheel_boost_x: Default::default(),
+ wheel_boost_y: Default::default(),
fling: Default::default(),
auto_scroll: Default::default(),
selected: BTreeSet::new(),
@@ -835,6 +840,7 @@ impl DocView {
self.flash = None;
// A thumbnail drag holds page indexes from before the change.
self.panel_drag = None;
+ self.reset_coarse_state();
}
/// The document was replaced by a newer version of its file (#431). The reader's place
@@ -1153,6 +1159,7 @@ impl DocView {
.unwrap_or(0.0);
self.goto = Some((page, frac));
self.current = page;
+ self.reset_coarse_state();
}
/// Screen position of the centre of glyph `glyph` on `page`, if that page is on screen and its
@@ -1174,6 +1181,14 @@ impl DocView {
self.thumbs.get(&page).map(|t| &t.tex)
}
+ /// Drops wheel state after a discontinuous jump so the destination cannot
+ /// inherit momentum from the previous viewport.
+ fn reset_coarse_state(&mut self) {
+ self.wheel.clear_motion();
+ self.wheel_boost_x.reset();
+ self.wheel_boost_y.reset();
+ }
+
pub fn go_to_page(&mut self, page: usize) {
let page = page.min(self.page_count.saturating_sub(1));
if page != self.current {
@@ -1187,7 +1202,7 @@ impl DocView {
self.forward.clear();
}
// Partial wheel motion belongs to the old place.
- self.wheel.clear_motion();
+ self.reset_coarse_state();
self.goto = Some((page, 0.0));
self.current = page;
}
@@ -1322,6 +1337,7 @@ impl DocView {
let page = page.min(self.page_count.saturating_sub(1));
self.goto = Some((page, 0.0));
self.current = page;
+ self.reset_coarse_state();
true
}
@@ -1346,12 +1362,14 @@ impl DocView {
pub fn rotate_view(&mut self, clockwise: bool) {
self.rotation = (self.rotation + if clockwise { 90 } else { 270 }) % 360;
self.goto = Some((self.current, 0.0));
+ self.reset_coarse_state();
}
/// Switch the zoom mode, keeping the current page in view.
pub fn set_fit(&mut self, fit: Fit) {
self.fit = fit;
self.goto = Some((self.current, 0.0));
+ self.reset_coarse_state();
}
/// Switch the base page display (Single or Two-page), leaving continuous scrolling as it is.
@@ -1363,6 +1381,7 @@ impl DocView {
}
self.layout = layout;
self.goto = Some((self.current, 0.0));
+ self.reset_coarse_state();
}
/// Whether the view scrolls continuously (as opposed to one page or spread at a time).
@@ -1376,6 +1395,7 @@ impl DocView {
if self.continuous != continuous {
self.continuous = continuous;
self.goto = Some((self.current, 0.0));
+ self.reset_coarse_state();
}
}
@@ -1391,6 +1411,7 @@ impl DocView {
if self.cover != cover {
self.cover = cover;
self.goto = Some((self.current, 0.0));
+ self.reset_coarse_state();
}
}
@@ -2318,6 +2339,12 @@ pub fn shortcuts(view: &mut DocView, ctx: &egui::Context, speed: crate::scroll_s
/// How far ↓ / ↑ scroll at Normal speed (Scroll speed halves or doubles it). Page Down / Page Up
/// keep this much of the previous screen in view.
const KEY_SCROLL_LINE: f32 = 40.0;
+/// Suppression window for egui's eased copy of point input.
+///
+/// egui 0.36.2 drains `WheelState::after_events` tail deltas by 90% per 0.1 s.
+const SUPPRESS_SECS: f64 = 0.25;
+/// Short rolling extension while egui still has an unprocessed wheel tail.
+const SUPPRESS_TAIL_SECS: f64 = 0.05;
pub fn document_area(app: &mut PdfCraftApp, index: usize, ui: &mut egui::Ui) {
let t = Tokens::get(ui.ctx());
@@ -2507,36 +2534,25 @@ pub fn document_area(app: &mut PdfCraftApp, index: usize, ui: &mut egui::Ui) {
ui.input_mut(|i| i.smooth_scroll_delta.y = 0.0);
}
}
- // Touchpad scrolling 1:1 (#759). A precision touchpad sends pixel-precise `MouseWheel`
- // events; on Windows they carry no `TouchPhase::Start`, so egui never takes its no-smoothing
- // path and eases deltas of 8 px or more, leaving the page a frame behind the finger. Apply
- // the precise delta ourselves this frame through the un-animated scroll path, and suppress
- // egui's eased copy while it decays so it neither doubles nor trails the motion. A coarse
- // mouse wheel keeps egui's easing (pleasant for chunky notches).
+ // Point input bypasses egui smoothing for 1:1 motion. Line input keeps egui's configured,
+ // smoothed base and adds only the accelerated surplus. While point suppression is active,
+ // reconstruct line and page input here so current coarse events survive without exposing an
+ // older eased tail.
let now = ui.input(|i| i.time);
- let mut precise_scroll = Vec2::ZERO;
- if ui.rect_contains_pointer(avail) {
- let mut coarse_wheel = false;
- ui.input(|i| {
- for e in &i.events {
- if let egui::Event::MouseWheel { unit, delta, modifiers, .. } = e {
- if modifiers.command || modifiers.ctrl {
- continue; // Zooming, not scrolling.
- }
- match unit {
- // Shift scrolls sideways, matching egui's own wheel handling.
- egui::MouseWheelUnit::Point if modifiers.shift => precise_scroll.x += delta.x + delta.y,
- egui::MouseWheelUnit::Point => precise_scroll += *delta,
- egui::MouseWheelUnit::Line | egui::MouseWheelUnit::Page => coarse_wheel = true,
- }
- }
- }
- });
- if coarse_wheel {
- view.precise_scroll_until = 0.0;
- } else if precise_scroll != Vec2::ZERO {
- view.precise_scroll_until = now + 0.25;
- }
+ let input = if ui.rect_contains_pointer(avail) {
+ // Match the line distance used by egui's other scroll areas.
+ let live = ui.ctx().options(|o| o.input_options.line_scroll_speed);
+ let line_points = if live.is_finite() && live > 0.0 { live } else { crate::scroll_speed::EGUI_LINE_POINTS };
+ ui.input(|i| crate::wheel_boost::accumulate(&i.events, line_points, single_paging))
+ } else {
+ crate::wheel_boost::FrameInput::default()
+ };
+ let mut precise_scroll = input.precise;
+ let mut line_base = input.line;
+ let mut boost_extra = crate::wheel_boost::boost_extra(input, &mut view.wheel_boost_x, &mut view.wheel_boost_y, now);
+ let has_page = input.page != Vec2::ZERO;
+ if now.is_finite() && precise_scroll != Vec2::ZERO && !has_page {
+ view.precise_scroll_until = now + SUPPRESS_SECS;
}
if edge_turned {
// The turn leaves egui's eased copy of the notch to decay for a few frames; it must not
@@ -2549,11 +2565,40 @@ pub fn document_area(app: &mut PdfCraftApp, index: usize, ui: &mut egui::Ui) {
let paging_owns_vertical = single_paging || (!view.continuous && view.wheel.gesture_turned());
if paging_owns_vertical {
precise_scroll.y = 0.0;
+ line_base.y = 0.0;
+ boost_extra.y = 0.0;
}
if now < view.precise_scroll_until {
+ if ui.input(|i| i.is_scrolling()) {
+ // Keep hiding egui's unprocessed copy until it has drained. The short rolling grace
+ // avoids a delayed duplicate without prolonging suppression after the tail is gone.
+ view.precise_scroll_until = view.precise_scroll_until.max(now + SUPPRESS_TAIL_SECS);
+ }
ui.input_mut(|i| i.smooth_scroll_delta = Vec2::ZERO);
+ // The point path suppresses every eased wheel delta. Restore the line base through the
+ // same un-eased path as its surplus; paging-owned axes were cleared above.
+ boost_extra = crate::wheel_boost::cap_frame(boost_extra + line_base);
+ if has_page {
+ // Page units normally stay on egui's viewport-sized path. During suppression only,
+ // reproduce the current event directly so an older hidden tail stays hidden.
+ let page_scale = ui.input(|i| i.viewport_rect().height());
+ let mut page_delta =
+ if page_scale.is_finite() && page_scale > 0.0 { crate::wheel_boost::cap_frame(input.page * page_scale) } else { Vec2::ZERO };
+ if paging_owns_vertical {
+ page_delta.y = 0.0;
+ }
+ boost_extra = crate::wheel_boost::cap_frame(boost_extra + page_delta);
+ }
} else {
precise_scroll = Vec2::ZERO;
+ if line_base != Vec2::ZERO || has_page {
+ // Selection handling below uses zero to distinguish an inactive window.
+ view.precise_scroll_until = 0.0;
+ }
+ }
+ if has_page && paging_owns_vertical {
+ // Page input belongs to egui on the free axis; the pager still owns vertical motion.
+ ui.input_mut(|i| i.smooth_scroll_delta.y = 0.0);
}
// Touchpad momentum (#531): a swipe over the page lifted at speed glides on and slows to a
// stop, through the same un-eased path. Never while the pager owns the wheel, a gesture or
@@ -2766,6 +2811,7 @@ pub fn document_area(app: &mut PdfCraftApp, index: usize, ui: &mut egui::Ui) {
scroll = scroll.vertical_scroll_offset((r.top() - y_shift + fy * r.height() - rel.y).clamp(0.0, max_y));
}
}
+ let selection_wheel = view.selection_drag && navigation_enabled && unobstructed && !single_paging && ui.rect_contains_pointer(avail);
let out = scroll.show_viewport(ui, |ui, viewport| {
// egui's drag responses accept every pointer button. Keep a wheel gesture from also
// selecting text, drawing a mark, or panning with the Hand tool.
@@ -2776,10 +2822,13 @@ pub fn document_area(app: &mut PdfCraftApp, index: usize, ui: &mut egui::Ui) {
ui.set_opacity(opacity);
}
// Middle-button auto-scroll, the keyboard, precise touchpad input (#759) and its glide
- // (#531), and a drawing's edge scroll; positive y moves the content down. All scroll
- // without easing, unlike egui's wheel default.
- let delta = auto_delta + edge_delta - vec2(0.0, key_scroll) + precise_scroll + glide;
- if delta != Vec2::ZERO {
+ // (#531), accelerated wheel surplus (#1072), and a drawing's edge scroll; positive y
+ // moves the content down. All scroll without easing, unlike egui's wheel default.
+ // ScrollArea ignores wheel input during a widget drag. Defer the wheel contribution
+ // until after the text widgets when a selection owns that drag.
+ let wheel_delta = if selection_wheel { Vec2::ZERO } else { precise_scroll + boost_extra };
+ let delta = auto_delta + edge_delta - vec2(0.0, key_scroll) + wheel_delta + glide;
+ if delta != Vec2::ZERO && delta.is_finite() {
ui.scroll_with_delta_animation(delta, egui::style::ScrollAnimation::none());
}
let (resp_rect, resp) = ui.allocate_exact_size(vec2(content_w, content_h), Sense::click_and_drag());
@@ -3512,17 +3561,13 @@ pub fn document_area(app: &mut PdfCraftApp, index: usize, ui: &mut egui::Ui) {
// ScrollArea ignores the wheel while any widget is dragged, so a text-selection drag
// scrolls the page itself (#721). After the page loop, so it keeps scrolling once the
// pointer leaves the selection's page for the gap or the next page; over the whole
- // document area, like #759. A Line/Page event clears precise_scroll_until; a Point
- // gesture's eased tail after its suppression deadline is not picked up.
- if view.selection_drag
- && navigation_enabled
- && unobstructed
- && !single_paging
- && view.precise_scroll_until == 0.0
- && ui.rect_contains_pointer(avail)
- {
- let delta = ui.input_mut(|input| std::mem::take(&mut input.smooth_scroll_delta));
- if delta != Vec2::ZERO {
+ // document area, like #759. Combine direct and smoothed motion in one request so mixed
+ // Point and Line events retain their full contribution.
+ if selection_wheel {
+ let smooth =
+ if view.precise_scroll_until == 0.0 { ui.input_mut(|input| std::mem::take(&mut input.smooth_scroll_delta)) } else { Vec2::ZERO };
+ let delta = precise_scroll + boost_extra + smooth;
+ if delta != Vec2::ZERO && delta.is_finite() {
ui.scroll_with_delta_animation(delta, egui::style::ScrollAnimation::none());
}
}
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 268214b16..7b421263d 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -139,6 +139,7 @@ mod tab_context;
pub mod theme;
pub mod ui_scale;
pub mod updates;
+mod wheel_boost;
mod wheel_pager;
mod widgets;
pub mod window_controls;
diff --git a/crates/ui-egui/src/scroll_speed.rs b/crates/ui-egui/src/scroll_speed.rs
index a2d857c74..cc3a7e86e 100644
--- a/crates/ui-egui/src/scroll_speed.rs
+++ b/crates/ui-egui/src/scroll_speed.rs
@@ -18,7 +18,7 @@ pub enum ScrollSpeed {
}
/// egui's own `line_scroll_speed`, which its wheel zoom rate is tuned against.
-const EGUI_LINE_POINTS: f32 = if cfg!(target_arch = "wasm32") { 8.0 } else { 40.0 };
+pub(crate) const EGUI_LINE_POINTS: f32 = if cfg!(target_arch = "wasm32") { 8.0 } else { 40.0 };
/// Points one wheel line scrolls at Normal speed.
const NORMAL_LINE_POINTS: f32 = if cfg!(target_arch = "wasm32") {
diff --git a/crates/ui-egui/src/wheel_boost.rs b/crates/ui-egui/src/wheel_boost.rs
new file mode 100644
index 000000000..b9d474565
--- /dev/null
+++ b/crates/ui-egui/src/wheel_boost.rs
@@ -0,0 +1,395 @@
+//! Bounded acceleration for coarse wheel input in the document viewport.
+//!
+//! Line-unit events describe notches, not motion over time. A fixed distance per
+//! notch makes a fast spin feel slow, so this module derives a bounded multiplier
+//! from arrival time and direction. The state is constant-size and leaves isolated
+//! notches unchanged.
+
+use egui::Vec2;
+
+/// Idle time after which a burst is forgotten, in seconds.
+const RESET_GAP_SECS: f64 = 0.3;
+/// Notch spacing at or below which input counts as a continuous spin, in seconds.
+const FAST_GAP_SECS: f64 = 0.08;
+/// Multiplier gained per fast consecutive notch.
+const BOOST_PER_TICK: f32 = 0.4;
+/// Hard limit for accelerated motion.
+const MAX_BOOST: f32 = 6.0;
+/// Ticks that affect the output.
+const MAX_TICKS: u32 = 13;
+/// Extra ticks retained beyond the ceiling. They absorb a few slow events
+/// before the visible multiplier starts to fall.
+const HYSTERESIS_TICKS: u32 = 4;
+/// Maximum scroll applied from one frame of coarse input, in points per axis.
+/// Each event is clamped first; this caps the sum.
+pub(crate) const MAX_FRAME_POINTS: f32 = 8192.0;
+/// Per-event clamp, in native units (lines or pages). Keeps one hostile event
+/// bounded before points conversion.
+pub(crate) const MAX_EVENT_UNITS: f32 = 10.0;
+
+/// Constant-size arrival-rate state for line wheel events.
+#[derive(Debug, Default)]
+pub(crate) struct WheelBoost {
+ last: f64,
+ burst: u32,
+ dir: i8,
+}
+
+impl WheelBoost {
+ /// Returns the multiplier for one bucket of motion carrying `motion`.
+ ///
+ /// `motion` is signed in native units; only its sign matters. Zero means
+ /// no motion. Non-finite time returns 1.0 and leaves state unchanged;
+ /// backwards time resets the burst and returns 1.0.
+ pub(crate) fn multiplier(&mut self, now: f64, motion: f32) -> f32 {
+ if !now.is_finite() || !motion.is_finite() || motion == 0.0 {
+ return 1.0;
+ }
+ let dir = if motion > 0.0 { 1 } else { -1 };
+ let idle = now - self.last;
+ if !idle.is_finite() || idle < 0.0 {
+ self.last = now;
+ self.burst = 0;
+ self.dir = dir;
+ return 1.0;
+ }
+ if idle > RESET_GAP_SECS || dir != self.dir {
+ self.burst = 0;
+ } else if idle <= FAST_GAP_SECS {
+ self.burst = self.burst.saturating_add(1).min(MAX_TICKS + HYSTERESIS_TICKS);
+ } else {
+ // Slow input in the same direction decays rather than extending the burst.
+ self.burst = self.burst.saturating_sub(1);
+ }
+ self.last = now;
+ self.dir = dir;
+ let ticks = self.burst.min(MAX_TICKS);
+ // MAX_TICKS is well within f32's exact integer range.
+ (1.0 + ticks as f32 * BOOST_PER_TICK).min(MAX_BOOST)
+ }
+
+ /// Drops burst state. Call after a programmatic scroll jump.
+ pub(crate) fn reset(&mut self) {
+ *self = Self::default();
+ }
+}
+
+/// Caps one frame of accumulated coarse scroll, per axis. A non-finite axis
+/// maps to zero; the other axis is preserved.
+pub(crate) fn cap_frame(delta: Vec2) -> Vec2 {
+ Vec2::new(
+ if delta.x.is_finite() { delta.x.clamp(-MAX_FRAME_POINTS, MAX_FRAME_POINTS) } else { 0.0 },
+ if delta.y.is_finite() { delta.y.clamp(-MAX_FRAME_POINTS, MAX_FRAME_POINTS) } else { 0.0 },
+ )
+}
+
+/// One frame of wheel input, normalized in a single pass. Line motion is in
+/// points; page motion remains in native units until the viewport is known.
+#[derive(Clone, Copy, Debug, Default, PartialEq)]
+pub(crate) struct FrameInput {
+ pub precise: Vec2,
+ pub line: Vec2,
+ pub page: Vec2,
+}
+
+/// Folds raw wheel events into frame totals without consulting time or UI state.
+/// Invalid scales contribute no line motion.
+pub(crate) fn accumulate(events: &[egui::Event], line_points: f32, single_paging: bool) -> FrameInput {
+ let line_points = sanitize_scale(line_points);
+ let mut out = FrameInput::default();
+ for event in events {
+ let egui::Event::MouseWheel { unit, delta, modifiers, .. } = event else { continue };
+ if modifiers.command || modifiers.ctrl {
+ continue; // Zooming, not scrolling.
+ }
+ if !delta.is_finite() {
+ continue;
+ }
+ match unit {
+ egui::MouseWheelUnit::Point if modifiers.shift => {
+ // Shift scrolls sideways, matching egui's own wheel handling.
+ let sum = delta.x + delta.y;
+ if sum.is_finite() {
+ out.precise.x += sum;
+ }
+ }
+ egui::MouseWheelUnit::Point => out.precise += *delta,
+ egui::MouseWheelUnit::Line if modifiers.shift => {
+ let sum = delta.x + delta.y;
+ if sum.is_finite() {
+ out.line.x += sum.clamp(-MAX_EVENT_UNITS, MAX_EVENT_UNITS) * line_points;
+ }
+ }
+ egui::MouseWheelUnit::Line => {
+ out.line.x += delta.x.clamp(-MAX_EVENT_UNITS, MAX_EVENT_UNITS) * line_points;
+ if !single_paging {
+ out.line.y += delta.y.clamp(-MAX_EVENT_UNITS, MAX_EVENT_UNITS) * line_points;
+ }
+ }
+ egui::MouseWheelUnit::Page if modifiers.shift => {
+ let sum = delta.x + delta.y;
+ if sum.is_finite() {
+ out.page.x += sum.clamp(-MAX_EVENT_UNITS, MAX_EVENT_UNITS);
+ }
+ }
+ egui::MouseWheelUnit::Page => {
+ out.page.x += delta.x.clamp(-MAX_EVENT_UNITS, MAX_EVENT_UNITS);
+ out.page.y += delta.y.clamp(-MAX_EVENT_UNITS, MAX_EVENT_UNITS);
+ }
+ }
+ }
+ // Bound batched events before acceleration is applied.
+ out.precise = cap_frame(out.precise);
+ out.line = cap_frame(out.line);
+ out.page = cap_frame(out.page);
+ out
+}
+
+/// Converts the frame total to the extra scroll delta beyond egui's share.
+///
+/// egui applies the base distance through its own smoothing. This function
+/// returns only the accelerated surplus, so an isolated notch adds nothing.
+/// Each axis advances once per frame; events batched into one frame cannot
+/// accelerate one another. Output is frame-capped.
+pub(crate) fn boost_extra(input: FrameInput, boost_x: &mut WheelBoost, boost_y: &mut WheelBoost, now: f64) -> Vec2 {
+ let mut extra = Vec2::ZERO;
+ if input.line.x != 0.0 && input.line.x.is_finite() {
+ extra.x += input.line.x * (boost_x.multiplier(now, input.line.x) - 1.0);
+ }
+ if input.line.y != 0.0 && input.line.y.is_finite() {
+ extra.y += input.line.y * (boost_y.multiplier(now, input.line.y) - 1.0);
+ }
+ cap_frame(extra)
+}
+
+/// Maps a non-finite or non-positive points-per-unit scale to zero.
+fn sanitize_scale(points: f32) -> f32 {
+ if points.is_finite() && points > 0.0 { points } else { 0.0 }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn single_and_slow_notches_stay_unboosted() {
+ let mut b = WheelBoost::default();
+ assert_eq!(b.multiplier(0.0, -1.0), 1.0);
+ assert_eq!(b.multiplier(0.5, -1.0), 1.0);
+ assert_eq!(b.multiplier(1.0, -1.0), 1.0);
+ }
+
+ #[test]
+ fn fast_spin_accelerates_then_caps() {
+ let mut b = WheelBoost::default();
+ assert_eq!(b.multiplier(1.0, -1.0), 1.0);
+ let mut last = 1.0;
+ for (i, t) in (1..=20).map(|i| 1.0 + f64::from(i) * 0.02).enumerate() {
+ last = b.multiplier(t, -1.0);
+ if i < 2 {
+ assert!(last >= 1.0, "early burst must not jump, got {last}");
+ }
+ }
+ assert!(last > 2.0, "a sustained fast spin must accelerate, got {last}");
+ assert!(last <= MAX_BOOST, "boost must be capped, got {last}");
+ assert_eq!(last, MAX_BOOST, "a long spin should reach the ceiling");
+ for i in 1..=20 {
+ let m = b.multiplier(1.5 + f64::from(i) * 0.02, -1.0);
+ assert!(m <= MAX_BOOST, "boost must stay capped, got {m}");
+ }
+ }
+
+ #[test]
+ fn reversal_and_idle_reset_the_burst() {
+ let mut b = WheelBoost::default();
+ b.multiplier(1.0, -1.0);
+ for i in 1..=6 {
+ b.multiplier(1.0 + f64::from(i) * 0.02, -1.0);
+ }
+ let boosted = b.multiplier(1.0 + 7.0 * 0.02, -1.0);
+ assert!(boosted > 1.0, "precondition: burst is boosted, got {boosted}");
+ assert_eq!(b.multiplier(1.0 + 8.0 * 0.02, 1.0), 1.0);
+ for i in 9..=14 {
+ b.multiplier(1.0 + f64::from(i) * 0.02, 1.0);
+ }
+ assert_eq!(b.multiplier(5.0, 1.0), 1.0);
+ }
+
+ #[test]
+ fn invalid_input_never_boosts_or_corrupts() {
+ let mut b = WheelBoost::default();
+ for bad_now in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
+ assert_eq!(b.multiplier(bad_now, -1.0), 1.0);
+ }
+ for bad_motion in [0.0, -0.0, f32::NAN, f32::INFINITY, f32::NEG_INFINITY] {
+ assert_eq!(b.multiplier(1.0, bad_motion), 1.0);
+ }
+ assert_eq!(b.multiplier(2.0, -1.0), 1.0);
+ b.reset();
+ assert_eq!(b.multiplier(3.0, -1.0), 1.0);
+ }
+
+ #[test]
+ fn same_timestamp_events_count_as_a_burst() {
+ let mut b = WheelBoost::default();
+ assert_eq!(b.multiplier(1.0, -1.0), 1.0);
+ for _ in 0..6 {
+ b.multiplier(1.0, -1.0);
+ }
+ assert!(b.multiplier(1.0, -1.0) > 1.0);
+ }
+
+ #[test]
+ fn fractional_notches_share_the_burst() {
+ let mut b = WheelBoost::default();
+ b.multiplier(1.0, -0.25);
+ for i in 1..=8 {
+ b.multiplier(1.0 + f64::from(i) * 0.02, -0.25);
+ }
+ assert!(b.multiplier(1.0 + 9.0 * 0.02, -0.25) > 1.0);
+ }
+
+ #[test]
+ fn sleep_wake_resets_the_burst() {
+ let mut b = WheelBoost::default();
+ b.multiplier(1.0, -1.0);
+ for i in 1..=6 {
+ b.multiplier(1.0 + f64::from(i) * 0.02, -1.0);
+ }
+ assert_eq!(b.multiplier(3600.0, -1.0), 1.0);
+ }
+
+ #[test]
+ fn backwards_time_resets_without_corrupting() {
+ let mut b = WheelBoost::default();
+ b.multiplier(1.0, -1.0);
+ b.multiplier(1.02, -1.0);
+ assert_eq!(b.multiplier(0.5, -1.0), 1.0);
+ assert_eq!(b.multiplier(0.52, -1.0), 1.4);
+ }
+
+ #[test]
+ fn unhurried_spin_decays() {
+ let mut b = WheelBoost::default();
+ b.multiplier(1.0, -1.0);
+ for i in 1..=8 {
+ b.multiplier(1.0 + f64::from(i) * 0.02, -1.0);
+ }
+ let peak = b.multiplier(1.0 + 9.0 * 0.02, -1.0);
+ assert!(peak > 1.0);
+ // Same direction but slower than the fast gap: each event sheds one tick.
+ let mut prev = peak;
+ for i in 10..=13 {
+ let m = b.multiplier(1.0 + f64::from(i) * 0.15, -1.0);
+ assert!(m <= prev, "decay must not grow: {m} > {prev}");
+ prev = m;
+ }
+ }
+
+ #[test]
+ fn hysteresis_holds_the_ceiling_briefly() {
+ let mut b = WheelBoost::default();
+ let mut t = 1.0;
+ b.multiplier(t, -1.0);
+ for _ in 0..20 {
+ t += 0.02;
+ b.multiplier(t, -1.0);
+ }
+ assert_eq!(b.multiplier(t + 0.02, -1.0), MAX_BOOST);
+ // A couple of unhurried events consume hysteresis before output falls.
+ t += 0.15;
+ assert_eq!(b.multiplier(t, -1.0), MAX_BOOST);
+ t += 0.15;
+ assert_eq!(b.multiplier(t, -1.0), MAX_BOOST);
+ }
+
+ #[test]
+ fn frame_cap_bounds_each_axis() {
+ use egui::vec2;
+ assert_eq!(cap_frame(vec2(40.0, -40.0)), vec2(40.0, -40.0));
+ assert_eq!(cap_frame(vec2(1e9, -1e9)), vec2(MAX_FRAME_POINTS, -MAX_FRAME_POINTS));
+ assert_eq!(cap_frame(vec2(f32::NAN, 40.0)), vec2(0.0, 40.0));
+ assert_eq!(cap_frame(vec2(40.0, f32::INFINITY)), vec2(40.0, 0.0));
+ }
+
+ fn wheel(unit: egui::MouseWheelUnit, dx: f32, dy: f32) -> egui::Event {
+ egui::Event::MouseWheel { unit, delta: egui::vec2(dx, dy), phase: egui::TouchPhase::Move, modifiers: egui::Modifiers::NONE }
+ }
+
+ fn shifted(unit: egui::MouseWheelUnit, dx: f32, dy: f32) -> egui::Event {
+ egui::Event::MouseWheel { unit, delta: egui::vec2(dx, dy), phase: egui::TouchPhase::Move, modifiers: egui::Modifiers::SHIFT }
+ }
+
+ #[test]
+ fn isolated_notch_adds_no_extra() {
+ use egui::MouseWheelUnit::Line;
+ for base in [50.0, 100.0, 200.0, 40.0, 8.0] {
+ let input = accumulate(&[wheel(Line, 0.0, -1.0)], base, false);
+ assert_eq!(input.line, egui::vec2(0.0, -base));
+ let mut bx = WheelBoost::default();
+ let mut by = WheelBoost::default();
+ assert_eq!(boost_extra(input, &mut bx, &mut by, 1.0), egui::Vec2::ZERO);
+ }
+ }
+
+ #[test]
+ fn fast_spin_adds_a_bounded_extra() {
+ use egui::MouseWheelUnit::Line;
+ let mut bx = WheelBoost::default();
+ let mut by = WheelBoost::default();
+ let mut peak = 0.0f32;
+ for i in 0..20 {
+ let input = accumulate(&[wheel(Line, 0.0, -1.0)], 100.0, false);
+ let extra = boost_extra(input, &mut bx, &mut by, 1.0 + f64::from(i) * 0.02);
+ assert!(extra.y <= 0.0, "extra follows the spin direction, got {extra:?}");
+ peak = peak.min(extra.y);
+ }
+ assert!(peak < 0.0, "a sustained spin must add an extra");
+ assert!(peak >= -MAX_FRAME_POINTS, "extra stays frame-capped, got {peak}");
+ }
+
+ #[test]
+ fn point_and_page_events_stay_out_of_the_line_boost() {
+ use egui::{MouseWheelUnit::Line, MouseWheelUnit::Page, MouseWheelUnit::Point};
+ let input = accumulate(&[wheel(Page, 0.0, -1.0), wheel(Point, 0.0, -30.0)], 100.0, false);
+ assert_eq!(input.precise, egui::vec2(0.0, -30.0));
+ assert_eq!(input.page, egui::vec2(0.0, -1.0));
+ assert_eq!(input.line, egui::Vec2::ZERO);
+ let input = accumulate(&[wheel(Line, 0.0, -1.0), wheel(Page, 0.0, -1.0)], 100.0, false);
+ assert_eq!(input.line, egui::vec2(0.0, -100.0));
+ assert_eq!(input.page, egui::vec2(0.0, -1.0));
+ let mut bx = WheelBoost::default();
+ let mut by = WheelBoost::default();
+ assert_eq!(boost_extra(input, &mut bx, &mut by, 1.0), egui::Vec2::ZERO);
+ }
+
+ #[test]
+ fn single_paging_drops_vertical_keeps_horizontal() {
+ use egui::MouseWheelUnit::Line;
+ let input = accumulate(&[wheel(Line, 5.0, -1.0)], 100.0, true);
+ assert_eq!(input.line, egui::vec2(500.0, 0.0));
+ }
+
+ #[test]
+ fn shift_merges_into_horizontal() {
+ use egui::MouseWheelUnit::{Line, Page};
+ let input = accumulate(&[shifted(Line, 1.0, -2.0)], 100.0, false);
+ assert_eq!(input.line, egui::vec2(-100.0, 0.0));
+ let input = accumulate(&[shifted(Page, 1.0, -2.0)], 100.0, false);
+ assert_eq!(input.page, egui::vec2(-1.0, 0.0));
+ }
+
+ #[test]
+ fn invalid_scales_and_events_contribute_nothing() {
+ use egui::MouseWheelUnit::Line;
+ for bad in [f32::NAN, 0.0, -10.0, f32::INFINITY] {
+ let input = accumulate(&[wheel(Line, 0.0, -1.0)], bad, false);
+ assert_eq!(input.line, egui::Vec2::ZERO, "line scale {bad}");
+ }
+ let zoom =
+ egui::Event::MouseWheel { unit: Line, delta: egui::vec2(0.0, -1.0), phase: egui::TouchPhase::Move, modifiers: egui::Modifiers::COMMAND };
+ let key = egui::Event::Key { key: egui::Key::Escape, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::NONE };
+ let input = accumulate(&[zoom, key, wheel(Line, 0.0, f32::NAN)], 100.0, false);
+ assert_eq!(input, FrameInput::default());
+ }
+}
diff --git a/crates/ui-egui/tests/view.rs b/crates/ui-egui/tests/view.rs
index 60150c931..9b6d57bf6 100644
--- a/crates/ui-egui/tests/view.rs
+++ b/crates/ui-egui/tests/view.rs
@@ -392,10 +392,78 @@ fn precise_touchpad_wheel_scrolls_one_to_one() {
assert!(drift.abs() < 2.0, "no easing tail after the swipe: drifted {drift}");
}
+#[test]
+fn rapid_wheel_notches_accelerate_and_follow_touchpad_input() {
+ use pdfcraft_ui_egui::scroll_speed::ScrollSpeed;
+
+ let mut h = harness_stepping(1.0 / 60.0, &[("layout", "continuous"), ("zoom", "300")]);
+ h.state_mut().set_option("animation_effects", "off").unwrap();
+ let at = h.state().views[0].viewport_rect().center();
+ h.hover_at(at);
+ h.run_steps(2);
+
+ h.event(wheel(MouseWheelUnit::Line, -1.0, TouchPhase::Move));
+ let single = scroll_over(&mut h, 2);
+ let base = ScrollSpeed::default().line_points();
+ assert!((single - base).abs() < 1.0, "one notch keeps the configured base: moved {single}, base {base}");
+
+ // Let the isolated notch expire, then spin five notches one frame apart. The burst must move
+ // farther than five base steps without adding work on idle frames.
+ h.run_steps(30);
+ let mut burst = 0.0;
+ for _ in 0..5 {
+ h.event(wheel(MouseWheelUnit::Line, -1.0, TouchPhase::Move));
+ burst += scroll_over(&mut h, 2);
+ }
+ assert!(burst > 7.0 * base, "a rapid five-notch spin accelerates: moved {burst}, base {base}");
+
+ // A line notch inside the precise path's suppression window keeps its base share (#1072):
+ // suppressing egui's touchpad tail must not swallow mixed-device input.
+ h.run_steps(30);
+ h.event(wheel(MouseWheelUnit::Point, -20.0, TouchPhase::Move));
+ scroll_over(&mut h, 2);
+ // Arrive near the old suppression deadline: the line's new easing tail must extend it.
+ h.run_steps(11);
+ h.event(wheel(MouseWheelUnit::Line, -1.0, TouchPhase::Move));
+ let after_touchpad = scroll_over(&mut h, 2);
+ assert!(after_touchpad >= base, "a notch after point input is not dropped: moved {after_touchpad}, base {base}");
+ let delayed = scroll_over(&mut h, 20);
+ assert!(delayed.abs() < 2.0, "the reconstructed notch has no delayed easing copy: moved {delayed}");
+
+ h.event(wheel(MouseWheelUnit::Point, -20.0, TouchPhase::Move));
+ scroll_over(&mut h, 2);
+ h.event(wheel(MouseWheelUnit::Page, -1.0, TouchPhase::Move));
+ let page_after_touchpad = scroll_over(&mut h, 2);
+ assert!(page_after_touchpad > base, "a page event after point input is preserved: moved {page_after_touchpad}");
+ let delayed_page = scroll_over(&mut h, 20);
+ assert!(delayed_page.abs() < 2.0, "the reconstructed page has no delayed easing copy: moved {delayed_page}");
+}
+
+#[test]
+fn changing_continuous_layout_resets_wheel_acceleration() {
+ use pdfcraft_ui_egui::scroll_speed::ScrollSpeed;
+
+ let mut h = harness_stepping(1.0 / 60.0, &[("layout", "continuous"), ("zoom", "300")]);
+ h.state_mut().set_option("animation_effects", "off").unwrap();
+ let at = h.state().views[0].viewport_rect().center();
+ h.hover_at(at);
+ h.run_steps(2);
+ for _ in 0..5 {
+ h.event(wheel(MouseWheelUnit::Line, -1.0, TouchPhase::Move));
+ scroll_over(&mut h, 2);
+ }
+
+ h.state_mut().views[0].set_continuous(false);
+ h.run_steps(3);
+ h.event(wheel(MouseWheelUnit::Line, -1.0, TouchPhase::Move));
+ let moved = scroll_over(&mut h, 2);
+ let base = ScrollSpeed::default().line_points();
+ assert!((moved - base).abs() < 1.0, "a layout jump clears the old burst: moved {moved}, base {base}");
+}
+
/// Steps `frames` frames and returns how far the document scrolled down over them, from the
/// pages on screen in each pair of consecutive frames (a page can leave the screen on the way).
-/// Only the non-macOS glide tests below use it, hence the matching gate.
-#[cfg(not(target_os = "macos"))]
+/// Used by wheel and non-macOS glide tests below.
fn scroll_over(h: &mut Harness<'static, PdfCraftApp>, frames: usize) -> f32 {
let tops = |h: &Harness<'static, PdfCraftApp>| (0..3).map(|p| rect(h, p).map(|r| r.min.y)).collect::<Vec<_>>();
let mut moved = 0.0;
diff --git a/parity/acrobat-features.toml b/parity/acrobat-features.toml
index 1a608878e..b1805d522 100644
--- a/parity/acrobat-features.toml
+++ b/parity/acrobat-features.toml
@@ -854,8 +854,8 @@ milestone = "M3"
status = "shipped"
commands = ["view.continuous"]
tools = []
-evidence = ["crates/ui-egui/tests/view.rs::continuous_layout_stacks_pages_vertically", "crates/ui-egui/tests/view.rs::page_display_commands_switch_layouts", "crates/ui-egui/tests/view.rs::default_page_display_is_used_for_new_documents", "crates/ui-egui/src/canvas.rs::fit_page_and_height_hold_still_while_mixed_page_sizes_scroll_past", "crates/ui-egui/tests/view.rs::touchpad_swipe_glides_on_after_the_fingers_lift_and_stops", "crates/ui-egui/tests/view.rs::touching_the_touchpad_again_or_turning_animations_off_stops_the_glide"]
-notes = "the default for PDFs that don't ask for a layout (Preferences ▸ Default page display); pages stack with no snapping. Fit page and Fit height fit the largest page, so the zoom holds still across mixed page sizes. A touchpad swipe glides on after the fingers lift on Wayland (#531; macOS glides by itself; Windows and X11 report no lift through winit), unless Animation effects is off; a click, key, zoom or new scroll catches it. Not yet: fingers resting on the touchpad don't catch it (needs Wayland hold gestures, which the vendored winit doesn't bind)"
+evidence = ["crates/ui-egui/tests/view.rs::continuous_layout_stacks_pages_vertically", "crates/ui-egui/tests/view.rs::page_display_commands_switch_layouts", "crates/ui-egui/tests/view.rs::default_page_display_is_used_for_new_documents", "crates/ui-egui/src/canvas.rs::fit_page_and_height_hold_still_while_mixed_page_sizes_scroll_past", "crates/ui-egui/tests/view.rs::touchpad_swipe_glides_on_after_the_fingers_lift_and_stops", "crates/ui-egui/tests/view.rs::touching_the_touchpad_again_or_turning_animations_off_stops_the_glide", "crates/ui-egui/tests/view.rs::rapid_wheel_notches_accelerate_and_follow_touchpad_input", "crates/ui-egui/tests/view.rs::changing_continuous_layout_resets_wheel_acceleration"]
+notes = "the default for PDFs that don't ask for a layout (Preferences ▸ Default page display); pages stack with no snapping. Fit page and Fit height fit the largest page, so the zoom holds still across mixed page sizes. Rapid line-wheel notches gain a bounded, constant-space arrival-rate boost (#1072); isolated notches still use the chosen Scroll speed, and page units stay unboosted. A touchpad swipe glides on after the fingers lift on Wayland (#531; macOS glides by itself; Windows and X11 report no lift through winit), unless Animation effects is off; a click, key, zoom or new scroll catches it. Not yet: fingers resting on the touchpad don't catch it (needs Wayland hold gestures, which the vendored winit doesn't bind)"
[[feature]]
id = "view.layout-single-page"