quickgui 0.1.5

A damage-driven, GPU-accelerated desktop GUI foundation
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
use crate::{
    AccessibilityRole, Checkbox, ClickListener, Element, ElementId, EventContext, StateAccessor,
    ToggleState, ViewContext, div,
};

/// Maximum declared values one checkbox group retains.
///
/// Both the declared universe and the checked subset are bounded by this constant, so one group
/// keeps a fixed worst-case size no matter what application data drives it.
pub const MAX_CHECKBOX_GROUP_VALUES: usize = 256;

const CHECKBOX_GROUP_ITEM_ID_TAG: u64 = 0x4c8b_2f17_ae60_93d5;
const CHECKBOX_GROUP_PARENT_ID_TAG: u64 = 0xd207_66be_1f39_c84a;

/// Controlled, bounded value state for one checkbox group.
///
/// The state owns the declared universe of values, the checked subset, and the group's disabled
/// flag. The application owns every visual declaration, the labels, and the listener that reacts
/// to a change. Nothing here observes, schedules, or animates.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct CheckboxGroupState {
    all_values: Vec<ElementId>,
    values: Vec<ElementId>,
    disabled: bool,
}

impl CheckboxGroupState {
    /// Declare the group's complete, ordered universe of values.
    ///
    /// # Panics
    ///
    /// Panics with more than [`MAX_CHECKBOX_GROUP_VALUES`] values or with a duplicate value,
    /// because either makes the parent checkbox's derived state ambiguous.
    pub fn new<I>(all_values: I) -> Self
    where
        I: IntoIterator,
        I::Item: Into<ElementId>,
    {
        let all_values: Vec<ElementId> = all_values.into_iter().map(Into::into).collect();
        assert!(
            all_values.len() <= MAX_CHECKBOX_GROUP_VALUES,
            "a checkbox group retains at most {MAX_CHECKBOX_GROUP_VALUES} values"
        );
        for (index, value) in all_values.iter().enumerate() {
            assert!(
                !all_values[..index].contains(value),
                "checkbox group values must be distinct"
            );
        }
        Self {
            all_values,
            values: Vec::new(),
            disabled: false,
        }
    }

    /// Declare the initially checked subset, ignoring values outside the declared universe.
    #[must_use]
    pub fn checked<I>(mut self, values: I) -> Self
    where
        I: IntoIterator,
        I::Item: Into<ElementId>,
    {
        self.values.clear();
        for value in values.into_iter().map(Into::into) {
            if self.all_values.contains(&value) && !self.values.contains(&value) {
                self.values.push(value);
            }
        }
        self
    }

    #[must_use]
    pub const fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    /// The declared universe, in the order the application gave.
    pub fn all_values(&self) -> &[ElementId] {
        &self.all_values
    }

    /// The checked subset, in the order the application gave for the universe.
    pub fn values(&self) -> &[ElementId] {
        &self.values
    }

    pub const fn is_disabled(&self) -> bool {
        self.disabled
    }

    pub fn is_checked(&self, value: impl Into<ElementId>) -> bool {
        self.values.contains(&value.into())
    }

    /// Replace one value's checked state, returning whether the group changed.
    ///
    /// A disabled group, or a value outside the declared universe, changes nothing.
    pub fn set_checked(&mut self, value: impl Into<ElementId>, checked: bool) -> bool {
        if self.disabled {
            return false;
        }
        let value = value.into();
        if !self.all_values.contains(&value) {
            return false;
        }
        let position = self.values.iter().position(|candidate| *candidate == value);
        match (checked, position) {
            (true, None) => {
                if self.values.len() == MAX_CHECKBOX_GROUP_VALUES {
                    return false;
                }
                self.values.push(value);
                self.sort_by_declaration();
                true
            }
            (false, Some(position)) => {
                self.values.remove(position);
                true
            }
            _ => false,
        }
    }

    /// Flip one value, returning whether the group changed.
    pub fn toggle(&mut self, value: impl Into<ElementId>) -> bool {
        let value = value.into();
        let checked = self.is_checked(value);
        self.set_checked(value, !checked)
    }

    /// The parent checkbox's derived state: on when every value is checked, mixed when some are.
    pub fn parent_state(&self) -> ToggleState {
        if self.all_values.is_empty() || self.values.is_empty() {
            ToggleState::Off
        } else if self.values.len() == self.all_values.len() {
            ToggleState::On
        } else {
            ToggleState::Mixed
        }
    }

    /// Apply the parent checkbox: check everything unless everything is already checked.
    ///
    /// Returns whether the group changed.
    pub fn toggle_parent(&mut self) -> bool {
        if self.disabled {
            return false;
        }
        if self.parent_state().is_on() {
            if self.values.is_empty() {
                return false;
            }
            self.values.clear();
            return true;
        }
        if self.values.len() == self.all_values.len() {
            return false;
        }
        self.values = self.all_values.clone();
        true
    }

    /// Uncheck every value, returning whether the group changed.
    pub fn clear(&mut self) -> bool {
        if self.disabled || self.values.is_empty() {
            return false;
        }
        self.values.clear();
        true
    }

    fn sort_by_declaration(&mut self) {
        let order = &self.all_values;
        self.values.sort_by_key(|value| {
            order
                .iter()
                .position(|candidate| candidate == value)
                .unwrap_or(usize::MAX)
        });
    }
}

/// A controlled, unstyled checkbox-group descriptor.
///
/// The application owns the layout, labels, indicators, and colors. QuickGUI supplies the Group
/// role, stable per-value part identities, exact checked/mixed semantics reused from [`Checkbox`],
/// explicit group-disabled propagation, and a parent checkbox whose state is derived from the
/// children rather than retained separately.
///
/// The descriptor borrows the caller's state and retains no registry, task, timer, observer, or
/// idle scheduler source.
#[derive(Clone, Copy, Debug)]
#[must_use = "a CheckboxGroup descriptor has no effect until its parts are mounted"]
pub struct CheckboxGroup<'a> {
    root_id: ElementId,
    state: &'a CheckboxGroupState,
}

impl<'a> CheckboxGroup<'a> {
    pub fn new(root_id: impl Into<ElementId>, state: &'a CheckboxGroupState) -> Self {
        Self {
            root_id: root_id.into(),
            state,
        }
    }

    pub const fn root_id(self) -> ElementId {
        self.root_id
    }

    pub const fn state(self) -> &'a CheckboxGroupState {
        self.state
    }

    pub fn checkbox_id(self, value: impl Into<ElementId>) -> ElementId {
        derived_group_id(self.root_id, CHECKBOX_GROUP_ITEM_ID_TAG, value.into())
    }

    pub fn parent_id(self) -> ElementId {
        derived_group_id(
            self.root_id,
            CHECKBOX_GROUP_PARENT_ID_TAG,
            ElementId::new(0),
        )
    }

    /// Decorate an application-owned group root without adding layout or appearance.
    ///
    /// Pair it with a [`crate::Field`] label, or with
    /// [`crate::Element::accessibility_labelled_by`], to name the group.
    pub fn root_with(self, root: Element) -> Element {
        root.id(self.root_id)
            .accessibility_role(AccessibilityRole::Group)
            .app_region_no_drag()
    }
    /// Create the unstyled root part. Use [`Self::root_with`] to supply an existing element.
    pub fn root(self) -> Element {
        self.root_with(crate::div())
    }

    /// Decorate one application-owned checkbox for `value`.
    ///
    /// The part reuses the existing [`Checkbox`] descriptor, so the checkbox role, exact
    /// on/off/mixed semantics, click behavior, and desktop pointer contract are identical to a
    /// standalone checkbox. A disabled group disables every item.
    pub fn checkbox_with(self, value: impl Into<ElementId>, checkbox: Element) -> Element {
        let value = value.into();
        let disabled = self.state.disabled || checkbox.accessibility.disabled;
        Checkbox::new(self.state.is_checked(value))
            .root_with(checkbox)
            .id(self.checkbox_id(value))
            .disabled(disabled)
    }
    /// Create the unstyled checkbox part. Use [`Self::checkbox_with`] to supply an existing element.
    pub fn checkbox(self, value: impl Into<ElementId>) -> Element {
        self.checkbox_with(value, crate::div())
    }

    /// Hide an application-owned indicator inside one item from the accessible name.
    pub fn indicator_with(self, indicator: Element) -> Element {
        indicator.accessibility_hidden(true)
    }
    /// Create the unstyled indicator part. Use [`Self::indicator_with`] to supply an existing element.
    pub fn indicator(self) -> Element {
        self.indicator_with(crate::div())
    }

    /// Decorate the application-owned parent checkbox.
    ///
    /// Its checked state is derived: on when every declared value is checked, mixed when only
    /// some are, and off when none are. QuickGUI retains no separate parent value, so the parent
    /// can never disagree with its children.
    pub fn parent_with(self, parent: Element) -> Element {
        let disabled = self.state.disabled || parent.accessibility.disabled;
        Checkbox::new(self.state.parent_state())
            .root_with(parent)
            .id(self.parent_id())
            .disabled(disabled)
    }
    /// Create the unstyled parent part. Use [`Self::parent_with`] to supply an existing element.
    pub fn parent(self) -> Element {
        self.parent_with(crate::div())
    }

    /// Build the click behavior for one item, reporting the whole new value set.
    ///
    /// Attach the returned handle with [`crate::Element::on_click`].
    pub fn on_checkbox_click<V: 'static, Change>(
        self,
        cx: &mut ViewContext<'_, V>,
        value: impl Into<ElementId>,
        access: fn(&mut V) -> &mut CheckboxGroupState,
        on_value_change: Change,
    ) -> ClickListener<V>
    where
        Change: Fn(&mut V, &[ElementId], &mut EventContext) + 'static,
    {
        self.on_checkbox_click_with(cx, value, StateAccessor::from(access), on_value_change)
    }

    /// Build one item's click behavior against a per-instance state accessor.
    pub fn on_checkbox_click_with<V: 'static, Change>(
        self,
        cx: &mut ViewContext<'_, V>,
        value: impl Into<ElementId>,
        access: StateAccessor<V, CheckboxGroupState>,
        on_value_change: Change,
    ) -> ClickListener<V>
    where
        Change: Fn(&mut V, &[ElementId], &mut EventContext) + 'static,
    {
        let value = value.into();
        cx.listener(self.checkbox_id(value), move |view, cx| {
            if access.get(view).toggle(value) {
                let values = access.get(view).values().to_vec();
                on_value_change(view, &values, cx);
                cx.invalidate();
            }
        })
    }

    /// Build the parent checkbox's click behavior, reporting the whole new value set.
    pub fn on_parent_click<V: 'static, Change>(
        self,
        cx: &mut ViewContext<'_, V>,
        access: fn(&mut V) -> &mut CheckboxGroupState,
        on_value_change: Change,
    ) -> ClickListener<V>
    where
        Change: Fn(&mut V, &[ElementId], &mut EventContext) + 'static,
    {
        self.on_parent_click_with(cx, StateAccessor::from(access), on_value_change)
    }

    /// Build the parent checkbox's click behavior against a per-instance state accessor.
    pub fn on_parent_click_with<V: 'static, Change>(
        self,
        cx: &mut ViewContext<'_, V>,
        access: StateAccessor<V, CheckboxGroupState>,
        on_value_change: Change,
    ) -> ClickListener<V>
    where
        Change: Fn(&mut V, &[ElementId], &mut EventContext) + 'static,
    {
        cx.listener(self.parent_id(), move |view, cx| {
            if access.get(view).toggle_parent() {
                let values = access.get(view).values().to_vec();
                on_value_change(view, &values, cx);
                cx.invalidate();
            }
        })
    }
}

/// Create an unstyled semantic checkbox-group root.
///
/// This shorthand is equivalent to `CheckboxGroup::new(id, state).root_with(div())`.
pub fn checkbox_group(id: impl Into<ElementId>, state: &CheckboxGroupState) -> Element {
    CheckboxGroup::new(id, state).root_with(div())
}

fn derived_group_id(scope: ElementId, tag: u64, value: ElementId) -> ElementId {
    let mut hash = scope
        .as_u64()
        .rotate_left(29)
        .wrapping_add(value.as_u64().rotate_right(13))
        ^ tag;
    hash ^= hash >> 30;
    hash = hash.wrapping_mul(0xbf58_476d_1ce4_e5b9);
    hash ^= hash >> 27;
    hash = hash.wrapping_mul(0x94d0_49bb_1331_11eb);
    hash ^= hash >> 31;
    if hash == 0 || hash == u64::MAX || hash == scope.as_u64() {
        hash ^= tag.rotate_left(7);
    }
    ElementId::new(hash)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Color, IntoElement, TestAppContext, View, text};

    fn ids(values: &[&str]) -> Vec<ElementId> {
        values.iter().map(|value| (*value).into()).collect()
    }

    #[test]
    fn bounded_values_derive_the_parent_state() {
        let mut state = CheckboxGroupState::new(["red", "green", "blue"]);
        assert_eq!(state.all_values(), ids(&["red", "green", "blue"]));
        assert_eq!(state.values(), Vec::<ElementId>::new());
        assert_eq!(state.parent_state(), ToggleState::Off);

        assert!(state.set_checked("green", true));
        assert!(!state.set_checked("green", true));
        assert_eq!(state.parent_state(), ToggleState::Mixed);
        assert!(state.toggle("red"));
        // Checked values keep the declared order regardless of click order.
        assert_eq!(state.values(), ids(&["red", "green"]));

        assert!(state.set_checked("blue", true));
        assert_eq!(state.parent_state(), ToggleState::On);
        assert!(state.toggle_parent());
        assert_eq!(state.values(), Vec::<ElementId>::new());
        assert!(state.toggle_parent());
        assert_eq!(state.values(), ids(&["red", "green", "blue"]));
        assert!(!state.set_checked("teal", true), "undeclared value ignored");
        assert!(state.clear());
        assert!(!state.clear());

        let disabled = &mut CheckboxGroupState::new(["a"]).disabled(true);
        assert!(!disabled.set_checked("a", true));
        assert!(!disabled.toggle_parent());
        assert!(!disabled.clear());
        assert!(disabled.is_disabled());

        let empty = CheckboxGroupState::new(Vec::<ElementId>::new());
        assert_eq!(empty.parent_state(), ToggleState::Off);

        let preset = CheckboxGroupState::new(["a", "b"]).checked(["b", "b", "z"]);
        assert_eq!(preset.values(), ids(&["b"]));
    }

    #[test]
    #[should_panic(expected = "at most")]
    fn oversized_groups_are_rejected() {
        let values: Vec<ElementId> = (0..=MAX_CHECKBOX_GROUP_VALUES)
            .map(|index| ElementId::new(index as u64 + 1))
            .collect();
        let _ = CheckboxGroupState::new(values);
    }

    #[test]
    #[should_panic(expected = "distinct")]
    fn duplicate_values_are_rejected() {
        let _ = CheckboxGroupState::new(["a", "a"]);
    }

    #[test]
    fn parts_add_exact_semantics_without_appearance() {
        let state = CheckboxGroupState::new(["red", "green"]).checked(["red"]);
        let group = CheckboxGroup::new("colors", &state);
        let root = group.root_with(div().bg(Color::rgb8(1, 2, 3)));
        assert_eq!(root.explicit_id, Some("colors".into()));
        assert_eq!(root.accessibility.role, AccessibilityRole::Group);
        assert_eq!(root.visual.background, Some(Color::rgb8(1, 2, 3)));

        let checked = group.checkbox_with("red", div());
        assert_eq!(checked.explicit_id, Some(group.checkbox_id("red")));
        assert_eq!(checked.accessibility.role, AccessibilityRole::CheckBox);
        assert_eq!(checked.accessibility.toggled, Some(ToggleState::On));
        assert!(checked.clickable);
        assert_eq!(checked.visual.background, None);

        let unchecked = group.checkbox_with("green", div());
        assert_eq!(unchecked.accessibility.toggled, Some(ToggleState::Off));

        let parent = group.parent_with(div());
        assert_eq!(parent.explicit_id, Some(group.parent_id()));
        assert_eq!(parent.accessibility.toggled, Some(ToggleState::Mixed));

        let indicator = group.indicator_with(div());
        assert!(indicator.accessibility.hidden);

        let disabled_state = CheckboxGroupState::new(["red"]).disabled(true);
        let disabled = CheckboxGroup::new("colors", &disabled_state);
        assert!(disabled.checkbox_with("red", div()).accessibility.disabled);
        assert!(disabled.parent_with(div()).accessibility.disabled);

        let all = [
            group.root_id(),
            group.parent_id(),
            group.checkbox_id("red"),
            group.checkbox_id("green"),
        ];
        for (index, id) in all.iter().enumerate() {
            assert_ne!(*id, ElementId::new(0));
            assert_ne!(*id, ElementId::new(u64::MAX));
            assert!(!all[..index].contains(id));
        }

        let shorthand = checkbox_group("colors", &state);
        assert_eq!(shorthand.accessibility.role, AccessibilityRole::Group);
        assert!(shorthand.children.is_empty());
    }

    struct GroupView {
        colors: CheckboxGroupState,
        reported: Vec<Vec<ElementId>>,
    }

    impl GroupView {
        fn colors(view: &mut Self) -> &mut CheckboxGroupState {
            &mut view.colors
        }
    }

    impl View for GroupView {
        fn render(&mut self, cx: &mut ViewContext<'_, Self>) -> impl IntoElement {
            let group = CheckboxGroup::new("colors", &self.colors);
            let parent = group.on_parent_click(cx, Self::colors, |view, values, _| {
                view.reported.push(values.to_vec());
            });
            let mut root = group
                .root_with(div())
                .child(group.parent_with(div().child(text("All"))).on_click(parent));
            for value in ["red", "green", "blue"] {
                let click = group.on_checkbox_click(cx, value, Self::colors, |view, values, _| {
                    view.reported.push(values.to_vec());
                });
                root = root.child(
                    group
                        .checkbox_with(value, div().child(text(value)))
                        .on_click(click),
                );
            }
            root
        }
    }

    #[test]
    fn clicks_toggle_children_and_the_derived_parent() {
        let (mut cx, view) = TestAppContext::new(GroupView {
            colors: CheckboxGroupState::new(["red", "green", "blue"]),
            reported: Vec::new(),
        })
        .unwrap();
        let window = view.window_handle();
        let state = CheckboxGroupState::new(["red", "green", "blue"]);
        let group = CheckboxGroup::new("colors", &state);

        cx.click(window, group.checkbox_id("green")).unwrap();
        assert_eq!(
            cx.read(view, |view| view.colors.values().to_vec()).unwrap(),
            ids(&["green"])
        );
        assert_eq!(
            cx.read(view, |view| view.colors.parent_state()).unwrap(),
            ToggleState::Mixed
        );

        cx.click(window, group.parent_id()).unwrap();
        assert_eq!(
            cx.read(view, |view| view.colors.values().to_vec()).unwrap(),
            ids(&["red", "green", "blue"])
        );
        cx.click(window, group.parent_id()).unwrap();
        assert_eq!(
            cx.read(view, |view| view.colors.values().to_vec()).unwrap(),
            Vec::<ElementId>::new()
        );
        assert_eq!(cx.read(view, |view| view.reported.len()).unwrap(), 3);

        let update = cx.accessibility_update(window).unwrap();
        let node = |id: ElementId| {
            update
                .nodes
                .iter()
                .find_map(|(node_id, node)| (node_id.0 == id.as_u64()).then_some(node))
                .expect("checkbox group accessibility node")
        };
        assert_eq!(node(group.root_id()).role(), accesskit::Role::Group);
        let parent = node(group.parent_id());
        assert_eq!(parent.role(), accesskit::Role::CheckBox);
        assert_eq!(parent.toggled(), Some(accesskit::Toggled::False));
        assert_eq!(
            node(group.checkbox_id("red")).role(),
            accesskit::Role::CheckBox
        );

        let renders = cx.render_count(window).unwrap();
        cx.run_until_idle().unwrap();
        assert_eq!(cx.render_count(window).unwrap(), renders);
    }
}