From e795c1f8f342b48433946c0dfdb3693c06074cb8 Mon Sep 17 00:00:00 2001 From: Charles Edward Gagnon Date: Mon, 12 Aug 2024 19:55:33 -0400 Subject: [PATCH 1/8] make `UseMouseEventExtractor` not return an option This makes it so that `Movement` always returns (0., 0.) --- src/use_mouse.rs | 53 ++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/src/use_mouse.rs b/src/use_mouse.rs index 74f943f..63b4d0c 100644 --- a/src/use_mouse.rs +++ b/src/use_mouse.rs @@ -6,6 +6,7 @@ use cfg_if::cfg_if; use default_struct_builder::DefaultBuilder; use leptos::ev::{dragover, mousemove, touchend, touchmove, touchstart}; use leptos::*; +use std::convert::Infallible; use std::marker::PhantomData; use wasm_bindgen::{JsCast, JsValue}; @@ -108,7 +109,7 @@ where move |event: web_sys::MouseEvent| { let result = coord_type.extract_mouse_coords(&event); - if let Some((x, y)) = result { + if let (x, y) = result { set_x.set(x); set_y.set(y); set_source_type.set(UseMouseSourceType::Mouse); @@ -137,7 +138,7 @@ where .expect("Just checked that there's at least on touch"), ); - if let Some((x, y)) = result { + if let (x, y) = result { set_x.set(x); set_y.set(y); set_source_type.set(UseMouseSourceType::Touch); @@ -230,10 +231,10 @@ where _marker: PhantomData, } -impl Default for UseMouseOptions { +impl Default for UseMouseOptions { fn default() -> Self { Self { - coord_type: UseMouseCoordType::::default(), + coord_type: UseMouseCoordType::::default(), target: use_window(), touch: true, reset_on_touch_ends: false, @@ -245,7 +246,7 @@ impl Default for UseMouseOptions { +pub enum UseMouseCoordType { Page, Client, Screen, @@ -253,54 +254,54 @@ pub enum UseMouseCoordType { Custom(E), } -impl Default for UseMouseCoordType { +impl Default for UseMouseCoordType { fn default() -> Self { Self::Page } } /// Trait to implement if you want to specify a custom extractor -#[allow(unused_variables)] pub trait UseMouseEventExtractor { /// Return the coordinates from mouse events (`Some(x, y)`) or `None` - fn extract_mouse_coords(&self, event: &web_sys::MouseEvent) -> Option<(f64, f64)> { - None - } + fn extract_mouse_coords(&self, event: &web_sys::MouseEvent) -> (f64, f64); /// Return the coordinates from touches (`Some(x, y)`) or `None` - fn extract_touch_coords(&self, touch: &web_sys::Touch) -> Option<(f64, f64)> { - None - } + fn extract_touch_coords(&self, touch: &web_sys::Touch) -> (f64, f64); } impl UseMouseEventExtractor for UseMouseCoordType { - fn extract_mouse_coords(&self, event: &web_sys::MouseEvent) -> Option<(f64, f64)> { + fn extract_mouse_coords(&self, event: &web_sys::MouseEvent) -> (f64, f64) { match self { - UseMouseCoordType::Page => Some((event.page_x() as f64, event.page_y() as f64)), - UseMouseCoordType::Client => Some((event.client_x() as f64, event.client_y() as f64)), - UseMouseCoordType::Screen => Some((event.screen_x() as f64, event.client_y() as f64)), + UseMouseCoordType::Page => (event.page_x() as f64, event.page_y() as f64), + UseMouseCoordType::Client => (event.client_x() as f64, event.client_y() as f64), + UseMouseCoordType::Screen => (event.screen_x() as f64, event.client_y() as f64), UseMouseCoordType::Movement => { - Some((event.movement_x() as f64, event.movement_y() as f64)) + (event.movement_x() as f64, event.movement_y() as f64) } UseMouseCoordType::Custom(ref extractor) => extractor.extract_mouse_coords(event), } } - fn extract_touch_coords(&self, touch: &web_sys::Touch) -> Option<(f64, f64)> { + fn extract_touch_coords(&self, touch: &web_sys::Touch) -> (f64, f64) { match self { - UseMouseCoordType::Page => Some((touch.page_x() as f64, touch.page_y() as f64)), - UseMouseCoordType::Client => Some((touch.client_x() as f64, touch.client_y() as f64)), - UseMouseCoordType::Screen => Some((touch.screen_x() as f64, touch.client_y() as f64)), - UseMouseCoordType::Movement => None, + UseMouseCoordType::Page => (touch.page_x() as f64, touch.page_y() as f64), + UseMouseCoordType::Client => (touch.client_x() as f64, touch.client_y() as f64), + UseMouseCoordType::Screen => (touch.screen_x() as f64, touch.client_y() as f64), + UseMouseCoordType::Movement => (0., 0.), UseMouseCoordType::Custom(ref extractor) => extractor.extract_touch_coords(touch), } } } -#[derive(Clone)] -pub struct UseMouseEventExtractorDefault; +impl UseMouseEventExtractor for Infallible { + fn extract_mouse_coords(&self, _: &web_sys::MouseEvent) -> (f64, f64) { + unreachable!() + } -impl UseMouseEventExtractor for UseMouseEventExtractorDefault {} + fn extract_touch_coords(&self, _: &web_sys::Touch) -> (f64, f64) { + unreachable!() + } +} /// Return type of [`use_mouse`]. pub struct UseMouseReturn { From fe7d51c70f6a1438468c2cd4ea94750e0e60e0b3 Mon Sep 17 00:00:00 2001 From: Charles Edward Gagnon Date: Mon, 12 Aug 2024 20:01:08 -0400 Subject: [PATCH 2/8] fix building issues --- src/use_mouse.rs | 22 ++++++++++------------ src/use_mouse_in_element.rs | 7 ++++--- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/use_mouse.rs b/src/use_mouse.rs index 63b4d0c..1c65547 100644 --- a/src/use_mouse.rs +++ b/src/use_mouse.rs @@ -109,11 +109,10 @@ where move |event: web_sys::MouseEvent| { let result = coord_type.extract_mouse_coords(&event); - if let (x, y) = result { - set_x.set(x); - set_y.set(y); - set_source_type.set(UseMouseSourceType::Mouse); - } + let (x, y) = result; + set_x.set(x); + set_y.set(y); + set_source_type.set(UseMouseSourceType::Mouse); } }; @@ -138,11 +137,10 @@ where .expect("Just checked that there's at least on touch"), ); - if let (x, y) = result { - set_x.set(x); - set_y.set(y); - set_source_type.set(UseMouseSourceType::Touch); - } + let (x, y) = result; + set_x.set(x); + set_y.set(y); + set_source_type.set(UseMouseSourceType::Touch); } } }; @@ -234,7 +232,7 @@ where impl Default for UseMouseOptions { fn default() -> Self { Self { - coord_type: UseMouseCoordType::::default(), + coord_type: UseMouseCoordType::default(), target: use_window(), touch: true, reset_on_touch_ends: false, @@ -254,7 +252,7 @@ pub enum UseMouseCoordType { Custom(E), } -impl Default for UseMouseCoordType { +impl Default for UseMouseCoordType { fn default() -> Self { Self::Page } diff --git a/src/use_mouse_in_element.rs b/src/use_mouse_in_element.rs index 29ad0f9..df80b2e 100644 --- a/src/use_mouse_in_element.rs +++ b/src/use_mouse_in_element.rs @@ -1,11 +1,12 @@ use crate::core::{ElementMaybeSignal, Position}; use crate::{ use_mouse_with_options, use_window, UseMouseCoordType, UseMouseEventExtractor, - UseMouseEventExtractorDefault, UseMouseOptions, UseMouseReturn, UseMouseSourceType, UseWindow, + UseMouseOptions, UseMouseReturn, UseMouseSourceType, UseWindow, }; use cfg_if::cfg_if; use default_struct_builder::DefaultBuilder; use leptos::*; +use std::convert::Infallible; use std::marker::PhantomData; /// Reactive mouse position related to an element. @@ -196,11 +197,11 @@ where } impl Default - for UseMouseInElementOptions + for UseMouseInElementOptions { fn default() -> Self { Self { - coord_type: UseMouseCoordType::::default(), + coord_type: UseMouseCoordType::::default(), target: use_window(), touch: true, reset_on_touch_ends: false, From 0b7e8af9950a08417acb9d98c197a43bfdfe0758 Mon Sep 17 00:00:00 2001 From: Charles Edward Gagnon Date: Mon, 12 Aug 2024 20:01:53 -0400 Subject: [PATCH 3/8] chore: fmt; --- src/use_mouse.rs | 4 +--- src/use_mouse_in_element.rs | 8 +++----- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/use_mouse.rs b/src/use_mouse.rs index 1c65547..2ff9a7a 100644 --- a/src/use_mouse.rs +++ b/src/use_mouse.rs @@ -273,9 +273,7 @@ impl UseMouseEventExtractor for UseMouseCoord UseMouseCoordType::Page => (event.page_x() as f64, event.page_y() as f64), UseMouseCoordType::Client => (event.client_x() as f64, event.client_y() as f64), UseMouseCoordType::Screen => (event.screen_x() as f64, event.client_y() as f64), - UseMouseCoordType::Movement => { - (event.movement_x() as f64, event.movement_y() as f64) - } + UseMouseCoordType::Movement => (event.movement_x() as f64, event.movement_y() as f64), UseMouseCoordType::Custom(ref extractor) => extractor.extract_mouse_coords(event), } } diff --git a/src/use_mouse_in_element.rs b/src/use_mouse_in_element.rs index df80b2e..8b37f4d 100644 --- a/src/use_mouse_in_element.rs +++ b/src/use_mouse_in_element.rs @@ -1,7 +1,7 @@ use crate::core::{ElementMaybeSignal, Position}; use crate::{ - use_mouse_with_options, use_window, UseMouseCoordType, UseMouseEventExtractor, - UseMouseOptions, UseMouseReturn, UseMouseSourceType, UseWindow, + use_mouse_with_options, use_window, UseMouseCoordType, UseMouseEventExtractor, UseMouseOptions, + UseMouseReturn, UseMouseSourceType, UseWindow, }; use cfg_if::cfg_if; use default_struct_builder::DefaultBuilder; @@ -196,9 +196,7 @@ where _marker: PhantomData, } -impl Default - for UseMouseInElementOptions -{ +impl Default for UseMouseInElementOptions { fn default() -> Self { Self { coord_type: UseMouseCoordType::::default(), From 31f019a944cad089279d29c1c59de7447e24caa4 Mon Sep 17 00:00:00 2001 From: Charles Edward Gagnon Date: Mon, 12 Aug 2024 20:27:04 -0400 Subject: [PATCH 4/8] make it take Some again --- src/use_mouse.rs | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/use_mouse.rs b/src/use_mouse.rs index 2ff9a7a..e8ca691 100644 --- a/src/use_mouse.rs +++ b/src/use_mouse.rs @@ -259,42 +259,47 @@ impl Default for UseMouseCoordType { } /// Trait to implement if you want to specify a custom extractor +#[allow(unused_variables)] pub trait UseMouseEventExtractor { /// Return the coordinates from mouse events (`Some(x, y)`) or `None` - fn extract_mouse_coords(&self, event: &web_sys::MouseEvent) -> (f64, f64); + fn extract_mouse_coords(&self, event: &web_sys::MouseEvent) -> Option<(f64, f64)> { + None + } /// Return the coordinates from touches (`Some(x, y)`) or `None` - fn extract_touch_coords(&self, touch: &web_sys::Touch) -> (f64, f64); + fn extract_touch_coords(&self, touch: &web_sys::Touch) -> Option<(f64, f64)> { + None + } } impl UseMouseEventExtractor for UseMouseCoordType { - fn extract_mouse_coords(&self, event: &web_sys::MouseEvent) -> (f64, f64) { - match self { + fn extract_mouse_coords(&self, event: &web_sys::MouseEvent) -> Option<(f64, f64)> { + Some(match self { UseMouseCoordType::Page => (event.page_x() as f64, event.page_y() as f64), UseMouseCoordType::Client => (event.client_x() as f64, event.client_y() as f64), UseMouseCoordType::Screen => (event.screen_x() as f64, event.client_y() as f64), UseMouseCoordType::Movement => (event.movement_x() as f64, event.movement_y() as f64), - UseMouseCoordType::Custom(ref extractor) => extractor.extract_mouse_coords(event), - } + UseMouseCoordType::Custom(ref extractor) => return extractor.extract_mouse_coords(event), + }) } - fn extract_touch_coords(&self, touch: &web_sys::Touch) -> (f64, f64) { - match self { + fn extract_touch_coords(&self, touch: &web_sys::Touch) -> Option<(f64, f64)> { + Some(match self { UseMouseCoordType::Page => (touch.page_x() as f64, touch.page_y() as f64), UseMouseCoordType::Client => (touch.client_x() as f64, touch.client_y() as f64), UseMouseCoordType::Screen => (touch.screen_x() as f64, touch.client_y() as f64), - UseMouseCoordType::Movement => (0., 0.), - UseMouseCoordType::Custom(ref extractor) => extractor.extract_touch_coords(touch), - } + UseMouseCoordType::Movement => return None, + UseMouseCoordType::Custom(ref extractor) => return extractor.extract_touch_coords(touch), + }) } } impl UseMouseEventExtractor for Infallible { - fn extract_mouse_coords(&self, _: &web_sys::MouseEvent) -> (f64, f64) { + fn extract_mouse_coords(&self, _: &web_sys::MouseEvent) -> Option<(f64, f64)> { unreachable!() } - fn extract_touch_coords(&self, _: &web_sys::Touch) -> (f64, f64) { + fn extract_touch_coords(&self, _: &web_sys::Touch) -> Option<(f64, f64)> { unreachable!() } } From cb889066ce6947f22450fa28d379d590657c6bcc Mon Sep 17 00:00:00 2001 From: Charles Edward Gagnon Date: Mon, 12 Aug 2024 20:34:11 -0400 Subject: [PATCH 5/8] fix build issues --- src/use_mouse.rs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/use_mouse.rs b/src/use_mouse.rs index e8ca691..8e61eaa 100644 --- a/src/use_mouse.rs +++ b/src/use_mouse.rs @@ -109,10 +109,11 @@ where move |event: web_sys::MouseEvent| { let result = coord_type.extract_mouse_coords(&event); - let (x, y) = result; - set_x.set(x); - set_y.set(y); - set_source_type.set(UseMouseSourceType::Mouse); + if let Some((x, y)) = result { + set_x.set(x); + set_y.set(y); + set_source_type.set(UseMouseSourceType::Mouse); + } } }; @@ -137,10 +138,11 @@ where .expect("Just checked that there's at least on touch"), ); - let (x, y) = result; - set_x.set(x); - set_y.set(y); - set_source_type.set(UseMouseSourceType::Touch); + if let Some((x, y)) = result { + set_x.set(x); + set_y.set(y); + set_source_type.set(UseMouseSourceType::Touch); + } } } }; @@ -244,7 +246,7 @@ impl Default for UseMouseOptions { /// Defines how to get the coordinates from the event. #[derive(Clone)] -pub enum UseMouseCoordType { +pub enum UseMouseCoordType { Page, Client, Screen, @@ -279,7 +281,9 @@ impl UseMouseEventExtractor for UseMouseCoord UseMouseCoordType::Client => (event.client_x() as f64, event.client_y() as f64), UseMouseCoordType::Screen => (event.screen_x() as f64, event.client_y() as f64), UseMouseCoordType::Movement => (event.movement_x() as f64, event.movement_y() as f64), - UseMouseCoordType::Custom(ref extractor) => return extractor.extract_mouse_coords(event), + UseMouseCoordType::Custom(ref extractor) => { + return extractor.extract_mouse_coords(event) + } }) } @@ -289,7 +293,9 @@ impl UseMouseEventExtractor for UseMouseCoord UseMouseCoordType::Client => (touch.client_x() as f64, touch.client_y() as f64), UseMouseCoordType::Screen => (touch.screen_x() as f64, touch.client_y() as f64), UseMouseCoordType::Movement => return None, - UseMouseCoordType::Custom(ref extractor) => return extractor.extract_touch_coords(touch), + UseMouseCoordType::Custom(ref extractor) => { + return extractor.extract_touch_coords(touch) + } }) } } From 5d6d55dd89edaa04a81a558ebe5e6413569769e6 Mon Sep 17 00:00:00 2001 From: Charles Edward Gagnon Date: Mon, 12 Aug 2024 20:35:51 -0400 Subject: [PATCH 6/8] update changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b7a398..2553308 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Removed 🗑 + +- Removed `UseMouseEventExtractorDefault` + ## [0.11.4] - 2024-08-12 ### New Features 🚀 From 72cc82713d315bd8bd3b45533fc4f7da072e283e Mon Sep 17 00:00:00 2001 From: Charles Edward Gagnon Date: Mon, 12 Aug 2024 20:38:58 -0400 Subject: [PATCH 7/8] remove useless code churn remove useless type --- src/use_mouse.rs | 32 +++++++++++++++----------------- src/use_mouse_in_element.rs | 2 +- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/use_mouse.rs b/src/use_mouse.rs index 8e61eaa..337523c 100644 --- a/src/use_mouse.rs +++ b/src/use_mouse.rs @@ -276,27 +276,25 @@ pub trait UseMouseEventExtractor { impl UseMouseEventExtractor for UseMouseCoordType { fn extract_mouse_coords(&self, event: &web_sys::MouseEvent) -> Option<(f64, f64)> { - Some(match self { - UseMouseCoordType::Page => (event.page_x() as f64, event.page_y() as f64), - UseMouseCoordType::Client => (event.client_x() as f64, event.client_y() as f64), - UseMouseCoordType::Screen => (event.screen_x() as f64, event.client_y() as f64), - UseMouseCoordType::Movement => (event.movement_x() as f64, event.movement_y() as f64), - UseMouseCoordType::Custom(ref extractor) => { - return extractor.extract_mouse_coords(event) + match self { + UseMouseCoordType::Page => Some((event.page_x() as f64, event.page_y() as f64)), + UseMouseCoordType::Client => Some((event.client_x() as f64, event.client_y() as f64)), + UseMouseCoordType::Screen => Some((event.screen_x() as f64, event.client_y() as f64)), + UseMouseCoordType::Movement => { + Some((event.movement_x() as f64, event.movement_y() as f64)) } - }) + UseMouseCoordType::Custom(ref extractor) => extractor.extract_mouse_coords(event), + } } fn extract_touch_coords(&self, touch: &web_sys::Touch) -> Option<(f64, f64)> { - Some(match self { - UseMouseCoordType::Page => (touch.page_x() as f64, touch.page_y() as f64), - UseMouseCoordType::Client => (touch.client_x() as f64, touch.client_y() as f64), - UseMouseCoordType::Screen => (touch.screen_x() as f64, touch.client_y() as f64), - UseMouseCoordType::Movement => return None, - UseMouseCoordType::Custom(ref extractor) => { - return extractor.extract_touch_coords(touch) - } - }) + match self { + UseMouseCoordType::Page => Some((touch.page_x() as f64, touch.page_y() as f64)), + UseMouseCoordType::Client => Some((touch.client_x() as f64, touch.client_y() as f64)), + UseMouseCoordType::Screen => Some((touch.screen_x() as f64, touch.client_y() as f64)), + UseMouseCoordType::Movement => None, + UseMouseCoordType::Custom(ref extractor) => extractor.extract_touch_coords(touch), + } } } diff --git a/src/use_mouse_in_element.rs b/src/use_mouse_in_element.rs index 8b37f4d..c3d23ae 100644 --- a/src/use_mouse_in_element.rs +++ b/src/use_mouse_in_element.rs @@ -199,7 +199,7 @@ where impl Default for UseMouseInElementOptions { fn default() -> Self { Self { - coord_type: UseMouseCoordType::::default(), + coord_type: UseMouseCoordType::default(), target: use_window(), touch: true, reset_on_touch_ends: false, From f245bf07d03135259adc664f3f72a00a11367949 Mon Sep 17 00:00:00 2001 From: Charles Edward Gagnon Date: Tue, 13 Aug 2024 22:11:43 -0400 Subject: [PATCH 8/8] fix some clippy lints --- src/use_locale.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/use_locale.rs b/src/use_locale.rs index be781a4..b423179 100644 --- a/src/use_locale.rs +++ b/src/use_locale.rs @@ -58,8 +58,8 @@ where .map(|l| l.as_ref().clone()) .collect::>(); - const EMPTY_ERR_MSG: &'static str = "Empty supported list. You have to provide at least one locale in the `supported` parameter"; - assert!(supported.len() > 0, "{}", EMPTY_ERR_MSG); + const EMPTY_ERR_MSG: &str = "Empty supported list. You have to provide at least one locale in the `supported` parameter"; + assert!(!supported.is_empty(), "{}", EMPTY_ERR_MSG); Signal::derive(move || { let supported = supported.clone();