Skip to content

Commit

Permalink
Merge pull request #2207 from VAWVAW/mouse-area-interaction
Browse files Browse the repository at this point in the history
Add `Interaction` overriding to `MouseArea`
  • Loading branch information
hecrj authored Feb 7, 2024
2 parents d3619b5 + d5a3e59 commit bdd1891
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 25 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
- `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)
- `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)
- 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)
Expand Down Expand Up @@ -135,6 +136,7 @@ Many thanks to...
- @tarkah
- @tzemanovic
- @varbhat
- @VAWVAW
- @william-shere

## [0.10.0] - 2023-07-28
Expand Down
66 changes: 41 additions & 25 deletions widget/src/mouse_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ 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>,
}

impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> {
Expand Down Expand Up @@ -78,25 +79,32 @@ 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_mouse_enter(mut self, message: Message) -> Self {
self.on_mouse_enter = Some(message);
pub fn on_enter(mut self, message: Message) -> Self {
self.on_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
pub fn on_move<F>(mut self, build_message: F) -> Self
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_mouse_exit(mut self, message: Message) -> Self {
self.on_mouse_exit = Some(message);
pub fn on_exit(mut self, message: Message) -> Self {
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
}
}
Expand All @@ -120,9 +128,10 @@ 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 @@ -214,13 +223,22 @@ where
viewport: &Rectangle,
renderer: &Renderer,
) -> mouse::Interaction {
self.content.as_widget().mouse_interaction(
let content_interaction = self.content.as_widget().mouse_interaction(
&tree.children[0],
layout,
cursor,
viewport,
renderer,
)
);

match (self.interaction, content_interaction) {
(Some(interaction), mouse::Interaction::Idle)
if cursor.is_over(layout.bounds()) =>
{
interaction
}
_ => content_interaction,
}
}

fn draw(
Expand Down Expand Up @@ -293,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 bdd1891

Please sign in to comment.