gpui-material-extras 0.3.2

Optional Material 3 Expressive component families for GPUI
Documentation
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
use std::{cell::Cell, rc::Rc, time::Duration};

use gpui::{
    AccessibleAction, AnyElement, App, BackgroundExecutor, Context, Div, ElementId, Entity,
    FocusHandle, Hsla, InteractiveElement, IntoElement, MouseButton, ParentElement, RenderOnce,
    Role, ScrollEvent, SharedString, Stateful, StatefulInteractiveElement, Styled, Task,
    TouchPhase, Window,
    accesskit::{ActionData, Orientation},
    canvas, div,
    prelude::FluentBuilder as _,
    px,
};
use gpui_base::TestSupportExt;
use gpui_material_components::Text;
use gpui_material_foundation::{
    InteractionFeedback, InteractionSource, InteractionState, InteractionTracker, Shape, animate,
    color, current_material_theme,
};
use gpui_material_theme::{
    ColorRole, CornerRole, CornerShape, MaterialTheme, MotionKind, MotionSpec, MotionSpeed,
    TypeRole,
};

const HEIGHT: f32 = 200.;
const ROW_HEIGHT: f32 = HEIGHT / 3.;
const PRECISE_SCROLL_SETTLE: Duration = Duration::from_millis(120);
// Move near a distant destination before animating the last few rows.
const ANIMATED_ROWS: usize = 3;

struct SettleTimer {
    _task: Task<()>,
    bump: Box<dyn Fn(&BackgroundExecutor)>,
}

/// Caller-owned position and focus for one Material scroll field.
pub struct ScrollFieldState {
    item_count: usize,
    position: Position,
    target: usize,
    motion: Option<Motion>,
    gesture_start: Option<usize>,
    settle: Option<SettleTimer>,
    focus: FocusHandle,
}

#[derive(Clone, Copy, PartialEq)]
struct Position {
    index: usize,
    fraction: f32,
}

impl Position {
    fn at(index: usize) -> Self {
        Self {
            index,
            fraction: 0.,
        }
    }

    fn shift(self, rows: f32, item_count: usize) -> Self {
        let offset = self.fraction + rows;
        let shift = offset.round();
        Self {
            index: wrap(self.index, shift as isize, item_count),
            fraction: offset - shift,
        }
    }

    fn offset_from(self, target: usize) -> f32 {
        let distance = self.index.abs_diff(target).min(ANIMATED_ROWS) as f32;
        self.fraction
            + if self.index < target {
                -distance
            } else {
                distance
            }
    }
}

fn wrap(index: usize, rows: isize, count: usize) -> usize {
    let step = rows.unsigned_abs() % count;
    if rows < 0 {
        index
            .checked_sub(step)
            .unwrap_or_else(|| count - (step - index))
    } else if step >= count - index {
        step - (count - index)
    } else {
        index + step
    }
}

#[derive(Clone, Copy)]
struct Motion {
    start: f32,
    restart: bool,
    origin: ScrollOrigin,
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum ScrollOrigin {
    Application,
    Interaction,
}

impl ScrollFieldState {
    /// Creates a position at the requested option, clamped to the available indices.
    pub fn new(item_count: usize, index: usize, cx: &mut Context<Self>) -> Self {
        let index = index.min(item_count.saturating_sub(1));
        Self {
            item_count,
            position: Position::at(index),
            target: index,
            motion: None,
            gesture_start: None,
            settle: None,
            focus: cx.focus_handle(),
        }
    }

    /// Returns the total number of selectable options in this scroll field.
    pub fn item_count(&self) -> usize {
        self.item_count
    }

    /// Updates the options and centers the clamped destination immediately.
    pub fn set_item_count(&mut self, count: usize, cx: &mut Context<Self>) {
        if self.item_count != count {
            self.item_count = count;
            self.scroll_to_option(self.target, cx);
        }
    }

    /// Reads the nearest option or `None` when the field is empty.
    pub fn selected_option(&self) -> Option<usize> {
        (self.item_count > 0).then_some(self.position.index)
    }

    /// Reads the destination, including while the wheel is settling.
    pub fn target_option(&self) -> Option<usize> {
        (self.item_count > 0).then_some(self.target)
    }

    /// Returns whether a scroll gesture or settling animation is currently active.
    pub fn is_scroll_in_progress(&self) -> bool {
        self.motion.is_some() || self.gesture_start.is_some()
    }

    /// Returns the retained focus handle for this scroll field.
    pub fn focus_handle(&self) -> &FocusHandle {
        &self.focus
    }

    /// Centers the clamped option without animation.
    pub fn scroll_to_option(&mut self, index: usize, cx: &mut Context<Self>) {
        let target = index.min(self.item_count.saturating_sub(1));
        let position = Position::at(target);
        if self.target == target
            && self.position == position
            && self.gesture_start.is_none()
            && self.settle.is_none()
            && self.motion.is_none()
        {
            return;
        }
        self.target = target;
        self.gesture_start = None;
        self.settle = None;
        self.motion = None;
        self.position = position;
        cx.notify();
    }

    /// Scrolls to the clamped option using the window's motion policy.
    pub fn animate_scroll_to_option(&mut self, index: usize, cx: &mut Context<Self>) {
        self.animate_to(index, ScrollOrigin::Application, cx);
    }

    fn animate_to(&mut self, index: usize, origin: ScrollOrigin, cx: &mut Context<Self>) {
        let index = index.min(self.item_count.saturating_sub(1));
        if self.item_count > 0 && (self.target != index || self.gesture_start.is_some()) {
            self.snap(index, self.position.offset_from(index), origin, cx);
        }
    }

    fn snap(&mut self, index: usize, start: f32, origin: ScrollOrigin, cx: &mut Context<Self>) {
        if start == 0. || cx.reduce_motion() {
            self.scroll_to_option(index, cx);
        } else {
            self.target = index;
            self.gesture_start = None;
            self.settle = None;
            self.motion = Some(Motion {
                start,
                restart: true,
                origin,
            });
            cx.notify();
        }
    }

    fn sample(&mut self, offset: f32, cx: &mut Context<Self>) {
        let position = Position::at(self.target).shift(offset, self.item_count);
        let changed = self.position.index != position.index || offset == 0.;
        self.position = position;
        self.motion = self.motion.filter(|_| offset != 0.).map(|mut motion| {
            motion.restart = false;
            motion
        });
        if changed {
            cx.notify();
        }
    }

    fn step(&mut self, delta: isize, cx: &mut Context<Self>) {
        if self.item_count < 2 {
            return;
        }
        let index = wrap(self.target, delta, self.item_count);
        let (origin, destination) = if delta > 0 {
            (self.position.index, index)
        } else {
            (index, self.position.index)
        };
        let distance = if destination >= origin {
            destination - origin
        } else {
            self.item_count - (origin - destination)
        };
        let start =
            self.position.fraction - delta.signum() as f32 * distance.min(ANIMATED_ROWS) as f32;
        self.snap(index, start, ScrollOrigin::Interaction, cx);
    }

    fn select_row(&mut self, row: isize, cx: &mut Context<Self>) {
        if self.item_count > 0 {
            let index = wrap(self.position.index, row, self.item_count);
            self.snap(
                index,
                self.position.fraction - row as f32,
                ScrollOrigin::Interaction,
                cx,
            );
        }
    }

    fn scroll(&mut self, event: &ScrollEvent, window: &mut Window, cx: &mut Context<Self>) -> bool {
        if self.item_count < 2 {
            return false;
        }
        let delta = event.delta.pixel_delta(px(ROW_HEIGHT));
        let x = f32::from(delta.x);
        let y = f32::from(delta.y);
        let vertical = y.is_finite() && x.abs() <= y.abs() && y != 0.;
        if !event.delta.precise() {
            if vertical {
                self.step(if y < 0. { 1 } else { -1 }, cx);
            }
            return vertical;
        }
        if event.touch_phase == TouchPhase::Cancelled {
            return self.finish_gesture(true, cx);
        }
        if vertical {
            self.gesture_start.get_or_insert(self.position.index);
            self.motion = None;
            self.position = self.position.shift(-y / ROW_HEIGHT, self.item_count);
            self.target = self.position.index;
            cx.notify();
        }
        if event.touch_phase == TouchPhase::Ended {
            return self.finish_gesture(false, cx);
        }
        if vertical {
            // Some discrete touchpad updates omit a terminal phase. Reuse the
            // active settle timer across rapid scroll updates instead of
            // spawning and cancelling a task on every event.
            if let Some(settle) = &self.settle {
                (settle.bump)(cx.background_executor());
            } else {
                let origin = cx.background_executor().now();
                let last_offset = Rc::new(Cell::new(Duration::ZERO));
                let bump_offset = last_offset.clone();
                let bump = Box::new(move |executor: &BackgroundExecutor| {
                    bump_offset.set(executor.now().saturating_duration_since(origin));
                });
                let state = cx.weak_entity();
                let task = window.spawn(cx, async move |cx| {
                    let mut wait = PRECISE_SCROLL_SETTLE;
                    loop {
                        let Ok(timer) = cx.update(|_, cx| cx.background_executor().timer(wait))
                        else {
                            return;
                        };
                        timer.await;
                        let next_wait = cx.update(|_, cx| {
                            let elapsed = cx
                                .background_executor()
                                .now()
                                .saturating_duration_since(origin);
                            let deadline = last_offset.get() + PRECISE_SCROLL_SETTLE;
                            if elapsed < deadline {
                                Some(deadline - elapsed)
                            } else {
                                let _ =
                                    state.update(cx, |state, cx| state.finish_gesture(false, cx));
                                None
                            }
                        });
                        match next_wait {
                            Ok(Some(remaining)) => wait = remaining,
                            _ => return,
                        }
                    }
                });
                self.settle = Some(SettleTimer { _task: task, bump });
            }
        }
        vertical
    }

    fn finish_gesture(&mut self, cancelled: bool, cx: &mut Context<Self>) -> bool {
        let Some(start) = self.gesture_start else {
            return false;
        };
        let index = if cancelled {
            start
        } else {
            self.position.index
        };
        self.snap(
            index,
            self.position.offset_from(index),
            ScrollOrigin::Interaction,
            cx,
        );
        true
    }

    fn has_pending_interaction(&self) -> bool {
        self.gesture_start.is_some()
            || self
                .motion
                .is_some_and(|motion| motion.origin == ScrollOrigin::Interaction)
    }
}

/// Optional overrides for the window theme's scroll field colors.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct ScrollFieldColors {
    /// Background fill color when enabled.
    pub container: Option<Hsla>,
    /// Text color of non-selected rows when enabled.
    pub content: Option<Hsla>,
    /// Text color of the centered selected row when enabled.
    pub selected_content: Option<Hsla>,
    /// Background fill color when disabled.
    pub disabled_container: Option<Hsla>,
    /// Text color of non-selected rows when disabled.
    pub disabled_content: Option<Hsla>,
    /// Text color of the centered selected row when disabled.
    pub disabled_selected_content: Option<Hsla>,
}

impl ScrollFieldColors {
    fn resolve(self, enabled: bool, theme: &MaterialTheme) -> (Hsla, Hsla, Hsla) {
        let container = color(theme.colors.color(ColorRole::SurfaceContainerLowest));
        let selected = color(theme.colors.color(ColorRole::OnSurface));
        if enabled {
            (
                self.container.unwrap_or(container),
                self.content
                    .unwrap_or_else(|| color(theme.colors.color(ColorRole::Outline))),
                self.selected_content.unwrap_or(selected),
            )
        } else {
            (
                self.disabled_container
                    .unwrap_or_else(|| container.opacity(0.38)),
                self.disabled_content
                    .unwrap_or_else(|| selected.opacity(0.38)),
                self.disabled_selected_content
                    .unwrap_or_else(|| selected.opacity(0.38)),
            )
        }
    }
}

type ItemContent = Box<dyn Fn(usize, bool, bool) -> AnyElement>;
type ValueDescription = Box<dyn Fn(usize) -> SharedString>;

/// A vertically scrolling selection wheel with a single accessible focus stop.
/// The field fills its parent's width and displays three rows at a time.
#[derive(IntoElement)]
pub struct ScrollField {
    id: ElementId,
    name: SharedString,
    state: Entity<ScrollFieldState>,
    disabled: bool,
    colors: ScrollFieldColors,
    time_field_shape: Option<CornerShape>,
    numeric_offset: usize,
    source: Option<Entity<InteractionSource>>,
    item: Option<ItemContent>,
    description: Option<ValueDescription>,
}

impl ScrollField {
    /// Creates a scroll field with an element `id`, accessible `name`, and caller-owned `state`.
    pub fn new(
        id: impl Into<ElementId>,
        name: impl Into<SharedString>,
        state: &Entity<ScrollFieldState>,
    ) -> Self {
        Self {
            id: id.into(),
            name: name.into(),
            state: state.clone(),
            disabled: false,
            colors: ScrollFieldColors::default(),
            time_field_shape: None,
            numeric_offset: 0,
            source: None,
            item: None,
            description: None,
        }
    }

    /// Disables pointer, scroll, keyboard, and accessibility interaction when `true`.
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    /// Overrides the scroll field's local colors.
    pub fn colors(mut self, colors: ScrollFieldColors) -> Self {
        self.colors = colors;
        self
    }

    pub(super) fn time_field(mut self, shape: Option<CornerShape>, numeric_offset: usize) -> Self {
        self.time_field_shape = shape;
        self.numeric_offset = numeric_offset;
        self
    }

    /// Connects an [`InteractionSource`] to observe hover, press, drag, and focus states.
    pub fn interaction_source(mut self, source: &Entity<InteractionSource>) -> Self {
        self.source = Some(source.clone());
        self
    }

    /// Renders a decorative row with its index, selected state and enabled state.
    pub fn item_content<E: IntoElement>(
        mut self,
        item: impl Fn(usize, bool, bool) -> E + 'static,
    ) -> Self {
        self.item = Some(Box::new(move |index, selected, enabled| {
            item(index, selected, enabled).into_any_element()
        }));
        self
    }

    /// Describes an option to assistive technology. By default, uses its index.
    pub fn value_description<S: Into<SharedString>>(
        mut self,
        description: impl Fn(usize) -> S + 'static,
    ) -> Self {
        self.description = Some(Box::new(move |index| description(index).into()));
        self
    }
}

impl RenderOnce for ScrollField {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let theme = current_material_theme(window, cx);
        let corner = self
            .time_field_shape
            .unwrap_or_else(|| CornerRole::Large.shape());
        let direction = theme.layout.direction;
        let [tl, tr, br, bl] = corner.radii(HEIGHT, HEIGHT, direction).map(px);
        let ring = color(theme.colors.color(ColorRole::Secondary));
        let motion_spec =
            theme
                .motion
                .spec(MotionKind::Spatial, MotionSpeed::Fast, cx.reduce_motion());
        if self.disabled && self.state.read(cx).has_pending_interaction() {
            self.state
                .update(cx, |state, cx| state.scroll_to_option(state.target, cx));
        }
        let motion = self.state.read(cx).motion;
        if let Some(motion) = motion {
            let key = (self.id.clone(), "scroll-position");
            if motion.restart {
                animate(key.clone(), motion.start, MotionSpec::Instant, window, cx);
            }
            let offset = animate(key, 0., motion_spec, window, cx);
            self.state.update(cx, |state, cx| state.sample(offset, cx));
        }
        let state = self.state.read(cx);
        let count = state.item_count;
        let position = state.position;
        let target = state.target;
        let focus = state.focus.clone();
        let enabled = !self.disabled && count > 0;
        let (container, content, selected_content) = self.colors.resolve(enabled, &theme);
        let feedback = window
            .use_keyed_state((self.id.clone(), "scroll-field-feedback"), cx, |_, _| {
                Rc::new(Cell::new(InteractionState::default()))
            })
            .read(cx)
            .clone();
        let input = Input {
            tracker: self
                .source
                .as_ref()
                .map(|source| InteractionSource::track(source, &self.id, window, cx)),
            state: self.state,
            feedback,
            numeric_offset: self.numeric_offset,
        };
        let mut interaction = input.feedback.get();
        if !enabled || !window.is_window_active() {
            interaction = InteractionState::default();
        }
        interaction.disabled = !enabled;
        input.update_feedback(interaction, window, cx);
        let feedback = InteractionFeedback::new(input.feedback.get(), selected_content, ring);
        let label = (count > 0).then(|| {
            self.description
                .as_ref()
                .map_or_else(|| target.to_string().into(), |describe| describe(target))
        });
        let rows = (-2_isize..=2)
            .filter(|row| count > 1 || (count == 1 && *row == 0))
            .map(|row| {
                let index = wrap(position.index, row, count);
                let selected = row == 0;
                let visual = self.item.as_ref().map_or_else(
                    || {
                        Text::new(super::format_two_digits(index))
                            .role(if selected {
                                TypeRole::DisplayLarge
                            } else {
                                TypeRole::DisplayMedium
                            })
                            .emphasized(selected)
                            .into_any_element()
                    },
                    |item| item(index, selected, enabled),
                );
                let input = input.state.clone();
                div()
                    .absolute()
                    .top(px((1. + row as f32 - position.fraction) * ROW_HEIGHT))
                    .h(px(ROW_HEIGHT))
                    .w_full()
                    .flex()
                    .items_center()
                    .justify_center()
                    .text_color(if selected { selected_content } else { content })
                    .id(("scroll-field-row", (row + 2) as usize))
                    .role(Role::Group)
                    .a11y_synthetic_children(|tree| tree.parent_node().set_hidden())
                    .when(enabled, |row_element| {
                        row_element.on_click(move |_, window, cx| {
                            input.update(cx, |state, cx| {
                                state.focus.focus(window, cx);
                                state.select_row(row, cx);
                            });
                        })
                    })
                    .child(visual)
                    .test_support()
            });
        let field = div()
            .id(self.id)
            .relative()
            .w_full()
            .min_w_0()
            .h(px(HEIGHT))
            .flex_none()
            .overflow_hidden()
            .rounded_tl(tl)
            .rounded_tr(tr)
            .rounded_br(br)
            .rounded_bl(bl)
            .bg(container)
            .role(Role::Slider)
            .aria_label(self.name)
            .aria_orientation(Orientation::Vertical)
            .when_some(label, |field, label| {
                field
                    .aria_value(label)
                    .aria_numeric_value(target as f64 + self.numeric_offset as f64)
                    .aria_min_numeric_value(self.numeric_offset as f64)
                    .aria_max_numeric_value((count - 1) as f64 + self.numeric_offset as f64)
                    .aria_numeric_value_step(1.)
            })
            .when(!enabled, |field| {
                field.a11y_synthetic_children(|tree| tree.parent_node().set_disabled())
            })
            .children(rows)
            .child(
                canvas(
                    |_, _, _| (),
                    move |bounds, _, window, cx| {
                        if let Some(shape) = Shape::rounded(bounds, corner, direction) {
                            feedback.paint(
                                &shape,
                                None,
                                cx.background_executor().now(),
                                cx.reduce_motion(),
                                window,
                            );
                        }
                    },
                )
                .absolute()
                .inset_0(),
            );
        field
            .when(enabled, |field| input.bind(field))
            .test_support()
            .when(enabled || focus.is_focused(window), |field| {
                field.track_focus(&focus.tab_stop(enabled))
            })
    }
}

#[derive(Clone)]
struct Input {
    state: Entity<ScrollFieldState>,
    feedback: Rc<Cell<InteractionState>>,
    numeric_offset: usize,
    tracker: Option<InteractionTracker>,
}

impl Input {
    fn update_feedback(&self, mut feedback: InteractionState, window: &mut Window, cx: &mut App) {
        let state = self.state.read(cx);
        feedback.focus_visible = !feedback.disabled
            && state.focus.is_focused(window)
            && window.last_input_was_keyboard();
        feedback.dragged = !feedback.disabled && state.gesture_start.is_some();
        if self.feedback.replace(feedback) != feedback {
            window.refresh();
        }
        if let Some(tracker) = &self.tracker {
            tracker.record(feedback, cx);
        }
    }

    fn bind(&self, field: Stateful<Div>) -> Stateful<Div> {
        let scroll = self.clone();
        let hover = self.clone();
        let press = self.clone();
        let release = self.clone();
        let release_out = self.clone();
        let keyboard = self.state.clone();
        let increment = self.state.clone();
        let decrement = self.state.clone();
        let set_value = self.state.clone();
        let numeric_offset = self.numeric_offset as f64;
        field
            .on_scroll(move |event, window, cx| {
                if scroll
                    .state
                    .update(cx, |state, cx| state.scroll(event, window, cx))
                {
                    scroll.update_feedback(scroll.feedback.get(), window, cx);
                    cx.stop_propagation();
                }
            })
            .on_hover(move |hovered, window, cx| {
                let mut feedback = hover.feedback.get();
                feedback.hovered = *hovered;
                hover.update_feedback(feedback, window, cx);
            })
            .on_mouse_down(MouseButton::Left, move |_, window, cx| {
                press.pointer(true, window, cx)
            })
            .on_mouse_up(MouseButton::Left, move |_, window, cx| {
                release.pointer(false, window, cx)
            })
            .on_mouse_up_out(MouseButton::Left, move |_, window, cx| {
                release_out.pointer(false, window, cx)
            })
            .on_key_down(move |event, _, cx| {
                match event.keystroke.key.as_str() {
                    "up" => keyboard.update(cx, |state, cx| state.step(-1, cx)),
                    "down" => keyboard.update(cx, |state, cx| state.step(1, cx)),
                    "home" => keyboard.update(cx, |state, cx| {
                        state.animate_to(0, ScrollOrigin::Interaction, cx)
                    }),
                    "end" => keyboard.update(cx, |state, cx| {
                        state.animate_to(
                            state.item_count.saturating_sub(1),
                            ScrollOrigin::Interaction,
                            cx,
                        )
                    }),
                    _ => return,
                }
                cx.stop_propagation();
            })
            .on_a11y_action(AccessibleAction::Increment, move |_, _, cx| {
                increment.update(cx, |state, cx| state.step(1, cx))
            })
            .on_a11y_action(AccessibleAction::Decrement, move |_, _, cx| {
                decrement.update(cx, |state, cx| state.step(-1, cx))
            })
            .on_a11y_action(AccessibleAction::SetValue, move |data, _, cx| {
                let value = match data {
                    Some(ActionData::NumericValue(value)) => Some(*value),
                    Some(ActionData::Value(value)) => value.trim().parse::<f64>().ok(),
                    _ => None,
                };
                if let Some(value) = value.filter(|value| value.is_finite()) {
                    set_value.update(cx, |state, cx| {
                        state.animate_to(
                            (value - numeric_offset).round() as usize,
                            ScrollOrigin::Interaction,
                            cx,
                        )
                    });
                }
            })
    }

    fn pointer(&self, pressed: bool, window: &mut Window, cx: &mut App) {
        let mut feedback = self.feedback.get();
        feedback.pressed = pressed;
        self.update_feedback(feedback, window, cx);
    }
}