sim_logic/application/input/
scroll.rs1use std::{error::Error, fmt};
4
5use super::{Action, ActionEdge, PointerSample};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9pub enum ScrollUnit {
10 Lines,
12 Pixels,
14}
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
22pub struct ScrollDelta {
23 unit: ScrollUnit,
24 bits: [u64; 2],
25}
26
27impl ScrollDelta {
28 pub const MAX_DISPLACEMENT: f64 = 1_000_000.0;
30
31 pub fn lines(x: f64, y: f64) -> Result<Self, ScrollDeltaError> {
33 Self::new(ScrollUnit::Lines, x, y)
34 }
35
36 pub fn pixels(x: f64, y: f64) -> Result<Self, ScrollDeltaError> {
38 Self::new(ScrollUnit::Pixels, x, y)
39 }
40
41 fn new(unit: ScrollUnit, x: f64, y: f64) -> Result<Self, ScrollDeltaError> {
42 if !x.is_finite() || !y.is_finite() {
43 return Err(ScrollDeltaError::NonFinite);
44 }
45 if x.abs() > Self::MAX_DISPLACEMENT || y.abs() > Self::MAX_DISPLACEMENT {
46 return Err(ScrollDeltaError::OutOfRange);
47 }
48 Ok(Self {
49 unit,
50 bits: [canonical_bits(x), canonical_bits(y)],
51 })
52 }
53
54 pub const fn unit(self) -> ScrollUnit {
56 self.unit
57 }
58
59 pub const fn x(self) -> f64 {
61 f64::from_bits(self.bits[0])
62 }
63
64 pub const fn y(self) -> f64 {
66 f64::from_bits(self.bits[1])
67 }
68}
69
70fn canonical_bits(value: f64) -> u64 {
71 if value == 0.0 { 0 } else { value.to_bits() }
72}
73
74#[derive(Debug, Clone, Copy, PartialEq, Eq)]
76pub enum ScrollDeltaError {
77 NonFinite,
79 OutOfRange,
81}
82
83impl fmt::Display for ScrollDeltaError {
84 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
85 formatter.write_str(match self {
86 Self::NonFinite => "wheel displacement must be finite",
87 Self::OutOfRange => "wheel displacement exceeds the per-event limit",
88 })
89 }
90}
91
92impl Error for ScrollDeltaError {}
93
94#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
100pub struct PointerScrollEvent {
101 pub(super) delta: ScrollDelta,
102 pub(super) pointer: Option<PointerSample>,
103 pub(super) ordinal: usize,
104}
105
106impl PointerScrollEvent {
107 pub const fn delta(self) -> ScrollDelta {
109 self.delta
110 }
111
112 pub const fn pointer(self) -> Option<PointerSample> {
115 self.pointer
116 }
117}
118
119#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
125pub enum FrameInputEvent<A: Action> {
126 Action(ActionEdge<A>),
128 Scroll(PointerScrollEvent),
130}
131
132pub(super) fn ordered_events<A: Action>(
133 edges: &[ActionEdge<A>],
134 scrolls: &[PointerScrollEvent],
135) -> impl Iterator<Item = FrameInputEvent<A>> {
136 let mut edges = edges.iter().copied().peekable();
137 let mut scrolls = scrolls.iter().copied().peekable();
138 std::iter::from_fn(move || match (edges.peek(), scrolls.peek()) {
139 (Some(edge), Some(scroll)) if edge.ordinal <= scroll.ordinal => {
140 edges.next().map(FrameInputEvent::Action)
141 }
142 (Some(_), Some(_)) | (None, Some(_)) => scrolls.next().map(FrameInputEvent::Scroll),
143 (Some(_), None) => edges.next().map(FrameInputEvent::Action),
144 (None, None) => None,
145 })
146}