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)]
27pub struct TouchUpdate {
28 /// Identify a continuous touch contact or *finger*.
29 ///
30 /// Multiple points of contact can happen in the same device at the same time,
31 /// this ID identifies each uninterrupted contact. IDs are unique only among other concurrent touches
32 /// on the same device, after a touch is ended an ID may be reused.
33 pub touch: TouchId,
34 /// Touch phase for the `id`.
35 pub phase: TouchPhase,
36 /// Touch center, relative to the window top-left in device independent pixels.
37 pub position: DipPoint,
38 /// Touch pressure force and angle.
39 pub force: Option<TouchForce>,
40}
41
42/// Describes the force of a touch event.
43#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
44pub enum TouchForce {
45 /// On iOS, the force is calibrated so that the same number corresponds to
46 /// roughly the same amount of pressure on the screen regardless of the
47 /// device.
48 Calibrated {
49 /// The force of the touch, where a value of 1.0 represents the force of
50 /// an average touch (predetermined by the system, not user-specific).
51 ///
52 /// The force reported by Apple Pencil is measured along the axis of the
53 /// pencil. If you want a force perpendicular to the device, you need to
54 /// calculate this value using the `altitude_angle` value.
55 force: f64,
56 /// The maximum possible force for a touch.
57 ///
58 /// The value of this field is sufficiently high to provide a wide
59 /// dynamic range for values of the `force` field.
60 max_possible_force: f64,
61 /// The altitude (in radians) of the stylus.
62 ///
63 /// A value of 0 radians indicates that the stylus is parallel to the
64 /// surface. The value of this property is Pi/2 when the stylus is
65 /// perpendicular to the surface.
66 altitude_angle: Option<f64>,
67 },
68 /// If the platform reports the force as normalized, we have no way of
69 /// knowing how much pressure 1.0 corresponds to – we know it's the maximum
70 /// amount of force, but as to how much force, you might either have to
71 /// press really hard, or not hard at all, depending on the device.
72 Normalized(f64),
73}