libbhoo 0.3.0

Minimal UI foundations for SpectreOS
Documentation
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use crate::*;

/// Material switch icon visibility.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum SwitchIconMode {
    #[default]
    None,
    Selected,
    Both,
}
#[derive(Clone, Copy)]
pub(crate) struct SwitchLayout {
    pub min_width: f32,
    pub padding: f32,
    pub thumb_text_padding: f32,
}
impl Default for SwitchLayout {
    fn default() -> Self {
        Self {
            min_width: 52.0,
            padding: 4.0,
            thumb_text_padding: 4.0,
        }
    }
}
pub(crate) fn switch_widths(
    texts: Option<(&str, &str)>,
    metrics: crate::TextMetrics,
    layout: SwitchLayout,
) -> (f32, f32) {
    let text_min = texts
        .map(|(on, off)| {
            let style = TextStyle {
                size: 9.0,
                ..TextStyle::default()
            };
            (metrics.width(on, style).max(metrics.width(off, style))
                + 2.0 * layout.thumb_text_padding)
                .max(24.0)
        })
        .unwrap_or(0.0);
    (
        text_min,
        (2.0 * text_min + 4.0).max(layout.min_width).max(52.0),
    )
}

impl Frame<'_> {
    /// Paint an animated switch inside a custom control's track bounds.
    /// Colors are ordered unchecked, then checked. The caller owns input.
    pub fn switch_indicator(
        &mut self,
        id: u64,
        track: Rect,
        checked: bool,
        track_colors: [Color; 2],
        thumb_colors: [Color; 2],
    ) {
        let checked = f32::from(checked);
        let position = self
            .animate_geometry(id, 222, checked, checked)
            .clamp(0.0, 1.0);
        self.paint_rect(
            track,
            track.height * 0.5,
            track_colors[0].mix(track_colors[1], position),
        );
        let diameter = track.height * (18.0 + 4.0 * position) / 28.0;
        let inset = track.height * (5.0 - 2.0 * position) / 28.0;
        let travel = (track.width - diameter - 2.0 * inset).max(0.0);
        let thumb = Rect {
            x: track.x + inset + travel * position,
            y: track.y + (track.height - diameter) * 0.5,
            width: diameter,
            height: diameter,
        };
        self.paint_rect(
            thumb,
            diameter * 0.5,
            thumb_colors[0].mix(thumb_colors[1], position),
        );
    }

    pub fn switch(&mut self, id: u64, label: &str, value: &mut bool, enabled: bool) -> Response {
        self.switch_impl(
            id,
            label,
            value,
            enabled,
            false,
            false,
            SwitchIconMode::None,
            [[None; 2]; 2],
            None,
            SwitchLayout::default(),
        )
    }
    /// Switch with the control following its label, as in an Android Switch view.
    pub fn switch_trailing(
        &mut self,
        id: u64,
        label: &str,
        value: &mut bool,
        enabled: bool,
    ) -> Response {
        self.switch_impl(
            id,
            label,
            value,
            enabled,
            true,
            false,
            SwitchIconMode::None,
            [[None; 2]; 2],
            None,
            SwitchLayout::default(),
        )
    }
    /// Android-style trailing switch with layout-direction-aware placement.
    pub fn switch_trailing_directional(
        &mut self,
        id: u64,
        label: &str,
        value: &mut bool,
        enabled: bool,
        rtl: bool,
    ) -> Response {
        self.switch_impl(
            id,
            label,
            value,
            enabled,
            !rtl,
            rtl,
            SwitchIconMode::None,
            [[None; 2]; 2],
            None,
            SwitchLayout::default(),
        )
    }
    /// Switch with optional selected and unselected icons.
    pub fn switch_with_icons(
        &mut self,
        id: u64,
        label: &str,
        value: &mut bool,
        enabled: bool,
        icons: SwitchIconMode,
    ) -> Response {
        self.switch_impl(
            id,
            label,
            value,
            enabled,
            false,
            false,
            icons,
            [[None; 2]; 2],
            None,
            SwitchLayout::default(),
        )
    }
    /// Directional switch with optional icons and configurable control placement.
    pub fn switch_with_icons_directional(
        &mut self,
        id: u64,
        label: &str,
        value: &mut bool,
        enabled: bool,
        trailing: bool,
        rtl: bool,
        icons: SwitchIconMode,
    ) -> Response {
        self.switch_impl(
            id,
            label,
            value,
            enabled,
            trailing,
            rtl,
            icons,
            [[None; 2]; 2],
            None,
            SwitchLayout::default(),
        )
    }
    pub(crate) fn switch_trailing_tinted(
        &mut self,
        id: u64,
        label: &str,
        value: &mut bool,
        enabled: bool,
        rtl: bool,
        tints: [[Option<Color>; 2]; 2],
        texts: Option<(&str, &str)>,
        layout: SwitchLayout,
    ) -> Response {
        self.switch_impl(
            id,
            label,
            value,
            enabled,
            !rtl,
            rtl,
            SwitchIconMode::None,
            tints,
            texts,
            layout,
        )
    }
    fn switch_impl(
        &mut self,
        id: u64,
        label: &str,
        value: &mut bool,
        enabled: bool,
        trailing: bool,
        rtl: bool,
        icons: SwitchIconMode,
        tints: [[Option<Color>; 2]; 2],
        texts: Option<(&str, &str)>,
        layout: SwitchLayout,
    ) -> Response {
        let b = self.allocate(36.0);
        let mut r = self.interact(id, b, enabled);
        if r.clicked {
            *value = !*value;
            r.changed = true;
        }
        let a = self.animate_geometry(id, 3, f32::from(*value), f32::from(*value));
        let t = self.ui.theme;
        let (text_min_width, preferred_width) = switch_widths(texts, self.ui.text_metrics, layout);
        let track_width = preferred_width.min(b.width.max(0.0));
        let track = Rect {
            x: if trailing {
                (b.x + b.width - track_width).max(b.x)
            } else {
                b.x
            },
            y: b.y + 2.0,
            width: track_width,
            height: 32.0,
        };
        let disabled = t.background.mix(t.text, 0.12);
        let edge = tints[1][usize::from(*value)].unwrap_or(if r.enabled {
            t.outline.mix(t.primary, a)
        } else {
            disabled
        });
        let fill = tints[1][usize::from(*value)].unwrap_or(if r.enabled {
            t.surface_highest.mix(t.primary, a)
        } else {
            disabled
        });
        self.rect(track, 16.0, edge);
        self.rect(track.inset(2.0), 14.0, fill);
        let icon_visible = match icons {
            SwitchIconMode::None => false,
            SwitchIconMode::Selected => *value,
            SwitchIconMode::Both => true,
        };
        let base_size = if icons == SwitchIconMode::Both {
            24.0
        } else {
            16.0 + 8.0 * a
        };
        let pressed_target = f32::from(r.enabled && r.pressed);
        let press = self.ui.motion.css_tween(
            id,
            220,
            pressed_target,
            0.0,
            crate::timing::TweenSpec::new(0.1, crate::timing::Easing::Linear),
            crate::timing::TweenSpec::new(0.25, crate::timing::Easing::Standard),
        );
        let diameter = base_size + (28.0 - base_size) * press;
        let thumb_width = diameter
            .max(text_min_width)
            .min((track.width - 4.0).max(0.0));
        let start_center = if texts.is_some() {
            track.x + 2.0 + thumb_width / 2.0
        } else {
            track.x + 16.0
        };
        let travel = if texts.is_some() {
            (track.width - 4.0 - thumb_width).max(0.0)
        } else {
            (track.width - 32.0).max(0.0)
        };
        let center_x = start_center + travel * if rtl { 1.0 - a } else { a };
        let thumb = Rect {
            x: center_x - thumb_width / 2.0,
            y: track.y + 16.0 - diameter / 2.0,
            width: thumb_width,
            height: diameter,
        };
        let hover = self.animate(id, 0, f32::from(r.hovered), 0.0);
        let ink = Rect {
            x: center_x - 20.0,
            y: track.y - 4.0,
            width: 40.0,
            height: 40.0,
        };
        if r.enabled && (hover > 0.001 || r.focused && self.ui.keyboard_focus) {
            self.rect(
                ink,
                20.0,
                (if *value { t.primary } else { t.text }).opacity(
                    if r.focused && self.ui.keyboard_focus {
                        0.12
                    } else {
                        0.08 * hover
                    },
                ),
            );
        }
        self.ink(
            Response { bounds: ink, ..r },
            20.0,
            if *value { t.primary } else { t.text },
        );
        let handle = tints[0][usize::from(*value)].unwrap_or(if r.enabled {
            t.outline.mix(t.on_primary, a)
        } else if *value {
            t.background
        } else {
            t.text.opacity(0.38)
        });
        self.rect(thumb, diameter / 2.0, handle);
        if let Some((on, off)) = texts {
            let text = if a > 0.5 { on } else { off };
            self.commands
                .push(DrawCommand::PushClip(thumb.intersect(self.clip)));
            self.styled_text(
                thumb,
                text,
                t.on_surface_variant
                    .opacity(if r.enabled { 1.0 } else { 0.38 }),
                TextStyle {
                    size: 9.0,
                    align: 1,
                    ..TextStyle::default()
                },
            );
            self.commands.push(DrawCommand::PopClip);
        }
        let mark = if r.enabled {
            t.surface_highest.mix(t.on_primary_container, a)
        } else if *value {
            t.text.opacity(0.38)
        } else {
            t.surface_highest.opacity(0.38)
        };
        if icon_visible {
            let cx = thumb.x + thumb.width / 2.0;
            let cy = thumb.y + thumb.height / 2.0;
            if *value {
                self.paint_line([cx - 5.0, cy], [cx - 2.0, cy + 3.0], 1.5, mark);
                self.paint_line([cx - 2.0, cy + 3.0], [cx + 5.0, cy - 4.0], 1.5, mark);
            } else {
                self.paint_line([cx - 4.0, cy - 4.0], [cx + 4.0, cy + 4.0], 1.5, mark);
                self.paint_line([cx + 4.0, cy - 4.0], [cx - 4.0, cy + 4.0], 1.5, mark);
            }
        }
        self.styled_text(
            Rect {
                x: if trailing {
                    b.x
                } else {
                    b.x + track_width + layout.padding
                },
                width: (b.width - track_width - layout.padding).max(0.0),
                ..b
            },
            label,
            t.text.opacity(if r.enabled { 1.0 } else { 0.38 }),
            TextStyle {
                size: 14.0,
                align: if rtl { 2 } else { 0 },
                letter_spacing: 0.25,
                ..TextStyle::default()
            },
        );
        self.focus_ring(r);
        self.semantic(
            r,
            Role::Switch,
            label,
            *value,
            Some(if *value {
                CheckState::Checked
            } else {
                CheckState::Unchecked
            }),
            None,
            None,
        );
        r
    }
}

#[cfg(test)]
#[path = "switch.spec.rs"]
mod tests;