Skip to content

Commit

Permalink
Shorten properties in MouseArea
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Feb 7, 2024
1 parent ab3aa77 commit 8b22e0e
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions widget/src/mouse_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ pub struct MouseArea<
on_right_release: Option<Message>,
on_middle_press: Option<Message>,
on_middle_release: Option<Message>,
on_mouse_enter: Option<Message>,
on_mouse_move: Option<Box<dyn Fn(Point) -> Message>>,
on_mouse_exit: Option<Message>,
on_enter: Option<Message>,
on_move: Option<Box<dyn Fn(Point) -> Message>>,
on_exit: Option<Message>,
interaction: Option<mouse::Interaction>,
}

Expand Down Expand Up @@ -80,7 +80,7 @@ impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> {
/// The message to emit when the mouse enters the area.
#[must_use]
pub fn on_enter(mut self, message: Message) -> Self {
self.on_mouse_enter = Some(message);
self.on_enter = Some(message);
self
}

Expand All @@ -90,14 +90,14 @@ impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> {
where
F: Fn(Point) -> Message + 'static,
{
self.on_mouse_move = Some(Box::new(build_message));
self.on_move = Some(Box::new(build_message));
self
}

/// The message to emit when the mouse exits the area.
#[must_use]
pub fn on_exit(mut self, message: Message) -> Self {
self.on_mouse_exit = Some(message);
self.on_exit = Some(message);
self
}

Expand Down Expand Up @@ -128,9 +128,9 @@ impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> {
on_right_release: None,
on_middle_press: None,
on_middle_release: None,
on_mouse_enter: None,
on_mouse_move: None,
on_mouse_exit: None,
on_enter: None,
on_move: None,
on_exit: None,
interaction: None,
}
}
Expand Down Expand Up @@ -311,22 +311,20 @@ fn update<Message: Clone, Theme, Renderer>(
state.is_hovered = cursor.is_over(layout.bounds());

match (
widget.on_mouse_enter.as_ref(),
widget.on_mouse_move.as_ref(),
widget.on_mouse_exit.as_ref(),
widget.on_enter.as_ref(),
widget.on_move.as_ref(),
widget.on_exit.as_ref(),
) {
(Some(on_mouse_enter), _, _)
if state.is_hovered && !was_hovered =>
{
shell.publish(on_mouse_enter.clone());
(Some(on_enter), _, _) if state.is_hovered && !was_hovered => {
shell.publish(on_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()) {
shell.publish(on_mouse_move(position));
shell.publish(on_move(position));
}
}
(_, _, Some(on_mouse_exit)) if !state.is_hovered && was_hovered => {
shell.publish(on_mouse_exit.clone());
(_, _, Some(on_exit)) if !state.is_hovered && was_hovered => {
shell.publish(on_exit.clone());
}
_ => {}
}
Expand Down

0 comments on commit 8b22e0e

Please sign in to comment.