use std::collections::HashMap;
use embedder_traits::{TouchId, TouchSequenceId};
use euclid::{Point2D, Scale, Vector2D};
use log::{debug, error, warn};
use webrender_api::units::{DeviceIntPoint, DevicePixel, DevicePoint, LayoutVector2D};
use self::TouchSequenceState::*;
const TOUCH_PAN_MIN_SCREEN_PX: f32 = 20.0;
const FLING_SCALING_FACTOR: f32 = 0.95;
const FLING_MIN_SCREEN_PX: f32 = 3.0;
const FLING_MAX_SCREEN_PX: f32 = 4000.0;
pub struct TouchHandler {
pub current_sequence_id: TouchSequenceId,
touch_sequence_map: HashMap<TouchSequenceId, TouchSequenceInfo>,
}
#[derive(Debug, Eq, PartialEq)]
pub enum TouchMoveAllowed {
Prevented,
Allowed,
Pending,
}
pub struct TouchSequenceInfo {
pub(crate) state: TouchSequenceState,
active_touch_points: Vec<TouchPoint>,
handling_touch_move: bool,
pub prevent_click: bool,
pub prevent_move: TouchMoveAllowed,
pending_touch_move_action: Option<TouchMoveAction>,
}
impl TouchSequenceInfo {
fn touch_count(&self) -> usize {
self.active_touch_points.len()
}
fn pinch_distance_and_center(&self) -> (f32, Point2D<f32, DevicePixel>) {
debug_assert_eq!(self.touch_count(), 2);
let p0 = self.active_touch_points[0].point;
let p1 = self.active_touch_points[1].point;
let center = p0.lerp(p1, 0.5);
let distance = (p0 - p1).length();
(distance, center)
}
fn update_pending_touch_move_action(&mut self, action: TouchMoveAction) {
debug_assert!(self.prevent_move == TouchMoveAllowed::Pending);
if let Some(pre_action) = self.pending_touch_move_action {
let combine_action = match (pre_action, action) {
(TouchMoveAction::NoAction, _) | (_, TouchMoveAction::NoAction) => action,
(TouchMoveAction::Scroll(delta, point), TouchMoveAction::Scroll(delta_new, _)) => {
TouchMoveAction::Scroll(delta + delta_new, point)
},
(
TouchMoveAction::Scroll(delta, _),
TouchMoveAction::Zoom(magnification, scroll_delta),
) |
(
TouchMoveAction::Zoom(magnification, scroll_delta),
TouchMoveAction::Scroll(delta, _),
) => {
TouchMoveAction::Zoom(magnification, delta + scroll_delta)
},
(
TouchMoveAction::Zoom(magnification, scroll_delta),
TouchMoveAction::Zoom(magnification_new, scroll_delta_new),
) => TouchMoveAction::Zoom(
magnification * magnification_new,
scroll_delta + scroll_delta_new,
),
};
self.pending_touch_move_action = Some(combine_action);
} else {
self.pending_touch_move_action = Some(action);
}
}
fn is_finished(&self) -> bool {
matches!(
self.state,
Finished | Flinging { .. } | PendingFling { .. } | PendingClick(_)
)
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum TouchMoveAction {
Scroll(Vector2D<f32, DevicePixel>, DevicePoint),
Zoom(f32, Vector2D<f32, DevicePixel>),
NoAction,
}
#[derive(Clone, Copy, Debug)]
pub struct TouchPoint {
pub id: TouchId,
pub point: Point2D<f32, DevicePixel>,
}
impl TouchPoint {
pub fn new(id: TouchId, point: Point2D<f32, DevicePixel>) -> Self {
TouchPoint { id, point }
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum TouchSequenceState {
Touching,
Panning {
velocity: Vector2D<f32, DevicePixel>,
},
Pinching,
MultiTouch,
PendingFling {
velocity: Vector2D<f32, DevicePixel>,
cursor: DeviceIntPoint,
},
Flinging {
velocity: Vector2D<f32, DevicePixel>,
cursor: DeviceIntPoint,
},
PendingClick(DevicePoint),
Finished,
}
pub(crate) struct FlingAction {
pub delta: LayoutVector2D,
pub cursor: DeviceIntPoint,
}
impl TouchHandler {
pub fn new() -> Self {
let finished_info = TouchSequenceInfo {
state: TouchSequenceState::Finished,
active_touch_points: vec![],
handling_touch_move: false,
prevent_click: false,
prevent_move: TouchMoveAllowed::Pending,
pending_touch_move_action: None,
};
TouchHandler {
current_sequence_id: TouchSequenceId::new(),
touch_sequence_map: HashMap::from([(TouchSequenceId::new(), finished_info)]),
}
}
pub(crate) fn set_handling_touch_move(&mut self, sequence_id: TouchSequenceId, flag: bool) {
if let Some(sequence) = self.touch_sequence_map.get_mut(&sequence_id) {
sequence.handling_touch_move = flag;
}
}
pub(crate) fn is_handling_touch_move(&self, sequence_id: TouchSequenceId) -> bool {
if let Some(sequence) = self.touch_sequence_map.get(&sequence_id) {
sequence.handling_touch_move
} else {
false
}
}
pub(crate) fn prevent_click(&mut self, sequence_id: TouchSequenceId) {
if let Some(sequence) = self.touch_sequence_map.get_mut(&sequence_id) {
sequence.prevent_click = true;
} else {
warn!("TouchSequenceInfo corresponding to the sequence number has been deleted.");
}
}
pub(crate) fn prevent_move(&mut self, sequence_id: TouchSequenceId) {
if let Some(sequence) = self.touch_sequence_map.get_mut(&sequence_id) {
sequence.prevent_move = TouchMoveAllowed::Prevented;
} else {
warn!("TouchSequenceInfo corresponding to the sequence number has been deleted.");
}
}
pub(crate) fn move_allowed(&mut self, sequence_id: TouchSequenceId) -> bool {
if let Some(sequence) = self.touch_sequence_map.get_mut(&sequence_id) {
sequence.prevent_move == TouchMoveAllowed::Allowed
} else {
true
}
}
pub(crate) fn pending_touch_move_action(
&mut self,
sequence_id: TouchSequenceId,
) -> Option<TouchMoveAction> {
match self.touch_sequence_map.get(&sequence_id) {
Some(sequence) => sequence.pending_touch_move_action,
None => None,
}
}
pub(crate) fn remove_pending_touch_move_action(&mut self, sequence_id: TouchSequenceId) {
if let Some(sequence) = self.touch_sequence_map.get_mut(&sequence_id) {
sequence.pending_touch_move_action = None;
}
}
pub(crate) fn try_remove_touch_sequence(&mut self, sequence_id: TouchSequenceId) {
if let Some(sequence) = self.touch_sequence_map.get(&sequence_id) {
if sequence.pending_touch_move_action.is_none() && sequence.state == Finished {
self.touch_sequence_map.remove(&sequence_id);
}
}
}
pub(crate) fn remove_touch_sequence(&mut self, sequence_id: TouchSequenceId) {
let old = self.touch_sequence_map.remove(&sequence_id);
debug_assert!(old.is_some(), "Sequence already removed?");
}
pub fn try_get_current_touch_sequence(&self) -> Option<&TouchSequenceInfo> {
self.touch_sequence_map.get(&self.current_sequence_id)
}
pub fn get_current_touch_sequence_mut(&mut self) -> &mut TouchSequenceInfo {
self.touch_sequence_map
.get_mut(&self.current_sequence_id)
.expect("Current Touch sequence does not exist")
}
pub(crate) fn get_touch_sequence(&self, sequence_id: TouchSequenceId) -> &TouchSequenceInfo {
self.touch_sequence_map
.get(&sequence_id)
.expect("Touch sequence not found.")
}
pub(crate) fn get_touch_sequence_mut(
&mut self,
sequence_id: TouchSequenceId,
) -> Option<&mut TouchSequenceInfo> {
self.touch_sequence_map.get_mut(&sequence_id)
}
pub fn on_touch_down(&mut self, id: TouchId, point: Point2D<f32, DevicePixel>) {
if !self
.touch_sequence_map
.contains_key(&self.current_sequence_id) ||
self.get_touch_sequence(self.current_sequence_id)
.is_finished()
{
self.current_sequence_id.next();
debug!("Entered new touch sequence: {:?}", self.current_sequence_id);
let active_touch_points = vec![TouchPoint::new(id, point)];
self.touch_sequence_map.insert(
self.current_sequence_id,
TouchSequenceInfo {
state: Touching,
active_touch_points,
handling_touch_move: false,
prevent_click: false,
prevent_move: TouchMoveAllowed::Pending,
pending_touch_move_action: None,
},
);
} else {
debug!("Touch down in sequence {:?}.", self.current_sequence_id);
let touch_sequence = self.get_current_touch_sequence_mut();
touch_sequence
.active_touch_points
.push(TouchPoint::new(id, point));
match touch_sequence.active_touch_points.len() {
2.. => {
touch_sequence.state = MultiTouch;
},
0..2 => {
unreachable!("Secondary touch_down event with less than 2 fingers active?");
},
}
touch_sequence.prevent_click = true;
}
}
pub fn on_vsync(&mut self) -> Option<FlingAction> {
let touch_sequence = self.touch_sequence_map.get_mut(&self.current_sequence_id)?;
let Flinging { velocity, cursor } = &mut touch_sequence.state else {
return None;
};
if velocity.length().abs() < FLING_MIN_SCREEN_PX {
touch_sequence.state = Finished;
self.try_remove_touch_sequence(self.current_sequence_id);
None
} else {
*velocity *= FLING_SCALING_FACTOR;
debug_assert!(velocity.length() <= FLING_MAX_SCREEN_PX);
Some(FlingAction {
delta: LayoutVector2D::new(velocity.x, velocity.y),
cursor: *cursor,
})
}
}
pub fn on_touch_move(
&mut self,
id: TouchId,
point: Point2D<f32, DevicePixel>,
) -> TouchMoveAction {
let touch_sequence = self.get_current_touch_sequence_mut();
let idx = match touch_sequence
.active_touch_points
.iter_mut()
.position(|t| t.id == id)
{
Some(i) => i,
None => {
error!("Got a touchmove event for a non-active touch point");
return TouchMoveAction::NoAction;
},
};
let old_point = touch_sequence.active_touch_points[idx].point;
let delta = point - old_point;
let action = match touch_sequence.touch_count() {
1 => {
if let Panning { ref mut velocity } = touch_sequence.state {
*velocity += delta;
*velocity /= 2.0;
touch_sequence.active_touch_points[idx].point = point;
TouchMoveAction::Scroll(delta, point)
} else if delta.x.abs() > TOUCH_PAN_MIN_SCREEN_PX ||
delta.y.abs() > TOUCH_PAN_MIN_SCREEN_PX
{
touch_sequence.state = Panning {
velocity: Vector2D::new(delta.x, delta.y),
};
touch_sequence.prevent_click = true;
touch_sequence.active_touch_points[idx].point = point;
TouchMoveAction::Scroll(delta, point)
} else {
TouchMoveAction::NoAction
}
},
2 => {
if touch_sequence.state == Pinching ||
delta.x.abs() > TOUCH_PAN_MIN_SCREEN_PX ||
delta.y.abs() > TOUCH_PAN_MIN_SCREEN_PX
{
touch_sequence.state = Pinching;
let (d0, c0) = touch_sequence.pinch_distance_and_center();
touch_sequence.active_touch_points[idx].point = point;
let (d1, c1) = touch_sequence.pinch_distance_and_center();
let magnification = d1 / d0;
let scroll_delta = c1 - c0 * Scale::new(magnification);
TouchMoveAction::Zoom(magnification, scroll_delta)
} else {
TouchMoveAction::NoAction
}
},
_ => {
touch_sequence.active_touch_points[idx].point = point;
touch_sequence.state = MultiTouch;
TouchMoveAction::NoAction
},
};
if TouchMoveAction::NoAction != action &&
touch_sequence.prevent_move == TouchMoveAllowed::Pending
{
touch_sequence.update_pending_touch_move_action(action);
}
action
}
pub fn on_touch_up(&mut self, id: TouchId, point: Point2D<f32, DevicePixel>) {
let touch_sequence = self.get_current_touch_sequence_mut();
let old = match touch_sequence
.active_touch_points
.iter()
.position(|t| t.id == id)
{
Some(i) => Some(touch_sequence.active_touch_points.swap_remove(i).point),
None => {
warn!("Got a touch up event for a non-active touch point");
None
},
};
match touch_sequence.state {
Touching => {
if touch_sequence.prevent_click {
touch_sequence.state = Finished;
} else {
touch_sequence.state = PendingClick(point);
}
},
Panning { velocity } => {
if velocity.length().abs() >= FLING_MIN_SCREEN_PX {
debug!(
"Transitioning to Fling. Cursor is {point:?}. Old cursor was {old:?}. \
Raw velocity is {velocity:?}."
);
debug_assert!((point.x as i64) < (i32::MAX as i64));
debug_assert!((point.y as i64) < (i32::MAX as i64));
let cursor = DeviceIntPoint::new(point.x as i32, point.y as i32);
let velocity = (velocity * 2.0).with_max_length(FLING_MAX_SCREEN_PX);
match touch_sequence.prevent_move {
TouchMoveAllowed::Allowed => {
touch_sequence.state = Flinging { velocity, cursor }
},
TouchMoveAllowed::Pending => {
touch_sequence.state = PendingFling { velocity, cursor }
},
TouchMoveAllowed::Prevented => touch_sequence.state = Finished,
}
} else {
touch_sequence.state = Finished;
}
},
Pinching => {
touch_sequence.state = Touching;
},
MultiTouch => {
if touch_sequence.active_touch_points.is_empty() {
touch_sequence.state = Finished;
}
},
PendingFling { .. } | Flinging { .. } | PendingClick(_) | Finished => {
error!("Touch-up received, but touch handler already in post-touchup state.")
},
}
#[cfg(debug_assertions)]
if touch_sequence.active_touch_points.is_empty() {
debug_assert!(
touch_sequence.is_finished(),
"Did not transition to a finished state: {:?}",
touch_sequence.state
);
}
debug!(
"Touch up with remaining active touchpoints: {:?}, in sequence {:?}",
touch_sequence.active_touch_points.len(),
self.current_sequence_id
);
}
pub fn on_touch_cancel(&mut self, id: TouchId, _point: Point2D<f32, DevicePixel>) {
let touch_sequence = self.get_current_touch_sequence_mut();
match touch_sequence
.active_touch_points
.iter()
.position(|t| t.id == id)
{
Some(i) => {
touch_sequence.active_touch_points.swap_remove(i);
},
None => {
warn!("Got a touchcancel event for a non-active touch point");
return;
},
}
if touch_sequence.active_touch_points.is_empty() {
touch_sequence.state = Finished;
}
}
}