Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to detect scrolling to the MouseArea #2450

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions core/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 custom_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(
Expand Down Expand Up @@ -226,6 +231,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(
Expand Down
21 changes: 21 additions & 0 deletions widget/src/mouse_area.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -28,6 +29,7 @@ pub struct MouseArea<
on_right_release: Option<Message>,
on_middle_press: Option<Message>,
on_middle_release: Option<Message>,
on_scroll: Option<Box<dyn Fn(ScrollDelta) -> Message + 'a>>,
on_enter: Option<Message>,
on_move: Option<Box<dyn Fn(Point) -> Message>>,
on_exit: Option<Message>,
Expand Down Expand Up @@ -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<F>(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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -397,5 +410,13 @@ fn update<Message: Clone, Theme, Renderer>(
}
}

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
}