dioxus_html/events/
wheel.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
use dioxus_core::Event;
use std::fmt::Formatter;

use crate::geometry::*;
use crate::input_data::{MouseButton, MouseButtonSet};
use crate::prelude::*;

use super::HasMouseData;

/// A synthetic event that wraps a web-style
/// [`WheelEvent`](https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent)
pub type WheelEvent = Event<WheelData>;

/// Data associated with a [WheelEvent]
pub struct WheelData {
    inner: Box<dyn HasWheelData>,
}

impl<E: HasWheelData> From<E> for WheelData {
    fn from(e: E) -> Self {
        Self { inner: Box::new(e) }
    }
}

impl std::fmt::Debug for WheelData {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("WheelData")
            .field("delta", &self.delta())
            .field("coordinates", &self.coordinates())
            .field("modifiers", &self.modifiers())
            .field("held_buttons", &self.held_buttons())
            .field("trigger_button", &self.trigger_button())
            .finish()
    }
}

impl PartialEq for WheelData {
    fn eq(&self, other: &Self) -> bool {
        self.inner.delta() == other.inner.delta()
    }
}

impl WheelData {
    pub fn new(inner: impl HasWheelData + 'static) -> Self {
        Self {
            inner: Box::new(inner),
        }
    }

    /// The amount of wheel movement
    #[allow(deprecated)]
    pub fn delta(&self) -> WheelDelta {
        self.inner.delta()
    }

    /// Downcast this event to a concrete event type
    #[inline(always)]
    pub fn downcast<T: 'static>(&self) -> Option<&T> {
        HasWheelData::as_any(&*self.inner).downcast_ref::<T>()
    }
}

impl InteractionLocation for WheelData {
    fn client_coordinates(&self) -> ClientPoint {
        self.inner.client_coordinates()
    }

    fn page_coordinates(&self) -> PagePoint {
        self.inner.page_coordinates()
    }

    fn screen_coordinates(&self) -> ScreenPoint {
        self.inner.screen_coordinates()
    }
}

impl InteractionElementOffset for WheelData {
    fn element_coordinates(&self) -> ElementPoint {
        self.inner.element_coordinates()
    }

    fn coordinates(&self) -> Coordinates {
        self.inner.coordinates()
    }
}

impl ModifiersInteraction for WheelData {
    fn modifiers(&self) -> Modifiers {
        self.inner.modifiers()
    }
}

impl PointerInteraction for WheelData {
    fn held_buttons(&self) -> MouseButtonSet {
        self.inner.held_buttons()
    }

    // todo the following is kind of bad; should we just return None when the trigger_button is unreliable (and frankly irrelevant)? i guess we would need the event_type here
    fn trigger_button(&self) -> Option<MouseButton> {
        self.inner.trigger_button()
    }
}

#[cfg(feature = "serialize")]
/// A serialized version of WheelData
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
pub struct SerializedWheelData {
    #[serde(flatten)]
    pub mouse: crate::point_interaction::SerializedPointInteraction,

    pub delta_mode: u32,
    pub delta_x: f64,
    pub delta_y: f64,
    pub delta_z: f64,
}

#[cfg(feature = "serialize")]
impl SerializedWheelData {
    /// Create a new SerializedWheelData
    pub fn new(wheel: &WheelData) -> Self {
        let delta_mode = match wheel.delta() {
            WheelDelta::Pixels(_) => 0,
            WheelDelta::Lines(_) => 1,
            WheelDelta::Pages(_) => 2,
        };
        let delta_raw = wheel.delta().strip_units();
        Self {
            mouse: crate::point_interaction::SerializedPointInteraction::from(wheel),
            delta_mode,
            delta_x: delta_raw.x,
            delta_y: delta_raw.y,
            delta_z: delta_raw.z,
        }
    }
}

#[cfg(feature = "serialize")]
impl From<&WheelData> for SerializedWheelData {
    fn from(data: &WheelData) -> Self {
        Self::new(data)
    }
}

#[cfg(feature = "serialize")]
impl HasWheelData for SerializedWheelData {
    fn delta(&self) -> WheelDelta {
        WheelDelta::from_web_attributes(self.delta_mode, self.delta_x, self.delta_y, self.delta_z)
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

#[cfg(feature = "serialize")]
impl HasMouseData for SerializedWheelData {
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

#[cfg(feature = "serialize")]
impl InteractionLocation for SerializedWheelData {
    fn client_coordinates(&self) -> ClientPoint {
        self.mouse.client_coordinates()
    }

    fn page_coordinates(&self) -> PagePoint {
        self.mouse.page_coordinates()
    }

    fn screen_coordinates(&self) -> ScreenPoint {
        self.mouse.screen_coordinates()
    }
}

#[cfg(feature = "serialize")]
impl InteractionElementOffset for SerializedWheelData {
    fn element_coordinates(&self) -> ElementPoint {
        self.mouse.element_coordinates()
    }

    fn coordinates(&self) -> Coordinates {
        self.mouse.coordinates()
    }
}

#[cfg(feature = "serialize")]
impl ModifiersInteraction for SerializedWheelData {
    fn modifiers(&self) -> Modifiers {
        self.mouse.modifiers()
    }
}

#[cfg(feature = "serialize")]
impl PointerInteraction for SerializedWheelData {
    fn held_buttons(&self) -> MouseButtonSet {
        self.mouse.held_buttons()
    }

    fn trigger_button(&self) -> Option<MouseButton> {
        self.mouse.trigger_button()
    }
}

#[cfg(feature = "serialize")]
impl serde::Serialize for WheelData {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        SerializedWheelData::from(self).serialize(serializer)
    }
}

#[cfg(feature = "serialize")]
impl<'de> serde::Deserialize<'de> for WheelData {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let data = SerializedWheelData::deserialize(deserializer)?;
        Ok(Self {
            inner: Box::new(data),
        })
    }
}

impl_event![
    WheelData;

    /// Called when the mouse wheel is rotated over an element.
    onwheel
];

pub trait HasWheelData: HasMouseData + std::any::Any {
    /// The amount of wheel movement
    fn delta(&self) -> WheelDelta;

    /// return self as Any
    fn as_any(&self) -> &dyn std::any::Any;
}