freya_elements/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
use crate::{
events::ErasedEventData,
impl_event,
};
impl_event! [
WheelData;
/// The `wheel` event fires when the user scrolls the mouse wheel while hovering over the element.
///
/// Event Data: [`WheelData`](crate::events::WheelData)
///
/// ### Example
///
/// ```rust, no_run
/// # use freya::prelude::*;
/// fn app() -> Element {
/// rsx!(
/// rect {
/// width: "100",
/// height: "100",
/// background: "red",
/// onwheel: |_| println!("Scrolling with the wheel!")
/// }
/// )
/// }
/// ```
onwheel
];
#[derive(Debug, Clone, PartialEq, Copy)]
pub enum WheelSource {
Device,
Custom,
}
/// Data of a Wheel event.
#[derive(Debug, Clone, PartialEq)]
pub struct WheelData {
source: WheelSource,
delta_x: f64,
delta_y: f64,
}
impl WheelData {
pub fn new(source: WheelSource, delta_x: f64, delta_y: f64) -> Self {
Self {
source,
delta_x,
delta_y,
}
}
}
impl WheelData {
/// Get the source of the wheel event.
pub fn get_source(&self) -> WheelSource {
self.source
}
/// Get the X delta.
pub fn get_delta_x(&self) -> f64 {
self.delta_x
}
/// Get the Y delta.
pub fn get_delta_y(&self) -> f64 {
self.delta_y
}
}
impl From<&ErasedEventData> for WheelData {
fn from(val: &ErasedEventData) -> Self {
val.downcast::<WheelData>().cloned().unwrap()
}
}