Shorten properties in MouseArea

This commit is contained in:
Héctor Ramón Jiménez 2024-02-07 12:05:47 +01:00
parent ab3aa77ebe
commit 8b22e0ebbc
No known key found for this signature in database
GPG key ID: 7CC46565708259A7

View file

@ -28,9 +28,9 @@ pub struct MouseArea<
on_right_release: Option<Message>, on_right_release: Option<Message>,
on_middle_press: Option<Message>, on_middle_press: Option<Message>,
on_middle_release: Option<Message>, on_middle_release: Option<Message>,
on_mouse_enter: Option<Message>, on_enter: Option<Message>,
on_mouse_move: Option<Box<dyn Fn(Point) -> Message>>, on_move: Option<Box<dyn Fn(Point) -> Message>>,
on_mouse_exit: Option<Message>, on_exit: Option<Message>,
interaction: Option<mouse::Interaction>, interaction: Option<mouse::Interaction>,
} }
@ -80,7 +80,7 @@ impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> {
/// The message to emit when the mouse enters the area. /// The message to emit when the mouse enters the area.
#[must_use] #[must_use]
pub fn on_enter(mut self, message: Message) -> Self { pub fn on_enter(mut self, message: Message) -> Self {
self.on_mouse_enter = Some(message); self.on_enter = Some(message);
self self
} }
@ -90,14 +90,14 @@ impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> {
where where
F: Fn(Point) -> Message + 'static, F: Fn(Point) -> Message + 'static,
{ {
self.on_mouse_move = Some(Box::new(build_message)); self.on_move = Some(Box::new(build_message));
self self
} }
/// The message to emit when the mouse exits the area. /// The message to emit when the mouse exits the area.
#[must_use] #[must_use]
pub fn on_exit(mut self, message: Message) -> Self { pub fn on_exit(mut self, message: Message) -> Self {
self.on_mouse_exit = Some(message); self.on_exit = Some(message);
self self
} }
@ -128,9 +128,9 @@ impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> {
on_right_release: None, on_right_release: None,
on_middle_press: None, on_middle_press: None,
on_middle_release: None, on_middle_release: None,
on_mouse_enter: None, on_enter: None,
on_mouse_move: None, on_move: None,
on_mouse_exit: None, on_exit: None,
interaction: None, interaction: None,
} }
} }
@ -311,22 +311,20 @@ fn update<Message: Clone, Theme, Renderer>(
state.is_hovered = cursor.is_over(layout.bounds()); state.is_hovered = cursor.is_over(layout.bounds());
match ( match (
widget.on_mouse_enter.as_ref(), widget.on_enter.as_ref(),
widget.on_mouse_move.as_ref(), widget.on_move.as_ref(),
widget.on_mouse_exit.as_ref(), widget.on_exit.as_ref(),
) { ) {
(Some(on_mouse_enter), _, _) (Some(on_enter), _, _) if state.is_hovered && !was_hovered => {
if state.is_hovered && !was_hovered => shell.publish(on_enter.clone());
{
shell.publish(on_mouse_enter.clone());
} }
(_, Some(on_mouse_move), _) if state.is_hovered => { (_, Some(on_move), _) if state.is_hovered => {
if let Some(position) = cursor.position_in(layout.bounds()) { if let Some(position) = cursor.position_in(layout.bounds()) {
shell.publish(on_mouse_move(position)); shell.publish(on_move(position));
} }
} }
(_, _, Some(on_mouse_exit)) if !state.is_hovered && was_hovered => { (_, _, Some(on_exit)) if !state.is_hovered && was_hovered => {
shell.publish(on_mouse_exit.clone()); shell.publish(on_exit.clone());
} }
_ => {} _ => {}
} }