From dc09da88789d5fa5392d61fb1b925748f46715f4 Mon Sep 17 00:00:00 2001 From: Olivier 'reivilibre Date: Sat, 26 Jun 2021 23:21:13 +0100 Subject: [PATCH] crude toggle touch impl --- native/src/widget/toggler.rs | 48 +++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/native/src/widget/toggler.rs b/native/src/widget/toggler.rs index e3fb27fd..022a2ca6 100644 --- a/native/src/widget/toggler.rs +++ b/native/src/widget/toggler.rs @@ -1,11 +1,13 @@ //! Show toggle controls using togglers. use std::hash::Hash; +use crate::touch::Finger; use crate::{ - event, layout, mouse, row, text, Align, Clipboard, Element, Event, Hasher, - HorizontalAlignment, Layout, Length, Point, Rectangle, Row, Text, + event, layout, mouse, row, text, touch, Align, Clipboard, Element, Event, + Hasher, HorizontalAlignment, Layout, Length, Point, Rectangle, Row, Text, VerticalAlignment, Widget, }; +use std::collections::HashSet; /// A toggler widget /// @@ -34,6 +36,8 @@ pub struct Toggler { spacing: u16, font: Renderer::Font, style: Renderer::Style, + + fingers_mid_press: HashSet, } impl @@ -66,6 +70,8 @@ impl spacing: 0, font: Renderer::Font::default(), style: Renderer::Style::default(), + + fingers_mid_press: HashSet::with_capacity(2), } } @@ -176,7 +182,43 @@ where } Event::Touch(x) => { eprintln!("{:?}", x); - event::Status::Ignored + + match x { + touch::Event::FingerPressed { id, position } => { + if layout.bounds().contains(position) { + let _ = self.fingers_mid_press.insert(id); + + event::Status::Captured + } else { + event::Status::Ignored + } + } + touch::Event::FingerMoved { id, position } => { + if !layout.bounds().contains(position) { + let _ = self.fingers_mid_press.remove(&id); + + event::Status::Captured + } else { + event::Status::Ignored + } + } + touch::Event::FingerLifted { id, position } => { + if self.fingers_mid_press.remove(&id) + && layout.bounds().contains(position) + { + // this should be treated as a click + messages.push((self.on_toggle)(!self.is_active)); + + event::Status::Captured + } else { + event::Status::Ignored + } + } + touch::Event::FingerLost { id, .. } => { + let _ = self.fingers_mid_press.remove(&id); + event::Status::Ignored + } + } } _ => event::Status::Ignored, }