use agg_gui::wheel::{to_notches, WheelDeltaMode};
use agg_gui::{App, Modifiers};
use winit::event::{MouseScrollDelta, Touch, TouchPhase};
pub(crate) fn wheel_delta(delta: MouseScrollDelta, shift: bool) -> (f64, f64) {
let (mut dx, mut dy) = match delta {
MouseScrollDelta::LineDelta(x, y) => (x as f64, y as f64),
MouseScrollDelta::PixelDelta(p) => (
to_notches(p.x, WheelDeltaMode::Pixel),
to_notches(p.y, WheelDeltaMode::Pixel),
),
};
if shift && dx == 0.0 {
dx = dy;
dy = 0.0;
}
(dx, dy)
}
pub(crate) fn dispatch_touch(app: &mut App, touch: Touch) {
let dev = agg_gui::TouchDeviceId(0);
let tid = agg_gui::TouchId(touch.id);
let (x, y) = (touch.location.x, touch.location.y);
let force = touch.force.map(|f| f.normalized() as f32);
match touch.phase {
TouchPhase::Started => app.on_touch_start(dev, tid, x, y, force),
TouchPhase::Moved => app.on_touch_move(dev, tid, x, y, force),
TouchPhase::Ended => app.on_touch_end(dev, tid),
TouchPhase::Cancelled => app.on_touch_cancel(dev, tid),
}
}
pub(crate) fn shift_held(mods: Modifiers) -> bool {
mods.shift
}
#[cfg(windows)]
pub(crate) fn live_cursor_in_window(window: &winit::window::Window) -> Option<(f64, f64)> {
use windows_sys::Win32::Foundation::POINT;
use windows_sys::Win32::UI::WindowsAndMessaging::GetCursorPos;
let mut pt = POINT { x: 0, y: 0 };
if unsafe { GetCursorPos(&mut pt) } == 0 {
return None;
}
let client_origin = window.inner_position().ok()?;
Some((
(pt.x - client_origin.x) as f64,
(pt.y - client_origin.y) as f64,
))
}
#[cfg(not(windows))]
pub(crate) fn live_cursor_in_window(_window: &winit::window::Window) -> Option<(f64, f64)> {
None
}
#[cfg(test)]
mod tests {
use super::*;
use winit::dpi::PhysicalPosition;
#[test]
fn line_deltas_pass_through_unnegated() {
assert_eq!(
wheel_delta(MouseScrollDelta::LineDelta(0.0, 3.0), false),
(0.0, 3.0)
);
assert_eq!(
wheel_delta(MouseScrollDelta::LineDelta(0.0, -3.0), false),
(0.0, -3.0)
);
}
#[test]
fn pixel_deltas_convert_to_lines() {
assert_eq!(
wheel_delta(
MouseScrollDelta::PixelDelta(PhysicalPosition::new(0.0, 40.0)),
false
),
(0.0, 1.0)
);
}
#[test]
fn shift_remaps_a_vertical_wheel_to_horizontal() {
assert_eq!(
wheel_delta(MouseScrollDelta::LineDelta(0.0, 2.0), true),
(2.0, 0.0)
);
}
#[test]
fn shift_leaves_a_device_that_already_scrolls_horizontally_alone() {
assert_eq!(
wheel_delta(MouseScrollDelta::LineDelta(1.0, 2.0), true),
(1.0, 2.0)
);
}
}