Turn Touch into a touch::Event enum

This commit is contained in:
Héctor Ramón Jiménez 2020-12-15 06:38:46 +01:00
parent 09110a93b0
commit 3bdf931925
9 changed files with 89 additions and 118 deletions

View File

@ -22,7 +22,7 @@ pub enum Event {
Window(window::Event), Window(window::Event),
/// A touch event /// A touch event
Touch(touch::Touch), Touch(touch::Event),
} }
/// The status of an [`Event`] after being processed. /// The status of an [`Event`] after being processed.

View File

@ -3,33 +3,21 @@ use crate::Point;
/// A touch interaction. /// A touch interaction.
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub struct Touch { #[allow(missing_docs)]
/// The finger of the touch. pub enum Event {
pub finger: Finger, /// A touch interaction was started.
FingerPressed { id: Finger, position: Point },
/// The position of the touch. /// An on-going touch interaction was moved.
pub position: Point, FingerMoved { id: Finger, position: Point },
/// The state of the touch. /// A touch interaction was ended.
pub phase: Phase, FingerLifted { id: Finger, position: Point },
/// A touch interaction was canceled.
FingerLost { id: Finger, position: Point },
} }
/// A unique identifier representing a finger on a touch interaction. /// A unique identifier representing a finger on a touch interaction.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Finger(pub u64); pub struct Finger(pub u64);
/// The state of a touch interaction.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Phase {
/// A touch interaction was started.
Started,
/// An on-going touch interaction was moved.
Moved,
/// A touch interaction was ended.
Ended,
/// A touch interaction was canceled.
Canceled,
}

View File

@ -4,7 +4,7 @@
use crate::event::{self, Event}; use crate::event::{self, Event};
use crate::layout; use crate::layout;
use crate::mouse; use crate::mouse;
use crate::touch::{self, Touch}; use crate::touch;
use crate::{ use crate::{
Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Widget, Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Widget,
}; };
@ -166,10 +166,7 @@ where
) -> event::Status { ) -> event::Status {
match event { match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
| Event::Touch(Touch { | Event::Touch(touch::Event::FingerPressed { .. }) => {
phase: touch::Phase::Started,
..
}) => {
if self.on_press.is_some() { if self.on_press.is_some() {
let bounds = layout.bounds(); let bounds = layout.bounds();
@ -181,10 +178,7 @@ where
} }
} }
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
| Event::Touch(Touch { | Event::Touch(touch::Event::FingerLifted { .. }) => {
phase: touch::Phase::Ended,
..
}) => {
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();
@ -199,10 +193,7 @@ where
} }
} }
} }
Event::Touch(Touch { Event::Touch(touch::Event::FingerLost { .. }) => {
phase: touch::Phase::Canceled,
..
}) => {
self.state.is_pressed = false; self.state.is_pressed = false;
} }
_ => {} _ => {}

View File

@ -6,7 +6,7 @@ use crate::layout;
use crate::mouse; use crate::mouse;
use crate::row; use crate::row;
use crate::text; use crate::text;
use crate::touch::{self, Touch}; use crate::touch;
use crate::{ use crate::{
Align, Clipboard, Element, Hasher, HorizontalAlignment, Layout, Length, Align, Clipboard, Element, Hasher, HorizontalAlignment, Layout, Length,
Point, Rectangle, Row, Text, VerticalAlignment, Widget, Point, Rectangle, Row, Text, VerticalAlignment, Widget,
@ -156,10 +156,7 @@ where
) -> event::Status { ) -> event::Status {
match event { match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
| Event::Touch(Touch { | Event::Touch(touch::Event::FingerPressed { .. }) => {
phase: touch::Phase::Started,
..
}) => {
let mouse_over = layout.bounds().contains(cursor_position); let mouse_over = layout.bounds().contains(cursor_position);
if mouse_over { if mouse_over {

View File

@ -4,7 +4,7 @@ use crate::layout;
use crate::mouse; use crate::mouse;
use crate::row; use crate::row;
use crate::text; use crate::text;
use crate::touch::{self, Touch}; use crate::touch;
use crate::{ use crate::{
Align, Clipboard, Element, Hasher, HorizontalAlignment, Layout, Length, Align, Clipboard, Element, Hasher, HorizontalAlignment, Layout, Length,
Point, Rectangle, Row, Text, VerticalAlignment, Widget, Point, Rectangle, Row, Text, VerticalAlignment, Widget,
@ -162,10 +162,7 @@ where
) -> event::Status { ) -> event::Status {
match event { match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
| Event::Touch(Touch { | Event::Touch(touch::Event::FingerPressed { .. }) => {
phase: touch::Phase::Started,
..
}) => {
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

@ -4,7 +4,7 @@ use crate::event::{self, Event};
use crate::layout; use crate::layout;
use crate::mouse; use crate::mouse;
use crate::overlay; use crate::overlay;
use crate::touch::{self, Touch}; use crate::touch;
use crate::{ use crate::{
Align, Clipboard, Column, Element, Hasher, Layout, Length, Point, Align, Clipboard, Column, Element, Hasher, Layout, Length, Point,
Rectangle, Size, Vector, Widget, Rectangle, Size, Vector, Widget,
@ -230,26 +230,37 @@ where
return event::Status::Captured; return event::Status::Captured;
} }
Event::Touch(Touch { phase, .. }) => match phase { Event::Touch(event) => {
touch::Phase::Started => { match event {
self.state.scroll_box_touched_at = touch::Event::FingerPressed { .. } => {
Some(cursor_position);
}
touch::Phase::Moved => {
if let Some(scroll_box_touched_at) =
self.state.scroll_box_touched_at
{
let delta =
cursor_position.y - scroll_box_touched_at.y;
self.state.scroll(delta, bounds, content_bounds);
self.state.scroll_box_touched_at = self.state.scroll_box_touched_at =
Some(cursor_position); Some(cursor_position);
} }
touch::Event::FingerMoved { .. } => {
if let Some(scroll_box_touched_at) =
self.state.scroll_box_touched_at
{
let delta =
cursor_position.y - scroll_box_touched_at.y;
self.state.scroll(
delta,
bounds,
content_bounds,
);
self.state.scroll_box_touched_at =
Some(cursor_position);
}
}
touch::Event::FingerLifted { .. }
| touch::Event::FingerLost { .. } => {
self.state.scroll_box_touched_at = None;
}
} }
touch::Phase::Ended | touch::Phase::Canceled => {
self.state.scroll_box_touched_at = None; return event::Status::Captured;
} }
},
_ => {} _ => {}
} }
} }
@ -259,23 +270,14 @@ where
Event::Mouse(mouse::Event::ButtonReleased( Event::Mouse(mouse::Event::ButtonReleased(
mouse::Button::Left, mouse::Button::Left,
)) ))
| Event::Touch(Touch { | Event::Touch(touch::Event::FingerLifted { .. })
phase: touch::Phase::Ended, | Event::Touch(touch::Event::FingerLost { .. }) => {
..
})
| Event::Touch(Touch {
phase: touch::Phase::Canceled,
..
}) => {
self.state.scroller_grabbed_at = None; self.state.scroller_grabbed_at = None;
return event::Status::Captured; return event::Status::Captured;
} }
Event::Mouse(mouse::Event::CursorMoved { .. }) Event::Mouse(mouse::Event::CursorMoved { .. })
| Event::Touch(Touch { | Event::Touch(touch::Event::FingerMoved { .. }) => {
phase: touch::Phase::Moved,
..
}) => {
if let (Some(scrollbar), Some(scroller_grabbed_at)) = if let (Some(scrollbar), Some(scroller_grabbed_at)) =
(scrollbar, self.state.scroller_grabbed_at) (scrollbar, self.state.scroller_grabbed_at)
{ {
@ -298,10 +300,7 @@ where
Event::Mouse(mouse::Event::ButtonPressed( Event::Mouse(mouse::Event::ButtonPressed(
mouse::Button::Left, mouse::Button::Left,
)) ))
| Event::Touch(Touch { | Event::Touch(touch::Event::FingerPressed { .. }) => {
phase: touch::Phase::Started,
..
}) => {
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

@ -4,7 +4,7 @@
use crate::event::{self, Event}; use crate::event::{self, Event};
use crate::layout; use crate::layout;
use crate::mouse; use crate::mouse;
use crate::touch::{self, Touch}; use crate::touch;
use crate::{ use crate::{
Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget, Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget,
}; };
@ -209,10 +209,7 @@ where
match event { match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
| Event::Touch(Touch { | Event::Touch(touch::Event::FingerPressed { .. }) => {
phase: touch::Phase::Started,
..
}) => {
if layout.bounds().contains(cursor_position) { if layout.bounds().contains(cursor_position) {
change(); change();
self.state.is_dragging = true; self.state.is_dragging = true;
@ -221,10 +218,8 @@ where
} }
} }
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
| Event::Touch(Touch { | Event::Touch(touch::Event::FingerLifted { .. })
phase: touch::Phase::Ended, | Event::Touch(touch::Event::FingerLost { .. }) => {
..
}) => {
if self.state.is_dragging { if self.state.is_dragging {
if let Some(on_release) = self.on_release.clone() { if let Some(on_release) = self.on_release.clone() {
messages.push(on_release); messages.push(on_release);
@ -235,10 +230,7 @@ where
} }
} }
Event::Mouse(mouse::Event::CursorMoved { .. }) Event::Mouse(mouse::Event::CursorMoved { .. })
| Event::Touch(Touch { | Event::Touch(touch::Event::FingerMoved { .. }) => {
phase: touch::Phase::Moved,
..
}) => {
if self.state.is_dragging { if self.state.is_dragging {
change(); change();

View File

@ -16,7 +16,7 @@ use crate::keyboard;
use crate::layout; use crate::layout;
use crate::mouse::{self, click}; use crate::mouse::{self, click};
use crate::text; use crate::text;
use crate::touch::{self, Touch}; use crate::touch;
use crate::{ use crate::{
Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget, Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget,
}; };
@ -249,10 +249,7 @@ where
) -> event::Status { ) -> event::Status {
match event { match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
| Event::Touch(Touch { | Event::Touch(touch::Event::FingerPressed { .. }) => {
phase: touch::Phase::Started,
..
}) => {
let is_clicked = layout.bounds().contains(cursor_position); let is_clicked = layout.bounds().contains(cursor_position);
self.state.is_focused = is_clicked; self.state.is_focused = is_clicked;

View File

@ -2,12 +2,11 @@
//! //!
//! [`winit`]: https://github.com/rust-windowing/winit //! [`winit`]: https://github.com/rust-windowing/winit
//! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native //! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
use crate::{ use crate::keyboard;
keyboard::{self, KeyCode, Modifiers}, use crate::mouse;
mouse, use crate::touch;
touch::{self, Touch}, use crate::window;
window, Event, Mode, Point, use crate::{Event, Mode, Point};
};
/// Converts a winit window event into an iced event. /// Converts a winit window event into an iced event.
pub fn window_event( pub fn window_event(
@ -183,8 +182,10 @@ pub fn mouse_button(mouse_button: winit::event::MouseButton) -> mouse::Button {
/// ///
/// [`winit`]: https://github.com/rust-windowing/winit /// [`winit`]: https://github.com/rust-windowing/winit
/// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native /// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
pub fn modifiers(modifiers: winit::event::ModifiersState) -> Modifiers { pub fn modifiers(
Modifiers { modifiers: winit::event::ModifiersState,
) -> keyboard::Modifiers {
keyboard::Modifiers {
shift: modifiers.shift(), shift: modifiers.shift(),
control: modifiers.ctrl(), control: modifiers.ctrl(),
alt: modifiers.alt(), alt: modifiers.alt(),
@ -206,18 +207,23 @@ pub fn cursor_position(
/// ///
/// [`winit`]: https://github.com/rust-windowing/winit /// [`winit`]: https://github.com/rust-windowing/winit
/// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native /// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
pub fn touch_event(touch: winit::event::Touch) -> Touch { pub fn touch_event(touch: winit::event::Touch) -> touch::Event {
let phase = match touch.phase { let id = touch::Finger(touch.id);
winit::event::TouchPhase::Started => touch::Phase::Started, let position = Point::new(touch.location.x as f32, touch.location.y as f32);
winit::event::TouchPhase::Moved => touch::Phase::Moved,
winit::event::TouchPhase::Ended => touch::Phase::Ended,
winit::event::TouchPhase::Cancelled => touch::Phase::Canceled,
};
Touch { match touch.phase {
finger: touch::Finger(touch.id), winit::event::TouchPhase::Started => {
position: Point::new(touch.location.x as f32, touch.location.y as f32), touch::Event::FingerPressed { id, position }
phase, }
winit::event::TouchPhase::Moved => {
touch::Event::FingerMoved { id, position }
}
winit::event::TouchPhase::Ended => {
touch::Event::FingerLifted { id, position }
}
winit::event::TouchPhase::Cancelled => {
touch::Event::FingerLost { id, position }
}
} }
} }
@ -225,7 +231,11 @@ pub fn touch_event(touch: winit::event::Touch) -> Touch {
/// ///
/// [`winit`]: https://github.com/rust-windowing/winit /// [`winit`]: https://github.com/rust-windowing/winit
/// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native /// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
pub fn key_code(virtual_keycode: winit::event::VirtualKeyCode) -> KeyCode { pub fn key_code(
virtual_keycode: winit::event::VirtualKeyCode,
) -> keyboard::KeyCode {
use keyboard::KeyCode;
match virtual_keycode { match virtual_keycode {
winit::event::VirtualKeyCode::Key1 => KeyCode::Key1, winit::event::VirtualKeyCode::Key1 => KeyCode::Key1,
winit::event::VirtualKeyCode::Key2 => KeyCode::Key2, winit::event::VirtualKeyCode::Key2 => KeyCode::Key2,