gpui_material_components/
slider.rs1mod geometry;
2mod render;
3
4use gpui::{
5 AnyElement, App, Axis, ElementId, Entity, FocusHandle, Hsla, IntoElement, Pixels, Point,
6 SharedString, Size, Window,
7};
8use gpui_material_foundation::InteractionSource;
9
10use crate::ControlSize;
11
12pub use gpui_base::slider::{SliderEvent, SliderScale, SliderState, SliderValue};
13pub use gpui_material_foundation::RangeThumb;
14
15#[derive(Clone, Copy, Debug, Default, PartialEq)]
17pub struct SliderTrackStyle {
18 pub thickness: Option<Pixels>,
20 pub outer_radius: Option<Pixels>,
22 pub inner_radius: Option<Pixels>,
24 pub thumb_gap: Option<Pixels>,
26}
27
28#[derive(Clone, Copy, Debug, Default, PartialEq)]
30pub struct SliderThumbStyle {
31 pub size: Option<Size<Pixels>>,
33 pub pressed_width: Option<Pixels>,
35 pub color: Option<Hsla>,
37 pub disabled_color: Option<Hsla>,
39 pub focused_color: Option<Hsla>,
41}
42
43#[derive(Clone, Copy, Debug, Default, PartialEq)]
45pub struct SliderColors {
46 pub thumb: Option<Hsla>,
48 pub active_track: Option<Hsla>,
50 pub inactive_track: Option<Hsla>,
52 pub active_tick: Option<Hsla>,
54 pub inactive_tick: Option<Hsla>,
56 pub disabled_thumb: Option<Hsla>,
58 pub disabled_active_track: Option<Hsla>,
60 pub disabled_inactive_track: Option<Hsla>,
62 pub disabled_active_tick: Option<Hsla>,
64 pub disabled_inactive_tick: Option<Hsla>,
66}
67
68#[derive(Clone, Copy, Debug, PartialEq)]
70#[non_exhaustive]
71pub struct SliderMarker {
72 pub center: Point<Pixels>,
74 pub diameter: Pixels,
76 pub color: Hsla,
78}
79
80type DrawMarker = Box<dyn Fn(SliderMarker, &mut Window, &mut App)>;
81type FormatValue = Box<dyn Fn(f32) -> SharedString>;
82
83#[derive(Default)]
84struct ThumbOptions {
85 style: SliderThumbStyle,
86 content: Option<AnyElement>,
87 focus: Option<FocusHandle>,
88 source: Option<Entity<InteractionSource>>,
89 source_key: Option<ElementId>,
90}
91
92fn thumb_index(which: RangeThumb) -> usize {
93 usize::from(which == RangeThumb::End)
94}
95
96fn thumb_id(id: &ElementId, which: RangeThumb) -> ElementId {
97 (
98 id.clone(),
99 if which == RangeThumb::Start {
100 "start"
101 } else {
102 "end"
103 },
104 )
105 .into()
106}
107
108#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
110pub enum SliderKind {
111 #[default]
113 Standard,
114 Centered,
116}
117
118#[derive(IntoElement)]
121pub struct Slider {
122 id: ElementId,
123 name: SharedString,
124 state: Entity<SliderState>,
125 kind: SliderKind,
126 axis: Axis,
127 reverse_vertical: bool,
128 size: ControlSize,
129 disabled: bool,
130 icon: Option<AnyElement>,
131 tick_intervals: u16,
132 range_end_name: Option<SharedString>,
133 track_style: SliderTrackStyle,
134 track: Option<AnyElement>,
135 thumbs: [ThumbOptions; 2],
136 colors: SliderColors,
137 draw_tick: Option<DrawMarker>,
138 draw_stop: Option<DrawMarker>,
139 show_stop_indicators: bool,
140 value_indicator: bool,
141 format_value: Option<FormatValue>,
142}
143
144impl Slider {
145 pub fn new(
147 id: impl Into<ElementId>,
148 name: impl Into<SharedString>,
149 state: &Entity<SliderState>,
150 ) -> Self {
151 Self {
152 id: id.into(),
153 name: name.into(),
154 state: state.clone(),
155 kind: SliderKind::Standard,
156 axis: Axis::Horizontal,
157 reverse_vertical: false,
158 size: ControlSize::ExtraSmall,
159 disabled: false,
160 icon: None,
161 tick_intervals: 0,
162 range_end_name: None,
163 track_style: SliderTrackStyle::default(),
164 track: None,
165 thumbs: Default::default(),
166 colors: SliderColors::default(),
167 draw_tick: None,
168 draw_stop: None,
169 show_stop_indicators: true,
170 value_indicator: true,
171 format_value: None,
172 }
173 }
174
175 pub fn range(
177 id: impl Into<ElementId>,
178 start_name: impl Into<SharedString>,
179 end_name: impl Into<SharedString>,
180 state: &Entity<SliderState>,
181 ) -> Self {
182 let mut slider = Self::new(id, start_name, state);
183 slider.range_end_name = Some(end_name.into());
184 slider
185 }
186
187 pub fn kind(mut self, kind: SliderKind) -> Self {
189 self.kind = kind;
190 self
191 }
192
193 pub fn axis(mut self, axis: Axis) -> Self {
195 self.axis = axis;
196 self
197 }
198
199 pub fn reverse_vertical(mut self, reverse: bool) -> Self {
201 self.reverse_vertical = reverse;
202 self
203 }
204
205 pub fn size(mut self, size: ControlSize) -> Self {
207 self.size = size;
208 self
209 }
210
211 pub fn disabled(mut self, disabled: bool) -> Self {
213 self.disabled = disabled;
214 self
215 }
216
217 pub fn tick_intervals(mut self, intervals: u16) -> Self {
220 self.tick_intervals = intervals;
221 self
222 }
223
224 pub fn icon(mut self, icon: impl IntoElement) -> Self {
226 self.icon = Some(icon.into_any_element());
227 self
228 }
229
230 pub fn track(mut self, element: impl IntoElement) -> Self {
232 self.track = Some(element.into_any_element());
233 self
234 }
235
236 pub fn track_style(mut self, style: SliderTrackStyle) -> Self {
238 self.track_style = style;
239 self
240 }
241
242 pub fn thumb(mut self, which: RangeThumb, element: impl IntoElement) -> Self {
244 self.thumbs[thumb_index(which)].content = Some(element.into_any_element());
245 self
246 }
247
248 pub fn thumb_style(mut self, which: RangeThumb, style: SliderThumbStyle) -> Self {
250 self.thumbs[thumb_index(which)].style = style;
251 self
252 }
253
254 pub fn colors(mut self, colors: SliderColors) -> Self {
256 self.colors = colors;
257 self
258 }
259
260 pub fn track_focus(mut self, which: RangeThumb, focus: &FocusHandle) -> Self {
263 self.thumbs[thumb_index(which)].focus = Some(focus.clone());
264 self
265 }
266
267 pub fn interaction_source(
269 mut self,
270 which: RangeThumb,
271 source: &Entity<InteractionSource>,
272 ) -> Self {
273 let entry = &mut self.thumbs[thumb_index(which)];
274 entry.source = Some(source.clone());
275 entry.source_key = None;
276 self
277 }
278
279 #[cfg(feature = "surfaces")]
280 pub(crate) fn interaction_source_with_id(
281 mut self,
282 which: RangeThumb,
283 id: ElementId,
284 source: &Entity<InteractionSource>,
285 ) -> Self {
286 let entry = &mut self.thumbs[thumb_index(which)];
287 entry.source = Some(source.clone());
288 entry.source_key = Some(id);
289 self
290 }
291
292 pub fn draw_ticks(
294 mut self,
295 draw: impl Fn(SliderMarker, &mut Window, &mut App) + 'static,
296 ) -> Self {
297 self.draw_tick = Some(Box::new(draw));
298 self
299 }
300
301 pub fn draw_stop_indicators(
303 mut self,
304 draw: impl Fn(SliderMarker, &mut Window, &mut App) + 'static,
305 ) -> Self {
306 self.draw_stop = Some(Box::new(draw));
307 self
308 }
309
310 pub fn show_stop_indicators(mut self, show: bool) -> Self {
312 self.show_stop_indicators = show;
313 self
314 }
315
316 pub fn value_indicator(mut self, show: bool) -> Self {
318 self.value_indicator = show;
319 self
320 }
321
322 pub fn format_value(mut self, format: impl Fn(f32) -> SharedString + 'static) -> Self {
324 self.format_value = Some(Box::new(format));
325 self
326 }
327}