From ce23498edf1e84ab7f70394ec21da1c8c466a444 Mon Sep 17 00:00:00 2001 From: Remmirad Date: Sun, 26 Nov 2023 14:25:19 +0100 Subject: Add mouse move events to `MouseArea` --- widget/src/mouse_area.rs | 75 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 2 deletions(-) (limited to 'widget') diff --git a/widget/src/mouse_area.rs b/widget/src/mouse_area.rs index 88f7d126..1487aaa5 100644 --- a/widget/src/mouse_area.rs +++ b/widget/src/mouse_area.rs @@ -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; @@ -26,6 +28,9 @@ pub struct MouseArea< on_right_release: Option, on_middle_press: Option, on_middle_release: Option, + on_mouse_enter: Option, + on_mouse_move: Option Message>>, + on_mouse_exit: Option, } impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> { @@ -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(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> { @@ -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, } } } @@ -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( @@ -246,11 +278,19 @@ where /// accordingly. fn update( 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; } @@ -320,3 +360,34 @@ fn update( event::Status::Ignored } + +fn handle_mouse_move( + 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; +} -- cgit From bd13c580f0658d812d58212100be08bdfc05b908 Mon Sep 17 00:00:00 2001 From: Remmirad Date: Sun, 26 Nov 2023 15:00:40 +0100 Subject: Fix lints --- widget/src/mouse_area.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'widget') diff --git a/widget/src/mouse_area.rs b/widget/src/mouse_area.rs index 1487aaa5..a2166436 100644 --- a/widget/src/mouse_area.rs +++ b/widget/src/mouse_area.rs @@ -288,7 +288,7 @@ fn update( | 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()) + handle_mouse_move(widget, state, shell, *position, layout.bounds()); } if !cursor.is_over(layout.bounds()) { @@ -376,15 +376,15 @@ fn handle_mouse_move( widget.on_mouse_exit.as_mut(), ) { (Some(enter_msg), _, _) if mouse_in_area && !state.mouse_in_area => { - shell.publish(enter_msg.clone()) + shell.publish(enter_msg.clone()); } (_, Some(build_move_msg), _) if mouse_in_area && state.mouse_in_area => { - shell.publish(build_move_msg(position)) + shell.publish(build_move_msg(position)); } (_, _, Some(exit_msg)) if !mouse_in_area && state.mouse_in_area => { - shell.publish(exit_msg.clone()) + shell.publish(exit_msg.clone()); } _ => {} } -- cgit From 8ea85f3b12c087560fdfeeebd8eb3c6efd754bbc Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 3 Feb 2024 18:05:40 +0100 Subject: Use `Fn` instead of `FnMut` in `MouseArea` ... and simplify event logic a bit. --- widget/src/mouse_area.rs | 70 +++++++++++++++++++++--------------------------- 1 file changed, 31 insertions(+), 39 deletions(-) (limited to 'widget') diff --git a/widget/src/mouse_area.rs b/widget/src/mouse_area.rs index a2166436..f82a75f4 100644 --- a/widget/src/mouse_area.rs +++ b/widget/src/mouse_area.rs @@ -29,7 +29,7 @@ pub struct MouseArea< on_middle_press: Option, on_middle_release: Option, on_mouse_enter: Option, - on_mouse_move: Option Message>>, + on_mouse_move: Option Message>>, on_mouse_exit: Option, } @@ -87,7 +87,7 @@ impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> { #[must_use] pub fn on_mouse_move(mut self, build_message: F) -> Self where - F: FnMut(Point) -> Message + 'static, + F: Fn(Point) -> Message + 'static, { self.on_mouse_move = Some(Box::new(build_message)); self @@ -104,7 +104,7 @@ impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> { /// Local state of the [`MouseArea`]. #[derive(Default)] struct State { - mouse_in_area: bool, + is_hovered: bool, } impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> { @@ -203,7 +203,7 @@ where return event::Status::Captured; } - update(self, tree, &event, layout, cursor, shell) + update(self, tree, event, layout, cursor, shell) } fn mouse_interaction( @@ -279,16 +279,39 @@ where fn update( widget: &mut MouseArea<'_, Message, Theme, Renderer>, tree: &mut Tree, - event: &Event, + 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 + if let Event::Mouse(mouse::Event::CursorMoved { .. }) + | Event::Touch(touch::Event::FingerMoved { .. }) = event { let state: &mut State = tree.state.downcast_mut(); - handle_mouse_move(widget, state, shell, *position, layout.bounds()); + + let was_hovered = state.is_hovered; + 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(), + ) { + (Some(on_mouse_enter), _, _) + if state.is_hovered && !was_hovered => + { + shell.publish(on_mouse_enter.clone()); + } + (_, Some(on_mouse_move), _) if state.is_hovered => { + if let Some(position) = cursor.position_in(layout.bounds()) { + shell.publish(on_mouse_move(position)); + } + } + (_, _, Some(on_mouse_exit)) if !state.is_hovered && was_hovered => { + shell.publish(on_mouse_exit.clone()); + } + _ => {} + } } if !cursor.is_over(layout.bounds()) { @@ -360,34 +383,3 @@ fn update( event::Status::Ignored } - -fn handle_mouse_move( - 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; -} -- cgit