Skip to content

Commit

Permalink
Add mouse move events to MouseArea
Browse files Browse the repository at this point in the history
  • Loading branch information
Remmirad authored and hecrj committed Feb 3, 2024
1 parent 1d58d44 commit ce23498
Showing 1 changed file with 73 additions and 2 deletions.
75 changes: 73 additions & 2 deletions widget/src/mouse_area.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! A container for capturing mouse events.
use iced_renderer::core::Point;

use crate::core::event::{self, Event};
use crate::core::layout;
use crate::core::mouse;
Expand All @@ -26,6 +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 FnMut(Point) -> Message>>,
on_mouse_exit: Option<Message>,
}

impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> {
Expand Down Expand Up @@ -70,12 +75,36 @@ impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> {
self.on_middle_release = Some(message);
self
}

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

/// The message to emit when the mouse moves in the area.
#[must_use]
pub fn on_mouse_move<F>(mut self, build_message: F) -> Self
where
F: FnMut(Point) -> Message + 'static,
{
self.on_mouse_move = Some(Box::new(build_message));
self
}

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

/// Local state of the [`MouseArea`].
#[derive(Default)]
struct State {
// TODO: Support on_mouse_enter and on_mouse_exit
mouse_in_area: bool,
}

impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> {
Expand All @@ -91,6 +120,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,
}
}
}
Expand Down Expand Up @@ -171,7 +203,7 @@ where
return event::Status::Captured;
}

update(self, &event, layout, cursor, shell)
update(self, tree, &event, layout, cursor, shell)
}

fn mouse_interaction(
Expand Down Expand Up @@ -246,11 +278,19 @@ where
/// accordingly.
fn update<Message: Clone, Theme, Renderer>(
widget: &mut MouseArea<'_, Message, Theme, Renderer>,
tree: &mut Tree,
event: &Event,
layout: Layout<'_>,
cursor: mouse::Cursor,
shell: &mut Shell<'_, Message>,
) -> event::Status {
if let Event::Mouse(mouse::Event::CursorMoved { position })
| Event::Touch(touch::Event::FingerMoved { id: _, position }) = event
{
let state: &mut State = tree.state.downcast_mut();
handle_mouse_move(widget, state, shell, *position, layout.bounds())
}

if !cursor.is_over(layout.bounds()) {
return event::Status::Ignored;
}
Expand Down Expand Up @@ -320,3 +360,34 @@ fn update<Message: Clone, Theme, Renderer>(

event::Status::Ignored
}

fn handle_mouse_move<Message: Clone, Theme, Renderer>(
widget: &mut MouseArea<'_, Message, Theme, Renderer>,
state: &mut State,
shell: &mut Shell<'_, Message>,
position: Point,
area_bounds: Rectangle,
) {
let mouse_in_area = area_bounds.contains(position);

match (
widget.on_mouse_enter.as_mut(),
widget.on_mouse_move.as_mut(),
widget.on_mouse_exit.as_mut(),
) {
(Some(enter_msg), _, _) if mouse_in_area && !state.mouse_in_area => {
shell.publish(enter_msg.clone())
}
(_, Some(build_move_msg), _)
if mouse_in_area && state.mouse_in_area =>
{
shell.publish(build_move_msg(position))
}
(_, _, Some(exit_msg)) if !mouse_in_area && state.mouse_in_area => {
shell.publish(exit_msg.clone())
}
_ => {}
}

state.mouse_in_area = mouse_in_area;
}

0 comments on commit ce23498

Please sign in to comment.