Skip to content

Commit

Permalink
feat(mouse area): add double click
Browse files Browse the repository at this point in the history
mouse area: add double click
  • Loading branch information
leb-kuchen authored and mmstick committed Jul 23, 2024
1 parent cb315f6 commit fa817c7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }


28 changes: 28 additions & 0 deletions widget/src/mouse_area.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -25,6 +26,7 @@ pub struct MouseArea<
content: Element<'a, Message, Theme, Renderer>,
on_drag: Option<Message>,
on_press: Option<Message>,
on_double_press: Option<Message>,
on_release: Option<Message>,
on_right_press: Option<Message>,
on_right_release: Option<Message>,
Expand All @@ -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]
Expand Down Expand Up @@ -102,12 +110,14 @@ struct State {
// TODO: Support on_mouse_enter and on_mouse_exit
drag_initiated: Option<Point>,
is_out_of_bounds: bool,
last_click: Option<Click>,
}
impl Default for State {
fn default() -> Self {
Self {
drag_initiated: Default::default(),
is_out_of_bounds: true,
last_click: Default::default(),
}
}
}
Expand All @@ -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,
Expand Down Expand Up @@ -330,6 +341,22 @@ fn update<Message: Clone, Theme, Renderer>(
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
Expand Down Expand Up @@ -394,6 +421,7 @@ fn update<Message: Clone, Theme, Renderer>(
return event::Status::Captured;
}
}

if let Some(message) = widget
.on_mouse_enter
.as_ref()
Expand Down

0 comments on commit fa817c7

Please sign in to comment.