zng_view_api/
touch.rs

1//! Touch types.
2
3use serde::{Deserialize, Serialize};
4
5use zng_unit::DipPoint;
6
7/// Identifier for a continuous touch contact.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
9#[serde(transparent)]
10pub struct TouchId(pub u64);
11
12/// Describes touch-screen input state.
13#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
14pub enum TouchPhase {
15    /// A finger touched the screen.
16    Start,
17    /// A finger moved on the screen.
18    Move,
19    /// A finger was lifted from the screen.
20    End,
21    /// The system cancelled tracking for the touch.
22    Cancel,
23}
24
25/// Identify a new touch contact or a contact update.
26#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
27#[non_exhaustive]
28pub struct TouchUpdate {
29    /// Identify a continuous touch contact or *finger*.
30    ///
31    /// Multiple points of contact can happen in the same device at the same time,
32    /// this ID identifies each uninterrupted contact. IDs are unique only among other concurrent touches
33    /// on the same device, after a touch is ended an ID may be reused.
34    pub touch: TouchId,
35    /// Touch phase for the `id`.
36    pub phase: TouchPhase,
37    /// Touch center, relative to the window top-left in device independent pixels.
38    pub position: DipPoint,
39    /// Touch pressure force and angle.
40    pub force: Option<TouchForce>,
41}
42impl TouchUpdate {
43    /// New update.
44    pub fn new(touch: TouchId, phase: TouchPhase, position: DipPoint, force: Option<TouchForce>) -> Self {
45        Self {
46            touch,
47            phase,
48            position,
49            force,
50        }
51    }
52}
53
54/// Describes the force of a touch event.
55#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
56#[non_exhaustive]
57pub enum TouchForce {
58    /// On iOS, the force is calibrated so that the same number corresponds to
59    /// roughly the same amount of pressure on the screen regardless of the
60    /// device.
61    Calibrated {
62        /// The force of the touch, where a value of 1.0 represents the force of
63        /// an average touch (predetermined by the system, not user-specific).
64        ///
65        /// The force reported by Apple Pencil is measured along the axis of the
66        /// pencil. If you want a force perpendicular to the device, you need to
67        /// calculate this value using the `altitude_angle` value.
68        force: f64,
69        /// The maximum possible force for a touch.
70        ///
71        /// The value of this field is sufficiently high to provide a wide
72        /// dynamic range for values of the `force` field.
73        max_possible_force: f64,
74        /// The altitude (in radians) of the stylus.
75        ///
76        /// A value of 0 radians indicates that the stylus is parallel to the
77        /// surface. The value of this property is Pi/2 when the stylus is
78        /// perpendicular to the surface.
79        altitude_angle: Option<f64>,
80    },
81    /// If the platform reports the force as normalized, we have no way of
82    /// knowing how much pressure 1.0 corresponds to – we know it's the maximum
83    /// amount of force, but as to how much force, you might either have to
84    /// press really hard, or not hard at all, depending on the device.
85    Normalized(f64),
86}