gpui_material_components/
slider.rs1mod geometry;
2mod render;
3
4use gpui::{
5 AnyElement, App, Axis, ElementId, Entity, Hsla, IntoElement, Pixels, Point, SharedString, Size,
6 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>,
19 pub outer_radius: Option<Pixels>,
20 pub inner_radius: Option<Pixels>,
21 pub thumb_gap: Option<Pixels>,
22}
23
24#[derive(Clone, Copy, Debug, Default, PartialEq)]
26pub struct SliderThumbStyle {
27 pub size: Option<Size<Pixels>>,
29 pub pressed_width: Option<Pixels>,
31 pub color: Option<Hsla>,
32 pub disabled_color: Option<Hsla>,
33 pub focused_color: Option<Hsla>,
34}
35
36#[derive(Clone, Copy, Debug, Default, PartialEq)]
38pub struct SliderColors {
39 pub thumb: Option<Hsla>,
40 pub active_track: Option<Hsla>,
41 pub inactive_track: Option<Hsla>,
42 pub active_tick: Option<Hsla>,
43 pub inactive_tick: Option<Hsla>,
44 pub disabled_thumb: Option<Hsla>,
45 pub disabled_active_track: Option<Hsla>,
46 pub disabled_inactive_track: Option<Hsla>,
47 pub disabled_active_tick: Option<Hsla>,
48 pub disabled_inactive_tick: Option<Hsla>,
49}
50
51#[derive(Clone, Copy, Debug, PartialEq)]
53#[non_exhaustive]
54pub struct SliderMarker {
55 pub center: Point<Pixels>,
56 pub diameter: Pixels,
57 pub color: Hsla,
58}
59
60type DrawMarker = Box<dyn Fn(SliderMarker, &mut Window, &mut App)>;
61type FormatValue = Box<dyn Fn(f32) -> SharedString>;
62
63#[derive(Default)]
64struct ThumbOptions {
65 style: SliderThumbStyle,
66 content: Option<AnyElement>,
67 source: Option<Entity<InteractionSource>>,
68}
69
70fn thumb_index(which: RangeThumb) -> usize {
71 usize::from(which == RangeThumb::End)
72}
73
74fn thumb_id(id: &ElementId, which: RangeThumb) -> ElementId {
75 (
76 id.clone(),
77 if which == RangeThumb::Start {
78 "start"
79 } else {
80 "end"
81 },
82 )
83 .into()
84}
85
86#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
87pub enum SliderKind {
88 #[default]
89 Standard,
90 Centered,
91}
92
93#[derive(IntoElement)]
96pub struct Slider {
97 id: ElementId,
98 name: SharedString,
99 state: Entity<SliderState>,
100 kind: SliderKind,
101 axis: Axis,
102 reverse_vertical: bool,
103 size: ControlSize,
104 disabled: bool,
105 icon: Option<AnyElement>,
106 tick_intervals: u16,
107 range_end_name: Option<SharedString>,
108 track_style: SliderTrackStyle,
109 track: Option<AnyElement>,
110 thumbs: [ThumbOptions; 2],
111 colors: SliderColors,
112 draw_tick: Option<DrawMarker>,
113 draw_stop: Option<DrawMarker>,
114 show_stop_indicators: bool,
115 value_indicator: bool,
116 format_value: Option<FormatValue>,
117}
118
119impl Slider {
120 pub fn new(
121 id: impl Into<ElementId>,
122 name: impl Into<SharedString>,
123 state: &Entity<SliderState>,
124 ) -> Self {
125 Self {
126 id: id.into(),
127 name: name.into(),
128 state: state.clone(),
129 kind: SliderKind::Standard,
130 axis: Axis::Horizontal,
131 reverse_vertical: false,
132 size: ControlSize::ExtraSmall,
133 disabled: false,
134 icon: None,
135 tick_intervals: 0,
136 range_end_name: None,
137 track_style: SliderTrackStyle::default(),
138 track: None,
139 thumbs: Default::default(),
140 colors: SliderColors::default(),
141 draw_tick: None,
142 draw_stop: None,
143 show_stop_indicators: true,
144 value_indicator: true,
145 format_value: None,
146 }
147 }
148
149 pub fn range(
151 id: impl Into<ElementId>,
152 start_name: impl Into<SharedString>,
153 end_name: impl Into<SharedString>,
154 state: &Entity<SliderState>,
155 ) -> Self {
156 let mut slider = Self::new(id, start_name, state);
157 slider.range_end_name = Some(end_name.into());
158 slider
159 }
160
161 pub fn kind(mut self, kind: SliderKind) -> Self {
162 self.kind = kind;
163 self
164 }
165
166 pub fn axis(mut self, axis: Axis) -> Self {
167 self.axis = axis;
168 self
169 }
170
171 pub fn reverse_vertical(mut self, reverse: bool) -> Self {
173 self.reverse_vertical = reverse;
174 self
175 }
176
177 pub fn size(mut self, size: ControlSize) -> Self {
179 self.size = size;
180 self
181 }
182
183 pub fn disabled(mut self, disabled: bool) -> Self {
184 self.disabled = disabled;
185 self
186 }
187
188 pub fn tick_intervals(mut self, intervals: u16) -> Self {
191 self.tick_intervals = intervals;
192 self
193 }
194
195 pub fn icon(mut self, icon: impl IntoElement) -> Self {
196 self.icon = Some(icon.into_any_element());
197 self
198 }
199
200 pub fn track(mut self, element: impl IntoElement) -> Self {
202 self.track = Some(element.into_any_element());
203 self
204 }
205
206 pub fn track_style(mut self, style: SliderTrackStyle) -> Self {
208 self.track_style = style;
209 self
210 }
211
212 pub fn thumb(mut self, which: RangeThumb, element: impl IntoElement) -> Self {
214 self.thumbs[thumb_index(which)].content = Some(element.into_any_element());
215 self
216 }
217
218 pub fn thumb_style(mut self, which: RangeThumb, style: SliderThumbStyle) -> Self {
220 self.thumbs[thumb_index(which)].style = style;
221 self
222 }
223
224 pub fn colors(mut self, colors: SliderColors) -> Self {
225 self.colors = colors;
226 self
227 }
228
229 pub fn interaction_source(
231 mut self,
232 which: RangeThumb,
233 source: &Entity<InteractionSource>,
234 ) -> Self {
235 self.thumbs[thumb_index(which)].source = Some(source.clone());
236 self
237 }
238
239 pub fn draw_ticks(
241 mut self,
242 draw: impl Fn(SliderMarker, &mut Window, &mut App) + 'static,
243 ) -> Self {
244 self.draw_tick = Some(Box::new(draw));
245 self
246 }
247
248 pub fn draw_stop_indicators(
250 mut self,
251 draw: impl Fn(SliderMarker, &mut Window, &mut App) + 'static,
252 ) -> Self {
253 self.draw_stop = Some(Box::new(draw));
254 self
255 }
256
257 pub fn show_stop_indicators(mut self, show: bool) -> Self {
258 self.show_stop_indicators = show;
259 self
260 }
261
262 pub fn value_indicator(mut self, show: bool) -> Self {
264 self.value_indicator = show;
265 self
266 }
267
268 pub fn format_value(mut self, format: impl Fn(f32) -> SharedString + 'static) -> Self {
270 self.format_value = Some(Box::new(format));
271 self
272 }
273}