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

feat(mouse area): add double click #163

Merged
merged 1 commit into from
Jul 23, 2024
Merged
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
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
Loading