gpui_material_foundation/
range.rs1use gpui::{
2 AccessibleAction, App, Axis, Div, Entity, InteractiveElement, Role, SharedString, Stateful,
3 StatefulInteractiveElement, Window,
4 accesskit::{ActionData, Orientation},
5};
6use gpui_base::slider::{SliderState, SliderValue};
7
8#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10pub enum RangeThumb {
11 Start,
12 End,
13}
14
15impl RangeThumb {
16 fn value(self, value: SliderValue) -> f32 {
17 match self {
18 Self::Start => value.start(),
19 Self::End => value.end(),
20 }
21 }
22}
23
24pub fn value_label(
27 state: &Entity<SliderState>,
28 thumb: RangeThumb,
29 cx: &App,
30 format: impl FnOnce(f32) -> SharedString,
31) -> SharedString {
32 format(thumb.value(state.read(cx).value()))
33}
34
35pub fn range_semantics(
39 element: Stateful<Div>,
40 state: &Entity<SliderState>,
41 thumb: RangeThumb,
42 axis: Axis,
43 cx: &App,
44) -> Stateful<Div> {
45 let slider = state.read(cx);
46 let value = slider.value();
47 let (min, max) = match (value.is_range(), thumb) {
48 (true, RangeThumb::Start) => (slider.min_value(), value.end()),
49 (true, RangeThumb::End) => (value.start(), slider.max_value()),
50 _ => (slider.min_value(), slider.max_value()),
51 };
52 let element = element
53 .role(Role::Slider)
54 .aria_numeric_value(f64::from(thumb.value(value)))
55 .aria_min_numeric_value(f64::from(min))
56 .aria_max_numeric_value(f64::from(max))
57 .aria_numeric_value_step(f64::from(slider.accessibility_step()))
58 .aria_orientation(if axis == Axis::Horizontal {
59 Orientation::Horizontal
60 } else {
61 Orientation::Vertical
62 });
63
64 let set = state.clone();
65 let increment = state.clone();
66 let decrement = state.clone();
67 let keyboard = state.clone();
68 let release = state.clone();
69 let release_outside = state.clone();
70 element
71 .on_a11y_action(AccessibleAction::SetValue, move |data, window, cx| {
72 if let Some(ActionData::NumericValue(target)) = data
73 && target.is_finite()
74 {
75 set.update(cx, |state, cx| {
76 let target = target
77 .clamp(f64::from(state.min_value()), f64::from(state.max_value()))
78 as f32;
79 let delta = target - thumb.value(state.value());
80 state.step_thumb(thumb == RangeThumb::Start, delta, window, cx);
81 });
82 }
83 })
84 .on_a11y_action(AccessibleAction::Increment, move |_, window, cx| {
85 step(&increment, thumb, 1., window, cx)
86 })
87 .on_a11y_action(AccessibleAction::Decrement, move |_, window, cx| {
88 step(&decrement, thumb, -1., window, cx)
89 })
90 .on_key_down(move |event, window, cx| {
91 let direction = match event.keystroke.key.as_str() {
92 "left" | "down" => -1.,
93 "right" | "up" => 1.,
94 _ => return,
95 };
96 step(&keyboard, thumb, direction, window, cx);
97 cx.stop_propagation();
98 })
99 .on_mouse_up(gpui::MouseButton::Left, move |_, _, cx| {
100 release.update(cx, |state, cx| state.handle_release(cx));
101 })
102 .on_mouse_up_out(gpui::MouseButton::Left, move |_, _, cx| {
103 release_outside.update(cx, |state, cx| state.handle_release(cx));
104 })
105}
106
107fn step(
108 state: &Entity<SliderState>,
109 thumb: RangeThumb,
110 direction: f32,
111 window: &mut Window,
112 cx: &mut App,
113) {
114 state.update(cx, |state, cx| {
115 state.step_thumb(
116 thumb == RangeThumb::Start,
117 direction * state.accessibility_step(),
118 window,
119 cx,
120 );
121 });
122}