Skip to content

Commit

Permalink
Add keyboard modifiers to mouse events
Browse files Browse the repository at this point in the history
  • Loading branch information
edwloef committed Jan 18, 2025
1 parent f2c9b6b commit 99b2b35
Show file tree
Hide file tree
Showing 24 changed files with 150 additions and 77 deletions.
11 changes: 9 additions & 2 deletions core/src/mouse/event.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Point;
use crate::{keyboard::Modifiers, Point};

use super::Button;

Expand All @@ -23,7 +23,12 @@ pub enum Event {
},

/// A mouse button was pressed.
ButtonPressed(Button),
ButtonPressed {
/// the button that was pressed
button: Button,
/// the modifier keys at the time of the button press
modifiers: Modifiers,
},

/// A mouse button was released.
ButtonReleased(Button),
Expand All @@ -32,6 +37,8 @@ pub enum Event {
WheelScrolled {
/// The scroll movement.
delta: ScrollDelta,
/// the modifier keys at the time of the button press
modifiers: Modifiers,
},
}

Expand Down
7 changes: 4 additions & 3 deletions examples/bezier_tool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ mod bezier {
let cursor_position = cursor.position_in(bounds)?;

match event {
Event::Mouse(mouse::Event::ButtonPressed(
mouse::Button::Left,
)) => Some(
Event::Mouse(mouse::Event::ButtonPressed {
button: mouse::Button::Left,
..
}) => Some(
match *state {
None => {
*state = Some(Pending::One {
Expand Down
6 changes: 3 additions & 3 deletions examples/game_of_life/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ mod grid {
bounds: Rectangle,
cursor: mouse::Cursor,
) -> Option<canvas::Action<Message>> {
if let Event::Mouse(mouse::Event::ButtonReleased(_)) = event {
if let Event::Mouse(mouse::Event::ButtonReleased { .. }) = event {
*interaction = Interaction::None;
}

Expand Down Expand Up @@ -420,7 +420,7 @@ mod grid {
)
}
Event::Mouse(mouse_event) => match mouse_event {
mouse::Event::ButtonPressed(button) => {
mouse::Event::ButtonPressed { button, .. } => {
let message = match button {
mouse::Button::Left => {
*interaction = if is_populated {
Expand Down Expand Up @@ -472,7 +472,7 @@ mod grid {
_ => action.and_capture(),
})
}
mouse::Event::WheelScrolled { delta } => match delta {
mouse::Event::WheelScrolled { delta, .. } => match delta {
mouse::ScrollDelta::Lines { y, .. }
| mouse::ScrollDelta::Pixels { y, .. } => {
if y < 0.0 && self.scaling > Self::MIN_SCALING
Expand Down
18 changes: 10 additions & 8 deletions examples/sierpinski_triangle/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,17 @@ impl canvas::Program<Message> for SierpinskiGraph {
let cursor_position = cursor.position_in(bounds)?;

match event {
Event::Mouse(mouse::Event::ButtonPressed(button)) => match button {
mouse::Button::Left => Some(canvas::Action::publish(
Message::PointAdded(cursor_position),
)),
mouse::Button::Right => {
Some(canvas::Action::publish(Message::PointRemoved))
Event::Mouse(mouse::Event::ButtonPressed { button, .. }) => {
match button {
mouse::Button::Left => Some(canvas::Action::publish(
Message::PointAdded(cursor_position),
)),
mouse::Button::Right => {
Some(canvas::Action::publish(Message::PointRemoved))
}
_ => None,
}
_ => None,
},
}
_ => None,
}
.map(canvas::Action::and_capture)
Expand Down
2 changes: 1 addition & 1 deletion examples/visible_bounds/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl Example {

fn subscription(&self) -> Subscription<Message> {
event::listen_with(|event, _status, _window| match event {
Event::Mouse(mouse::Event::CursorMoved { position }) => {
Event::Mouse(mouse::Event::CursorMoved { position, .. }) => {
Some(Message::MouseMoved(position))
}
Event::Window(window::Event::Resized { .. }) => {
Expand Down
6 changes: 5 additions & 1 deletion test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
//! [the classical counter interface]: https://book.iced.rs/architecture.html#dissecting-an-interface
pub mod selector;

use iced_runtime::keyboard::Modifiers;
pub use selector::Selector;

use iced_renderer as renderer;
Expand Down Expand Up @@ -544,7 +545,10 @@ impl Snapshot {
/// Returns the sequence of events of a click.
pub fn click() -> impl Iterator<Item = Event> {
[
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)),
Event::Mouse(mouse::Event::ButtonPressed {
button: mouse::Button::Left,
modifiers: Modifiers::empty(),
}),
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)),
]
.into_iter()
Expand Down
9 changes: 6 additions & 3 deletions widget/src/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,10 @@ where
}

match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
Event::Mouse(mouse::Event::ButtonPressed {
button: mouse::Button::Left,
..
})
| Event::Touch(touch::Event::FingerPressed { .. }) => {
if self.on_press.is_some() {
let bounds = layout.bounds();
Expand Down Expand Up @@ -533,11 +536,11 @@ impl Default for Style {
///
/// impl Catalog for MyTheme {
/// type Class<'a> = ButtonClass;
///
///
/// fn default<'a>() -> Self::Class<'a> {
/// ButtonClass::default()
/// }
///
///
///
/// fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
/// let mut style = Style::default();
Expand Down
5 changes: 4 additions & 1 deletion widget/src/checkbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,10 @@ where
_viewport: &Rectangle,
) {
match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
Event::Mouse(mouse::Event::ButtonPressed {
button: mouse::Button::Left,
..
})
| Event::Touch(touch::Event::FingerPressed { .. }) => {
let mouse_over = cursor.is_over(layout.bounds());

Expand Down
8 changes: 5 additions & 3 deletions widget/src/combo_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ use crate::core::{
Vector,
};
use crate::overlay::menu;
use crate::runtime::keyboard::Modifiers;
use crate::text::LineHeight;
use crate::text_input::{self, TextInput};

Expand Down Expand Up @@ -744,9 +745,10 @@ where
// Unfocus the input
self.text_input.update(
&mut tree.children[0],
Event::Mouse(mouse::Event::ButtonPressed(
mouse::Button::Left,
)),
Event::Mouse(mouse::Event::ButtonPressed {
button: mouse::Button::Left,
modifiers: Modifiers::empty(),
}),
layout,
mouse::Cursor::Unavailable,
renderer,
Expand Down
4 changes: 2 additions & 2 deletions widget/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ where
) {
let is_mouse_press = matches!(
event,
core::Event::Mouse(mouse::Event::ButtonPressed(_))
core::Event::Mouse(mouse::Event::ButtonPressed { .. })
);

self.content.as_widget_mut().update(
Expand Down Expand Up @@ -715,7 +715,7 @@ where
event,
Event::Mouse(
mouse::Event::CursorMoved { .. }
| mouse::Event::ButtonReleased(_)
| mouse::Event::ButtonReleased { .. }
)
) || is_hovered
|| self.is_top_focused
Expand Down
9 changes: 6 additions & 3 deletions widget/src/image/viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ where
let bounds = layout.bounds();

match event {
Event::Mouse(mouse::Event::WheelScrolled { delta }) => {
Event::Mouse(mouse::Event::WheelScrolled { delta, .. }) => {
let Some(cursor_position) = cursor.position_over(bounds) else {
return;
};
Expand Down Expand Up @@ -217,7 +217,10 @@ where

shell.capture_event();
}
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
Event::Mouse(mouse::Event::ButtonPressed {
button: mouse::Button::Left,
..
}) => {
let Some(cursor_position) = cursor.position_over(bounds) else {
return;
};
Expand All @@ -236,7 +239,7 @@ where
shell.capture_event();
}
}
Event::Mouse(mouse::Event::CursorMoved { position }) => {
Event::Mouse(mouse::Event::CursorMoved { position, .. }) => {
let state = tree.state.downcast_mut::<State>();

if let Some(origin) = state.cursor_grabbed_at {
Expand Down
17 changes: 13 additions & 4 deletions widget/src/mouse_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,10 @@ fn update<Message: Clone, Theme, Renderer>(
}

match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
Event::Mouse(mouse::Event::ButtonPressed {
button: mouse::Button::Left,
..
})
| Event::Touch(touch::Event::FingerPressed { .. }) => {
if let Some(message) = widget.on_press.as_ref() {
shell.publish(message.clone());
Expand Down Expand Up @@ -401,7 +404,10 @@ fn update<Message: Clone, Theme, Renderer>(
shell.publish(message.clone());
}
}
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Right)) => {
Event::Mouse(mouse::Event::ButtonPressed {
button: mouse::Button::Right,
..
}) => {
if let Some(message) = widget.on_right_press.as_ref() {
shell.publish(message.clone());
shell.capture_event();
Expand All @@ -412,7 +418,10 @@ fn update<Message: Clone, Theme, Renderer>(
shell.publish(message.clone());
}
}
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Middle)) => {
Event::Mouse(mouse::Event::ButtonPressed {
button: mouse::Button::Middle,
..
}) => {
if let Some(message) = widget.on_middle_press.as_ref() {
shell.publish(message.clone());
shell.capture_event();
Expand All @@ -423,7 +432,7 @@ fn update<Message: Clone, Theme, Renderer>(
shell.publish(message.clone());
}
}
Event::Mouse(mouse::Event::WheelScrolled { delta }) => {
Event::Mouse(mouse::Event::WheelScrolled { delta, .. }) => {
if let Some(on_scroll) = widget.on_scroll.as_ref() {
shell.publish(on_scroll(delta));
shell.capture_event();
Expand Down
5 changes: 4 additions & 1 deletion widget/src/overlay/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,10 @@ where
_viewport: &Rectangle,
) {
match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
Event::Mouse(mouse::Event::ButtonPressed {
button: mouse::Button::Left,
..
}) => {
if cursor.is_over(layout.bounds()) {
if let Some(index) = *self.hovered_option {
if let Some(option) = self.options.get(index) {
Expand Down
5 changes: 4 additions & 1 deletion widget/src/pane_grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,10 @@ where
}

match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
Event::Mouse(mouse::Event::ButtonPressed {
button: mouse::Button::Left,
..
})
| Event::Touch(touch::Event::FingerPressed { .. }) => {
let bounds = layout.bounds();

Expand Down
6 changes: 5 additions & 1 deletion widget/src/pick_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,10 @@ where
let state = tree.state.downcast_mut::<State<Renderer::Paragraph>>();

match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
Event::Mouse(mouse::Event::ButtonPressed {
button: mouse::Button::Left,
..
})
| Event::Touch(touch::Event::FingerPressed { .. }) => {
if state.is_open {
// Event wasn't processed by overlay, so cursor was clicked either outside its
Expand Down Expand Up @@ -472,6 +475,7 @@ where
}
Event::Mouse(mouse::Event::WheelScrolled {
delta: mouse::ScrollDelta::Lines { y, .. },
..
}) => {
if state.keyboard_modifiers.command()
&& cursor.is_over(layout.bounds())
Expand Down
5 changes: 4 additions & 1 deletion widget/src/radio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,10 @@ where
_viewport: &Rectangle,
) {
match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
Event::Mouse(mouse::Event::ButtonPressed {
button: mouse::Button::Left,
..
})
| Event::Touch(touch::Event::FingerPressed { .. }) => {
if cursor.is_over(layout.bounds()) {
shell.publish(self.on_click.clone());
Expand Down
20 changes: 11 additions & 9 deletions widget/src/scrollable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,8 @@ where
if let Some(last_scrolled) = state.last_scrolled {
let clear_transaction = match event {
Event::Mouse(
mouse::Event::ButtonPressed(_)
| mouse::Event::ButtonReleased(_)
mouse::Event::ButtonPressed { .. }
| mouse::Event::ButtonReleased { .. }
| mouse::Event::CursorLeft,
) => true,
Event::Mouse(mouse::Event::CursorMoved { .. }) => {
Expand Down Expand Up @@ -587,9 +587,10 @@ where
}
} else if mouse_over_y_scrollbar {
match event {
Event::Mouse(mouse::Event::ButtonPressed(
mouse::Button::Left,
))
Event::Mouse(mouse::Event::ButtonPressed {
button: mouse::Button::Left,
..
})
| Event::Touch(touch::Event::FingerPressed { .. }) => {
let Some(cursor_position) = cursor.position() else {
return;
Expand Down Expand Up @@ -659,9 +660,10 @@ where
}
} else if mouse_over_x_scrollbar {
match event {
Event::Mouse(mouse::Event::ButtonPressed(
mouse::Button::Left,
))
Event::Mouse(mouse::Event::ButtonPressed {
button: mouse::Button::Left,
..
})
| Event::Touch(touch::Event::FingerPressed { .. }) => {
let Some(cursor_position) = cursor.position() else {
return;
Expand Down Expand Up @@ -769,7 +771,7 @@ where
}

match event {
Event::Mouse(mouse::Event::WheelScrolled { delta }) => {
Event::Mouse(mouse::Event::WheelScrolled { delta, .. }) => {
if cursor_over_scrollable.is_none() {
return;
}
Expand Down
Loading

0 comments on commit 99b2b35

Please sign in to comment.