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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//! Haptic Feedback Example
//!
//! This example demonstrates haptic feedback in GPUI (macOS only):
//!
//! - Check if haptic feedback is supported
//! - Play the three [`NSHapticFeedbackPattern`] styles
//! - A quantized slider with haptic feedback
//!
//! On macOS, haptics are delivered via `NSHapticFeedbackManager`. On other
//! platforms, the calls are currently no-ops.
#[path = "../shared/prelude.rs"]
mod example_prelude;
use example_prelude::init_example;
use gpui::{
App, Bounds, Context, DragMoveEvent, FontWeight, HapticFeedbackStyle, InteractiveElement,
IntoElement, IntoHsla, MouseButton, MouseDownEvent, ParentElement, Pixels, Render,
StatefulInteractiveElement, Styled, Window, WindowBounds, WindowOptions, colors::Colors, div,
prelude::*, px, relative, rgb, size,
};
#[cfg(target_os = "macos")]
use gpui_ce_platform::MacAppExt;
/// Whether the current machine supports haptic feedback.
///
/// Haptics are a macOS escape hatch reached by downcasting, so off macOS there is
/// no method to call and this reports `false`.
fn supports_haptic_feedback(_cx: &App) -> bool {
#[cfg(target_os = "macos")]
{
_cx.supports_haptic_feedback()
}
#[cfg(not(target_os = "macos"))]
{
false
}
}
/// Plays a haptic of the given style on platforms that support it; a no-op elsewhere.
fn play_haptic_feedback(_cx: &App, _style: HapticFeedbackStyle) {
#[cfg(target_os = "macos")]
{
_cx.play_haptic_feedback(_style);
}
#[cfg(not(target_os = "macos"))]
{
let _ = (_cx, _style);
}
}
const SLIDER_MIN: f32 = 0.0;
const SLIDER_MAX: f32 = 100.0;
const SLIDER_STEP: f32 = 10.0;
const SLIDER_STEP_COUNT: usize = ((SLIDER_MAX - SLIDER_MIN) / SLIDER_STEP) as usize;
#[derive(Clone)]
struct SliderDrag;
impl Render for SliderDrag {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
gpui::Empty
}
}
struct HapticFeedbackExample {
supported: bool,
slider_value: f32,
slider_prev_step: i32,
slider_bounds: Option<Bounds<Pixels>>,
}
impl HapticFeedbackExample {
fn new(cx: &mut App) -> Self {
Self {
supported: supports_haptic_feedback(cx),
slider_value: 0.0,
slider_prev_step: 0,
slider_bounds: None,
}
}
fn haptic_button(
&self,
id: &'static str,
label: &'static str,
style: HapticFeedbackStyle,
color: impl IntoHsla,
colors: &Colors,
cx: &mut Context<Self>,
) -> impl IntoElement {
let _ = cx;
let color = color.into_hsla();
div()
.id(id)
.flex()
.flex_col()
.w_48()
.items_center()
.justify_center()
.px_4()
.py_3()
.rounded_md()
.bg(color)
.cursor_pointer()
.hover(move |s| s.bg(color.opacity(0.8)))
.active(move |s| s.bg(color.opacity(0.6)))
.child(
div()
.text_base()
.font_weight(FontWeight::MEDIUM)
.text_color(colors.selected_text)
.child(label),
)
.on_hover(move |hovered, _, cx| {
if *hovered {
play_haptic_feedback(cx, style);
}
})
}
fn value_to_step(value: f32) -> i32 {
((value - SLIDER_MIN) / SLIDER_STEP).round() as i32
}
fn snap_to_step(raw: f32) -> f32 {
let snapped = (raw / SLIDER_STEP).round() * SLIDER_STEP;
snapped.clamp(SLIDER_MIN, SLIDER_MAX)
}
fn value_to_percentage(value: f32) -> f32 {
((value - SLIDER_MIN) / (SLIDER_MAX - SLIDER_MIN)).clamp(0.0, 1.0)
}
fn update_slider_from_position(
&mut self,
position_x: Pixels,
bounds: Bounds<Pixels>,
cx: &mut Context<Self>,
) {
let inner_x = (position_x - bounds.left()).clamp(px(0.), bounds.size.width);
let percentage = inner_x / bounds.size.width;
let raw = SLIDER_MIN + (SLIDER_MAX - SLIDER_MIN) * percentage;
let new_value = Self::snap_to_step(raw);
let new_step = Self::value_to_step(new_value);
self.slider_value = new_value;
self.slider_bounds = Some(bounds);
if new_step != self.slider_prev_step {
play_haptic_feedback(cx, HapticFeedbackStyle::LevelChange);
self.slider_prev_step = new_step;
}
cx.notify();
}
}
impl Render for HapticFeedbackExample {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let colors = Colors::for_appearance(window);
let slider_percentage = Self::value_to_percentage(self.slider_value);
let slider_color = rgb(0x3b82f6);
div()
.flex()
.flex_col()
.size_full()
.bg(colors.background)
.p_8()
.gap_6()
.child(
div()
.flex()
.flex_col()
.gap_2()
.child(
div()
.text_2xl()
.font_weight(FontWeight::SEMIBOLD)
.text_color(colors.text)
.child("Haptic Feedback"),
)
.child(
div().text_sm().text_color(colors.disabled).child(
"macOS Force Touch trackpad haptics via NSHapticFeedbackManager",
),
),
)
.child(
div()
.flex()
.items_center()
.gap_2()
.px_4()
.py_2()
.rounded_md()
.bg(if self.supported {
rgb(0x22c55e)
} else {
rgb(0xf59e0b)
}
.opacity(0.1))
.child({
let dot = if self.supported {
rgb(0x22c55e)
} else {
rgb(0xf59e0b)
};
div().size_2().rounded_full().bg(dot)
})
.child(
div()
.text_sm()
.font_weight(FontWeight::MEDIUM)
.text_color(colors.text)
.when_else(
self.supported,
|this| this.child("Haptics is supported on this machine"),
|this| this.child("Haptics is not supported on this machine"),
),
),
)
.child(
div()
.child("Hover over each button to trigger haptic feedback")
.text_sm()
.text_color(colors.text),
)
.child(
div()
.flex()
.gap_3()
.child(self.haptic_button(
"btn-generic",
"Generic",
HapticFeedbackStyle::Generic,
rgb(0x3b82f6),
&colors,
cx,
))
.child(self.haptic_button(
"btn-alignment",
"Alignment",
HapticFeedbackStyle::Alignment,
rgb(0x10b981),
&colors,
cx,
))
.child(self.haptic_button(
"btn-levelchange",
"LevelChange",
HapticFeedbackStyle::LevelChange,
rgb(0x8b5cf6),
&colors,
cx,
)),
)
.child(
div()
.flex()
.flex_col()
.gap_3()
.child(
div()
.text_lg()
.font_weight(FontWeight::SEMIBOLD)
.text_color(colors.text)
.child("Slider with LevelChange haptic"),
)
.child(
div().flex().items_center().gap_2().child(
div()
.font_weight(FontWeight::MEDIUM)
.text_color(colors.selected_text)
.child(format!("Value: {:.0}", self.slider_value)),
),
)
.child(
div()
.id("slider-container")
.w_full()
.h_4()
.flex()
.items_center()
.px_2()
.cursor_pointer()
.on_drag(SliderDrag, |_, _, _, cx| cx.new(|_| SliderDrag))
.on_drag_move(cx.listener(
|this, e: &DragMoveEvent<SliderDrag>, _, cx| {
this.update_slider_from_position(
e.event.position.x,
e.bounds,
cx,
);
},
))
.on_mouse_down(
MouseButton::Left,
cx.listener(move |this, e: &MouseDownEvent, _, cx| {
if let Some(bounds) = this.slider_bounds {
this.update_slider_from_position(e.position.x, bounds, cx);
}
}),
)
.child(
div()
.w_full()
.h_1p5()
.rounded_full()
.bg(colors.text.opacity(0.12))
.flex()
.items_center()
.justify_between()
.children((0..(SLIDER_STEP_COUNT + 1)).map(|_| {
div()
.size_1p5()
.rounded_full()
.bg(colors.disabled)
.into_any()
}))
.child(
div()
.absolute()
.h_full()
.left(px(0.))
.right(relative(1.0 - slider_percentage))
.bg(slider_color.opacity(0.7))
.rounded_full(),
)
.child(
div()
.absolute()
.top(px(-6.))
.left(relative(slider_percentage))
.ml(px(-8.))
.size_4()
.rounded_full()
.bg(slider_color)
.shadow_md(),
),
),
),
)
}
}
fn main() {
gpui_ce_platform::application().run(|cx: &mut App| {
let bounds = Bounds::centered(None, size(px(520.), px(520.)), cx);
cx.open_window(
WindowOptions {
window_bounds: Some(WindowBounds::Windowed(bounds)),
..Default::default()
},
|_, cx| cx.new(|cx| HapticFeedbackExample::new(cx)),
)
.expect("Failed to open window");
init_example(cx, "Haptic Feedback");
});
}