waft_widgets/widgets/feedback.rs
1//! Flutter counterpart: `widgets/feedback.dart`.
2//!
3//! Theme-less widgets version: haptic feedback only. Semantics events and
4//! [`SystemSound`] wait; see PORTING.md.
5
6use waft_embedder::TargetPlatform;
7use waft_foundation::App;
8use waft_gestures::{GestureLongPressCallback, GestureTapCallback};
9use waft_services::HapticFeedback;
10
11use crate::framework::BuildContext;
12
13/// Provides platform-specific acoustic and/or haptic feedback for certain
14/// actions.
15///
16/// For example, to play the Android-typically click sound when a button is
17/// tapped, call [`for_tap`](Self::for_tap). For the Android-specific vibration when long pressing
18/// an element, call [`for_long_press`](Self::for_long_press). Alternatively, you can also wrap your
19/// `GestureDetector.on_tap` or `GestureDetector.on_long_press` callback in
20/// [`wrap_for_tap`](Self::wrap_for_tap) or [`wrap_for_long_press`](Self::wrap_for_long_press) to achieve the same.
21///
22/// All methods in this class are usually called from within a
23/// `StatelessWidget::build` method or from a `State`'s methods as you have to
24/// provide a [`BuildContext`].
25pub struct Feedback;
26
27impl Feedback {
28 /// Provides platform-specific feedback for a tap.
29 ///
30 /// On Android the click system sound is played. On iOS this is a no-op.
31 ///
32 /// See also:
33 ///
34 /// * [`wrap_for_tap`](Self::wrap_for_tap) to trigger platform-specific feedback before executing a
35 /// [`GestureTapCallback`].
36 pub fn for_tap(app: &App, _context: BuildContext) {
37 match app.platform().target_platform() {
38 TargetPlatform::Android | TargetPlatform::Fuchsia => {}
39 TargetPlatform::IOS
40 | TargetPlatform::Linux
41 | TargetPlatform::MacOS
42 | TargetPlatform::Windows => {}
43 }
44 }
45
46 /// Wraps a [`GestureTapCallback`] to provide platform specific feedback for a
47 /// tap before the provided callback is executed.
48 ///
49 /// On Android the platform-typical click system sound is played. On iOS this
50 /// is a no-op as that platform usually doesn't provide feedback for a tap.
51 ///
52 /// See also:
53 ///
54 /// * [`for_tap`](Self::for_tap) to just trigger the platform-specific feedback without wrapping
55 /// a [`GestureTapCallback`].
56 pub fn wrap_for_tap(
57 callback: Option<GestureTapCallback>,
58 context: BuildContext,
59 ) -> Option<GestureTapCallback> {
60 let callback = callback?;
61 Some(waft_foundation::Listener::new(move |app| {
62 Feedback::for_tap(app, context);
63 callback.call(app);
64 }))
65 }
66
67 /// Provides platform-specific feedback for a long press.
68 ///
69 /// On Android the platform-typical vibration is triggered. On iOS a
70 /// heavy-impact haptic feedback is triggered alongside the click system
71 /// sound, which was observed to be the default behavior on a physical iPhone
72 /// 15 Pro running iOS version 17.5.
73 ///
74 /// See also:
75 ///
76 /// * [`wrap_for_long_press`](Self::wrap_for_long_press) to trigger platform-specific feedback before
77 /// executing a [`GestureLongPressCallback`].
78 pub fn for_long_press(app: &App, _context: BuildContext) {
79 match app.platform().target_platform() {
80 TargetPlatform::Android | TargetPlatform::Fuchsia => {
81 HapticFeedback::vibrate(app);
82 }
83 TargetPlatform::IOS => {
84 HapticFeedback::heavy_impact(app);
85 }
86 TargetPlatform::Linux | TargetPlatform::MacOS | TargetPlatform::Windows => {}
87 }
88 }
89
90 /// Wraps a [`GestureLongPressCallback`] to provide platform specific feedback
91 /// for a long press before the provided callback is executed.
92 ///
93 /// On Android the platform-typical vibration is triggered. On iOS a
94 /// heavy-impact haptic feedback is triggered alongside the click system
95 /// sound, which was observed to be the default behavior on a physical iPhone
96 /// 15 Pro running iOS version 17.5.
97 ///
98 /// See also:
99 ///
100 /// * [`for_long_press`](Self::for_long_press) to just trigger the platform-specific feedback without
101 /// wrapping a [`GestureLongPressCallback`].
102 pub fn wrap_for_long_press(
103 callback: Option<GestureLongPressCallback>,
104 context: BuildContext,
105 ) -> Option<GestureLongPressCallback> {
106 let callback = callback?;
107 Some(waft_foundation::Listener::new(move |app| {
108 Feedback::for_long_press(app, context);
109 callback.call(app);
110 }))
111 }
112}