Split Input mouse event by ButtonState

This commit is contained in:
Héctor Ramón Jiménez 2020-04-30 04:53:15 +02:00
parent e139aae143
commit e55cd9652e
11 changed files with 180 additions and 203 deletions

View File

@ -1,5 +1,4 @@
use super::Button; use super::Button;
use crate::ButtonState;
/// A mouse event. /// A mouse event.
/// ///
@ -24,14 +23,11 @@ pub enum Event {
y: f32, y: f32,
}, },
/// A mouse button was pressed or released. /// A mouse button was pressed.
Input { ButtonPressed(Button),
/// The button identifier
button: Button,
/// The state of the button /// A mouse button was released.
state: ButtonState, ButtonReleased(Button),
},
/// The mouse wheel was scrolled. /// The mouse wheel was scrolled.
WheelScrolled { WheelScrolled {

View File

@ -70,7 +70,7 @@ impl Sandbox for Example {
mod bezier { mod bezier {
use iced::{ use iced::{
canvas::{self, Canvas, Cursor, Event, Frame, Geometry, Path, Stroke}, canvas::{self, Canvas, Cursor, Event, Frame, Geometry, Path, Stroke},
mouse, ButtonState, Element, Length, MouseCursor, Point, Rectangle, mouse, Element, Length, MouseCursor, Point, Rectangle,
}; };
#[derive(Default)] #[derive(Default)]
@ -114,34 +114,33 @@ mod bezier {
match event { match event {
Event::Mouse(mouse_event) => match mouse_event { Event::Mouse(mouse_event) => match mouse_event {
mouse::Event::Input { mouse::Event::ButtonPressed(mouse::Button::Left) => {
button: mouse::Button::Left, match self.state.pending {
state: ButtonState::Pressed, None => {
} => match self.state.pending { self.state.pending = Some(Pending::One {
None => { from: cursor_position,
self.state.pending = Some(Pending::One { });
from: cursor_position, None
}); }
None Some(Pending::One { from }) => {
} self.state.pending = Some(Pending::Two {
Some(Pending::One { from }) => { from,
self.state.pending = Some(Pending::Two { to: cursor_position,
from, });
to: cursor_position,
});
None None
} }
Some(Pending::Two { from, to }) => { Some(Pending::Two { from, to }) => {
self.state.pending = None; self.state.pending = None;
Some(Curve { Some(Curve {
from, from,
to, to,
control: cursor_position, control: cursor_position,
}) })
}
} }
}, }
_ => None, _ => None,
}, },
} }

View File

@ -158,8 +158,8 @@ impl Application for GameOfLife {
mod grid { mod grid {
use iced::{ use iced::{
canvas::{self, Canvas, Cursor, Event, Frame, Geometry, Path}, canvas::{self, Canvas, Cursor, Event, Frame, Geometry, Path},
mouse, ButtonState, Color, Element, Length, MouseCursor, Point, mouse, Color, Element, Length, MouseCursor, Point, Rectangle, Size,
Rectangle, Size, Vector, Vector,
}; };
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
@ -268,11 +268,7 @@ mod grid {
bounds: Rectangle, bounds: Rectangle,
cursor: Cursor, cursor: Cursor,
) -> Option<Message> { ) -> Option<Message> {
if let Event::Mouse(mouse::Event::Input { if let Event::Mouse(mouse::Event::ButtonReleased(_)) = event {
state: ButtonState::Released,
..
}) = event
{
self.interaction = None; self.interaction = None;
} }
@ -287,10 +283,7 @@ mod grid {
match event { match event {
Event::Mouse(mouse_event) => match mouse_event { Event::Mouse(mouse_event) => match mouse_event {
mouse::Event::Input { mouse::Event::ButtonPressed(button) => match button {
button,
state: ButtonState::Pressed,
} => match button {
mouse::Button::Left => { mouse::Button::Left => {
self.interaction = Some(Interaction::Drawing); self.interaction = Some(Interaction::Drawing);

View File

@ -5,9 +5,8 @@
//! [`Button`]: struct.Button.html //! [`Button`]: struct.Button.html
//! [`State`]: struct.State.html //! [`State`]: struct.State.html
use crate::{ use crate::{
input::{mouse, ButtonState}, input::mouse, layout, Clipboard, Element, Event, Hasher, Layout, Length,
layout, Clipboard, Element, Event, Hasher, Layout, Length, Point, Point, Rectangle, Widget,
Rectangle, Widget,
}; };
use std::hash::Hash; use std::hash::Hash;
@ -185,28 +184,24 @@ where
_clipboard: Option<&dyn Clipboard>, _clipboard: Option<&dyn Clipboard>,
) { ) {
match event { match event {
Event::Mouse(mouse::Event::Input { Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
button: mouse::Button::Left, if self.on_press.is_some() {
state, let bounds = layout.bounds();
}) => {
self.state.is_pressed = bounds.contains(cursor_position);
}
}
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => {
if let Some(on_press) = self.on_press.clone() { if let Some(on_press) = self.on_press.clone() {
let bounds = layout.bounds(); let bounds = layout.bounds();
match state { let is_clicked = self.state.is_pressed
ButtonState::Pressed => { && bounds.contains(cursor_position);
self.state.is_pressed =
bounds.contains(cursor_position);
}
ButtonState::Released => {
let is_clicked = self.state.is_pressed
&& bounds.contains(cursor_position);
self.state.is_pressed = false; self.state.is_pressed = false;
if is_clicked { if is_clicked {
messages.push(on_press); messages.push(on_press);
}
}
} }
} }
} }

View File

@ -2,8 +2,7 @@
use std::hash::Hash; use std::hash::Hash;
use crate::{ use crate::{
input::{mouse, ButtonState}, input::mouse, layout, row, text, Align, Clipboard, Element, Event, Hasher,
layout, row, text, Align, Clipboard, Element, Event, Hasher,
HorizontalAlignment, Layout, Length, Point, Rectangle, Row, Text, HorizontalAlignment, Layout, Length, Point, Rectangle, Row, Text,
VerticalAlignment, Widget, VerticalAlignment, Widget,
}; };
@ -152,10 +151,7 @@ where
_clipboard: Option<&dyn Clipboard>, _clipboard: Option<&dyn Clipboard>,
) { ) {
match event { match event {
Event::Mouse(mouse::Event::Input { Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
button: mouse::Button::Left,
state: ButtonState::Pressed,
}) => {
let mouse_over = layout.bounds().contains(cursor_position); let mouse_over = layout.bounds().contains(cursor_position);
if mouse_over { if mouse_over {

View File

@ -22,7 +22,7 @@ pub use split::Split;
pub use state::{Focus, State}; pub use state::{Focus, State};
use crate::{ use crate::{
input::{keyboard, mouse, ButtonState}, input::{keyboard, mouse},
layout, Clipboard, Element, Event, Hasher, Layout, Length, Point, Size, layout, Clipboard, Element, Event, Hasher, Layout, Length, Point, Size,
Widget, Widget,
}; };
@ -405,11 +405,8 @@ where
clipboard: Option<&dyn Clipboard>, clipboard: Option<&dyn Clipboard>,
) { ) {
match event { match event {
Event::Mouse(mouse::Event::Input { Event::Mouse(mouse_event) => match mouse_event {
button: mouse::Button::Left, mouse::Event::ButtonPressed(mouse::Button::Left) => {
state,
}) => match state {
ButtonState::Pressed => {
let mut clicked_region = let mut clicked_region =
self.elements.iter().zip(layout.children()).filter( self.elements.iter().zip(layout.children()).filter(
|(_, layout)| { |(_, layout)| {
@ -438,7 +435,7 @@ where
self.state.unfocus(); self.state.unfocus();
} }
} }
ButtonState::Released => { mouse::Event::ButtonReleased(mouse::Button::Left) => {
if let Some(pane) = self.state.picked_pane() { if let Some(pane) = self.state.picked_pane() {
self.state.focus(&pane); self.state.focus(&pane);
@ -465,97 +462,110 @@ where
} }
} }
} }
}, mouse::Event::ButtonPressed(mouse::Button::Right)
Event::Mouse(mouse::Event::Input { if self.on_resize.is_some()
button: mouse::Button::Right, && self.state.picked_pane().is_none()
state: ButtonState::Pressed, && self
}) if self.on_resize.is_some() .pressed_modifiers
&& self.state.picked_pane().is_none() .matches(self.modifier_keys) =>
&& self.pressed_modifiers.matches(self.modifier_keys) => {
{ let bounds = layout.bounds();
let bounds = layout.bounds();
if bounds.contains(cursor_position) { if bounds.contains(cursor_position) {
let relative_cursor = Point::new( let relative_cursor = Point::new(
cursor_position.x - bounds.x, cursor_position.x - bounds.x,
cursor_position.y - bounds.y, cursor_position.y - bounds.y,
); );
let splits = self.state.splits( let splits = self.state.splits(
f32::from(self.spacing), f32::from(self.spacing),
Size::new(bounds.width, bounds.height), Size::new(bounds.width, bounds.height),
); );
let mut sorted_splits: Vec<_> = splits let mut sorted_splits: Vec<_> = splits
.iter() .iter()
.filter(|(_, (axis, rectangle, _))| match axis { .filter(|(_, (axis, rectangle, _))| match axis {
Axis::Horizontal => { Axis::Horizontal => {
relative_cursor.x > rectangle.x relative_cursor.x > rectangle.x
&& relative_cursor.x && relative_cursor.x
< rectangle.x + rectangle.width < rectangle.x + rectangle.width
} }
Axis::Vertical => { Axis::Vertical => {
relative_cursor.y > rectangle.y relative_cursor.y > rectangle.y
&& relative_cursor.y && relative_cursor.y
< rectangle.y + rectangle.height < rectangle.y + rectangle.height
} }
}) })
.collect(); .collect();
sorted_splits.sort_by_key( sorted_splits.sort_by_key(
|(_, (axis, rectangle, ratio))| { |(_, (axis, rectangle, ratio))| {
let distance = match axis { let distance = match axis {
Axis::Horizontal => (relative_cursor.y Axis::Horizontal => (relative_cursor.y
- (rectangle.y + rectangle.height * ratio)) - (rectangle.y
.abs(), + rectangle.height * ratio))
Axis::Vertical => (relative_cursor.x .abs(),
- (rectangle.x + rectangle.width * ratio)) Axis::Vertical => (relative_cursor.x
.abs(), - (rectangle.x
}; + rectangle.width * ratio))
.abs(),
};
distance.round() as u32 distance.round() as u32
}, },
); );
if let Some((split, (axis, _, _))) = sorted_splits.first() { if let Some((split, (axis, _, _))) =
self.state.pick_split(split, *axis); sorted_splits.first()
self.trigger_resize(layout, cursor_position, messages); {
} self.state.pick_split(split, *axis);
} self.trigger_resize(
} layout,
Event::Mouse(mouse::Event::Input { cursor_position,
button: mouse::Button::Right, messages,
state: ButtonState::Released, );
}) if self.state.picked_split().is_some() => {
self.state.drop_split();
}
Event::Mouse(mouse::Event::CursorMoved { .. }) => {
self.trigger_resize(layout, cursor_position, messages);
}
Event::Keyboard(keyboard::Event::KeyPressed {
modifiers,
key_code,
}) => {
if let Some(on_key_press) = &self.on_key_press {
// TODO: Discard when event is captured
if let Some(_) = self.state.active_pane() {
if modifiers.matches(self.modifier_keys) {
if let Some(message) = on_key_press(KeyPressEvent {
key_code,
modifiers,
}) {
messages.push(message);
}
} }
} }
} }
mouse::Event::ButtonPressed(mouse::Button::Right)
if self.state.picked_split().is_some() =>
{
self.state.drop_split();
}
mouse::Event::CursorMoved { .. } => {
self.trigger_resize(layout, cursor_position, messages);
}
_ => {}
},
Event::Keyboard(keyboard_event) => {
match keyboard_event {
keyboard::Event::KeyPressed {
modifiers,
key_code,
} => {
if let Some(on_key_press) = &self.on_key_press {
// TODO: Discard when event is captured
if let Some(_) = self.state.active_pane() {
if modifiers.matches(self.modifier_keys) {
if let Some(message) =
on_key_press(KeyPressEvent {
key_code,
modifiers,
})
{
messages.push(message);
}
}
}
}
*self.pressed_modifiers = modifiers; *self.pressed_modifiers = modifiers;
} }
Event::Keyboard(keyboard::Event::KeyReleased { keyboard::Event::KeyReleased { modifiers, .. } => {
modifiers, .. *self.pressed_modifiers = modifiers;
}) => { }
*self.pressed_modifiers = modifiers; _ => {}
}
} }
_ => {} _ => {}
} }

View File

@ -1,7 +1,6 @@
//! Create choices using radio buttons. //! Create choices using radio buttons.
use crate::{ use crate::{
input::{mouse, ButtonState}, input::mouse, layout, row, text, Align, Clipboard, Element, Event, Hasher,
layout, row, text, Align, Clipboard, Element, Event, Hasher,
HorizontalAlignment, Layout, Length, Point, Rectangle, Row, Text, HorizontalAlignment, Layout, Length, Point, Rectangle, Row, Text,
VerticalAlignment, Widget, VerticalAlignment, Widget,
}; };
@ -123,10 +122,7 @@ where
_clipboard: Option<&dyn Clipboard>, _clipboard: Option<&dyn Clipboard>,
) { ) {
match event { match event {
Event::Mouse(mouse::Event::Input { Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
button: mouse::Button::Left,
state: ButtonState::Pressed,
}) => {
if layout.bounds().contains(cursor_position) { if layout.bounds().contains(cursor_position) {
messages.push(self.on_click.clone()); messages.push(self.on_click.clone());
} }

View File

@ -1,9 +1,7 @@
//! Navigate an endless amount of content with a scrollbar. //! Navigate an endless amount of content with a scrollbar.
use crate::{ use crate::{
column, column, input::mouse, layout, Align, Clipboard, Column, Element, Event,
input::{mouse, ButtonState}, Hasher, Layout, Length, Point, Rectangle, Size, Widget,
layout, Align, Clipboard, Column, Element, Event, Hasher, Layout, Length,
Point, Rectangle, Size, Widget,
}; };
use std::{f32, hash::Hash, u32}; use std::{f32, hash::Hash, u32};
@ -188,10 +186,9 @@ where
if self.state.is_scroller_grabbed() { if self.state.is_scroller_grabbed() {
match event { match event {
Event::Mouse(mouse::Event::Input { Event::Mouse(mouse::Event::ButtonReleased(
button: mouse::Button::Left, mouse::Button::Left,
state: ButtonState::Released, )) => {
}) => {
self.state.scroller_grabbed_at = None; self.state.scroller_grabbed_at = None;
} }
Event::Mouse(mouse::Event::CursorMoved { .. }) => { Event::Mouse(mouse::Event::CursorMoved { .. }) => {
@ -212,10 +209,9 @@ where
} }
} else if is_mouse_over_scrollbar { } else if is_mouse_over_scrollbar {
match event { match event {
Event::Mouse(mouse::Event::Input { Event::Mouse(mouse::Event::ButtonPressed(
button: mouse::Button::Left, mouse::Button::Left,
state: ButtonState::Pressed, )) => {
}) => {
if let Some(scrollbar) = scrollbar { if let Some(scrollbar) = scrollbar {
if let Some(scroller_grabbed_at) = if let Some(scroller_grabbed_at) =
scrollbar.grab_scroller(cursor_position) scrollbar.grab_scroller(cursor_position)

View File

@ -5,9 +5,8 @@
//! [`Slider`]: struct.Slider.html //! [`Slider`]: struct.Slider.html
//! [`State`]: struct.State.html //! [`State`]: struct.State.html
use crate::{ use crate::{
input::{mouse, ButtonState}, input::mouse, layout, Clipboard, Element, Event, Hasher, Layout, Length,
layout, Clipboard, Element, Event, Hasher, Layout, Length, Point, Point, Rectangle, Size, Widget,
Rectangle, Size, Widget,
}; };
use std::{hash::Hash, ops::RangeInclusive}; use std::{hash::Hash, ops::RangeInclusive};
@ -164,25 +163,23 @@ where
}; };
match event { match event {
Event::Mouse(mouse::Event::Input { Event::Mouse(mouse_event) => match mouse_event {
button: mouse::Button::Left, mouse::Event::ButtonPressed(mouse::Button::Left) => {
state,
}) => match state {
ButtonState::Pressed => {
if layout.bounds().contains(cursor_position) { if layout.bounds().contains(cursor_position) {
change(); change();
self.state.is_dragging = true; self.state.is_dragging = true;
} }
} }
ButtonState::Released => { mouse::Event::ButtonReleased(mouse::Button::Left) => {
self.state.is_dragging = false; self.state.is_dragging = false;
} }
}, mouse::Event::CursorMoved { .. } => {
Event::Mouse(mouse::Event::CursorMoved { .. }) => { if self.state.is_dragging {
if self.state.is_dragging { change();
change(); }
} }
} _ => {}
},
_ => {} _ => {}
} }
} }

View File

@ -18,7 +18,6 @@ use crate::{
input::{ input::{
keyboard, keyboard,
mouse::{self, click}, mouse::{self, click},
ButtonState,
}, },
layout, Clipboard, Element, Event, Font, Hasher, Layout, Length, Point, layout, Clipboard, Element, Event, Font, Hasher, Layout, Length, Point,
Rectangle, Size, Widget, Rectangle, Size, Widget,
@ -212,10 +211,7 @@ where
clipboard: Option<&dyn Clipboard>, clipboard: Option<&dyn Clipboard>,
) { ) {
match event { match event {
Event::Mouse(mouse::Event::Input { Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
button: mouse::Button::Left,
state: ButtonState::Pressed,
}) => {
let is_clicked = layout.bounds().contains(cursor_position); let is_clicked = layout.bounds().contains(cursor_position);
if is_clicked { if is_clicked {
@ -280,10 +276,7 @@ where
self.state.is_dragging = is_clicked; self.state.is_dragging = is_clicked;
self.state.is_focused = is_clicked; self.state.is_focused = is_clicked;
} }
Event::Mouse(mouse::Event::Input { Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => {
button: mouse::Button::Left,
state: ButtonState::Released,
}) => {
self.state.is_dragging = false; self.state.is_dragging = false;
} }
Event::Mouse(mouse::Event::CursorMoved { x, .. }) => { Event::Mouse(mouse::Event::CursorMoved { x, .. }) => {

View File

@ -36,9 +36,15 @@ pub fn window_event(
})) }))
} }
WindowEvent::MouseInput { button, state, .. } => { WindowEvent::MouseInput { button, state, .. } => {
Some(Event::Mouse(mouse::Event::Input { let button = mouse_button(*button);
button: mouse_button(*button),
state: button_state(*state), Some(Event::Mouse(match state {
winit::event::ElementState::Pressed => {
mouse::Event::ButtonPressed(button)
}
winit::event::ElementState::Released => {
mouse::Event::ButtonReleased(button)
}
})) }))
} }
WindowEvent::MouseWheel { delta, .. } => match delta { WindowEvent::MouseWheel { delta, .. } => match delta {