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 if event.keystroke.modifiers.modified() {
92 return;
93 }
94 match event.keystroke.key.as_str() {
95 "left" | "down" => step(&keyboard, thumb, -1., window, cx),
96 "right" | "up" => step(&keyboard, thumb, 1., window, cx),
97 "pageup" => step_large(&keyboard, thumb, 1., window, cx),
98 "pagedown" => step_large(&keyboard, thumb, -1., window, cx),
99 "home" => jump_bound(&keyboard, thumb, false, window, cx),
100 "end" => jump_bound(&keyboard, thumb, true, window, cx),
101 _ => return,
102 }
103 cx.stop_propagation();
104 })
105 .on_mouse_up(gpui::MouseButton::Left, move |_, _, cx| {
106 release.update(cx, |state, cx| state.handle_release(cx));
107 })
108 .on_mouse_up_out(gpui::MouseButton::Left, move |_, _, cx| {
109 release_outside.update(cx, |state, cx| state.handle_release(cx));
110 })
111}
112
113fn step(
114 state: &Entity<SliderState>,
115 thumb: RangeThumb,
116 direction: f32,
117 window: &mut Window,
118 cx: &mut App,
119) {
120 state.update(cx, |state, cx| {
121 state.step_thumb(
122 thumb == RangeThumb::Start,
123 direction * state.accessibility_step(),
124 window,
125 cx,
126 );
127 });
128}
129
130fn step_large(
131 state: &Entity<SliderState>,
132 thumb: RangeThumb,
133 direction: f32,
134 window: &mut Window,
135 cx: &mut App,
136) {
137 state.update(cx, |state, cx| {
138 let base_step = state.accessibility_step();
139 let span = (state.max_value() - state.min_value()).abs();
140 let target_step = (span * 0.1).max(base_step);
141 let delta = if base_step > 0. && base_step.is_finite() {
142 (target_step / base_step).round().max(1.) * base_step
143 } else {
144 target_step
145 };
146 state.step_thumb(thumb == RangeThumb::Start, direction * delta, window, cx);
147 });
148}
149
150fn jump_bound(
151 state: &Entity<SliderState>,
152 thumb: RangeThumb,
153 to_max: bool,
154 window: &mut Window,
155 cx: &mut App,
156) {
157 state.update(cx, |state, cx| {
158 let value = state.value();
159 let target = match (value.is_range(), thumb, to_max) {
160 (true, RangeThumb::Start, true) => value.end(),
161 (true, RangeThumb::End, false) => value.start(),
162 (_, _, false) => state.min_value(),
163 (_, _, true) => state.max_value(),
164 };
165 let delta = target - thumb.value(value);
166 if delta != 0. {
167 state.step_thumb(thumb == RangeThumb::Start, delta, window, cx);
168 }
169 });
170}