Skip to content

Commit bdd1891

Browse files
authored
Merge pull request #2207 from VAWVAW/mouse-area-interaction
Add `Interaction` overriding to `MouseArea`
2 parents d3619b5 + d5a3e59 commit bdd1891

2 files changed

Lines changed: 43 additions & 25 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
- `fetch_maximized` and `fetch_minimized` commands in `window`. [#2189](https://github.com/iced-rs/iced/pull/2189)
2323
- `run_with_handle` command in `window`. [#2200](https://github.com/iced-rs/iced/pull/2200)
2424
- `text_shaping` method for `Tooltip`. [#2172](https://github.com/iced-rs/iced/pull/2172)
25+
- `interaction` method for `MouseArea`. [#2207](https://github.com/iced-rs/iced/pull/2207)
2526
- `hovered` styling for `Svg` widget. [#2163](https://github.com/iced-rs/iced/pull/2163)
2627
- Customizable style for `TextEditor`. [#2159](https://github.com/iced-rs/iced/pull/2159)
2728
- `RawText` variant for `Primitive` in `iced_graphics`. [#2158](https://github.com/iced-rs/iced/pull/2158)
@@ -135,6 +136,7 @@ Many thanks to...
135136
- @tarkah
136137
- @tzemanovic
137138
- @varbhat
139+
- @VAWVAW
138140
- @william-shere
139141

140142
## [0.10.0] - 2023-07-28

widget/src/mouse_area.rs

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ pub struct MouseArea<
2828
on_right_release: Option<Message>,
2929
on_middle_press: Option<Message>,
3030
on_middle_release: Option<Message>,
31-
on_mouse_enter: Option<Message>,
32-
on_mouse_move: Option<Box<dyn Fn(Point) -> Message>>,
33-
on_mouse_exit: Option<Message>,
31+
on_enter: Option<Message>,
32+
on_move: Option<Box<dyn Fn(Point) -> Message>>,
33+
on_exit: Option<Message>,
34+
interaction: Option<mouse::Interaction>,
3435
}
3536

3637
impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> {
@@ -78,25 +79,32 @@ impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> {
7879

7980
/// The message to emit when the mouse enters the area.
8081
#[must_use]
81-
pub fn on_mouse_enter(mut self, message: Message) -> Self {
82-
self.on_mouse_enter = Some(message);
82+
pub fn on_enter(mut self, message: Message) -> Self {
83+
self.on_enter = Some(message);
8384
self
8485
}
8586

8687
/// The message to emit when the mouse moves in the area.
8788
#[must_use]
88-
pub fn on_mouse_move<F>(mut self, build_message: F) -> Self
89+
pub fn on_move<F>(mut self, build_message: F) -> Self
8990
where
9091
F: Fn(Point) -> Message + 'static,
9192
{
92-
self.on_mouse_move = Some(Box::new(build_message));
93+
self.on_move = Some(Box::new(build_message));
9394
self
9495
}
9596

9697
/// The message to emit when the mouse exits the area.
9798
#[must_use]
98-
pub fn on_mouse_exit(mut self, message: Message) -> Self {
99-
self.on_mouse_exit = Some(message);
99+
pub fn on_exit(mut self, message: Message) -> Self {
100+
self.on_exit = Some(message);
101+
self
102+
}
103+
104+
/// The [`mouse::Interaction`] to use when hovering the area.
105+
#[must_use]
106+
pub fn interaction(mut self, interaction: mouse::Interaction) -> Self {
107+
self.interaction = Some(interaction);
100108
self
101109
}
102110
}
@@ -120,9 +128,10 @@ impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> {
120128
on_right_release: None,
121129
on_middle_press: None,
122130
on_middle_release: None,
123-
on_mouse_enter: None,
124-
on_mouse_move: None,
125-
on_mouse_exit: None,
131+
on_enter: None,
132+
on_move: None,
133+
on_exit: None,
134+
interaction: None,
126135
}
127136
}
128137
}
@@ -214,13 +223,22 @@ where
214223
viewport: &Rectangle,
215224
renderer: &Renderer,
216225
) -> mouse::Interaction {
217-
self.content.as_widget().mouse_interaction(
226+
let content_interaction = self.content.as_widget().mouse_interaction(
218227
&tree.children[0],
219228
layout,
220229
cursor,
221230
viewport,
222231
renderer,
223-
)
232+
);
233+
234+
match (self.interaction, content_interaction) {
235+
(Some(interaction), mouse::Interaction::Idle)
236+
if cursor.is_over(layout.bounds()) =>
237+
{
238+
interaction
239+
}
240+
_ => content_interaction,
241+
}
224242
}
225243

226244
fn draw(
@@ -293,22 +311,20 @@ fn update<Message: Clone, Theme, Renderer>(
293311
state.is_hovered = cursor.is_over(layout.bounds());
294312

295313
match (
296-
widget.on_mouse_enter.as_ref(),
297-
widget.on_mouse_move.as_ref(),
298-
widget.on_mouse_exit.as_ref(),
314+
widget.on_enter.as_ref(),
315+
widget.on_move.as_ref(),
316+
widget.on_exit.as_ref(),
299317
) {
300-
(Some(on_mouse_enter), _, _)
301-
if state.is_hovered && !was_hovered =>
302-
{
303-
shell.publish(on_mouse_enter.clone());
318+
(Some(on_enter), _, _) if state.is_hovered && !was_hovered => {
319+
shell.publish(on_enter.clone());
304320
}
305-
(_, Some(on_mouse_move), _) if state.is_hovered => {
321+
(_, Some(on_move), _) if state.is_hovered => {
306322
if let Some(position) = cursor.position_in(layout.bounds()) {
307-
shell.publish(on_mouse_move(position));
323+
shell.publish(on_move(position));
308324
}
309325
}
310-
(_, _, Some(on_mouse_exit)) if !state.is_hovered && was_hovered => {
311-
shell.publish(on_mouse_exit.clone());
326+
(_, _, Some(on_exit)) if !state.is_hovered && was_hovered => {
327+
shell.publish(on_exit.clone());
312328
}
313329
_ => {}
314330
}

0 commit comments

Comments
 (0)