input_pipeline/gestures/
utils.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use super::gesture_arena::TouchpadEvent;
use crate::utils::{euclidean_distance, Position};
use std::collections::HashMap;

/// Result of movement_from_events.
#[derive(Debug, PartialEq)]
pub(super) struct MovementDetail {
    pub(super) euclidean_distance: f32,
    pub(super) movement: Position,
}

/// `movement_from_events` is used to compute the movement in click-drag events.
/// It take the finger that moves the furthest as events' movement.
pub(super) fn movement_from_events(
    previous_event: &TouchpadEvent,
    new_event: &TouchpadEvent,
) -> MovementDetail {
    let mut previous_contacts: HashMap<u32, Position> = HashMap::new();
    for c in &previous_event.contacts {
        previous_contacts.insert(c.id, c.position.clone());
    }

    let mut movement = Position { x: 0.0, y: 0.0 };
    let mut max_distance = 0.0;
    for new_contact in &new_event.contacts {
        let previous = previous_contacts.get(&new_contact.id);
        match previous {
            None => {}
            Some(&previous) => {
                let dis = euclidean_distance(new_contact.position, previous.clone());
                if dis > max_distance {
                    max_distance = dis;
                    movement = new_contact.position - previous;
                }
            }
        }
    }

    MovementDetail { euclidean_distance: max_distance, movement }
}

#[cfg(test)]
mod tests {

    use super::*;
    use crate::gestures::gesture_arena::TouchpadEvent;
    use crate::touch_binding;
    use crate::utils::Position;

    use test_case::test_case;

    fn make_touch_contact(id: u32, position: Position) -> touch_binding::TouchContact {
        touch_binding::TouchContact { id, position, pressure: None, contact_size: None }
    }

    #[test_case(
        TouchpadEvent {
            timestamp: zx::Instant::ZERO,
            pressed_buttons: vec![],
            contacts: vec![
                make_touch_contact(1, Position{x: 1.0, y: 1.0}),
                make_touch_contact(2, Position{x: 5.0, y: 5.0}),
            ],
            filtered_palm_contacts: vec![],
        }; "previous contact stay, place new finger")]
    #[test_case(
        TouchpadEvent {
            timestamp: zx::Instant::ZERO,
            pressed_buttons: vec![],
            contacts: vec![
                make_touch_contact(2, Position{x: 5.0, y: 5.0}),
            ],
            filtered_palm_contacts: vec![],
        }; "previous contact lift, place new finger")]
    #[test_case(
        TouchpadEvent {
            timestamp: zx::Instant::ZERO,
            pressed_buttons: vec![],
            contacts: vec![],
            filtered_palm_contacts: vec![],
        }; "previous contact lift")]
    #[fuchsia::test]
    fn movement_from_events_no_movement(new_event: TouchpadEvent) {
        let previous_event = TouchpadEvent {
            timestamp: zx::Instant::ZERO,
            pressed_buttons: vec![],
            contacts: vec![make_touch_contact(1, Position { x: 1.0, y: 1.0 })],
            filtered_palm_contacts: vec![],
        };

        let got = movement_from_events(&previous_event, &new_event);
        let want = MovementDetail { euclidean_distance: 0.0, movement: Position::zero() };
        pretty_assertions::assert_eq!(got, want);
    }

    #[test_case(
        TouchpadEvent {
            timestamp: zx::Instant::ZERO,
            pressed_buttons: vec![],
            contacts: vec![
                make_touch_contact(1, Position{x: 1.0, y: 6.0}),
                make_touch_contact(2, Position{x: 5.0, y: 5.0}),
            ],
            filtered_palm_contacts: vec![],
        }; "contact 1 move, contact 2 stay")]
    #[test_case(
        TouchpadEvent {
            timestamp: zx::Instant::ZERO,
            pressed_buttons: vec![],
            contacts: vec![
                make_touch_contact(1, Position{x: 1.0, y: 1.0}),
                make_touch_contact(2, Position{x: 5.0, y: 10.0}),
            ],
            filtered_palm_contacts: vec![],
        }; "contact 2 move, contact 1 stay")]
    #[test_case(
        TouchpadEvent {
            timestamp: zx::Instant::ZERO,
            pressed_buttons: vec![],
            contacts: vec![
                make_touch_contact(1, Position{x: 1.0, y: 6.0}),
                make_touch_contact(2, Position{x: 3.0, y: 5.0}),
            ],
            filtered_palm_contacts: vec![],
        }; "contact 1 movement larger")]
    #[test_case(
        TouchpadEvent {
            timestamp: zx::Instant::ZERO,
            pressed_buttons: vec![],
            contacts: vec![
                make_touch_contact(1, Position{x: 1.0, y: 6.0}),
            ],
            filtered_palm_contacts: vec![],
        }; "1 contact lift")]
    #[fuchsia::test]
    fn movement_from_events_furthest_movement(new_event: TouchpadEvent) {
        let previous_event = TouchpadEvent {
            timestamp: zx::Instant::ZERO,
            pressed_buttons: vec![],
            contacts: vec![
                make_touch_contact(1, Position { x: 1.0, y: 1.0 }),
                make_touch_contact(2, Position { x: 5.0, y: 5.0 }),
            ],
            filtered_palm_contacts: vec![],
        };

        let got = movement_from_events(&previous_event, &new_event);
        let want =
            MovementDetail { euclidean_distance: 5.0, movement: Position { x: 0.0, y: 5.0 } };
        pretty_assertions::assert_eq!(got, want);
    }
}