diff options
author | 2020-12-15 06:38:46 +0100 | |
---|---|---|
committer | 2020-12-15 06:38:46 +0100 | |
commit | 3bdf931925067acbaabf040f6c437a54640ed1a0 (patch) | |
tree | 451ff441ff5272d2612af57b625ce12046574884 /native | |
parent | 09110a93b06ae33af6870b4aded8637748cecace (diff) | |
download | iced-3bdf931925067acbaabf040f6c437a54640ed1a0.tar.gz iced-3bdf931925067acbaabf040f6c437a54640ed1a0.tar.bz2 iced-3bdf931925067acbaabf040f6c437a54640ed1a0.zip |
Turn `Touch` into a `touch::Event` enum
Diffstat (limited to 'native')
-rw-r--r-- | native/src/event.rs | 2 | ||||
-rw-r--r-- | native/src/touch.rs | 32 | ||||
-rw-r--r-- | native/src/widget/button.rs | 17 | ||||
-rw-r--r-- | native/src/widget/checkbox.rs | 7 | ||||
-rw-r--r-- | native/src/widget/radio.rs | 7 | ||||
-rw-r--r-- | native/src/widget/scrollable.rs | 65 | ||||
-rw-r--r-- | native/src/widget/slider.rs | 18 | ||||
-rw-r--r-- | native/src/widget/text_input.rs | 7 |
8 files changed, 58 insertions, 97 deletions
diff --git a/native/src/event.rs b/native/src/event.rs index f3c260c0..205bb797 100644 --- a/native/src/event.rs +++ b/native/src/event.rs @@ -22,7 +22,7 @@ pub enum Event { Window(window::Event), /// A touch event - Touch(touch::Touch), + Touch(touch::Event), } /// The status of an [`Event`] after being processed. diff --git a/native/src/touch.rs b/native/src/touch.rs index 88bd83bb..18120644 100644 --- a/native/src/touch.rs +++ b/native/src/touch.rs @@ -3,33 +3,21 @@ use crate::Point; /// A touch interaction. #[derive(Debug, Clone, Copy, PartialEq)] -pub struct Touch { - /// The finger of the touch. - pub finger: Finger, - - /// The position of the touch. - pub position: Point, - - /// The state of the touch. - pub phase: Phase, -} - -/// A unique identifier representing a finger on a touch interaction. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct Finger(pub u64); - -/// The state of a touch interaction. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Phase { +#[allow(missing_docs)] +pub enum Event { /// A touch interaction was started. - Started, + FingerPressed { id: Finger, position: Point }, /// An on-going touch interaction was moved. - Moved, + FingerMoved { id: Finger, position: Point }, /// A touch interaction was ended. - Ended, + FingerLifted { id: Finger, position: Point }, /// A touch interaction was canceled. - Canceled, + FingerLost { id: Finger, position: Point }, } + +/// A unique identifier representing a finger on a touch interaction. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct Finger(pub u64); diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index 7d5eb30c..8e2450de 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -4,7 +4,7 @@ use crate::event::{self, Event}; use crate::layout; use crate::mouse; -use crate::touch::{self, Touch}; +use crate::touch; use crate::{ Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Widget, }; @@ -166,10 +166,7 @@ where ) -> event::Status { match event { Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) - | Event::Touch(Touch { - phase: touch::Phase::Started, - .. - }) => { + | Event::Touch(touch::Event::FingerPressed { .. }) => { if self.on_press.is_some() { let bounds = layout.bounds(); @@ -181,10 +178,7 @@ where } } Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) - | Event::Touch(Touch { - phase: touch::Phase::Ended, - .. - }) => { + | Event::Touch(touch::Event::FingerLifted { .. }) => { if let Some(on_press) = self.on_press.clone() { let bounds = layout.bounds(); @@ -199,10 +193,7 @@ where } } } - Event::Touch(Touch { - phase: touch::Phase::Canceled, - .. - }) => { + Event::Touch(touch::Event::FingerLost { .. }) => { self.state.is_pressed = false; } _ => {} diff --git a/native/src/widget/checkbox.rs b/native/src/widget/checkbox.rs index 92175b25..77a82fad 100644 --- a/native/src/widget/checkbox.rs +++ b/native/src/widget/checkbox.rs @@ -6,7 +6,7 @@ use crate::layout; use crate::mouse; use crate::row; use crate::text; -use crate::touch::{self, Touch}; +use crate::touch; use crate::{ Align, Clipboard, Element, Hasher, HorizontalAlignment, Layout, Length, Point, Rectangle, Row, Text, VerticalAlignment, Widget, @@ -156,10 +156,7 @@ where ) -> event::Status { match event { Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) - | Event::Touch(Touch { - phase: touch::Phase::Started, - .. - }) => { + | Event::Touch(touch::Event::FingerPressed { .. }) => { let mouse_over = layout.bounds().contains(cursor_position); if mouse_over { diff --git a/native/src/widget/radio.rs b/native/src/widget/radio.rs index 3a1dd386..69952345 100644 --- a/native/src/widget/radio.rs +++ b/native/src/widget/radio.rs @@ -4,7 +4,7 @@ use crate::layout; use crate::mouse; use crate::row; use crate::text; -use crate::touch::{self, Touch}; +use crate::touch; use crate::{ Align, Clipboard, Element, Hasher, HorizontalAlignment, Layout, Length, Point, Rectangle, Row, Text, VerticalAlignment, Widget, @@ -162,10 +162,7 @@ where ) -> event::Status { match event { Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) - | Event::Touch(Touch { - phase: touch::Phase::Started, - .. - }) => { + | Event::Touch(touch::Event::FingerPressed { .. }) => { if layout.bounds().contains(cursor_position) { messages.push(self.on_click.clone()); diff --git a/native/src/widget/scrollable.rs b/native/src/widget/scrollable.rs index 8c321ee5..18cdf169 100644 --- a/native/src/widget/scrollable.rs +++ b/native/src/widget/scrollable.rs @@ -4,7 +4,7 @@ use crate::event::{self, Event}; use crate::layout; use crate::mouse; use crate::overlay; -use crate::touch::{self, Touch}; +use crate::touch; use crate::{ Align, Clipboard, Column, Element, Hasher, Layout, Length, Point, Rectangle, Size, Vector, Widget, @@ -230,26 +230,37 @@ where return event::Status::Captured; } - Event::Touch(Touch { phase, .. }) => match phase { - touch::Phase::Started => { - self.state.scroll_box_touched_at = - 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); + Event::Touch(event) => { + match event { + touch::Event::FingerPressed { .. } => { self.state.scroll_box_touched_at = 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( mouse::Button::Left, )) - | Event::Touch(Touch { - phase: touch::Phase::Ended, - .. - }) - | Event::Touch(Touch { - phase: touch::Phase::Canceled, - .. - }) => { + | Event::Touch(touch::Event::FingerLifted { .. }) + | Event::Touch(touch::Event::FingerLost { .. }) => { self.state.scroller_grabbed_at = None; return event::Status::Captured; } Event::Mouse(mouse::Event::CursorMoved { .. }) - | Event::Touch(Touch { - phase: touch::Phase::Moved, - .. - }) => { + | Event::Touch(touch::Event::FingerMoved { .. }) => { if let (Some(scrollbar), Some(scroller_grabbed_at)) = (scrollbar, self.state.scroller_grabbed_at) { @@ -298,10 +300,7 @@ where Event::Mouse(mouse::Event::ButtonPressed( mouse::Button::Left, )) - | Event::Touch(Touch { - phase: touch::Phase::Started, - .. - }) => { + | Event::Touch(touch::Event::FingerPressed { .. }) => { if let Some(scrollbar) = scrollbar { if let Some(scroller_grabbed_at) = scrollbar.grab_scroller(cursor_position) diff --git a/native/src/widget/slider.rs b/native/src/widget/slider.rs index 755e6b2b..010c6e53 100644 --- a/native/src/widget/slider.rs +++ b/native/src/widget/slider.rs @@ -4,7 +4,7 @@ use crate::event::{self, Event}; use crate::layout; use crate::mouse; -use crate::touch::{self, Touch}; +use crate::touch; use crate::{ Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget, }; @@ -209,10 +209,7 @@ where match event { Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) - | Event::Touch(Touch { - phase: touch::Phase::Started, - .. - }) => { + | Event::Touch(touch::Event::FingerPressed { .. }) => { if layout.bounds().contains(cursor_position) { change(); self.state.is_dragging = true; @@ -221,10 +218,8 @@ where } } Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) - | Event::Touch(Touch { - phase: touch::Phase::Ended, - .. - }) => { + | Event::Touch(touch::Event::FingerLifted { .. }) + | Event::Touch(touch::Event::FingerLost { .. }) => { if self.state.is_dragging { if let Some(on_release) = self.on_release.clone() { messages.push(on_release); @@ -235,10 +230,7 @@ where } } Event::Mouse(mouse::Event::CursorMoved { .. }) - | Event::Touch(Touch { - phase: touch::Phase::Moved, - .. - }) => { + | Event::Touch(touch::Event::FingerMoved { .. }) => { if self.state.is_dragging { change(); diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index ca71c20c..1e84e22a 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -16,7 +16,7 @@ use crate::keyboard; use crate::layout; use crate::mouse::{self, click}; use crate::text; -use crate::touch::{self, Touch}; +use crate::touch; use crate::{ Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget, }; @@ -249,10 +249,7 @@ where ) -> event::Status { match event { Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) - | Event::Touch(Touch { - phase: touch::Phase::Started, - .. - }) => { + | Event::Touch(touch::Event::FingerPressed { .. }) => { let is_clicked = layout.bounds().contains(cursor_position); self.state.is_focused = is_clicked; |