gpui-deck 0.0.1

Headless GPUI primitives with application-owned presentation.
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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
//! A carousel showing one or more slides per view, with native focus, discrete
//! swiping, and logical navigation.

use std::time::Duration;

use gpui::{
    AnyElement, App, AppContext as _, Axis, Context, DragMoveEvent, ElementId, Empty, Entity,
    EntityId, EventEmitter, FocusHandle, InteractiveElement, IntoElement, KeyBinding, MouseButton,
    ParentElement, Pixels, Point, Render, RenderOnce, Role, SharedString,
    StatefulInteractiveElement, StyleRefinement, Styled, Task, Window, div, px,
};

use crate::{
    Button, Direction, TestSupportExt as _,
    actions::{SelectDown, SelectFirst, SelectLast, SelectLeft, SelectRight, SelectUp},
};

pub(crate) fn init(cx: &mut App) {
    cx.bind_keys([
        KeyBinding::new("left", SelectLeft, Some("Carousel && axis == horizontal")),
        KeyBinding::new("right", SelectRight, Some("Carousel && axis == horizontal")),
        KeyBinding::new("up", SelectUp, Some("Carousel && axis == vertical")),
        KeyBinding::new("down", SelectDown, Some("Carousel && axis == vertical")),
        KeyBinding::new("home", SelectFirst, Some("Carousel")),
        KeyBinding::new("end", SelectLast, Some("Carousel")),
    ]);
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum CarouselEvent {
    Change(usize),
}

/// Retained state for a fixed ordered set of slides. Labels name each slide.
///
/// A view shows `slides_per_view` consecutive slides starting at the selected
/// (leading) slide; one is the default. Only those slides are mounted, and the
/// application sizes them, for example with `relative(1. / n)` widths. Each
/// step moves by one slide, and the last view is always full: the leading index
/// ranges over [`snap_count`](Self::snap_count) positions. Looping wraps between
/// the last full view and the first. Application entities retain slide data;
/// there is no autoplay, timer, or focus movement merely because data changes.
pub struct CarouselState {
    labels: Vec<SharedString>,
    selected: Option<usize>,
    per_view: usize,
    looping: bool,
    axis: Axis,
    direction: Direction,
    focus: FocusHandle,
    slide_focus: Vec<FocusHandle>,
    drag_origin: Point<Pixels>,
    drag_committed: bool,
    hovered: bool,
    autoplay: Option<Task<()>>,
}

impl CarouselState {
    pub fn new<L: Into<SharedString>>(
        labels: impl IntoIterator<Item = L>,
        cx: &mut Context<Self>,
    ) -> Self {
        let labels: Vec<_> = labels.into_iter().map(Into::into).collect();
        Self {
            selected: (!labels.is_empty()).then_some(0),
            slide_focus: labels.iter().map(|_| cx.focus_handle()).collect(),
            labels,
            per_view: 1,
            looping: false,
            axis: Axis::Horizontal,
            direction: Direction::Ltr,
            focus: cx.focus_handle(),
            drag_origin: Point::default(),
            drag_committed: false,
            hovered: false,
            autoplay: None,
        }
    }

    pub fn looping(mut self, looping: bool) -> Self {
        self.looping = looping;
        self
    }
    /// Shows `count` slides per view; panics when `count` is zero.
    pub fn slides_per_view(mut self, count: usize) -> Self {
        assert!(count > 0, "A carousel view shows at least one slide");
        self.per_view = count;
        self.selected = self.selected.map(|index| index.min(self.last_start()));
        self
    }
    /// Changes the view size, keeping the leading slide where possible and
    /// moving focus out of slides that leave the view.
    pub fn set_slides_per_view(
        &mut self,
        count: usize,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> bool {
        assert!(count > 0, "A carousel view shows at least one slide");
        if self.per_view == count {
            return false;
        }
        let previous = self.visible_range();
        self.per_view = count;
        let Some(selected) = self.selected else {
            return true;
        };
        let start = selected.min(self.last_start());
        self.release_hidden_focus(previous, start, window, cx);
        self.selected = Some(start);
        if start != selected {
            cx.emit(CarouselEvent::Change(start));
        }
        cx.notify();
        window.refresh();
        true
    }
    /// The leading visible slide.
    pub fn selected_index(&self) -> Option<usize> {
        self.selected
    }
    /// The slides currently mounted, in order.
    pub fn visible_range(&self) -> std::ops::Range<usize> {
        self.selected.map_or(0..0, |start| {
            start..(start + self.per_view).min(self.labels.len())
        })
    }
    /// The number of distinct leading positions, one per full view.
    pub fn snap_count(&self) -> usize {
        if self.labels.is_empty() {
            0
        } else {
            self.last_start() + 1
        }
    }
    pub fn labels(&self) -> &[SharedString] {
        &self.labels
    }
    pub fn focus_handle(&self) -> &FocusHandle {
        &self.focus
    }
    pub fn can_previous(&self) -> bool {
        self.snap_count() > 1 && (self.looping || self.selected.is_some_and(|index| index > 0))
    }
    pub fn can_next(&self) -> bool {
        self.snap_count() > 1
            && (self.looping || self.selected.is_some_and(|index| index < self.last_start()))
    }
    fn last_start(&self) -> usize {
        self.labels.len().saturating_sub(self.per_view)
    }
    fn release_hidden_focus(
        &self,
        previous: std::ops::Range<usize>,
        start: usize,
        window: &mut Window,
        cx: &mut App,
    ) {
        let next = start..start + self.per_view;
        if previous
            .filter(|index| !next.contains(index))
            .any(|index| self.slide_focus[index].contains_focused(window, cx))
        {
            self.focus.focus(window, cx);
        }
    }

    /// Makes `index` the leading slide when it starts a full view. Focus inside
    /// slides leaving the view returns to the carousel scope before that content
    /// is unmounted; focus in slides that remain visible or outside stays put.
    pub fn select(&mut self, index: usize, window: &mut Window, cx: &mut Context<Self>) -> bool {
        if index >= self.snap_count() || self.selected == Some(index) {
            return false;
        }
        self.release_hidden_focus(self.visible_range(), index, window, cx);
        self.selected = Some(index);
        cx.emit(CarouselEvent::Change(index));
        cx.notify();
        window.refresh();
        true
    }

    pub fn previous(&mut self, window: &mut Window, cx: &mut Context<Self>) -> bool {
        self.advance(false, window, cx)
    }
    pub fn next(&mut self, window: &mut Window, cx: &mut Context<Self>) -> bool {
        self.advance(true, window, cx)
    }
    fn advance(&mut self, forward: bool, window: &mut Window, cx: &mut Context<Self>) -> bool {
        let Some(index) = self.selected else {
            return false;
        };
        if if forward {
            !self.can_next()
        } else {
            !self.can_previous()
        } {
            return false;
        }
        let snaps = self.snap_count();
        let next = if forward {
            (index + 1) % snaps
        } else {
            (index + snaps - 1) % snaps
        };
        self.select(next, window, cx)
    }

    /// Starts or stops automatic advancing. Each `interval` moves one slide
    /// forward, wrapping from the last view to the first. Rotation pauses while
    /// the pointer is over the carousel, focus is inside it, the window is
    /// inactive, or the application asks for reduced motion. Keyboard, swipe and
    /// [`CarouselControl`] navigation stop it, as does `None`. Render a visible
    /// control that calls this method so people can stop or restart rotation.
    pub fn set_autoplay(
        &mut self,
        interval: Option<Duration>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.autoplay = interval.map(|interval| {
            cx.spawn_in(window, async move |state, cx| {
                loop {
                    cx.background_executor().timer(interval).await;
                    if state
                        .update_in(cx, |state, window, cx| state.autoplay_tick(window, cx))
                        .is_err()
                    {
                        break;
                    }
                }
            })
        });
        cx.notify();
    }
    pub fn is_autoplaying(&self) -> bool {
        self.autoplay.is_some()
    }
    fn autoplay_tick(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        if self.hovered
            || !window.is_window_active()
            || cx.reduce_motion()
            || self.focus.contains_focused(window, cx)
            || self.snap_count() < 2
        {
            return;
        }
        let Some(index) = self.selected else {
            return;
        };
        let next = if index + 1 < self.snap_count() {
            index + 1
        } else {
            0
        };
        self.select(next, window, cx);
    }
    fn stop_autoplay(&mut self, cx: &mut Context<Self>) {
        if self.autoplay.take().is_some() {
            cx.notify();
        }
    }
    fn swipe_at_pointer(
        &mut self,
        position: Point<Pixels>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self.drag_committed {
            return;
        }
        let delta = match self.axis {
            Axis::Horizontal => position.x - self.drag_origin.x,
            Axis::Vertical => position.y - self.drag_origin.y,
        };
        if delta.abs() < px(40.) {
            return;
        }
        self.drag_committed = true;
        self.stop_autoplay(cx);
        let forward = match self.axis {
            Axis::Horizontal => self.direction.horizontal_forward(delta < px(0.)),
            Axis::Vertical => delta < px(0.),
        };
        self.advance(forward, window, cx);
    }

    fn key_advance(&mut self, forward: bool, window: &mut Window, cx: &mut Context<Self>) {
        if self.focus.is_focused(window) {
            self.stop_autoplay(cx);
            self.advance(forward, window, cx);
        } else {
            cx.propagate();
        }
    }
    fn key_edge(&mut self, last: bool, window: &mut Window, cx: &mut Context<Self>) {
        if !self.focus.is_focused(window) {
            cx.propagate();
            return;
        }
        self.stop_autoplay(cx);
        if !self.labels.is_empty() {
            self.select(if last { self.last_start() } else { 0 }, window, cx);
        }
    }
}
impl EventEmitter<CarouselEvent> for CarouselState {}

/// Named carousel region. Arrow keys act only while the region itself has focus,
/// leaving interactive slide descendants their own key bindings. Use native
/// Buttons or [`CarouselControl`] for pointer and keyboard previous/next actions.
#[derive(IntoElement)]
pub struct Carousel {
    base: crate::ObservedElement<gpui::Stateful<gpui::Div>>,
    state: Entity<CarouselState>,
    label: SharedString,
    axis: Axis,
    direction: Option<Direction>,
}
impl Carousel {
    pub fn new(
        id: impl Into<ElementId>,
        state: &Entity<CarouselState>,
        label: impl Into<SharedString>,
    ) -> Self {
        Self {
            base: div().id(id).test_support(),
            state: state.clone(),
            label: label.into(),
            axis: Axis::Horizontal,
            direction: None,
        }
    }
    pub fn axis(mut self, axis: Axis) -> Self {
        self.axis = axis;
        self
    }
    pub fn direction(mut self, direction: Direction) -> Self {
        self.direction = Some(direction);
        self
    }
}
impl Styled for Carousel {
    fn style(&mut self) -> &mut StyleRefinement {
        self.base.style()
    }
}
impl ParentElement for Carousel {
    fn extend(&mut self, children: impl IntoIterator<Item = AnyElement>) {
        self.base.extend(children);
    }
}
impl RenderOnce for Carousel {
    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
        let direction = self.direction.unwrap_or_else(|| Direction::current(cx));
        self.state.update(cx, |state, _| {
            state.axis = self.axis;
            state.direction = direction;
        });
        let focus = self.state.read(cx).focus.clone();
        let first = self.state.clone();
        let last = self.state.clone();
        let previous = self.state.clone();
        let hover = self.state.clone();
        let next = self.state;
        let base = self
            .base
            .role(Role::Region)
            .aria_label(self.label)
            .track_focus(&focus)
            .on_hover(move |hovered, _, cx| {
                hover.update(cx, |state, _| state.hovered = *hovered);
            })
            .on_action(move |_: &SelectFirst, window, cx| {
                first.update(cx, |state, cx| state.key_edge(false, window, cx));
            })
            .on_action(move |_: &SelectLast, window, cx| {
                last.update(cx, |state, cx| state.key_edge(true, window, cx));
            });
        match self.axis {
            Axis::Horizontal => base
                .key_context("Carousel axis=horizontal")
                .on_action(move |_: &SelectLeft, window, cx| {
                    previous.update(cx, |state, cx| {
                        state.key_advance(direction.horizontal_forward(false), window, cx)
                    });
                })
                .on_action(move |_: &SelectRight, window, cx| {
                    next.update(cx, |state, cx| {
                        state.key_advance(direction.horizontal_forward(true), window, cx)
                    });
                }),
            Axis::Vertical => base
                .key_context("Carousel axis=vertical")
                .on_action(move |_: &SelectUp, window, cx| {
                    previous.update(cx, |state, cx| state.key_advance(false, window, cx));
                })
                .on_action(move |_: &SelectDown, window, cx| {
                    next.update(cx, |state, cx| state.key_advance(true, window, cx));
                }),
        }
    }
}

#[derive(Clone)]
struct DragCarousel(EntityId);
impl Render for DragCarousel {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        Empty
    }
}

/// One slide, mounted only while in the current view, with position/count
/// metadata and a stable focus scope.
/// Dragging its content by 40 logical pixels snaps once per gesture. GPUI owns
/// drag cancellation and suppresses click activation after a drag begins.
/// Axis and direction come from the enclosing [`Carousel`]. There is no inertia or
/// autoplay; use application animation only when the product calls for it.
#[derive(IntoElement)]
pub struct CarouselSlide {
    base: crate::ObservedElement<gpui::Stateful<gpui::Div>>,
    state: Entity<CarouselState>,
    index: usize,
}
impl CarouselSlide {
    pub fn new(state: &Entity<CarouselState>, index: usize) -> Self {
        Self {
            base: div()
                .id((
                    ElementId::from(("carousel-slide", state.entity_id())),
                    index.to_string(),
                ))
                .test_support(),
            state: state.clone(),
            index,
        }
    }
}
impl Styled for CarouselSlide {
    fn style(&mut self) -> &mut StyleRefinement {
        self.base.style()
    }
}
impl ParentElement for CarouselSlide {
    fn extend(&mut self, children: impl IntoIterator<Item = AnyElement>) {
        self.base.extend(children);
    }
}
impl RenderOnce for CarouselSlide {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let model = self.state.read(cx);
        assert!(
            self.index < model.labels.len(),
            "CarouselSlide index must identify a slide"
        );
        if !model.visible_range().contains(&self.index) {
            return Empty.into_any_element();
        }
        let id = self.state.entity_id();
        let drag = self.state.clone();
        self.base
            .role(Role::Group)
            .aria_label(model.labels[self.index].clone())
            .aria_position_in_set(self.index + 1)
            .aria_size_of_set(model.labels.len())
            .track_focus(&model.slide_focus[self.index].clone().tab_stop(false))
            .overflow_hidden()
            .on_mouse_down(
                MouseButton::Left,
                window.listener_for(&self.state, |state, event: &gpui::MouseDownEvent, _, _| {
                    state.drag_origin = event.position;
                    state.drag_committed = false;
                }),
            )
            .on_drag(DragCarousel(id), move |value, _, window, cx| {
                // GPUI starts dragging after this move's capture phase, so the
                // first displacement does not reach on_drag_move. Handle it now.
                drag.update(cx, |state, cx| {
                    state.drag_committed = false;
                    state.swipe_at_pointer(window.mouse_position(), window, cx);
                });
                cx.stop_propagation();
                cx.new(|_| value.clone())
            })
            .on_drag_move(window.listener_for(
                &self.state,
                move |state, event: &DragMoveEvent<DragCarousel>, window, cx| {
                    if event.drag(cx).0 == id {
                        state.swipe_at_pointer(event.event.position, window, cx);
                    }
                },
            ))
            .into_any_element()
    }
}

/// Previous/next Button composition, including boundary disabled behavior.
#[derive(IntoElement)]
pub struct CarouselControl {
    button: Button,
    state: Entity<CarouselState>,
    forward: bool,
}
impl CarouselControl {
    pub fn previous(id: impl Into<ElementId>, state: &Entity<CarouselState>) -> Self {
        Self {
            button: Button::new(id).accessibility_label("Previous slide"),
            state: state.clone(),
            forward: false,
        }
    }
    pub fn next(id: impl Into<ElementId>, state: &Entity<CarouselState>) -> Self {
        Self {
            button: Button::new(id).accessibility_label("Next slide"),
            state: state.clone(),
            forward: true,
        }
    }
}
impl Styled for CarouselControl {
    fn style(&mut self) -> &mut StyleRefinement {
        self.button.style()
    }
}
impl ParentElement for CarouselControl {
    fn extend(&mut self, children: impl IntoIterator<Item = AnyElement>) {
        self.button.extend(children);
    }
}
impl RenderOnce for CarouselControl {
    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
        let enabled = if self.forward {
            self.state.read(cx).can_next()
        } else {
            self.state.read(cx).can_previous()
        };
        self.button
            .disabled(!enabled)
            .on_click(move |_, window, cx| {
                self.state.update(cx, |state, cx| {
                    state.stop_autoplay(cx);
                    state.advance(self.forward, window, cx);
                });
            })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Root;
    use gpui::{Modifiers, Subscription, TestAppContext, point};

    struct Harness {
        state: Entity<CarouselState>,
        button: FocusHandle,
        axis: Axis,
        direction: Direction,
        _subscription: Subscription,
    }
    impl Render for Harness {
        fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
            Carousel::new("carousel", &self.state, "Screenshots")
                .axis(self.axis)
                .direction(self.direction)
                .w(px(300.))
                .child(
                    CarouselSlide::new(&self.state, 0)
                        .w(px(300.))
                        .h(px(120.))
                        .child("First image"),
                )
                .child(
                    CarouselSlide::new(&self.state, 1)
                        .w(px(300.))
                        .h(px(120.))
                        .child(
                            Button::new("slide-action")
                                .track_focus(&self.button)
                                .child("Inspect image"),
                        ),
                )
                .child(CarouselControl::previous("previous", &self.state).child("Previous"))
                .child(CarouselControl::next("next", &self.state).child("Next"))
        }
    }
    #[gpui::test]
    fn keys_swipe_and_outgoing_slide_keep_focus_reachable(cx: &mut TestAppContext) {
        cx.update(crate::init);
        let (root, cx) = cx.add_window_view(|window, cx| {
            let view = cx.new(|cx| {
                let state = cx.new(|cx| CarouselState::new(["First", "Second"], cx));
                let subscription = cx.observe(&state, |_, _, cx| cx.notify());
                Harness {
                    state,
                    button: cx.focus_handle(),
                    axis: Axis::Horizontal,
                    direction: Direction::Ltr,
                    _subscription: subscription,
                }
            });
            Root::new(view, window, cx)
        });
        let view = root
            .read_with(cx, |root, _| root.view().clone())
            .downcast::<Harness>()
            .unwrap();
        cx.update(|window, cx| {
            window.activate_window();
            let focus = view.read(cx).state.read(cx).focus.clone();
            focus.focus(window, cx);
            window.draw(cx).clear(cx);
        });
        cx.simulate_keystrokes("right");
        cx.run_until_parked();
        cx.update(|window, cx| {
            let state = view.read(cx).state.clone();
            assert_eq!(state.read(cx).selected_index(), Some(1));
            assert!(!state.read(cx).can_next());
            let button = view.read(cx).button.clone();
            button.focus(window, cx);
            window.draw(cx).clear(cx);
            state.update(cx, |state, cx| {
                assert!(state.previous(window, cx));
                assert!(state.focus.is_focused(window));
            });
            window.draw(cx).clear(cx);
        });
        // A fast swipe can arrive as one coalesced move followed by release.
        cx.simulate_mouse_down(
            point(px(180.), px(50.)),
            MouseButton::Left,
            Modifiers::default(),
        );
        cx.simulate_mouse_move(
            point(px(130.), px(50.)),
            Some(MouseButton::Left),
            Modifiers::default(),
        );
        cx.simulate_mouse_up(
            point(px(130.), px(50.)),
            MouseButton::Left,
            Modifiers::default(),
        );
        cx.run_until_parked();
        cx.update(|_, cx| {
            assert_eq!(view.read(cx).state.read(cx).selected_index(), Some(1));
        });
        for (axis, direction, key, end) in [
            (
                Axis::Horizontal,
                Direction::Rtl,
                "left",
                point(px(240.), px(90.)),
            ),
            (
                Axis::Vertical,
                Direction::Rtl,
                "down",
                point(px(180.), px(40.)),
            ),
        ] {
            view.update(cx, |view, cx| {
                view.axis = axis;
                view.direction = direction;
                cx.notify();
            });
            cx.update(|window, cx| {
                view.read(cx).state.clone().update(cx, |state, cx| {
                    state.select(0, window, cx);
                    state.focus.focus(window, cx);
                });
                window.draw(cx).clear(cx);
            });
            cx.simulate_keystrokes(key);
            cx.update(|window, cx| {
                view.read(cx).state.clone().update(cx, |state, cx| {
                    assert_eq!(state.selected_index(), Some(1));
                    state.select(0, window, cx);
                });
                window.draw(cx).clear(cx);
            });
            // Slides have no separate axis/direction configuration to drift.
            cx.simulate_mouse_down(
                point(px(180.), px(90.)),
                MouseButton::Left,
                Modifiers::default(),
            );
            cx.simulate_mouse_move(end, Some(MouseButton::Left), Modifiers::default());
            // A second move must not advance again, even with looping enabled.
            cx.update(|_, cx| {
                view.read(cx)
                    .state
                    .clone()
                    .update(cx, |state, _| state.looping = true)
            });
            cx.simulate_mouse_move(end, Some(MouseButton::Left), Modifiers::default());
            cx.simulate_mouse_up(end, MouseButton::Left, Modifiers::default());
            cx.update(|_, cx| {
                view.read(cx).state.clone().update(cx, |state, _| {
                    assert_eq!(state.selected_index(), Some(1));
                    state.looping = false;
                })
            });
        }
        cx.update(|window, cx| {
            let state = view.read(cx).state.clone();
            assert_eq!(state.read(cx).selected_index(), Some(1));
            state.update(cx, |state, cx| {
                assert!(!state.next(window, cx));
                state.looping = true;
                assert!(state.next(window, cx));
                assert_eq!(state.selected, Some(0));
            });
        });
    }

    struct MultiHarness {
        state: Entity<CarouselState>,
        actions: Vec<FocusHandle>,
        _subscription: Subscription,
    }
    impl Render for MultiHarness {
        fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
            Carousel::new("carousel", &self.state, "Albums")
                .flex()
                .w(px(300.))
                .children(self.actions.iter().enumerate().map(|(index, action)| {
                    CarouselSlide::new(&self.state, index)
                        .w(px(100.))
                        .h(px(80.))
                        .child(
                            Button::new(("open", index))
                                .track_focus(action)
                                .child("Open"),
                        )
                }))
        }
    }

    #[gpui::test]
    fn several_slides_per_view_snap_by_one_and_release_hidden_focus(cx: &mut TestAppContext) {
        cx.update(crate::init);
        let (root, cx) = cx.add_window_view(|window, cx| {
            let view = cx.new(|cx| {
                let state = cx
                    .new(|cx| CarouselState::new(["A", "B", "C", "D", "E"], cx).slides_per_view(3));
                let subscription = cx.observe(&state, |_, _, cx| cx.notify());
                MultiHarness {
                    state,
                    actions: (0..5).map(|_| cx.focus_handle()).collect(),
                    _subscription: subscription,
                }
            });
            Root::new(view, window, cx)
        });
        let view = root
            .read_with(cx, |root, _| root.view().clone())
            .downcast::<MultiHarness>()
            .unwrap();
        let state = view.read_with(cx, |view, _| view.state.clone());
        let mounted = |cx: &mut gpui::VisualTestContext| {
            cx.update(|window, _| {
                let mut slides: Vec<_> = crate::test_support::snapshots(window)
                    .into_iter()
                    .filter(|node| node.role() == Some(Role::Group))
                    .map(|node| (node.bounds().left(), node.label().unwrap().to_owned()))
                    .collect();
                slides.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());
                slides
                    .into_iter()
                    .map(|(_, label)| label)
                    .collect::<Vec<_>>()
            })
        };
        cx.update(|window, cx| {
            window.activate_window();
            state.read(cx).focus.clone().focus(window, cx);
            window.draw(cx).clear(cx);
        });
        assert_eq!(mounted(cx), ["A", "B", "C"]);
        cx.update(|_, cx| {
            let state = state.read(cx);
            assert_eq!(state.snap_count(), 3);
            assert!(!state.can_previous());
        });
        cx.simulate_keystrokes("end");
        cx.run_until_parked();
        assert_eq!(mounted(cx), ["C", "D", "E"], "the last view stays full");
        cx.update(|window, cx| {
            assert!(!state.read(cx).can_next());
            // Focus in a slide that stays visible is kept.
            let action = view.read(cx).actions[3].clone();
            action.focus(window, cx);
            state.update(cx, |state, cx| assert!(state.previous(window, cx)));
            assert!(action.is_focused(window));
            assert_eq!(state.read(cx).visible_range(), 1..4);
            // Focus in a slide that leaves the view returns to the carousel.
            state.update(cx, |state, cx| assert!(state.previous(window, cx)));
            assert!(state.read(cx).focus.is_focused(window));
            state.update(cx, |state, cx| {
                state.looping = true;
                assert!(
                    state.previous(window, cx),
                    "looping wraps to the last full view"
                );
                assert_eq!(state.selected_index(), Some(2));
                assert!(state.set_slides_per_view(4, window, cx));
                assert_eq!(
                    state.visible_range(),
                    1..5,
                    "a larger view keeps the last one full"
                );
            });
            window.draw(cx).clear(cx);
        });
        assert_eq!(mounted(cx), ["B", "C", "D", "E"]);
    }

    #[gpui::test]
    fn autoplay_wraps_pauses_for_hover_focus_and_motion_and_stops_on_navigation(
        cx: &mut TestAppContext,
    ) {
        cx.update(crate::init);
        let (root, cx) = cx.add_window_view(|window, cx| {
            let view = cx.new(|cx| {
                let state = cx
                    .new(|cx| CarouselState::new(["A", "B", "C", "D", "E"], cx).slides_per_view(3));
                let subscription = cx.observe(&state, |_, _, cx| cx.notify());
                MultiHarness {
                    state,
                    actions: (0..5).map(|_| cx.focus_handle()).collect(),
                    _subscription: subscription,
                }
            });
            Root::new(view, window, cx)
        });
        let state = root
            .read_with(cx, |root, _| root.view().clone())
            .downcast::<MultiHarness>()
            .unwrap()
            .read_with(cx, |view, _| view.state.clone());
        cx.update(|window, cx| {
            window.activate_window();
            window.draw(cx).clear(cx);
            state.update(cx, |state, cx| {
                state.set_autoplay(Some(Duration::from_secs(1)), window, cx)
            });
        });
        let tick = |cx: &mut gpui::VisualTestContext| {
            cx.executor().advance_clock(Duration::from_secs(1));
            cx.run_until_parked();
            cx.update(|window, cx| {
                window.draw(cx).clear(cx);
                state.read(cx).selected_index()
            })
        };
        // The test pointer rests at the window origin, over the carousel, and
        // the region reported that hover when it was drawn.
        cx.update(|_, cx| assert!(state.read(cx).hovered));
        cx.update(|_, cx| state.update(cx, |state, _| state.hovered = false));
        assert_eq!(tick(cx), Some(1));
        assert_eq!(tick(cx), Some(2));
        assert_eq!(tick(cx), Some(0), "the last full view wraps to the first");
        cx.update(|_, cx| state.update(cx, |state, _| state.hovered = true));
        assert_eq!(tick(cx), Some(0), "hovering pauses rotation");
        cx.update(|_, cx| state.update(cx, |state, _| state.hovered = false));
        assert_eq!(tick(cx), Some(1));
        cx.update(|_, cx| cx.set_reduce_motion(true));
        assert_eq!(tick(cx), Some(1), "reduced motion pauses rotation");
        cx.update(|_, cx| cx.set_reduce_motion(false));
        cx.update(|window, cx| state.read(cx).focus.clone().focus(window, cx));
        assert_eq!(tick(cx), Some(1), "focus inside pauses rotation");
        cx.simulate_keystrokes("right");
        cx.update(|_, cx| {
            let state = state.read(cx);
            assert_eq!(state.selected_index(), Some(2));
            assert!(
                !state.is_autoplaying(),
                "keyboard navigation stops rotation"
            );
        });
        cx.update(|window, cx| window.blur(cx));
        assert_eq!(tick(cx), Some(2));
    }
}