stdweb/webapi/touch.rs
1use webapi::event_target::EventTarget;
2use webcore::try_from::{
3 TryFrom,
4 TryInto,
5};
6use webcore::value::{
7 ConversionError,
8 Reference,
9 Value,
10};
11
12/// The Touch interface represents a single contact point on a touch-sensitive
13/// device. The contact point is commonly a finger or stylus and the device may
14/// be a touchscreen or trackpad.
15///
16/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Touch)
17// https://w3c.github.io/touch-events/#touch-interface
18#[derive(Clone, Debug, Eq, PartialEq, ReferenceType)]
19#[reference(instance_of = "Touch")]
20pub struct Touch( Reference );
21
22impl Touch {
23
24 /// Returns a unique identifier for this Touch object. A given touch point
25 /// (say, by a finger) will have the same identifier for the duration of
26 /// its movement around the surface. This lets you ensure that you're
27 /// tracking the same touch all the time.
28 ///
29 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Touch/identifier)
30 #[inline]
31 pub fn identifier(&self) -> i32 {
32 js!(
33 return @{self.as_ref()}.identifier;
34 ).try_into().unwrap()
35 }
36
37 /// Returns the X coordinate of the touch point relative to the left edge of the screen.
38 ///
39 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Touch/screenX)
40 #[inline]
41 pub fn screen_x(&self) -> f64 {
42 js!(
43 return @{self.as_ref()}.screenX;
44 ).try_into().unwrap()
45 }
46
47 /// Returns the Y coordinate of the touch point relative to the left edge of the screen.
48 ///
49 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Touch/screenY)
50 #[inline]
51 pub fn screen_y(&self) -> f64 {
52 js!(
53 return @{self.as_ref()}.screenY;
54 ).try_into().unwrap()
55 }
56
57 /// Returns the X coordinate of the touch point relative to the left edge of the browser viewport, not including any scroll offset.
58 ///
59 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Touch/clientX)
60 #[inline]
61 pub fn client_x(&self) -> f64 {
62 js!(
63 return @{self.as_ref()}.clientX;
64 ).try_into().unwrap()
65 }
66
67 /// Returns the Y coordinate of the touch point relative to the left edge of the browser viewport, not including any scroll offset.
68 ///
69 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Touch/clientY)
70 #[inline]
71 pub fn client_y(&self) -> f64 {
72 js!(
73 return @{self.as_ref()}.clientY;
74 ).try_into().unwrap()
75 }
76
77 /// Returns the X coordinate of the touch point relative to the left edge of the document. Unlike clientX, this value includes the horizontal scroll offset, if any.
78 ///
79 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Touch/pageX)
80 #[inline]
81 pub fn page_x(&self) -> f64 {
82 js!(
83 return @{self.as_ref()}.pageX;
84 ).try_into().unwrap()
85 }
86
87 /// Returns the Y coordinate of the touch point relative to the left edge of the document. Unlike clientX, this value includes the horizontal scroll offset, if any.
88 ///
89 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Touch/pageY)
90 #[inline]
91 pub fn page_y(&self) -> f64 {
92 js!(
93 return @{self.as_ref()}.pageY;
94 ).try_into().unwrap()
95 }
96
97 /// Returns the Element on which the touch point started when it was first placed on the surface, even if the touch point has since moved outside the interactive area of that element or even been removed from the document.
98 ///
99 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Touch/target)
100 #[inline]
101 pub fn target(&self) -> EventTarget {
102 js!(
103 return @{self.as_ref()}.target;
104 ).try_into().unwrap()
105 }
106
107
108
109 /// Returns the X radius of the ellipse that most closely circumscribes the area of contact with the screen. The value is in pixels of the same scale as screenX.
110 ///
111 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Touch/radiusX)
112 #[inline]
113 // TODO: Change the return type to `f32`.
114 pub fn radius_x(&self) -> f64 {
115 js!(
116 return @{self.as_ref()}.radiusX;
117 ).try_into().unwrap()
118 }
119
120 /// Returns the Y radius of the ellipse that most closely circumscribes the area of contact with the screen. The value is in pixels of the same scale as screenY.
121 ///
122 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Touch/radiusY)
123 #[inline]
124 pub fn radius_y(&self) -> f64 {
125 js!(
126 return @{self.as_ref()}.radiusY;
127 ).try_into().unwrap()
128 }
129
130 /// Returns the angle (in degrees) that the ellipse described by radiusX and radiusY must be rotated, clockwise, to most accurately cover the area of contact between the user and the surface.
131 ///
132 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Touch/rotationAngle)
133 #[inline]
134 pub fn rotation_angle(&self) -> f64 {
135 js!(
136 return @{self.as_ref()}.rotationAngle;
137 ).try_into().unwrap()
138 }
139
140 /// Returns the amount of pressure being applied to the surface by the user, as a float between 0.0 (no pressure) and 1.0 (maximum pressure).
141 ///
142 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Touch/force)
143 #[inline]
144 pub fn force(&self) -> f64 {
145 js!(
146 return @{self.as_ref()}.force;
147 ).try_into().unwrap()
148 }
149
150 /// The altitude (in radians) of a stylus, in the range 0 (parallel to the surface) to π/2 (perpendicular to the surface). The value 0 should be used for devices which do not support this property.
151 ///
152 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Touch/altitudeAngle)
153 // https://w3c.github.io/touch-events/#touch-interface
154 #[inline]
155 pub fn altitude_angle(&self) -> f64 {
156 js!(
157 return @{self.as_ref()}.altitudeAngle;
158 ).try_into().unwrap()
159 }
160
161 /// The azimuth angle (in radians) of a stylus, in the range 0 to 2π. 0 represents a stylus whose cap is pointing in the direction of increasing screenX values. π/2 represents a stylus whose cap is pointing in the direction of increasing screenY values. The value 0 should be used for devices which do not support this property.
162 ///
163 /// [(JavaScript docs)](https://w3c.github.io/touch-events/#touch-interface)
164 //
165 #[inline]
166 pub fn azimuth_angle(&self) -> f64 {
167 js!(
168 return @{self.as_ref()}.azimuthAngle;
169 ).try_into().unwrap()
170 }
171
172 /// The type of device used to trigger the touch.
173 ///
174 // https://w3c.github.io/touch-events/#touch-interface
175 #[inline]
176 pub fn touch_type(&self) -> TouchType {
177 js!(
178 return @{self.as_ref()}.touchType;
179 ).try_into().unwrap()
180 }
181}
182
183/// An enumeration representing the different types of possible touch input.
184///
185/// [(JavaScript docs)](https://w3c.github.io/touch-events/#touch-interface)
186#[derive(Copy, Clone, Debug, Eq, PartialEq)]
187pub enum TouchType {
188 /// A direct touch from a finger on the screen.
189 Direct,
190 /// A touch from a stylus or pen device.
191 Stylus,
192}
193
194impl TryFrom<Value> for TouchType {
195 type Error = ConversionError;
196
197 fn try_from(v: Value) -> Result<Self, Self::Error> {
198 let value: String = v.try_into()?;
199 match value.as_ref() {
200 "stylus" => Ok(TouchType::Stylus),
201 "direct" => Ok(TouchType::Direct),
202 s => Err(ConversionError::Custom(format!("invalid touchtype mapping type \"{}\"", s))),
203 }
204 }
205}