Skip to content

Commit

Permalink
Add Interaction overriding to MouseArea
Browse files Browse the repository at this point in the history
Add the ability to use a custom `iced::mouse::Interaction` for a
`iced::widget::MouseArea`.
  • Loading branch information
VAWVAW committed Feb 4, 2024
1 parent 8781348 commit 75a8126
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions widget/src/mouse_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct MouseArea<
on_mouse_enter: Option<Message>,
on_mouse_move: Option<Box<dyn Fn(Point) -> Message>>,
on_mouse_exit: Option<Message>,
interaction: Option<mouse::Interaction>,
}

impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> {
Expand Down Expand Up @@ -99,6 +100,16 @@ impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> {
self.on_mouse_exit = Some(message);
self
}

/// The version of the cursor to use when hovering.
#[must_use]
pub fn mouse_interaction(
mut self,
interaction: mouse::Interaction,
) -> Self {
self.interaction = Some(interaction);
self
}
}

/// Local state of the [`MouseArea`].
Expand All @@ -123,6 +134,7 @@ impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> {
on_mouse_enter: None,
on_mouse_move: None,
on_mouse_exit: None,
interaction: None,
}
}
}
Expand Down Expand Up @@ -214,6 +226,14 @@ where
viewport: &Rectangle,
renderer: &Renderer,
) -> mouse::Interaction {
if !cursor.is_over(layout.bounds()) {
return mouse::Interaction::default();
}

if let Some(interaction) = self.interaction {
return interaction;
}

self.content.as_widget().mouse_interaction(
&tree.children[0],
layout,
Expand Down

0 comments on commit 75a8126

Please sign in to comment.