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;
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct SliderTrackStyle {
pub thickness: Option<Pixels>,
pub outer_radius: Option<Pixels>,
pub inner_radius: Option<Pixels>,
pub thumb_gap: Option<Pixels>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct SliderThumbStyle {
pub size: Option<Size<Pixels>>,
pub pressed_width: Option<Pixels>,
pub color: Option<Hsla>,
pub disabled_color: Option<Hsla>,
pub focused_color: Option<Hsla>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct SliderColors {
pub thumb: Option<Hsla>,
pub active_track: Option<Hsla>,
pub inactive_track: Option<Hsla>,
pub active_tick: Option<Hsla>,
pub inactive_tick: Option<Hsla>,
pub disabled_thumb: Option<Hsla>,
pub disabled_active_track: Option<Hsla>,
pub disabled_inactive_track: Option<Hsla>,
pub disabled_active_tick: Option<Hsla>,
pub disabled_inactive_tick: Option<Hsla>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct SliderMarker {
pub center: Point<Pixels>,
pub diameter: Pixels,
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()
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum SliderKind {
#[default]
Standard,
Centered,
}
#[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 {
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,
}
}
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
}
pub fn kind(mut self, kind: SliderKind) -> Self {
self.kind = kind;
self
}
pub fn axis(mut self, axis: Axis) -> Self {
self.axis = axis;
self
}
pub fn reverse_vertical(mut self, reverse: bool) -> Self {
self.reverse_vertical = reverse;
self
}
pub fn size(mut self, size: ControlSize) -> Self {
self.size = size;
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn tick_intervals(mut self, intervals: u16) -> Self {
self.tick_intervals = intervals;
self
}
pub fn icon(mut self, icon: impl IntoElement) -> Self {
self.icon = Some(icon.into_any_element());
self
}
pub fn track(mut self, element: impl IntoElement) -> Self {
self.track = Some(element.into_any_element());
self
}
pub fn track_style(mut self, style: SliderTrackStyle) -> Self {
self.track_style = style;
self
}
pub fn thumb(mut self, which: RangeThumb, element: impl IntoElement) -> Self {
self.thumbs[thumb_index(which)].content = Some(element.into_any_element());
self
}
pub fn thumb_style(mut self, which: RangeThumb, style: SliderThumbStyle) -> Self {
self.thumbs[thumb_index(which)].style = style;
self
}
pub fn colors(mut self, colors: SliderColors) -> Self {
self.colors = colors;
self
}
pub fn track_focus(mut self, which: RangeThumb, focus: &FocusHandle) -> Self {
self.thumbs[thumb_index(which)].focus = Some(focus.clone());
self
}
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
}
pub fn draw_ticks(
mut self,
draw: impl Fn(SliderMarker, &mut Window, &mut App) + 'static,
) -> Self {
self.draw_tick = Some(Box::new(draw));
self
}
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
}
pub fn show_stop_indicators(mut self, show: bool) -> Self {
self.show_stop_indicators = show;
self
}
pub fn value_indicator(mut self, show: bool) -> Self {
self.value_indicator = show;
self
}
pub fn format_value(mut self, format: impl Fn(f32) -> SharedString + 'static) -> Self {
self.format_value = Some(Box::new(format));
self
}
}