Skip to main content

leptos_use_mouse/
options.rs

1use crate::{IntoElementMaybeSignal, Position, UseMouseEventExtractor, UseWindow, use_window};
2use default_struct_builder::DefaultBuilder;
3use std::convert::Infallible;
4use std::marker::PhantomData;
5
6#[derive(DefaultBuilder)]
7/// Options for [`use_mouse_with_options`](crate::use_mouse_with_options).
8pub struct UseMouseOptions<El, M, Ex>
9where
10    El: IntoElementMaybeSignal<web_sys::EventTarget, M>,
11    Ex: UseMouseEventExtractor + Clone,
12{
13    /// How to extract the x, y coordinates from mouse events or touches
14    pub(crate) coord_type: UseMouseCoordType<Ex>,
15
16    /// Listen events on `target` element. Defaults to `window`
17    pub(crate) target: El,
18
19    /// Listen to `touchmove` events. Defaults to `true`.
20    pub(crate) touch: bool,
21
22    /// Reset to initial value when `touchend` event fired. Defaults to `false`
23    pub(crate) reset_on_touch_ends: bool,
24
25    /// Initial values. Defaults to `{x: 0.0, y: 0.0}`.
26    pub(crate) initial_value: Position,
27
28    #[builder(skip)]
29    _marker: PhantomData<M>,
30}
31
32impl<M> Default for UseMouseOptions<UseWindow, M, Infallible>
33where
34    UseWindow: IntoElementMaybeSignal<web_sys::EventTarget, M>,
35{
36    fn default() -> Self {
37        Self {
38            coord_type: UseMouseCoordType::default(),
39            target: use_window(),
40            touch: true,
41            reset_on_touch_ends: false,
42            initial_value: Position { x: 0.0, y: 0.0 },
43            _marker: PhantomData,
44        }
45    }
46}
47
48/// Defines how to get the coordinates from the event.
49#[derive(Clone, Default)]
50pub enum UseMouseCoordType<E: UseMouseEventExtractor + Clone> {
51    #[default]
52    Page,
53    Client,
54    Screen,
55    Movement,
56    Custom(E),
57}
58
59impl<E: UseMouseEventExtractor + Clone> UseMouseEventExtractor for UseMouseCoordType<E> {
60    #[allow(clippy::unnecessary_cast)]
61    fn extract_mouse_coords(&self, event: &web_sys::MouseEvent) -> Option<(f64, f64)> {
62        match self {
63            UseMouseCoordType::Page => Some((event.page_x() as f64, event.page_y() as f64)),
64            UseMouseCoordType::Client => Some((event.client_x() as f64, event.client_y() as f64)),
65            UseMouseCoordType::Screen => Some((event.screen_x() as f64, event.screen_y() as f64)),
66            UseMouseCoordType::Movement => {
67                Some((event.movement_x() as f64, event.movement_y() as f64))
68            }
69            UseMouseCoordType::Custom(extractor) => extractor.extract_mouse_coords(event),
70        }
71    }
72
73    fn extract_touch_coords(&self, touch: &web_sys::Touch) -> Option<(f64, f64)> {
74        match self {
75            UseMouseCoordType::Page => Some((touch.page_x() as f64, touch.page_y() as f64)),
76            UseMouseCoordType::Client => Some((touch.client_x() as f64, touch.client_y() as f64)),
77            UseMouseCoordType::Screen => Some((touch.screen_x() as f64, touch.screen_y() as f64)),
78            UseMouseCoordType::Movement => None,
79            UseMouseCoordType::Custom(extractor) => extractor.extract_touch_coords(touch),
80        }
81    }
82}