use winit::event::MouseScrollDelta;
use super::WHEEL_LINE;
const NOTCH_PIXELS: f32 = 100.0;
const NOTCH_LINES: f32 = 3.0;
const NOTCH_TOLERANCE: f32 = 0.1;
fn notches(physical: f64, scale: f64) -> f32 {
(physical / scale.max(f64::MIN_POSITIVE)) as f32 / NOTCH_PIXELS
}
fn whole_notches(x: f32, y: f32) -> bool {
let whole = |notches: f32| (notches - notches.round()).abs() <= NOTCH_TOLERANCE * notches.round().abs();
whole(x) && whole(y) && (x.round() != 0.0 || y.round() != 0.0)
}
pub(super) fn scroll_distance(delta: MouseScrollDelta, scale: f64, system_distance: bool) -> (f32, f32) {
match delta {
MouseScrollDelta::LineDelta(x, y) => (x / NOTCH_LINES * WHEEL_LINE, y / NOTCH_LINES * WHEEL_LINE),
MouseScrollDelta::PixelDelta(physical) if system_distance => (physical.x as f32, physical.y as f32),
MouseScrollDelta::PixelDelta(physical) => {
let (mut x, mut y) = (notches(physical.x, scale), notches(physical.y, scale));
if whole_notches(x, y) {
(x, y) = (x.round(), y.round());
}
(x * WHEEL_LINE, y * WHEEL_LINE)
}
}
}
pub(super) fn is_notched(delta: MouseScrollDelta, scale: f64, system_distance: bool) -> bool {
match delta {
MouseScrollDelta::LineDelta(x, y) => x.fract() == 0.0 && y.fract() == 0.0,
MouseScrollDelta::PixelDelta(_) if system_distance => false,
MouseScrollDelta::PixelDelta(physical) => whole_notches(notches(physical.x, scale), notches(physical.y, scale)),
}
}
#[cfg(test)]
mod tests {
use winit::dpi::PhysicalPosition;
use super::*;
fn pixels(x: f64, y: f64) -> MouseScrollDelta {
MouseScrollDelta::PixelDelta(PhysicalPosition::new(x, y))
}
#[test]
fn a_notch_scrolls_as_far_as_on_a_desktop_whatever_the_scale() {
assert_eq!(scroll_distance(pixels(0.0, -100.0), 1.0, false), (0.0, -WHEEL_LINE));
assert_eq!(scroll_distance(pixels(0.0, -150.0), 1.5, false), (0.0, -WHEEL_LINE));
assert_eq!(scroll_distance(pixels(200.0, 0.0), 2.0, false), (WHEEL_LINE, 0.0));
}
#[test]
fn a_touchpads_step_is_a_part_of_a_notch() {
let (_, y) = scroll_distance(pixels(0.0, 15.0), 1.5, false);
assert!((y - WHEEL_LINE * 0.1).abs() < 1e-4, "{y}");
}
#[test]
fn a_notch_told_in_lines_is_one_notch() {
assert_eq!(
scroll_distance(MouseScrollDelta::LineDelta(0.0, -3.0), 1.5, false),
(0.0, -WHEEL_LINE)
);
}
#[test]
fn the_systems_distance_is_taken_as_it_is() {
assert_eq!(scroll_distance(pixels(4.0, -30.0), 2.0, true), (4.0, -30.0));
}
#[test]
fn a_notch_is_a_mouses_whatever_the_scale() {
for scale in [1.0, 1.25, 1.5, 2.0] {
assert!(is_notched(pixels(0.0, -100.0 * scale), scale, false), "down at {scale}");
assert!(is_notched(pixels(0.0, 100.0 * scale), scale, false), "up at {scale}");
}
}
#[test]
fn a_notch_told_a_little_off_is_a_mouses_and_scrolls_a_notch() {
for (told, scale) in [(99.999_997_8, 1.1), (102.0, 1.5), (108.0, 1.25), (108.0, 3.0)] {
let delta = pixels(0.0, -told * scale);
assert!(is_notched(delta, scale, false), "{told} at {scale}");
assert_eq!(
scroll_distance(delta, scale, false),
(0.0, -WHEEL_LINE),
"{told} at {scale}"
);
}
assert_eq!(
scroll_distance(pixels(0.0, 216.0 * 2.0), 2.0, false),
(0.0, 2.0 * WHEEL_LINE)
);
}
#[test]
fn notches_told_as_one_event_are_a_mouses() {
assert!(is_notched(pixels(0.0, -300.0 * 1.5), 1.5, false), "a fast spin");
assert!(
is_notched(pixels(100.0 * 1.25, 0.0), 1.25, false),
"a wheel tilted sideways"
);
}
#[test]
fn a_touchpads_step_is_not_a_notch() {
assert!(!is_notched(pixels(0.0, 15.0), 1.5, false));
assert!(!is_notched(pixels(-3.0, 22.5), 1.0, false));
assert!(
!is_notched(pixels(0.0, -60.0 * 1.25), 1.25, false),
"more than half a notch"
);
assert!(!is_notched(pixels(0.0, -140.0 * 2.0), 2.0, false), "a fast swipe");
assert!(!is_notched(pixels(0.0, 88.0), 1.0, false), "most of a notch");
assert!(
!is_notched(pixels(0.0, 80.0 * 1.25), 1.25, false),
"a notch on a page zoomed to 125%"
);
assert!(
!is_notched(pixels(4.0 * 1.5, -100.0 * 1.5), 1.5, false),
"a slanted swipe that is a notch long"
);
assert!(!is_notched(pixels(0.0, 0.0), 1.0, false), "no travel");
}
#[test]
fn the_systems_distance_is_not_a_notch() {
assert!(!is_notched(pixels(0.0, -100.0), 1.0, true));
assert!(!is_notched(pixels(0.0, 200.0), 2.0, true));
}
#[test]
fn a_whole_number_of_lines_is_a_mouses_notches() {
assert!(is_notched(MouseScrollDelta::LineDelta(0.0, -3.0), 1.5, false));
assert!(is_notched(MouseScrollDelta::LineDelta(1.0, 0.0), 1.0, true));
assert!(!is_notched(MouseScrollDelta::LineDelta(0.0, 0.25), 1.0, false));
}
}