dioxus_html/events/pointer.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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
use dioxus_core::Event;
use keyboard_types::Modifiers;
use crate::{geometry::*, input_data::*, prelude::*};
/// A synthetic event that wraps a web-style [`PointerEvent`](https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent)
pub type PointerEvent = Event<PointerData>;
pub struct PointerData {
inner: Box<dyn HasPointerData>,
}
impl PointerData {
/// Create a new PointerData
pub fn new(data: impl HasPointerData + 'static) -> Self {
Self::from(data)
}
/// Downcast this event to a concrete event type
#[inline(always)]
pub fn downcast<T: 'static>(&self) -> Option<&T> {
self.inner.as_any().downcast_ref::<T>()
}
}
impl<E: HasPointerData + 'static> From<E> for PointerData {
fn from(e: E) -> Self {
Self { inner: Box::new(e) }
}
}
impl std::fmt::Debug for PointerData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("PointerData")
.field("pointer_id", &self.pointer_id())
.field("width", &self.width())
.field("height", &self.height())
.field("pressure", &self.pressure())
.field("tangential_pressure", &self.tangential_pressure())
.field("tilt_x", &self.tilt_x())
.field("tilt_y", &self.tilt_y())
.field("twist", &self.twist())
.field("pointer_type", &self.pointer_type())
.field("is_primary", &self.is_primary())
.field("coordinates", &self.coordinates())
.field("modifiers", &self.modifiers())
.field("held_buttons", &self.held_buttons())
.field("trigger_button", &self.trigger_button())
.finish()
}
}
impl PartialEq for PointerData {
fn eq(&self, other: &Self) -> bool {
self.pointer_id() == other.pointer_id()
&& self.width() == other.width()
&& self.height() == other.height()
&& self.pressure() == other.pressure()
&& self.tangential_pressure() == other.tangential_pressure()
&& self.tilt_x() == other.tilt_x()
&& self.tilt_y() == other.tilt_y()
&& self.twist() == other.twist()
&& self.pointer_type() == other.pointer_type()
&& self.is_primary() == other.is_primary()
&& self.coordinates() == other.coordinates()
&& self.modifiers() == other.modifiers()
&& self.held_buttons() == other.held_buttons()
&& self.trigger_button() == other.trigger_button()
}
}
/// A trait for any object that has the data for a pointer event
pub trait HasPointerData: PointerInteraction {
/// Gets the unique identifier of the pointer causing the event.
fn pointer_id(&self) -> i32;
/// Gets the width (magnitude on the X axis), in CSS pixels, of the contact geometry of the pointer.
fn width(&self) -> i32;
/// Gets the height (magnitude on the Y axis), in CSS pixels, of the contact geometry of the pointer.
fn height(&self) -> i32;
/// Gets the normalized pressure of the pointer input in the range of 0 to 1,
fn pressure(&self) -> f32;
/// Gets the normalized tangential pressure of the pointer input (also known as barrel pressure or cylinder stress) in the range -1 to 1,
fn tangential_pressure(&self) -> f32;
/// Gets the plane angle (in degrees, in the range of -90 to 90) between the Y-Z plane and the plane containing both the transducer (e.g. pen stylus) axis and the Y axis.
fn tilt_x(&self) -> i32;
/// Gets the plane angle (in degrees, in the range of -90 to 90) between the X-Z plane and the plane containing both the transducer (e.g. pen stylus) axis and the X axis.
fn tilt_y(&self) -> i32;
/// Gets the clockwise rotation of the pointer (e.g. pen stylus) around its major axis in degrees, with a value in the range 0 to 359.The clockwise rotation of the pointer (e.g. pen stylus) around its major axis in degrees, with a value in the range 0 to 359.
fn twist(&self) -> i32;
/// Gets the device type that caused the event (mouse, pen, touch, etc.).
fn pointer_type(&self) -> String;
/// Gets if the pointer represents the primary pointer of this pointer type.
fn is_primary(&self) -> bool;
/// return self as Any
fn as_any(&self) -> &dyn std::any::Any;
}
impl_event![
PointerData;
/// pointerdown
onpointerdown
/// pointermove
onpointermove
/// pointerup
onpointerup
/// pointercancel
onpointercancel
/// gotpointercapture
ongotpointercapture
/// lostpointercapture
onlostpointercapture
/// pointerenter
onpointerenter
/// pointerleave
onpointerleave
/// pointerover
onpointerover
/// pointerout
onpointerout
];
impl PointerData {
/// Gets the unique identifier of the pointer causing the event.
pub fn pointer_id(&self) -> i32 {
self.inner.pointer_id()
}
/// Gets the width (magnitude on the X axis), in CSS pixels, of the contact geometry of the pointer.
pub fn width(&self) -> i32 {
self.inner.width()
}
/// Gets the height (magnitude on the Y axis), in CSS pixels, of the contact geometry of the pointer.
pub fn height(&self) -> i32 {
self.inner.height()
}
/// Gets the normalized pressure of the pointer input in the range of 0 to 1,
pub fn pressure(&self) -> f32 {
self.inner.pressure()
}
/// Gets the normalized tangential pressure of the pointer input (also known as barrel pressure or cylinder stress) in the range -1 to 1,
pub fn tangential_pressure(&self) -> f32 {
self.inner.tangential_pressure()
}
/// Gets the plane angle (in degrees, in the range of -90 to 90) between the Y-Z plane and the plane containing both the transducer (e.g. pen stylus) axis and the Y axis.
pub fn tilt_x(&self) -> i32 {
self.inner.tilt_x()
}
/// Gets the plane angle (in degrees, in the range of -90 to 90) between the X-Z plane and the plane containing both the transducer (e.g. pen stylus) axis and the X axis.
pub fn tilt_y(&self) -> i32 {
self.inner.tilt_y()
}
/// Gets the clockwise rotation of the pointer (e.g. pen stylus) around its major axis in degrees, with a value in the range 0 to 359.The clockwise rotation of the pointer (e.g. pen stylus) around its major axis in degrees, with a value in the range 0 to 359.
pub fn twist(&self) -> i32 {
self.inner.twist()
}
/// Gets the device type that caused the event (mouse, pen, touch, etc.).
pub fn pointer_type(&self) -> String {
self.inner.pointer_type()
}
/// Gets if the pointer represents the primary pointer of this pointer type.
pub fn is_primary(&self) -> bool {
self.inner.is_primary()
}
}
impl InteractionLocation for PointerData {
fn client_coordinates(&self) -> ClientPoint {
self.inner.client_coordinates()
}
fn screen_coordinates(&self) -> ScreenPoint {
self.inner.screen_coordinates()
}
fn page_coordinates(&self) -> PagePoint {
self.inner.page_coordinates()
}
}
impl InteractionElementOffset for PointerData {
fn element_coordinates(&self) -> ElementPoint {
self.inner.element_coordinates()
}
}
impl ModifiersInteraction for PointerData {
fn modifiers(&self) -> Modifiers {
self.inner.modifiers()
}
}
impl PointerInteraction for PointerData {
fn held_buttons(&self) -> MouseButtonSet {
self.inner.held_buttons()
}
fn trigger_button(&self) -> Option<MouseButton> {
self.inner.trigger_button()
}
}
#[cfg(feature = "serialize")]
/// A serialized version of PointerData
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
pub struct SerializedPointerData {
/// Common data for all pointer/mouse events
#[serde(flatten)]
point_data: crate::point_interaction::SerializedPointInteraction,
/// The unique identifier of the pointer causing the event.
pointer_id: i32,
/// The width (magnitude on the X axis), in CSS pixels, of the contact geometry of the pointer.
width: i32,
/// The height (magnitude on the Y axis), in CSS pixels, of the contact geometry of the pointer.
height: i32,
/// The normalized pressure of the pointer input in the range of 0 to 1,
pressure: f32,
/// The normalized tangential pressure of the pointer input (also known as barrel pressure or cylinder stress) in the range -1 to 1,
tangential_pressure: f32,
/// The plane angle (in degrees, in the range of -90 to 90) between the Y-Z plane and the plane containing both the transducer (e.g. pen stylus) axis and the Y axis.
tilt_x: i32,
/// The plane angle (in degrees, in the range of -90 to 90) between the X-Z plane and the plane containing both the transducer (e.g. pen stylus) axis and the X axis.
tilt_y: i32,
/// The clockwise rotation of the pointer (e.g. pen stylus) around its major axis in degrees, with a value in the range 0 to 359.The clockwise rotation of the pointer (e.g. pen stylus) around its major axis in degrees, with a value in the range 0 to 359.
twist: i32,
/// Indicates the device type that caused the event (mouse, pen, touch, etc.).
pointer_type: String,
/// Indicates if the pointer represents the primary pointer of this pointer type.
is_primary: bool,
}
#[cfg(feature = "serialize")]
impl HasPointerData for SerializedPointerData {
fn pointer_id(&self) -> i32 {
self.pointer_id
}
fn width(&self) -> i32 {
self.width
}
fn height(&self) -> i32 {
self.height
}
fn pressure(&self) -> f32 {
self.pressure
}
fn tangential_pressure(&self) -> f32 {
self.tangential_pressure
}
fn tilt_x(&self) -> i32 {
self.tilt_x
}
fn tilt_y(&self) -> i32 {
self.tilt_y
}
fn twist(&self) -> i32 {
self.twist
}
fn pointer_type(&self) -> String {
self.pointer_type.clone()
}
fn is_primary(&self) -> bool {
self.is_primary
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
}
#[cfg(feature = "serialize")]
impl InteractionLocation for SerializedPointerData {
fn client_coordinates(&self) -> ClientPoint {
self.point_data.client_coordinates()
}
fn screen_coordinates(&self) -> ScreenPoint {
self.point_data.screen_coordinates()
}
fn page_coordinates(&self) -> PagePoint {
self.point_data.page_coordinates()
}
}
#[cfg(feature = "serialize")]
impl InteractionElementOffset for SerializedPointerData {
fn element_coordinates(&self) -> ElementPoint {
self.point_data.element_coordinates()
}
}
#[cfg(feature = "serialize")]
impl ModifiersInteraction for SerializedPointerData {
fn modifiers(&self) -> Modifiers {
self.point_data.modifiers()
}
}
#[cfg(feature = "serialize")]
impl PointerInteraction for SerializedPointerData {
fn held_buttons(&self) -> MouseButtonSet {
self.point_data.held_buttons()
}
fn trigger_button(&self) -> Option<MouseButton> {
self.point_data.trigger_button()
}
}
#[cfg(feature = "serialize")]
impl From<&PointerData> for SerializedPointerData {
fn from(data: &PointerData) -> Self {
Self {
point_data: data.into(),
pointer_id: data.pointer_id(),
width: data.width(),
height: data.height(),
pressure: data.pressure(),
tangential_pressure: data.tangential_pressure(),
tilt_x: data.tilt_x(),
tilt_y: data.tilt_y(),
twist: data.twist(),
pointer_type: data.pointer_type().to_string(),
is_primary: data.is_primary(),
}
}
}
#[cfg(feature = "serialize")]
impl serde::Serialize for PointerData {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
SerializedPointerData::from(self).serialize(serializer)
}
}
#[cfg(feature = "serialize")]
impl<'de> serde::Deserialize<'de> for PointerData {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let data = SerializedPointerData::deserialize(deserializer)?;
Ok(Self {
inner: Box::new(data),
})
}
}