From 080557274239d095f4e862c82e0d873c681949f5 Mon Sep 17 00:00:00 2001 From: Siliwolf Date: Sat, 25 May 2024 18:13:34 -0300 Subject: [PATCH 1/4] Added the ability to detect scrolling to the MouseArea --- widget/src/mouse_area.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/widget/src/mouse_area.rs b/widget/src/mouse_area.rs index d7235cf602..cee2023a6a 100644 --- a/widget/src/mouse_area.rs +++ b/widget/src/mouse_area.rs @@ -1,6 +1,7 @@ //! A container for capturing mouse events. use iced_renderer::core::Point; +use iced_runtime::core::mouse::ScrollDelta; use crate::core::event::{self, Event}; use crate::core::layout; @@ -28,6 +29,7 @@ pub struct MouseArea< on_right_release: Option, on_middle_press: Option, on_middle_release: Option, + on_scroll: Option Message + 'a>>, on_enter: Option, on_move: Option Message>>, on_exit: Option, @@ -77,6 +79,16 @@ impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> { self } + /// The message to emit when scroll wheel is used + #[must_use] + pub fn on_scroll(mut self, build_message: F) -> Self + where + F: Fn(ScrollDelta) -> Message + 'static, + { + self.on_scroll = Some(Box::new(build_message)); + self + } + /// The message to emit when the mouse enters the area. #[must_use] pub fn on_enter(mut self, message: Message) -> Self { @@ -128,6 +140,7 @@ impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> { on_right_release: None, on_middle_press: None, on_middle_release: None, + on_scroll: None, on_enter: None, on_move: None, on_exit: None, @@ -397,5 +410,13 @@ fn update( } } + if let Some(message) = widget.on_scroll.as_ref() { + if let Event::Mouse(mouse::Event::WheelScrolled { delta }) = event { + shell.publish(message(delta)); + + return event::Status::Captured; + } + } + event::Status::Ignored } From d52609ee49ec5bf5dd3ff6032e1501db05fd7289 Mon Sep 17 00:00:00 2001 From: Siliwolf Date: Wed, 29 May 2024 22:19:50 -0300 Subject: [PATCH 2/4] Added the ability to create a custom palette while providing a custom extended palette --- core/src/theme.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core/src/theme.rs b/core/src/theme.rs index 6b2c04da4f..f7c47891f8 100644 --- a/core/src/theme.rs +++ b/core/src/theme.rs @@ -226,6 +226,15 @@ impl Custom { Self::with_fn(name, palette, palette::Extended::generate) } + /// Creates a [`Custom`] theme from the given [`Palette`] and a custom [`palette::Extended`] palette + pub fn new_with_extended(name: String, palette: Palette, extended: palette::Extended) -> Self { + Self { + name, + palette, + extended, + } + } + /// Creates a [`Custom`] theme from the given [`Palette`] with /// a custom generator of a [`palette::Extended`]. pub fn with_fn( From 152d3236ebf0d7da6ac12d9fcc71016f7399a595 Mon Sep 17 00:00:00 2001 From: Siliwolf Date: Wed, 29 May 2024 22:29:01 -0300 Subject: [PATCH 3/4] Added a new_with_extented function to create a theme directly --- core/src/theme.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/src/theme.rs b/core/src/theme.rs index f7c47891f8..3d6589a37e 100644 --- a/core/src/theme.rs +++ b/core/src/theme.rs @@ -89,6 +89,11 @@ impl Theme { Self::custom_with_fn(name, palette, palette::Extended::generate) } + /// Creates a new custom [`Theme`] from the given [`Palette`] and a custom [`palette::Extended`] palette + pub fn new_with_extended(name: String, palette: Palette, extended: palette::Extended) -> Self { + Self::Custom(Arc::new(Custom::new_with_extended(name, palette, extended))) + } + /// Creates a new custom [`Theme`] from the given [`Palette`], with /// a custom generator of a [`palette::Extended`]. pub fn custom_with_fn( From 88d49465a97f0faa10d94e41d5205358ed3eca89 Mon Sep 17 00:00:00 2001 From: Siliwolf Date: Wed, 29 May 2024 22:31:52 -0300 Subject: [PATCH 4/4] Actually gave the function a sensible name --- core/src/theme.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/theme.rs b/core/src/theme.rs index 3d6589a37e..9aacbc920a 100644 --- a/core/src/theme.rs +++ b/core/src/theme.rs @@ -90,7 +90,7 @@ impl Theme { } /// Creates a new custom [`Theme`] from the given [`Palette`] and a custom [`palette::Extended`] palette - pub fn new_with_extended(name: String, palette: Palette, extended: palette::Extended) -> Self { + pub fn custom_with_extended(name: String, palette: Palette, extended: palette::Extended) -> Self { Self::Custom(Arc::new(Custom::new_with_extended(name, palette, extended))) }