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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//! Flutter `services/haptic_feedback.dart`.
use inset_embedder::HapticFeedbackType;
use inset_foundation::App;
/// Allows access to the haptic feedback interface on the device.
///
/// This API is intentionally terse since it calls default platform behavior. It
/// is not suitable for precise control of the system's haptic feedback module.
///
/// See also:
///
/// * [Human Interface Haptics Guidelines](https://developer.apple.com/design/human-interface-guidelines/playing-haptics)
pub struct HapticFeedback;
impl HapticFeedback {
/// Provides vibration haptic feedback to the user for a short duration.
///
/// On iOS devices that support haptic feedback, this uses the default system
/// vibration value (`kSystemSoundID_Vibrate`).
///
/// On Android, this uses the platform haptic feedback API to simulate a
/// response to a long press (`HapticFeedbackConstants.LONG_PRESS`).
pub fn vibrate(app: &App) {
if let Some(haptics) = app.platform().haptics() {
haptics.feedback(HapticFeedbackType::Vibrate);
}
}
/// Provides a haptic feedback corresponding a collision impact with a light mass.
///
/// On iOS versions 10 and above, this uses a `UIImpactFeedbackGenerator` with
/// `UIImpactFeedbackStyleLight`. This call has no effects on iOS versions
/// below 10.
///
/// On Android, this uses `HapticFeedbackConstants.VIRTUAL_KEY`.
///
/// See also:
///
/// * [Human Interface Selection Playing Impact Haptic](https://developer.apple.com/design/human-interface-guidelines/playing-haptics#Impact)
pub fn light_impact(app: &App) {
if let Some(haptics) = app.platform().haptics() {
haptics.feedback(HapticFeedbackType::LightImpact);
}
}
/// Provides a haptic feedback corresponding a collision impact with a medium mass.
///
/// On iOS versions 10 and above, this uses a `UIImpactFeedbackGenerator` with
/// `UIImpactFeedbackStyleMedium`. This call has no effects on iOS versions
/// below 10.
///
/// On Android, this uses `HapticFeedbackConstants.KEYBOARD_TAP`.
pub fn medium_impact(app: &App) {
if let Some(haptics) = app.platform().haptics() {
haptics.feedback(HapticFeedbackType::MediumImpact);
}
}
/// Provides a haptic feedback corresponding a collision impact with a heavy mass.
///
/// On iOS versions 10 and above, this uses a `UIImpactFeedbackGenerator` with
/// `UIImpactFeedbackStyleHeavy`. This call has no effects on iOS versions
/// below 10.
///
/// On Android, this uses `HapticFeedbackConstants.CONTEXT_CLICK` on API levels
/// 23 and above. This call has no effects on Android API levels below 23.
pub fn heavy_impact(app: &App) {
if let Some(haptics) = app.platform().haptics() {
haptics.feedback(HapticFeedbackType::HeavyImpact);
}
}
/// Provides a haptic feedback indication selection changing through discrete values.
///
/// On iOS versions 10 and above, this uses a `UISelectionFeedbackGenerator`.
/// This call has no effects on iOS versions below 10.
///
/// On Android, this uses `HapticFeedbackConstants.CLOCK_TICK`.
///
/// See also:
///
/// * [Human Interface Selection Playing Selection Haptics](https://developer.apple.com/design/human-interface-guidelines/playing-haptics#Selection)
pub fn selection_click(app: &App) {
if let Some(haptics) = app.platform().haptics() {
haptics.feedback(HapticFeedbackType::SelectionClick);
}
}
/// Provides a haptic feedback indicating that a task or action has completed
/// successfully.
///
/// On iOS, this uses a `UINotificationFeedbackGenerator` with
/// `UINotificationFeedbackTypeSuccess`.
///
/// On Android, this uses `HapticFeedbackConstants.CONFIRM` on API levels 30
/// and above. This call has no effects on Android API levels below 30.
///
/// See also:
///
/// * [Human Interface Guidelines Playing Haptics](https://developer.apple.com/design/human-interface-guidelines/playing-haptics#Notification)
pub fn success_notification(app: &App) {
if let Some(haptics) = app.platform().haptics() {
haptics.feedback(HapticFeedbackType::SuccessNotification);
}
}
/// Provides a haptic feedback indicating that a task or action has produced
/// a warning.
///
/// On iOS, this uses a `UINotificationFeedbackGenerator` with
/// `UINotificationFeedbackTypeWarning`.
///
/// On Android, this uses `HapticFeedbackConstants.KEYBOARD_TAP` on API
/// levels 30 and above. This call has no effects on Android API levels below
/// 30.
pub fn warning_notification(app: &App) {
if let Some(haptics) = app.platform().haptics() {
haptics.feedback(HapticFeedbackType::WarningNotification);
}
}
/// Provides a haptic feedback indicating that a task or action has failed.
///
/// On iOS, this uses a `UINotificationFeedbackGenerator` with
/// `UINotificationFeedbackTypeError`.
///
/// On Android, this uses `HapticFeedbackConstants.REJECT` on API levels 30
/// and above. This call has no effects on Android API levels below 30.
pub fn error_notification(app: &App) {
if let Some(haptics) = app.platform().haptics() {
haptics.feedback(HapticFeedbackType::ErrorNotification);
}
}
}
#[cfg(test)]
mod tests {
use inset_foundation::AppCell;
use std::cell::RefCell;
use std::rc::Rc;
use std::time::Instant;
use inset_embedder::{Haptics, Platform, PlatformRef, TargetPlatform, ViewId, ViewRef};
use super::*;
#[derive(Default)]
struct RecordingPlatform {
kinds: RefCell<Vec<HapticFeedbackType>>,
}
impl Platform for RecordingPlatform {
fn target_platform(&self) -> TargetPlatform {
TargetPlatform::IOS
}
fn request_frame(&self) {}
fn now(&self) -> Instant {
Instant::now()
}
fn wake_at(&self, _deadline: Instant) {}
fn views(&self) -> Vec<ViewRef> {
Vec::new()
}
fn view(&self, _id: ViewId) -> Option<ViewRef> {
None
}
fn implicit_view(&self) -> Option<ViewRef> {
None
}
fn haptics(&self) -> Option<&dyn Haptics> {
Some(self)
}
}
impl Haptics for RecordingPlatform {
fn feedback(&self, kind: HapticFeedbackType) {
self.kinds.borrow_mut().push(kind);
}
}
#[test]
fn each_call_sends_its_own_feedback_kind_to_the_platform() {
let platform = Rc::new(RecordingPlatform::default());
let cell = AppCell::with_platform(Rc::clone(&platform) as PlatformRef);
let app = cell.borrow();
HapticFeedback::vibrate(&app);
HapticFeedback::light_impact(&app);
HapticFeedback::medium_impact(&app);
HapticFeedback::heavy_impact(&app);
HapticFeedback::selection_click(&app);
HapticFeedback::success_notification(&app);
HapticFeedback::warning_notification(&app);
HapticFeedback::error_notification(&app);
assert_eq!(
platform.kinds.borrow().as_slice(),
[
HapticFeedbackType::Vibrate,
HapticFeedbackType::LightImpact,
HapticFeedbackType::MediumImpact,
HapticFeedbackType::HeavyImpact,
HapticFeedbackType::SelectionClick,
HapticFeedbackType::SuccessNotification,
HapticFeedbackType::WarningNotification,
HapticFeedbackType::ErrorNotification,
]
);
}
}