quickgui 0.1.0

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
use crate::{AccessibilityPopup, AccessibilityRole, Element, ElementId, EventContext, FocusHandle};

const DIALOG_ROOT_ID_TAG: u64 = 0x6405_1ba9_158d_f8fd;
const DIALOG_BACKDROP_ID_TAG: u64 = 0xfbf2_90af_5f5b_982d;
const DIALOG_POPUP_ID_TAG: u64 = 0x21e9_d20f_8574_4dd8;
const DIALOG_TITLE_ID_TAG: u64 = 0xb50c_ee8e_b0c0_8ed7;
const DIALOG_DESCRIPTION_ID_TAG: u64 = 0x86a8_0ac0_5627_538e;
const DIALOG_CLOSE_ID_TAG: u64 = 0xe5d1_e6af_b19b_c827;

/// Native accessibility behavior for one in-window modal surface.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum DialogKind {
    /// A modal task or editing surface which may close from its backdrop by default.
    #[default]
    Dialog,
    /// A consequential confirmation surface whose backdrop does not dismiss it by default.
    AlertDialog,
}

impl DialogKind {
    const fn accessibility_role(self) -> AccessibilityRole {
        match self {
            Self::Dialog => AccessibilityRole::Dialog,
            Self::AlertDialog => AccessibilityRole::AlertDialog,
        }
    }

    const fn dismiss_on_backdrop(self) -> bool {
        matches!(self, Self::Dialog)
    }
}

/// Copyable declaration for one controlled, unstyled in-window dialog.
///
/// The application owns the `open` value, all visual declarations, and the listener that changes
/// that value. QuickGUI supplies stable part identities, a viewport overlay, nested topmost focus
/// containment, independent Escape/backdrop dismissal, focus restoration, app-drag exclusion,
/// native-view occlusion through the overlay plane, and exact dialog accessibility semantics.
/// Mount [`Self::root_part`] only while [`Self::is_open`] is true.
///
/// The descriptor retains no task, timer, observer, registry entry, or idle scheduler source.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Dialog {
    id: ElementId,
    open: bool,
    kind: DialogKind,
    initial_focus: Option<FocusHandle>,
    restore_focus: Option<FocusHandle>,
    dismiss_on_escape: bool,
    dismiss_on_backdrop: bool,
}

impl Dialog {
    /// Declare an ordinary modal dialog.
    pub fn new(id: impl Into<ElementId>, open: bool) -> Self {
        Self::with_kind(id, open, DialogKind::Dialog)
    }

    /// Declare a consequential alert dialog.
    ///
    /// Escape remains enabled, while a backdrop press is blocked without dismissing by default.
    pub fn alert(id: impl Into<ElementId>, open: bool) -> Self {
        Self::with_kind(id, open, DialogKind::AlertDialog)
    }

    pub fn with_kind(id: impl Into<ElementId>, open: bool, kind: DialogKind) -> Self {
        Self {
            id: id.into(),
            open,
            kind,
            initial_focus: None,
            restore_focus: None,
            dismiss_on_escape: true,
            dismiss_on_backdrop: kind.dismiss_on_backdrop(),
        }
    }

    pub const fn is_open(self) -> bool {
        self.open
    }

    pub const fn kind(self) -> DialogKind {
        self.kind
    }

    /// Prefer one mounted descendant when this dialog opens.
    ///
    /// Without an explicit target, the focus trap chooses its first enabled Tab stop and falls
    /// back to the popup root when no interactive descendant exists.
    pub fn initial_focus(mut self, focus: impl Into<ElementId>) -> Self {
        self.initial_focus = Some(FocusHandle::new(focus));
        self
    }

    /// Restore focus to one stable control after every dismissal path.
    pub fn restore_focus_to(mut self, focus: impl Into<ElementId>) -> Self {
        self.restore_focus = Some(FocusHandle::new(focus));
        self
    }

    pub const fn dismiss_on_escape(mut self, dismiss: bool) -> Self {
        self.dismiss_on_escape = dismiss;
        self
    }

    pub const fn dismiss_on_backdrop(mut self, dismiss: bool) -> Self {
        self.dismiss_on_backdrop = dismiss;
        self
    }

    pub fn root_id(self) -> ElementId {
        derived_dialog_id(self.id, DIALOG_ROOT_ID_TAG)
    }

    pub fn backdrop_id(self) -> ElementId {
        derived_dialog_id(self.id, DIALOG_BACKDROP_ID_TAG)
    }

    pub fn popup_id(self) -> ElementId {
        derived_dialog_id(self.id, DIALOG_POPUP_ID_TAG)
    }

    pub fn title_id(self) -> ElementId {
        derived_dialog_id(self.id, DIALOG_TITLE_ID_TAG)
    }

    pub fn description_id(self) -> ElementId {
        derived_dialog_id(self.id, DIALOG_DESCRIPTION_ID_TAG)
    }

    pub fn close_id(self) -> ElementId {
        derived_dialog_id(self.id, DIALOG_CLOSE_ID_TAG)
    }

    pub fn popup_focus(self) -> FocusHandle {
        FocusHandle::new(self.popup_id())
    }

    /// Request the declared initial focus in the same event that mounts the dialog.
    pub fn focus_initial(self, cx: &mut EventContext) {
        cx.focus(self.initial_focus.unwrap_or_else(|| self.popup_focus()));
    }

    /// Restore the declared focus after an explicit close action.
    ///
    /// Escape and backdrop dismissal already use the same retained target automatically.
    pub fn focus_restore(self, cx: &mut EventContext) {
        if let Some(focus) = self.restore_focus {
            cx.focus(focus);
        }
    }

    /// Decorate an application-owned trigger without adding appearance.
    pub fn trigger_part(self, id: impl Into<ElementId>, trigger: Element) -> Element {
        let trigger = trigger
            .id(id)
            .focusable()
            .accessibility_role(AccessibilityRole::Button)
            .accessibility_has_popup(AccessibilityPopup::Dialog)
            .accessibility_expanded(self.open)
            .app_region_no_drag()
            .user_select_none();
        if self.open {
            trigger.accessibility_controls(self.popup_id())
        } else {
            trigger
        }
    }

    /// Decorate the full-window portal and active modal focus boundary.
    pub fn root_part(self, root: Element) -> Element {
        root.id(self.root_id())
            .overlay()
            .inset_0()
            .size_full()
            .focus_trap()
            .app_region_no_drag()
            .cursor_default()
    }

    /// Decorate the caller-owned visual backdrop.
    pub fn backdrop_part(self, backdrop: Element) -> Element {
        backdrop
            .id(self.backdrop_id())
            .absolute()
            .inset_0()
            .size_full()
            .app_region_no_drag()
            .cursor_default()
    }

    /// Decorate the caller-owned modal popup.
    ///
    /// The popup is a negative-Tab-index focus fallback. Enabled descendant Tab stops are chosen
    /// first when the enclosing trap mounts. Title and description relationships project only
    /// when the corresponding parts are mounted, so incomplete compositions never emit dangling
    /// native node references.
    pub fn popup_part(self, popup: Element) -> Element {
        let mut popup = popup
            .id(self.popup_id())
            .track_focus(self.popup_focus())
            .tab_index(-1)
            .accessibility_role(self.kind.accessibility_role())
            .accessibility_modal(true)
            .accessibility_labelled_by(self.title_id())
            .accessibility_described_by(self.description_id())
            .app_region_no_drag()
            .cursor_default();
        if self.dismiss_on_escape {
            popup = popup.dismiss_on_escape();
        }
        if self.dismiss_on_backdrop {
            popup = popup.dismiss_on_pointer_outside();
        }
        if let Some(focus) = self.restore_focus {
            popup = popup.restore_focus_to(focus);
        }
        popup
    }

    /// Assign the stable visible label target used by the popup.
    pub fn title_part(self, title: Element) -> Element {
        title.id(self.title_id())
    }

    /// Assign the stable visible description target used by the popup.
    pub fn description_part(self, description: Element) -> Element {
        description.id(self.description_id())
    }

    /// Decorate a caller-owned close control with button behavior and no visual defaults.
    pub fn close_part(self, label: impl Into<std::sync::Arc<str>>, close: Element) -> Element {
        close
            .id(self.close_id())
            .clickable()
            .accessibility_role(AccessibilityRole::Button)
            .accessibility_label(label)
            .app_region_no_drag()
            .user_select_none()
    }
}

fn derived_dialog_id(parent: ElementId, tag: u64) -> ElementId {
    let mut hash = parent.as_u64() ^ 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 == parent.as_u64() || hash == u64::MAX {
        hash ^= tag.rotate_left(17);
    }
    ElementId::new(hash)
}

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

    #[test]
    fn parts_add_exact_behavior_without_appearance() {
        let dialog = Dialog::new("settings", true).restore_focus_to("open-settings");
        let trigger = dialog.trigger_part("open-settings", div());
        assert_eq!(trigger.accessibility.role, AccessibilityRole::Button);
        assert_eq!(
            trigger.accessibility.has_popup,
            Some(AccessibilityPopup::Dialog)
        );
        assert_eq!(trigger.accessibility.expanded, Some(true));
        assert_eq!(
            trigger.accessibility.relations.controls(),
            Some(dialog.popup_id())
        );

        let root = dialog.root_part(div());
        assert!(root.focus_trap);
        assert!(root.portal);
        assert!(root.blocks_pointer);
        assert_eq!(root.visual.background, None);
        assert_eq!(root.visual.border_color, None);

        let popup = dialog.popup_part(div());
        assert_eq!(popup.accessibility.role, AccessibilityRole::Dialog);
        assert!(popup.accessibility.modal);
        assert_eq!(
            popup.accessibility.relations.labelled_by(),
            Some(dialog.title_id())
        );
        assert_eq!(
            popup.accessibility.relations.described_by(),
            Some(dialog.description_id())
        );
        assert!(popup.dismiss_policy.on_escape());
        assert!(popup.dismiss_policy.on_pointer_outside());
        assert_eq!(popup.visual.background, None);
        assert_eq!(popup.visual.border_color, None);

        let alert = Dialog::alert("delete", true).popup_part(div());
        assert_eq!(alert.accessibility.role, AccessibilityRole::AlertDialog);
        assert!(alert.dismiss_policy.on_escape());
        assert!(!alert.dismiss_policy.on_pointer_outside());
    }

    #[test]
    fn derived_part_ids_are_stable_distinct_and_never_reuse_the_base() {
        let dialog = Dialog::new(0_u64, true);
        let ids = [
            dialog.root_id(),
            dialog.backdrop_id(),
            dialog.popup_id(),
            dialog.title_id(),
            dialog.description_id(),
            dialog.close_id(),
        ];
        for (index, id) in ids.iter().enumerate() {
            assert_ne!(*id, ElementId::new(0));
            assert_ne!(*id, ElementId::new(u64::MAX));
            assert!(!ids[..index].contains(id));
        }
        assert_eq!(dialog.popup_id(), Dialog::new(0_u64, false).popup_id());
    }

    #[derive(Default)]
    struct DialogView {
        open: bool,
        closed: usize,
    }

    impl View for DialogView {
        fn render(&mut self, cx: &mut ViewContext<'_, Self>) -> impl IntoElement {
            let dialog = Dialog::new("test-dialog", self.open)
                .initial_focus("dialog-first")
                .restore_focus_to("dialog-trigger");
            let open = cx.listener("dialog-trigger", move |view, cx| {
                view.open = true;
                dialog.focus_initial(cx);
                cx.invalidate();
            });
            let close = cx.listener(dialog.close_id(), move |view, cx| {
                view.open = false;
                view.closed += 1;
                dialog.focus_restore(cx);
                cx.invalidate();
            });
            let dismiss = cx.dismiss_listener(dialog.popup_id(), move |view, cx| {
                view.open = false;
                view.closed += 1;
                cx.invalidate();
            });

            let mut root = div()
                .size_full()
                .relative()
                .child(
                    dialog
                        .trigger_part("dialog-trigger", button().child("Open"))
                        .on_click(open),
                )
                .child(button().id("outside").child("Outside"));
            if self.open {
                let popup = dialog
                    .popup_part(
                        div()
                            .w(240.0)
                            .h(160.0)
                            .bg(Color::BLACK)
                            .child(dialog.title_part(text("Settings")))
                            .child(dialog.description_part(text("Change settings")))
                            .child(button().id("dialog-first").child("First"))
                            .child(button().id("dialog-second").child("Second"))
                            .child(dialog.close_part("Close settings", div()).on_click(close)),
                    )
                    .on_dismiss(dismiss);
                root = root.child(
                    dialog
                        .root_part(div().flex_row().items_center().justify_center())
                        .child(dialog.backdrop_part(div()))
                        .child(popup),
                );
            }
            root
        }
    }

    #[test]
    fn controlled_dialog_traps_tabs_restores_focus_and_sleeps() {
        let (mut cx, view) = App::new(DialogView::default()).into_test_context().unwrap();
        let window = view.window_handle();
        cx.click(window, "dialog-trigger").unwrap();
        assert_eq!(cx.focused(window).unwrap(), Some("dialog-first".into()));

        cx.simulate_keystrokes(window, "tab").unwrap();
        assert_eq!(cx.focused(window).unwrap(), Some("dialog-second".into()));
        cx.simulate_keystrokes(window, "tab").unwrap();
        assert_eq!(
            cx.focused(window).unwrap(),
            Some(Dialog::new("test-dialog", true).close_id())
        );
        cx.simulate_keystrokes(window, "tab").unwrap();
        assert_eq!(cx.focused(window).unwrap(), Some("dialog-first".into()));
        assert!(cx.focus(window, "outside").is_err());

        cx.simulate_keystrokes(window, "escape").unwrap();
        assert!(!cx.read(view, |view| view.open).unwrap());
        assert_eq!(cx.read(view, |view| view.closed).unwrap(), 1);
        assert_eq!(cx.focused(window).unwrap(), Some("dialog-trigger".into()));

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