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 Jan 20, 2024
1 parent 545cc90 commit 624bc8c
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 @@ -21,6 +21,7 @@ pub struct MouseArea<'a, Message, Renderer> {
on_right_release: Option<Message>,
on_middle_press: Option<Message>,
on_middle_release: Option<Message>,
interaction: Option<mouse::Interaction>,
}

impl<'a, Message, Renderer> MouseArea<'a, Message, Renderer> {
Expand Down Expand Up @@ -65,6 +66,16 @@ impl<'a, Message, Renderer> MouseArea<'a, Message, Renderer> {
self.on_middle_release = 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 @@ -84,6 +95,7 @@ impl<'a, Message, Renderer> MouseArea<'a, Message, Renderer> {
on_right_release: None,
on_middle_press: None,
on_middle_release: None,
interaction: None,
}
}
}
Expand Down Expand Up @@ -175,6 +187,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 624bc8c

Please sign in to comment.