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
//! Flutter counterpart: `widgets/feedback.dart`.
//!
//! Theme-less widgets version: haptic feedback only. Semantics events and
//! [`SystemSound`] wait; see PORTING.md.
use waft_embedder::TargetPlatform;
use waft_foundation::App;
use waft_gestures::{GestureLongPressCallback, GestureTapCallback};
use waft_services::HapticFeedback;
use crate::framework::BuildContext;
/// Provides platform-specific acoustic and/or haptic feedback for certain
/// actions.
///
/// For example, to play the Android-typically click sound when a button is
/// tapped, call [`for_tap`](Self::for_tap). For the Android-specific vibration when long pressing
/// an element, call [`for_long_press`](Self::for_long_press). Alternatively, you can also wrap your
/// `GestureDetector.on_tap` or `GestureDetector.on_long_press` callback in
/// [`wrap_for_tap`](Self::wrap_for_tap) or [`wrap_for_long_press`](Self::wrap_for_long_press) to achieve the same.
///
/// All methods in this class are usually called from within a
/// `StatelessWidget::build` method or from a `State`'s methods as you have to
/// provide a [`BuildContext`].
pub struct Feedback;
impl Feedback {
/// Provides platform-specific feedback for a tap.
///
/// On Android the click system sound is played. On iOS this is a no-op.
///
/// See also:
///
/// * [`wrap_for_tap`](Self::wrap_for_tap) to trigger platform-specific feedback before executing a
/// [`GestureTapCallback`].
pub fn for_tap(app: &App, _context: BuildContext) {
match app.platform().target_platform() {
TargetPlatform::Android | TargetPlatform::Fuchsia => {}
TargetPlatform::IOS
| TargetPlatform::Linux
| TargetPlatform::MacOS
| TargetPlatform::Windows => {}
}
}
/// Wraps a [`GestureTapCallback`] to provide platform specific feedback for a
/// tap before the provided callback is executed.
///
/// On Android the platform-typical click system sound is played. On iOS this
/// is a no-op as that platform usually doesn't provide feedback for a tap.
///
/// See also:
///
/// * [`for_tap`](Self::for_tap) to just trigger the platform-specific feedback without wrapping
/// a [`GestureTapCallback`].
pub fn wrap_for_tap(
callback: Option<GestureTapCallback>,
context: BuildContext,
) -> Option<GestureTapCallback> {
let callback = callback?;
Some(waft_foundation::Listener::new(move |app| {
Feedback::for_tap(app, context);
callback.call(app);
}))
}
/// Provides platform-specific feedback for a long press.
///
/// On Android the platform-typical vibration is triggered. On iOS a
/// heavy-impact haptic feedback is triggered alongside the click system
/// sound, which was observed to be the default behavior on a physical iPhone
/// 15 Pro running iOS version 17.5.
///
/// See also:
///
/// * [`wrap_for_long_press`](Self::wrap_for_long_press) to trigger platform-specific feedback before
/// executing a [`GestureLongPressCallback`].
pub fn for_long_press(app: &App, _context: BuildContext) {
match app.platform().target_platform() {
TargetPlatform::Android | TargetPlatform::Fuchsia => {
HapticFeedback::vibrate(app);
}
TargetPlatform::IOS => {
HapticFeedback::heavy_impact(app);
}
TargetPlatform::Linux | TargetPlatform::MacOS | TargetPlatform::Windows => {}
}
}
/// Wraps a [`GestureLongPressCallback`] to provide platform specific feedback
/// for a long press before the provided callback is executed.
///
/// On Android the platform-typical vibration is triggered. On iOS a
/// heavy-impact haptic feedback is triggered alongside the click system
/// sound, which was observed to be the default behavior on a physical iPhone
/// 15 Pro running iOS version 17.5.
///
/// See also:
///
/// * [`for_long_press`](Self::for_long_press) to just trigger the platform-specific feedback without
/// wrapping a [`GestureLongPressCallback`].
pub fn wrap_for_long_press(
callback: Option<GestureLongPressCallback>,
context: BuildContext,
) -> Option<GestureLongPressCallback> {
let callback = callback?;
Some(waft_foundation::Listener::new(move |app| {
Feedback::for_long_press(app, context);
callback.call(app);
}))
}
}