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