From 7ceec5015a9d89ebcee723e3a60c75b6172dafdd Mon Sep 17 00:00:00 2001 From: Dominic Gerhauser Date: Mon, 22 Jul 2024 19:04:31 +0200 Subject: [PATCH] feat(mouse area): add double click mouse area: add double click --- Cargo.toml | 2 ++ widget/src/mouse_area.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index fbc6057a8c..b4260463d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -199,3 +199,5 @@ mime = { git = "https://github.com/pop-os/window_clipboard.git", tag = "pop-dnd- # dnd = { path = "../../window_clipboard/dnd" } # mime = { path = "../../window_clipboard/mime" } winit = { git = "https://github.com/pop-os/winit.git", branch = "winit-0.29" } + + diff --git a/widget/src/mouse_area.rs b/widget/src/mouse_area.rs index 0e45ffe8e5..8ba079a53e 100644 --- a/widget/src/mouse_area.rs +++ b/widget/src/mouse_area.rs @@ -1,5 +1,6 @@ //! A container for capturing mouse events. +use iced_renderer::core::mouse::Click; use iced_renderer::core::widget::OperationOutputWrapper; use iced_renderer::core::Point; @@ -25,6 +26,7 @@ pub struct MouseArea< content: Element<'a, Message, Theme, Renderer>, on_drag: Option, on_press: Option, + on_double_press: Option, on_release: Option, on_right_press: Option, on_right_release: Option, @@ -48,6 +50,12 @@ impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> { self.on_press = Some(message); self } + /// The message to emit on a left double button press. + #[must_use] + pub fn on_double_press(mut self, message: Message) -> Self { + self.on_double_press = Some(message); + self + } /// The message to emit on a left button release. #[must_use] @@ -102,12 +110,14 @@ struct State { // TODO: Support on_mouse_enter and on_mouse_exit drag_initiated: Option, is_out_of_bounds: bool, + last_click: Option, } impl Default for State { fn default() -> Self { Self { drag_initiated: Default::default(), is_out_of_bounds: true, + last_click: Default::default(), } } } @@ -121,6 +131,7 @@ impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> { content: content.into(), on_drag: None, on_press: None, + on_double_press: None, on_release: None, on_right_press: None, on_right_release: None, @@ -330,6 +341,22 @@ fn update( return event::Status::Ignored; } + if let Some(message) = widget.on_double_press.as_ref() { + if let Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) = + event + { + if let Some(cursor_position) = cursor.position() { + let click = + mouse::Click::new(cursor_position, state.last_click); + state.last_click = Some(click); + if let mouse::click::Kind::Double = click.kind() { + shell.publish(message.clone()); + return event::Status::Captured; + } + } + } + } + if let Some(message) = widget.on_press.as_ref() { if let Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) | Event::Touch(touch::Event::FingerPressed { .. }) = event @@ -394,6 +421,7 @@ fn update( return event::Status::Captured; } } + if let Some(message) = widget .on_mouse_enter .as_ref()