Add custom touch handling code for buttons
This commit is contained in:
parent
dc09da8878
commit
cdb828c4e5
@ -6,10 +6,12 @@ use crate::layout;
|
|||||||
use crate::mouse;
|
use crate::mouse;
|
||||||
use crate::overlay;
|
use crate::overlay;
|
||||||
use crate::touch;
|
use crate::touch;
|
||||||
|
use crate::touch::Finger;
|
||||||
use crate::{
|
use crate::{
|
||||||
Clipboard, Element, Hasher, Layout, Length, Padding, Point, Rectangle,
|
Clipboard, Element, Hasher, Layout, Length, Padding, Point, Rectangle,
|
||||||
Widget,
|
Widget,
|
||||||
};
|
};
|
||||||
|
use std::collections::HashSet;
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
|
|
||||||
/// A generic widget that produces a message when pressed.
|
/// A generic widget that produces a message when pressed.
|
||||||
@ -63,6 +65,7 @@ pub struct Button<'a, Message, Renderer: self::Renderer> {
|
|||||||
min_height: u32,
|
min_height: u32,
|
||||||
padding: Padding,
|
padding: Padding,
|
||||||
style: Renderer::Style,
|
style: Renderer::Style,
|
||||||
|
fingers_mid_press: HashSet<Finger>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, Message, Renderer> Button<'a, Message, Renderer>
|
impl<'a, Message, Renderer> Button<'a, Message, Renderer>
|
||||||
@ -86,6 +89,8 @@ where
|
|||||||
min_height: 0,
|
min_height: 0,
|
||||||
padding: Renderer::DEFAULT_PADDING,
|
padding: Renderer::DEFAULT_PADDING,
|
||||||
style: Renderer::Style::default(),
|
style: Renderer::Style::default(),
|
||||||
|
|
||||||
|
fingers_mid_press: HashSet::with_capacity(2),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -204,8 +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::FingerPressed { .. }) => {
|
|
||||||
if self.on_press.is_some() {
|
if self.on_press.is_some() {
|
||||||
let bounds = layout.bounds();
|
let bounds = layout.bounds();
|
||||||
|
|
||||||
@ -216,8 +220,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
|
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => {
|
||||||
| Event::Touch(touch::Event::FingerLifted { .. }) => {
|
|
||||||
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();
|
||||||
|
|
||||||
@ -232,8 +235,38 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::Touch(touch::Event::FingerLost { .. }) => {
|
Event::Touch(x) => {
|
||||||
self.state.is_pressed = false;
|
match x {
|
||||||
|
touch::Event::FingerPressed { id, position } => {
|
||||||
|
if layout.bounds().contains(position) {
|
||||||
|
let _ = self.fingers_mid_press.insert(id);
|
||||||
|
|
||||||
|
return event::Status::Captured;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
touch::Event::FingerMoved { id, position } => {
|
||||||
|
if !layout.bounds().contains(position) {
|
||||||
|
let _ = self.fingers_mid_press.remove(&id);
|
||||||
|
|
||||||
|
return event::Status::Captured;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
touch::Event::FingerLifted { id, position } => {
|
||||||
|
if self.fingers_mid_press.remove(&id)
|
||||||
|
&& layout.bounds().contains(position)
|
||||||
|
{
|
||||||
|
// this should be treated as a click
|
||||||
|
if let Some(on_press) = self.on_press.clone() {
|
||||||
|
messages.push(on_press);
|
||||||
|
}
|
||||||
|
|
||||||
|
return event::Status::Captured;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
touch::Event::FingerLost { id, .. } => {
|
||||||
|
let _ = self.fingers_mid_press.remove(&id);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
@ -181,8 +181,6 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::Touch(x) => {
|
Event::Touch(x) => {
|
||||||
eprintln!("{:?}", x);
|
|
||||||
|
|
||||||
match x {
|
match x {
|
||||||
touch::Event::FingerPressed { id, position } => {
|
touch::Event::FingerPressed { id, position } => {
|
||||||
if layout.bounds().contains(position) {
|
if layout.bounds().contains(position) {
|
||||||
|
Loading…
Reference in New Issue
Block a user