main
ts 198 lines 4.61 KB
Raw
1 import {
2 vibrate,
3 impactFeedback,
4 notificationFeedback,
5 selectionFeedback,
6 } from "@tauri-apps/plugin-haptics";
7
8 /**
9 * Haptic feedback utility for consistent haptic responses across the app
10 */
11 export class HapticFeedback {
12 private static isEnabled = true;
13
14 /**
15 * Enable or disable haptic feedback globally
16 */
17 static setEnabled(enabled: boolean) {
18 this.isEnabled = enabled;
19 }
20
21 /**
22 * Check if haptic feedback is enabled
23 */
24 static getEnabled(): boolean {
25 return this.isEnabled;
26 }
27
28 /**
29 * Light haptic feedback for selections and navigation
30 * Use for: navigation taps, filter selections, toggle switches
31 */
32 static async selection(): Promise<void> {
33 if (!this.isEnabled) return;
34
35 try {
36 await selectionFeedback();
37 } catch (error) {
38 console.log("Selection haptic feedback not available:", error);
39 }
40 }
41
42 /**
43 * Light impact feedback for UI interactions
44 * Use for: button taps, menu selections
45 */
46 static async light(): Promise<void> {
47 if (!this.isEnabled) return;
48
49 try {
50 await impactFeedback("light");
51 } catch (error) {
52 console.log("Light haptic feedback not available:", error);
53 }
54 }
55
56 /**
57 * Medium impact feedback for important actions
58 * Use for: save/favorite actions, form submissions
59 */
60 static async medium(): Promise<void> {
61 if (!this.isEnabled) return;
62
63 try {
64 await impactFeedback("medium");
65 } catch (error) {
66 console.log("Medium haptic feedback not available:", error);
67 }
68 }
69
70 /**
71 * Heavy impact feedback for significant actions
72 * Use for: delete actions, important confirmations
73 */
74 static async heavy(): Promise<void> {
75 if (!this.isEnabled) return;
76
77 try {
78 await impactFeedback("heavy");
79 } catch (error) {
80 console.log("Heavy haptic feedback not available:", error);
81 }
82 }
83
84 /**
85 * Success notification feedback
86 * Use for: successful saves, completed actions
87 */
88 static async success(): Promise<void> {
89 if (!this.isEnabled) return;
90
91 try {
92 await notificationFeedback("success");
93 } catch (error) {
94 console.log("Success haptic feedback not available:", error);
95 }
96 }
97
98 /**
99 * Warning notification feedback
100 * Use for: validation errors, warnings
101 */
102 static async warning(): Promise<void> {
103 if (!this.isEnabled) return;
104
105 try {
106 await notificationFeedback("warning");
107 } catch (error) {
108 console.log("Warning haptic feedback not available:", error);
109 }
110 }
111
112 /**
113 * Error notification feedback
114 * Use for: errors, failed actions
115 */
116 static async error(): Promise<void> {
117 if (!this.isEnabled) return;
118
119 try {
120 await notificationFeedback("error");
121 } catch (error) {
122 console.log("Error haptic feedback not available:", error);
123 }
124 }
125
126 /**
127 * Custom vibration pattern
128 * Use for: special interactions, custom feedback
129 */
130 static async vibrate(duration: number = 200): Promise<void> {
131 if (!this.isEnabled) return;
132
133 try {
134 await vibrate(duration);
135 } catch (error) {
136 console.log("Vibration haptic feedback not available:", error);
137 }
138 }
139
140 /**
141 * Button press feedback - convenience method for button interactions
142 * Use for: most button presses and clickable elements
143 */
144 static async buttonPress(): Promise<void> {
145 return this.light();
146 }
147
148 /**
149 * Card tap feedback - convenience method for card interactions
150 * Use for: restaurant cards, list items
151 */
152 static async cardTap(): Promise<void> {
153 return this.selection();
154 }
155
156 /**
157 * Toggle feedback - convenience method for toggle actions
158 * Use for: favorite/unfavorite, like/unlike
159 */
160 static async toggle(): Promise<void> {
161 return this.medium();
162 }
163
164 /**
165 * Navigation feedback - convenience method for navigation actions
166 * Use for: tab switches, page navigation
167 */
168 static async navigation(): Promise<void> {
169 return this.selection();
170 }
171 }
172
173 /**
174 * Convenience functions for common haptic patterns
175 */
176 export const haptics = {
177 // Basic interactions
178 selection: () => HapticFeedback.selection(),
179 light: () => HapticFeedback.light(),
180 medium: () => HapticFeedback.medium(),
181 heavy: () => HapticFeedback.heavy(),
182
183 // Notifications
184 success: () => HapticFeedback.success(),
185 warning: () => HapticFeedback.warning(),
186 error: () => HapticFeedback.error(),
187
188 // Custom
189 vibrate: (duration?: number) => HapticFeedback.vibrate(duration),
190
191 // Convenience methods
192 buttonPress: () => HapticFeedback.buttonPress(),
193 cardTap: () => HapticFeedback.cardTap(),
194 toggle: () => HapticFeedback.toggle(),
195 navigation: () => HapticFeedback.navigation(),
196 };
197
198 export default haptics;