From bcdfbf487198b59f4432545288914f139cdcaf9d Mon Sep 17 00:00:00 2001 From: vawvaw Date: Sat, 20 Jan 2024 18:47:14 +0100 Subject: Add `Interaction` overriding to `MouseArea` Add the ability to use a custom `iced::mouse::Interaction` for a `iced::widget::MouseArea`. --- widget/src/mouse_area.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/widget/src/mouse_area.rs b/widget/src/mouse_area.rs index f82a75f4..a5967077 100644 --- a/widget/src/mouse_area.rs +++ b/widget/src/mouse_area.rs @@ -31,6 +31,7 @@ pub struct MouseArea< on_mouse_enter: Option, on_mouse_move: Option Message>>, on_mouse_exit: Option, + interaction: Option, } impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> { @@ -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`]. @@ -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, } } } @@ -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, -- cgit From d756a9210acd97eb41191f5199f4d7357f4d253a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 7 Feb 2024 12:00:33 +0100 Subject: Override `MouseArea` interaction only when contents are idle --- widget/src/mouse_area.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/widget/src/mouse_area.rs b/widget/src/mouse_area.rs index a5967077..c9258c40 100644 --- a/widget/src/mouse_area.rs +++ b/widget/src/mouse_area.rs @@ -101,7 +101,7 @@ impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> { self } - /// The version of the cursor to use when hovering. + /// The [`mouse::Interaction`] to use when hovering the area. #[must_use] pub fn mouse_interaction( mut self, @@ -226,21 +226,22 @@ 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( + 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( -- cgit From fef6fca369a5197e90742c03a60122952dd3e645 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 7 Feb 2024 12:02:56 +0100 Subject: Shorten some `MouseArea` methods --- widget/src/mouse_area.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/widget/src/mouse_area.rs b/widget/src/mouse_area.rs index c9258c40..63a8ae89 100644 --- a/widget/src/mouse_area.rs +++ b/widget/src/mouse_area.rs @@ -79,14 +79,14 @@ 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 { + pub fn on_enter(mut self, message: Message) -> Self { self.on_mouse_enter = Some(message); self } /// The message to emit when the mouse moves in the area. #[must_use] - pub fn on_mouse_move(mut self, build_message: F) -> Self + pub fn on_move(mut self, build_message: F) -> Self where F: Fn(Point) -> Message + 'static, { @@ -96,17 +96,14 @@ impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> { /// The message to emit when the mouse exits the area. #[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 } /// The [`mouse::Interaction`] to use when hovering the area. #[must_use] - pub fn mouse_interaction( - mut self, - interaction: mouse::Interaction, - ) -> Self { + pub fn interaction(mut self, interaction: mouse::Interaction) -> Self { self.interaction = Some(interaction); self } -- cgit From ab3aa77ebe31764b34f7ddc65436740abbf96eb7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 7 Feb 2024 12:03:15 +0100 Subject: Update `CHANGELOG` --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c62ab531..30704e64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) -- cgit From 8b22e0ebbce8f60c91f16795a3ea0def0a869859 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 7 Feb 2024 12:05:47 +0100 Subject: Shorten properties in `MouseArea` --- widget/src/mouse_area.rs | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/widget/src/mouse_area.rs b/widget/src/mouse_area.rs index 63a8ae89..9634e477 100644 --- a/widget/src/mouse_area.rs +++ b/widget/src/mouse_area.rs @@ -28,9 +28,9 @@ pub struct MouseArea< on_right_release: Option, on_middle_press: Option, on_middle_release: Option, - on_mouse_enter: Option, - on_mouse_move: Option Message>>, - on_mouse_exit: Option, + on_enter: Option, + on_move: Option Message>>, + on_exit: Option, interaction: Option, } @@ -80,7 +80,7 @@ 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_enter(mut self, message: Message) -> Self { - self.on_mouse_enter = Some(message); + self.on_enter = Some(message); self } @@ -90,14 +90,14 @@ impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> { 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_exit(mut self, message: Message) -> Self { - self.on_mouse_exit = Some(message); + self.on_exit = Some(message); self } @@ -128,9 +128,9 @@ 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, } } @@ -311,22 +311,20 @@ fn update( 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()); } _ => {} } -- cgit From d5a3e599b32860c6c7cef4c6aa2a69c713b57bcb Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 7 Feb 2024 12:06:19 +0100 Subject: Update `CHANGELOG` --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 30704e64..03535ccd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -136,6 +136,7 @@ Many thanks to... - @tarkah - @tzemanovic - @varbhat +- @VAWVAW - @william-shere ## [0.10.0] - 2023-07-28 -- cgit