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
mod geometry;
mod render;
use gpui::{
AnyElement, App, Axis, ElementId, Entity, FocusHandle, Hsla, IntoElement, Pixels, Point,
SharedString, Size, Window,
};
use gpui_material_foundation::InteractionSource;
use crate::ControlSize;
pub use gpui_base::slider::{SliderEvent, SliderScale, SliderState, SliderValue};
pub use gpui_material_foundation::RangeThumb;
/// Optional track dimensions in logical pixels.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct SliderTrackStyle {
/// Cross-axis thickness of the slider track.
pub thickness: Option<Pixels>,
/// Corner radius at the outer ends of the track.
pub outer_radius: Option<Pixels>,
/// Corner radius at the inner track edges adjacent to the thumb gap.
pub inner_radius: Option<Pixels>,
/// Gap between the thumb handle and the active/inactive track segments.
pub thumb_gap: Option<Pixels>,
}
/// Optional dimensions and colors for one handle.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct SliderThumbStyle {
/// The first dimension lies along the slider axis, the second across it.
pub size: Option<Size<Pixels>>,
/// The dimension along the slider axis while the handle is pressed.
pub pressed_width: Option<Pixels>,
/// Handle fill color when enabled.
pub color: Option<Hsla>,
/// Handle fill color when disabled.
pub disabled_color: Option<Hsla>,
/// Handle fill color when keyboard focus is visible.
pub focused_color: Option<Hsla>,
}
/// Optional slider colors. Unspecified fields use the window theme.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct SliderColors {
/// Enabled handle fill color.
pub thumb: Option<Hsla>,
/// Enabled active track fill color.
pub active_track: Option<Hsla>,
/// Enabled inactive track fill color.
pub inactive_track: Option<Hsla>,
/// Enabled tick and stop marker color on the active track segment.
pub active_tick: Option<Hsla>,
/// Enabled tick and stop marker color on the inactive track segment.
pub inactive_tick: Option<Hsla>,
/// Disabled handle fill color.
pub disabled_thumb: Option<Hsla>,
/// Disabled active track fill color.
pub disabled_active_track: Option<Hsla>,
/// Disabled inactive track fill color.
pub disabled_inactive_track: Option<Hsla>,
/// Disabled tick and stop marker color on the active track segment.
pub disabled_active_tick: Option<Hsla>,
/// Disabled tick and stop marker color on the inactive track segment.
pub disabled_inactive_tick: Option<Hsla>,
}
/// The window position and default paint of a track tick or stop.
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct SliderMarker {
/// Center point of the marker in window coordinates.
pub center: Point<Pixels>,
/// Default marker diameter in logical pixels.
pub diameter: Pixels,
/// Resolved marker color for the active or inactive track segment.
pub color: Hsla,
}
type DrawMarker = Box<dyn Fn(SliderMarker, &mut Window, &mut App)>;
type FormatValue = Box<dyn Fn(f32) -> SharedString>;
#[derive(Default)]
struct ThumbOptions {
style: SliderThumbStyle,
content: Option<AnyElement>,
focus: Option<FocusHandle>,
source: Option<Entity<InteractionSource>>,
source_key: Option<ElementId>,
}
fn thumb_index(which: RangeThumb) -> usize {
usize::from(which == RangeThumb::End)
}
fn thumb_id(id: &ElementId, which: RangeThumb) -> ElementId {
(
id.clone(),
if which == RangeThumb::Start {
"start"
} else {
"end"
},
)
.into()
}
/// Origin of the active track fill for a single-value [`Slider`].
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum SliderKind {
/// Active track extends from the start of the range to the handle (default).
#[default]
Standard,
/// Active track extends symmetrically from the midpoint of the range to the handle.
Centered,
}
/// An Expressive slider. Base owns the value and step grid. The caller retains
/// the state entity and observes its SliderEvent notifications.
#[derive(IntoElement)]
pub struct Slider {
id: ElementId,
name: SharedString,
state: Entity<SliderState>,
kind: SliderKind,
axis: Axis,
reverse_vertical: bool,
size: ControlSize,
disabled: bool,
icon: Option<AnyElement>,
tick_intervals: u16,
range_end_name: Option<SharedString>,
track_style: SliderTrackStyle,
track: Option<AnyElement>,
thumbs: [ThumbOptions; 2],
colors: SliderColors,
draw_tick: Option<DrawMarker>,
draw_stop: Option<DrawMarker>,
show_stop_indicators: bool,
value_indicator: bool,
format_value: Option<FormatValue>,
}
impl Slider {
/// Creates a horizontal single-value slider bound to `state` with an element ID and accessible name.
pub fn new(
id: impl Into<ElementId>,
name: impl Into<SharedString>,
state: &Entity<SliderState>,
) -> Self {
Self {
id: id.into(),
name: name.into(),
state: state.clone(),
kind: SliderKind::Standard,
axis: Axis::Horizontal,
reverse_vertical: false,
size: ControlSize::ExtraSmall,
disabled: false,
icon: None,
tick_intervals: 0,
range_end_name: None,
track_style: SliderTrackStyle::default(),
track: None,
thumbs: Default::default(),
colors: SliderColors::default(),
draw_tick: None,
draw_stop: None,
show_stop_indicators: true,
value_indicator: true,
format_value: None,
}
}
/// Supplies the two accessible names of a slider with a Base range value.
pub fn range(
id: impl Into<ElementId>,
start_name: impl Into<SharedString>,
end_name: impl Into<SharedString>,
state: &Entity<SliderState>,
) -> Self {
let mut slider = Self::new(id, start_name, state);
slider.range_end_name = Some(end_name.into());
slider
}
/// Selects standard start-anchored or centered active track geometry.
pub fn kind(mut self, kind: SliderKind) -> Self {
self.kind = kind;
self
}
/// Sets the slider orientation (`Axis::Horizontal` by default).
pub fn axis(mut self, axis: Axis) -> Self {
self.axis = axis;
self
}
/// Increases vertical sliders from top to bottom. Horizontal sliders follow the window direction.
pub fn reverse_vertical(mut self, reverse: bool) -> Self {
self.reverse_vertical = reverse;
self
}
/// Selects the expressive track size. ExtraSmall (16 dp) is the default.
pub fn size(mut self, size: ControlSize) -> Self {
self.size = size;
self
}
/// Disables pointer dragging, keyboard adjustment, and focus traversal when `true`.
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
/// Draws equally spaced ticks in the visual range without changing the Base value scale.
/// Pass the number of intervals, including the two endpoints.
pub fn tick_intervals(mut self, intervals: u16) -> Self {
self.tick_intervals = intervals;
self
}
/// Places an optional decorative icon inside the active track on medium and larger sizes.
pub fn icon(mut self, icon: impl IntoElement) -> Self {
self.icon = Some(icon.into_any_element());
self
}
/// Replaces the built-in track, icon, and marker artwork. The slider retains its input region.
pub fn track(mut self, element: impl IntoElement) -> Self {
self.track = Some(element.into_any_element());
self
}
/// Sets the built-in track dimensions. Thickness also sizes a custom track's slot.
pub fn track_style(mut self, style: SliderTrackStyle) -> Self {
self.track_style = style;
self
}
/// Replaces the handle artwork. Use `RangeThumb::End` for a single value.
pub fn thumb(mut self, which: RangeThumb, element: impl IntoElement) -> Self {
self.thumbs[thumb_index(which)].content = Some(element.into_any_element());
self
}
/// Sets per-handle size and colors. Use `RangeThumb::End` for a single value.
pub fn thumb_style(mut self, which: RangeThumb, style: SliderThumbStyle) -> Self {
self.thumbs[thumb_index(which)].style = style;
self
}
/// Overrides enabled and disabled track, thumb, and tick colors.
pub fn colors(mut self, colors: SliderColors) -> Self {
self.colors = colors;
self
}
/// Uses the caller's focus handle for one handle instead of an internal keyed handle.
/// Use `RangeThumb::End` for a single-value slider.
pub fn track_focus(mut self, which: RangeThumb, focus: &FocusHandle) -> Self {
self.thumbs[thumb_index(which)].focus = Some(focus.clone());
self
}
/// Observes one handle under the slider ID paired with `"start"` or `"end"`.
pub fn interaction_source(
mut self,
which: RangeThumb,
source: &Entity<InteractionSource>,
) -> Self {
let entry = &mut self.thumbs[thumb_index(which)];
entry.source = Some(source.clone());
entry.source_key = None;
self
}
#[cfg(feature = "surfaces")]
pub(crate) fn interaction_source_with_id(
mut self,
which: RangeThumb,
id: ElementId,
source: &Entity<InteractionSource>,
) -> Self {
let entry = &mut self.thumbs[thumb_index(which)];
entry.source = Some(source.clone());
entry.source_key = Some(id);
self
}
/// Replaces the default dot painter for visual ticks.
pub fn draw_ticks(
mut self,
draw: impl Fn(SliderMarker, &mut Window, &mut App) + 'static,
) -> Self {
self.draw_tick = Some(Box::new(draw));
self
}
/// Replaces the default dot painter for track stops.
pub fn draw_stop_indicators(
mut self,
draw: impl Fn(SliderMarker, &mut Window, &mut App) + 'static,
) -> Self {
self.draw_stop = Some(Box::new(draw));
self
}
/// Controls whether endpoint stop dots are drawn on the track. Defaults to `true`.
pub fn show_stop_indicators(mut self, show: bool) -> Self {
self.show_stop_indicators = show;
self
}
/// Enables the value indicator during dragging and keyboard focus. It is enabled by default.
pub fn value_indicator(mut self, show: bool) -> Self {
self.value_indicator = show;
self
}
/// Formats the current value of the handle whose indicator is visible.
pub fn format_value(mut self, format: impl Fn(f32) -> SharedString + 'static) -> Self {
self.format_value = Some(Box::new(format));
self
}
}