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
//! Horizontal carousel with next/prev controls and drag-to-snap.
//!
//! Snap uses instant positioning on drag-release (no inertia physics).
use gpui::{Context, Empty, Hsla, Render};
use crate::prelude::*;
/// Drag payload for carousel pointer tracking.
#[derive(Debug)]
struct CarouselDrag {
start_x: Pixels,
}
/// Horizontal carousel with next/prev navigation and drag-to-snap.
///
/// Create with `cx.new(|_| Carousel::new(slides))`.
pub struct Carousel {
slides: Vec<(SharedString, Hsla)>,
active_index: usize,
drag_offset: Pixels,
}
impl Carousel {
pub fn new(slides: impl IntoIterator<Item = (impl Into<SharedString>, Hsla)>) -> Self {
Self {
slides: slides
.into_iter()
.map(|(label, color)| (label.into(), color))
.collect(),
active_index: 0,
drag_offset: px(0.),
}
}
pub fn active_index(&self) -> usize {
self.active_index
}
fn snap_to_nearest(&mut self, cx: &mut Context<Self>) {
if self.slides.is_empty() {
return;
}
let threshold = px(48.);
if self.drag_offset < -threshold && self.active_index + 1 < self.slides.len() {
self.active_index += 1;
} else if self.drag_offset > threshold && self.active_index > 0 {
self.active_index -= 1;
}
self.drag_offset = px(0.);
cx.notify();
}
}
impl Render for Carousel {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let count = self.slides.len();
let active = self.active_index.min(count.saturating_sub(1));
let drag_offset = self.drag_offset;
let (label, color) = self
.slides
.get(active)
.cloned()
.unwrap_or_else(|| ("No items".into(), palette::neutral(100)));
let track = div()
.id("carousel-track")
.w_full()
.h(px(200.))
.overflow_hidden()
.cursor_pointer()
.on_drag(
CarouselDrag {
start_x: window.mouse_position().x,
},
|_, _, _, cx| cx.new(|_| Empty),
)
.on_drag_move::<CarouselDrag>(cx.listener(
|this, event: &gpui::DragMoveEvent<CarouselDrag>, _, cx| {
let drag = event.drag(cx);
this.drag_offset = event.event.position.x - drag.start_x;
cx.notify();
},
))
.on_drop::<CarouselDrag>(cx.listener(|this, _, _, cx| {
this.snap_to_nearest(cx);
}))
.child(
div()
.w_full()
.h_full()
.ml(drag_offset)
.flex()
.items_center()
.justify_center()
.bg(color)
.child(Headline::new(label).size(HeadlineSize::Small)),
);
let prev_disabled = active == 0;
let next_disabled = active + 1 >= count;
v_flex()
.id("carousel")
.w_full()
.max_w(px(480.))
.gap_3()
.child(track)
.child(
h_flex()
.items_center()
.justify_between()
.child(
// Wrapping `div` only exists so integration tests can
// locate the prev button's real rendered pixel bounds
// via `VisualTestContext::debug_bounds` (test-only,
// no-op in release builds — mirrors the
// `ActionPanel`/`Tab` `debug_selector` precedent).
// `IconButton::new` already sets its own
// `"ICON-{icon:?}"` debug_selector, but `Calendar`'s
// prev/next buttons use the same `ChevronLeft`/
// `ChevronRight` icons and both components can render
// simultaneously on the gallery's Layout page, so a
// distinct selector is needed to avoid ambiguity. The
// wrapping div does not intercept the click: the
// inner `IconButton` still owns the only click
// handler.
div().debug_selector(|| "CAROUSEL-PREV".into()).child(
IconButton::new("carousel-prev", IconName::ChevronLeft)
.disabled(prev_disabled)
.when(!prev_disabled, |this| {
this.on_click(cx.listener(|this, _, _, cx| {
if this.active_index > 0 {
this.active_index -= 1;
this.drag_offset = px(0.);
cx.notify();
}
}))
}),
),
)
.child(
Label::new(format!("{} / {}", active + 1, count.max(1)))
.size(LabelSize::Small)
.color(Color::Muted),
)
.child(
// See the prev button wrapper's comment above; same
// rationale.
div().debug_selector(|| "CAROUSEL-NEXT".into()).child(
IconButton::new("carousel-next", IconName::ChevronRight)
.disabled(next_disabled)
.when(!next_disabled, |this| {
this.on_click(cx.listener(|this, _, _, cx| {
if this.active_index + 1 < this.slides.len() {
this.active_index += 1;
this.drag_offset = px(0.);
cx.notify();
}
}))
}),
),
),
)
}
}
/// Gallery catalog entry for [`Carousel`].
#[derive(IntoElement, RegisterComponent)]
pub struct CarouselPreview;
impl RenderOnce for CarouselPreview {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
cx.new(|_| {
Carousel::new([
("Slide 1", palette::primary(100)),
("Slide 2", palette::success(100)),
("Slide 3", palette::warning(100)),
])
})
}
}
impl Component for CarouselPreview {
fn scope() -> ComponentScope {
ComponentScope::DataDisplay
}
fn description() -> Option<&'static str> {
Some("Horizontal carousel with next/prev navigation and instant drag-snap.")
}
fn preview(window: &mut Window, cx: &mut App) -> Option<AnyElement> {
CarouselPreview.render(window, cx).into_any_element().into()
}
}