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,
13 End,
15}
16
17impl RangeThumb {
18 fn value(self, value: SliderValue) -> f32 {
19 match self {
20 Self::Start => value.start(),
21 Self::End => value.end(),
22 }
23 }
24}
25
26pub fn value_label(
29 state: &Entity<SliderState>,
30 thumb: RangeThumb,
31 cx: &App,
32 format: impl FnOnce(f32) -> SharedString,
33) -> SharedString {
34 format(thumb.value(state.read(cx).value()))
35}
36
37pub fn range_semantics(
41 element: Stateful<Div>,
42 state: &Entity<SliderState>,
43 thumb: RangeThumb,
44 axis: Axis,
45 cx: &App,
46) -> Stateful<Div> {
47 let slider = state.read(cx);
48 let value = slider.value();
49 let (min, max) = match (value.is_range(), thumb) {
50 (true, RangeThumb::Start) => (slider.min_value(), value.end()),
51 (true, RangeThumb::End) => (value.start(), slider.max_value()),
52 _ => (slider.min_value(), slider.max_value()),
53 };
54 let element = element
55 .role(Role::Slider)
56 .aria_numeric_value(f64::from(thumb.value(value)))
57 .aria_min_numeric_value(f64::from(min))
58 .aria_max_numeric_value(f64::from(max))
59 .aria_numeric_value_step(f64::from(slider.accessibility_step()))
60 .aria_orientation(if axis == Axis::Horizontal {
61 Orientation::Horizontal
62 } else {
63 Orientation::Vertical
64 });
65
66 let set = state.clone();
67 let increment = state.clone();
68 let decrement = state.clone();
69 let keyboard = state.clone();
70 let release = state.clone();
71 let release_outside = state.clone();
72 element
73 .on_a11y_action(AccessibleAction::SetValue, move |data, window, cx| {
74 if let Some(ActionData::NumericValue(target)) = data
75 && target.is_finite()
76 {
77 set.update(cx, |state, cx| {
78 let target = target
79 .clamp(f64::from(state.min_value()), f64::from(state.max_value()))
80 as f32;
81 let delta = target - thumb.value(state.value());
82 state.step_thumb(thumb == RangeThumb::Start, delta, window, cx);
83 });
84 }
85 })
86 .on_a11y_action(AccessibleAction::Increment, move |_, window, cx| {
87 step(&increment, thumb, 1., window, cx)
88 })
89 .on_a11y_action(AccessibleAction::Decrement, move |_, window, cx| {
90 step(&decrement, thumb, -1., window, cx)
91 })
92 .on_key_down(move |event, window, cx| {
93 if event.keystroke.modifiers.modified() {
94 return;
95 }
96 match event.keystroke.key.as_str() {
97 "left" | "down" => step(&keyboard, thumb, -1., window, cx),
98 "right" | "up" => step(&keyboard, thumb, 1., window, cx),
99 "pageup" => step_large(&keyboard, thumb, 1., window, cx),
100 "pagedown" => step_large(&keyboard, thumb, -1., window, cx),
101 "home" => jump_bound(&keyboard, thumb, false, window, cx),
102 "end" => jump_bound(&keyboard, thumb, true, window, cx),
103 _ => return,
104 }
105 cx.stop_propagation();
106 })
107 .on_mouse_up(gpui::MouseButton::Left, move |_, _, cx| {
108 release.update(cx, |state, cx| state.handle_release(cx));
109 })
110 .on_mouse_up_out(gpui::MouseButton::Left, move |_, _, cx| {
111 release_outside.update(cx, |state, cx| state.handle_release(cx));
112 })
113}
114
115fn step(
116 state: &Entity<SliderState>,
117 thumb: RangeThumb,
118 direction: f32,
119 window: &mut Window,
120 cx: &mut App,
121) {
122 state.update(cx, |state, cx| {
123 state.step_thumb(
124 thumb == RangeThumb::Start,
125 direction * state.accessibility_step(),
126 window,
127 cx,
128 );
129 });
130}
131
132fn step_large(
133 state: &Entity<SliderState>,
134 thumb: RangeThumb,
135 direction: f32,
136 window: &mut Window,
137 cx: &mut App,
138) {
139 state.update(cx, |state, cx| {
140 let base_step = state.accessibility_step();
141 let span = (state.max_value() - state.min_value()).abs();
142 let target_step = (span * 0.1).max(base_step);
143 let delta = if base_step > 0. && base_step.is_finite() {
144 (target_step / base_step).round().max(1.) * base_step
145 } else {
146 target_step
147 };
148 state.step_thumb(thumb == RangeThumb::Start, direction * delta, window, cx);
149 });
150}
151
152fn jump_bound(
153 state: &Entity<SliderState>,
154 thumb: RangeThumb,
155 to_max: bool,
156 window: &mut Window,
157 cx: &mut App,
158) {
159 state.update(cx, |state, cx| {
160 let value = state.value();
161 let target = match (value.is_range(), thumb, to_max) {
162 (true, RangeThumb::Start, true) => value.end(),
163 (true, RangeThumb::End, false) => value.start(),
164 (_, _, false) => state.min_value(),
165 (_, _, true) => state.max_value(),
166 };
167 let delta = target - thumb.value(value);
168 if delta != 0. {
169 state.step_thumb(thumb == RangeThumb::Start, delta, window, cx);
170 }
171 });
172}