1use crate::*;
2
3impl Frame<'_> {
4 pub fn segmented(
6 &mut self,
7 id: u64,
8 labels: &[&str],
9 mask: &mut u32,
10 multiple: bool,
11 enabled: bool,
12 ) -> Response {
13 assert!(
14 labels.len() <= 32,
15 "Segmented controls support at most 32 choices"
16 );
17 let b = self.allocate(42.0);
18 let mut r = self.interact(id, b, enabled && !labels.is_empty());
19 if labels.is_empty() {
20 return r;
21 }
22 let old = *mask;
23 let count = labels.len();
24 let angular = self.ui.ripple_style == RippleStyle::AngularMaterial;
25 let divider = if angular { 1.0 } else { 0.0 };
26 let width = (b.width - 2.0 - divider * (count - 1) as f32).max(0.0) / count as f32;
27 if !multiple && *mask == 0 {
28 *mask = 1;
29 }
30 let mut selected = self
31 .ui
32 .part_focus
33 .get(&id)
34 .copied()
35 .unwrap_or_else(|| {
36 if multiple {
37 0
38 } else {
39 mask.trailing_zeros() as usize
40 }
41 })
42 .min(count - 1);
43 if r.clicked {
44 if self.ui.capture == Capture::Pointer
45 && self.input.released
46 && let Some(p) = self.input.pointer
47 {
48 selected = (((p[0] - b.x - 1.0) / (width + divider).max(1.0))
49 .floor()
50 .max(0.0) as usize)
51 .min(count - 1);
52 }
53 if self.ui.capture == Capture::Keyboard || self.input.released {
54 if multiple {
55 *mask ^= 1 << selected;
56 } else {
57 *mask = 1 << selected;
58 }
59 }
60 }
61 if let Some(key) = self.key_for(r) {
62 let navigation = match key {
63 Key::Right | Key::Down => {
64 selected = (selected + 1) % count;
65 true
66 }
67 Key::Left | Key::Up => {
68 selected = (selected + count - 1) % count;
69 true
70 }
71 Key::Home => {
72 selected = 0;
73 true
74 }
75 Key::End => {
76 selected = count - 1;
77 true
78 }
79 _ => false,
80 };
81 if navigation && !multiple {
82 *mask = 1 << selected;
83 }
84 }
85 if self.ui.part_focus.len() < 4096 || self.ui.part_focus.contains_key(&id) {
86 self.ui.part_focus.insert(id, selected);
87 }
88 let t = self.ui.theme;
89 self.rect(b, 21.0, t.outline);
90 self.rect(b.inset(1.0), 20.0, t.background);
91 for (i, label) in labels.iter().enumerate() {
92 let cell = Rect {
93 x: b.x + 1.0 + i as f32 * (width + divider),
94 y: b.y + 1.0,
95 width,
96 height: 40.0,
97 };
98 let checked = *mask & (1 << i) != 0;
99 let child = Response {
100 bounds: cell,
101 focused: r.focused && i == selected,
102 hovered: r.hovered && self.input.pointer.is_some_and(|p| cell.contains(p)),
103 ..r
104 };
105 let clip = cell.intersect(self.clip);
106 self.commands.push(DrawCommand::PushClip(clip));
107 if checked {
108 self.rect(
109 b.inset(1.0),
110 20.0,
111 if angular && !r.enabled {
112 t.text.opacity(0.12)
113 } else {
114 t.secondary
115 },
116 );
117 }
118 self.commands.push(DrawCommand::PopClip);
119 if i > 0 {
120 self.rect(
121 Rect {
122 x: cell.x - divider,
123 y: cell.y,
124 width: 1.0,
125 height: 40.0,
126 },
127 0.0,
128 t.outline,
129 );
130 }
131 let color = if r.enabled {
132 if checked {
133 t.on_secondary
134 } else {
135 if angular {
136 t.text
137 } else {
138 t.on_surface_variant
139 }
140 }
141 } else {
142 t.text.opacity(0.38)
143 };
144 let mark_amount = if angular {
145 use crate::timing::{Easing, TweenSpec};
146 let spec = TweenSpec {
147 delay: 0.045,
148 duration: 0.150,
149 easing: Easing::Cubic([0.4, 0.0, 0.2, 1.0]),
150 };
151 self.ui.motion.css_tween(
152 id,
153 80 + i as u8,
154 f32::from(checked),
155 f32::from(checked),
156 spec,
157 spec,
158 )
159 } else {
160 f32::from(checked)
161 };
162 if angular && mark_amount > 0.001 {
163 let x = cell.x + 12.0;
164 let clip = Rect {
165 x,
166 y: cell.y + 11.0,
167 width: 18.0 * mark_amount,
168 height: 18.0,
169 }
170 .intersect(self.clip);
171 self.commands.push(DrawCommand::PushClip(clip));
172 self.paint_line(
173 [x + 2.5, cell.y + 20.0],
174 [x + 6.5, cell.y + 24.0],
175 2.0,
176 color,
177 );
178 self.paint_line(
179 [x + 6.5, cell.y + 24.0],
180 [x + 15.0, cell.y + 15.5],
181 2.0,
182 color,
183 );
184 self.commands.push(DrawCommand::PopClip);
185 } else if !angular && checked {
186 let x = cell.x + cell.width / 2.0
187 - (self.text_width(label, TextStyle::LABEL) + 24.0) / 2.0;
188 self.paint_line(
189 [x + 1.0, cell.y + 20.0],
190 [x + 5.0, cell.y + 24.0],
191 1.5,
192 color,
193 );
194 self.paint_line(
195 [x + 5.0, cell.y + 24.0],
196 [x + 13.0, cell.y + 16.0],
197 1.5,
198 color,
199 );
200 }
201 if child.enabled {
202 let amount = if child.focused && self.ui.keyboard_focus {
203 0.12
204 } else if child.hovered {
205 0.08
206 } else {
207 0.0
208 };
209 if amount > 0.0 {
210 if angular {
211 self.commands.push(DrawCommand::PushClip(clip));
214 self.rect(b.inset(1.0), 20.0, t.text.opacity(amount));
215 self.commands.push(DrawCommand::PopClip);
216 } else {
217 self.rect(cell, 0.0, color.opacity(amount));
218 }
219 }
220 }
221 self.styled_text(
222 Rect {
223 x: cell.x
224 + if angular {
225 30.0 * mark_amount
226 } else if checked {
227 24.0
228 } else {
229 0.0
230 },
231 width: cell.width
232 - if angular {
233 30.0 * mark_amount
234 } else if checked {
235 24.0
236 } else {
237 0.0
238 },
239 ..cell
240 },
241 label,
242 color,
243 TextStyle::LABEL,
244 );
245 if angular {
246 self.commands.push(DrawCommand::PushClip(clip));
249 self.ink_part(child, i as u32 + 1, 0.0, t.text, false);
250 if let Some(DrawCommand::Ripple {
251 bounds,
252 corner_radius,
253 ..
254 }) = self.commands.last_mut()
255 {
256 *bounds = b.inset(1.0);
257 *corner_radius = 20.0;
258 self.commands.push(DrawCommand::PopClip);
259 } else {
260 self.commands.pop();
262 }
263 } else {
264 self.ink_part(child, i as u32 + 1, 0.0, color, false);
265 }
266 self.semantic(
267 child,
268 if multiple {
269 Role::Checkbox
270 } else {
271 Role::Radio
272 },
273 label,
274 checked,
275 Some(if checked {
276 CheckState::Checked
277 } else {
278 CheckState::Unchecked
279 }),
280 None,
281 None,
282 );
283 if let Some(node) = self.ui.semantics.last_mut() {
284 node.part = i as u32 + 1;
285 }
286 }
287 self.focus_ring(r);
288 self.semantic(
289 r,
290 Role::Group,
291 "Segmented selection",
292 false,
293 None,
294 Some(*mask as f32),
295 None,
296 );
297 r.changed = old != *mask;
298 r
299 }
300}
301
302#[cfg(test)]
303#[path = "segmented.spec.rs"]
304mod tests;