Merge pull request #2207 from VAWVAW/mouse-area-interaction

Add `Interaction` overriding to `MouseArea`
This commit is contained in:
Héctor Ramón 2024-02-07 12:20:45 +01:00 committed by GitHub
commit bdd1891f43
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 43 additions and 25 deletions

View file

@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `fetch_maximized` and `fetch_minimized` commands in `window`. [#2189](https://github.com/iced-rs/iced/pull/2189) - `fetch_maximized` and `fetch_minimized` commands in `window`. [#2189](https://github.com/iced-rs/iced/pull/2189)
- `run_with_handle` command in `window`. [#2200](https://github.com/iced-rs/iced/pull/2200) - `run_with_handle` command in `window`. [#2200](https://github.com/iced-rs/iced/pull/2200)
- `text_shaping` method for `Tooltip`. [#2172](https://github.com/iced-rs/iced/pull/2172) - `text_shaping` method for `Tooltip`. [#2172](https://github.com/iced-rs/iced/pull/2172)
- `interaction` method for `MouseArea`. [#2207](https://github.com/iced-rs/iced/pull/2207)
- `hovered` styling for `Svg` widget. [#2163](https://github.com/iced-rs/iced/pull/2163) - `hovered` styling for `Svg` widget. [#2163](https://github.com/iced-rs/iced/pull/2163)
- Customizable style for `TextEditor`. [#2159](https://github.com/iced-rs/iced/pull/2159) - Customizable style for `TextEditor`. [#2159](https://github.com/iced-rs/iced/pull/2159)
- `RawText` variant for `Primitive` in `iced_graphics`. [#2158](https://github.com/iced-rs/iced/pull/2158) - `RawText` variant for `Primitive` in `iced_graphics`. [#2158](https://github.com/iced-rs/iced/pull/2158)
@ -135,6 +136,7 @@ Many thanks to...
- @tarkah - @tarkah
- @tzemanovic - @tzemanovic
- @varbhat - @varbhat
- @VAWVAW
- @william-shere - @william-shere
## [0.10.0] - 2023-07-28 ## [0.10.0] - 2023-07-28

View file

@ -28,9 +28,10 @@ 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>,
} }
impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> { impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> {
@ -78,25 +79,32 @@ 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_mouse_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
} }
/// The message to emit when the mouse moves in the area. /// The message to emit when the mouse moves in the area.
#[must_use] #[must_use]
pub fn on_mouse_move<F>(mut self, build_message: F) -> Self pub fn on_move<F>(mut self, build_message: F) -> Self
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_mouse_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
}
/// The [`mouse::Interaction`] to use when hovering the area.
#[must_use]
pub fn interaction(mut self, interaction: mouse::Interaction) -> Self {
self.interaction = Some(interaction);
self self
} }
} }
@ -120,9 +128,10 @@ 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,
} }
} }
} }
@ -214,13 +223,22 @@ where
viewport: &Rectangle, viewport: &Rectangle,
renderer: &Renderer, renderer: &Renderer,
) -> mouse::Interaction { ) -> mouse::Interaction {
self.content.as_widget().mouse_interaction( let content_interaction = self.content.as_widget().mouse_interaction(
&tree.children[0], &tree.children[0],
layout, layout,
cursor, cursor,
viewport, viewport,
renderer, renderer,
) );
match (self.interaction, content_interaction) {
(Some(interaction), mouse::Interaction::Idle)
if cursor.is_over(layout.bounds()) =>
{
interaction
}
_ => content_interaction,
}
} }
fn draw( fn draw(
@ -293,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());
} }
_ => {} _ => {}
} }