1
//! Accessibility types for screen reader support.
2
//!
3
//! Key types:
4
//! - [`AccessibilityInfo`] — full accessibility metadata for a UI element
5
//! - [`SmallAriaInfo`] — lightweight alternative for common cases (label + role + description)
6
//! - [`AccessibilityRole`] — element purpose (button, link, checkbox, etc.)
7
//! - [`AccessibilityState`] — dynamic state (focused, checked, expanded, etc.)
8
//! - [`AccessibilityAction`] — actions performable on an element (click, scroll, etc.)
9
//!
10
//! These types are consumed by `layout/src/managers/a11y.rs` and mapped to
11
//! platform accessibility backends in `dll/src/desktop/shell2/`.
12

            
13
use alloc::vec::Vec;
14

            
15
use azul_css::{props::basic::length::FloatValue, AzString, OptionF32, OptionString};
16

            
17
use crate::{dom::OptionDomNodeId, geom::LogicalPosition, window::OptionVirtualKeyCodeCombo};
18

            
19
/// Holds information about a UI element for accessibility purposes (e.g., screen readers).
20
/// This is a wrapper for platform-specific accessibility APIs like MSAA.
21
#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
22
#[repr(C)]
23
pub struct AccessibilityInfo {
24
    /// The accessible name (e.g., button or checkbox text).
25
    pub accessibility_name: OptionString,
26
    /// The current value (e.g., slider number, link URL, or input text).
27
    pub accessibility_value: OptionString,
28
    /// Optional text description providing additional context about the element.
29
    /// Maps to `aria-description` / accesskit's `set_description()`.
30
    pub description: OptionString,
31
    /// Optional keyboard accelerator.
32
    pub accelerator: OptionVirtualKeyCodeCombo,
33
    /// Optional "default action" description. Only used when there is at least
34
    /// one `ComponentEventFilter::DefaultAction` callback present on this node.
35
    pub default_action: OptionString,
36
    /// Possible on/off states, such as focused, focusable, selected, selectable,
37
    /// visible, protected (for passwords), checked, etc.
38
    pub states: AccessibilityStateVec,
39
    /// A list of actions the user can perform on this element.
40
    /// Maps to accesskit's Action enum.
41
    pub supported_actions: AccessibilityActionVec,
42
    /// ID of another node that labels this one (for `aria-labelledby`).
43
    pub labelled_by: OptionDomNodeId,
44
    /// ID of another node that describes this one (for `aria-describedby`).
45
    pub described_by: OptionDomNodeId,
46
    /// The element's role (e.g., link, static text, checkbox).
47
    pub role: AccessibilityRole,
48
    /// For live regions that update automatically (e.g., chat messages, timers).
49
    /// Maps to accesskit's `Live` property.
50
    pub is_live_region: bool,
51
}
52

            
53
impl AccessibilityInfo {
54
    /// Creates an `AccessibilityInfo` with a name and a role.
55
    #[must_use]
56
186
    pub fn named(name: impl Into<AzString>, role: AccessibilityRole) -> Self {
57
186
        Self {
58
186
            accessibility_name: OptionString::Some(name.into()),
59
186
            role,
60
186
            ..Self::default()
61
186
        }
62
186
    }
63

            
64
    /// Creates an `AccessibilityInfo` labelled by another DOM node ID.
65
    #[must_use]
66
    pub fn labelled_by_node(label: crate::dom::DomNodeId, role: AccessibilityRole) -> Self {
67
        Self {
68
            labelled_by: OptionDomNodeId::Some(label),
69
            role,
70
            ..Self::default()
71
        }
72
    }
73

            
74
    /// Overlays `patch` onto `self`, updating only the fields explicitly set in `patch`.
75
    #[allow(clippy::needless_pass_by_value)] // by value: crosses the FFI, where the argument
76
                                             // arrives owned
77
159
    pub fn assign(&mut self, patch: Self) {
78
159
        if patch.accessibility_name.is_some() {
79
158
            self.accessibility_name = patch.accessibility_name.clone();
80
158
        }
81
159
        if patch.accessibility_value.is_some() {
82
            self.accessibility_value = patch.accessibility_value.clone();
83
159
        }
84
159
        if patch.description.is_some() {
85
            self.description = patch.description.clone();
86
159
        }
87
159
        if patch.accelerator.is_some() {
88
            self.accelerator = patch.accelerator.clone();
89
159
        }
90
159
        if patch.default_action.is_some() {
91
            self.default_action = patch.default_action.clone();
92
159
        }
93
159
        if !patch.states.as_ref().is_empty() {
94
            self.states = patch.states.clone();
95
159
        }
96
159
        if !patch.supported_actions.as_ref().is_empty() {
97
            self.supported_actions = patch.supported_actions.clone();
98
159
        }
99
159
        if patch.labelled_by.is_some() {
100
            self.labelled_by = patch.labelled_by;
101
159
        }
102
159
        if patch.described_by.is_some() {
103
            self.described_by = patch.described_by;
104
159
        }
105
159
        if !matches!(patch.role, AccessibilityRole::Unknown) {
106
109
            self.role = patch.role;
107
110
        }
108
159
        if patch.is_live_region {
109
            self.is_live_region = true;
110
159
        }
111
159
    }
112

            
113
    /// [`AccessibilityInfo::assign`] as a builder: returns the merged value.
114
    #[must_use]
115
156
    pub fn assigned(mut self, patch: Self) -> Self {
116
156
        self.assign(patch);
117
156
        self
118
156
    }
119

            
120
    /// Attach a live value — a slider's position, a progress percentage.
121
    #[must_use]
122
81
    pub fn with_value(mut self, value: impl Into<AzString>) -> Self {
123
81
        self.accessibility_value = OptionString::Some(value.into());
124
81
        self
125
81
    }
126
}
127

            
128
impl Default for AccessibilityInfo {
129
    /// Provides an empty declaration with an `Unknown` role.
130
340206
    fn default() -> Self {
131
340206
        Self {
132
340206
            accessibility_name: OptionString::None,
133
340206
            accessibility_value: OptionString::None,
134
340206
            description: OptionString::None,
135
340206
            accelerator: OptionVirtualKeyCodeCombo::None,
136
340206
            default_action: OptionString::None,
137
340206
            states: AccessibilityStateVec::from_const_slice(&[]),
138
340206
            supported_actions: AccessibilityActionVec::from_const_slice(&[]),
139
340206
            labelled_by: OptionDomNodeId::None,
140
340206
            described_by: OptionDomNodeId::None,
141
340206
            role: AccessibilityRole::Unknown,
142
340206
            is_live_region: false,
143
340206
        }
144
340206
    }
145
}
146

            
147
/// Actions that can be performed on an accessible element.
148
/// This is a simplified version of `accesskit::Action` to avoid direct dependency in core.
149
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
150
#[repr(C, u8)]
151
pub enum AccessibilityAction {
152
    /// The default action for the element (usually a click).
153
    Default,
154
    /// Set focus to this element.
155
    Focus,
156
    /// Remove focus from this element.
157
    Blur,
158
    /// Collapse an expandable element (e.g., tree node, accordion).
159
    Collapse,
160
    /// Expand a collapsible element (e.g., tree node, accordion).
161
    Expand,
162
    /// Scroll this element into view.
163
    ScrollIntoView,
164
    /// Increment a numeric value (e.g., slider, spinner).
165
    Increment,
166
    /// Decrement a numeric value (e.g., slider, spinner).
167
    Decrement,
168
    /// Show a context menu.
169
    ShowContextMenu,
170
    /// Hide a tooltip.
171
    HideTooltip,
172
    /// Show a tooltip.
173
    ShowTooltip,
174
    /// Scroll up.
175
    ScrollUp,
176
    /// Scroll down.
177
    ScrollDown,
178
    /// Scroll left.
179
    ScrollLeft,
180
    /// Scroll right.
181
    ScrollRight,
182
    /// Replace selected text with new text.
183
    ReplaceSelectedText(AzString),
184
    /// Scroll to a specific point.
185
    ScrollToPoint(LogicalPosition),
186
    /// Set scroll offset.
187
    SetScrollOffset(LogicalPosition),
188
    /// Set text selection.
189
    SetTextSelection(TextSelectionStartEnd),
190
    /// Set sequential focus navigation starting point.
191
    SetSequentialFocusNavigationStartingPoint,
192
    /// Set the value of a control.
193
    SetValue(AzString),
194
    /// Set numeric value of a control.
195
    SetNumericValue(FloatValue),
196
    /// Custom action with ID.
197
    CustomAction(i32),
198
}
199

            
200
/// Represents the start and end indices of a text selection.
201
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
202
#[repr(C)]
203
pub struct TextSelectionStartEnd {
204
    /// The starting index of the selection.
205
    pub selection_start: usize,
206
    /// The ending index of the selection.
207
    pub selection_end: usize,
208
}
209

            
210
impl_vec!(
211
    AccessibilityAction,
212
    AccessibilityActionVec,
213
    AccessibilityActionVecDestructor,
214
    AccessibilityActionVecDestructorType,
215
    AccessibilityActionVecSlice,
216
    OptionAccessibilityAction
217
);
218
impl_vec_debug!(AccessibilityAction, AccessibilityActionVec);
219
impl_vec_clone!(
220
    AccessibilityAction,
221
    AccessibilityActionVec,
222
    AccessibilityActionVecDestructor
223
);
224
impl_vec_partialeq!(AccessibilityAction, AccessibilityActionVec);
225
impl_vec_eq!(AccessibilityAction, AccessibilityActionVec);
226
impl_vec_partialord!(AccessibilityAction, AccessibilityActionVec);
227
impl_vec_ord!(AccessibilityAction, AccessibilityActionVec);
228
impl_vec_hash!(AccessibilityAction, AccessibilityActionVec);
229

            
230
impl_option![
231
    AccessibilityAction,
232
    OptionAccessibilityAction,
233
    copy = false,
234
    [Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
235
];
236

            
237
impl_option!(
238
    AccessibilityInfo,
239
    OptionAccessibilityInfo,
240
    copy = false,
241
    [Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
242
);
243

            
244
/// Defines the element's purpose for accessibility APIs, informing assistive technologies
245
/// like screen readers about the function of a UI element.
246
///
247
/// Each variant corresponds to a
248
/// standard control type or UI structure.
249
///
250
/// For more details, see the [MSDN Role Constants page](https://docs.microsoft.com/en-us/windows/winauto/object-roles).
251
#[repr(C)]
252
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
253
pub enum AccessibilityRole {
254
    /// Represents the title or caption bar of a window.
255
    TitleBar,
256
    /// Represents a menu bar at the top of a window.
257
    MenuBar,
258
    /// Represents a vertical or horizontal scroll bar.
259
    ScrollBar,
260
    /// Represents a handle or grip used for moving or resizing.
261
    Grip,
262
    /// Represents a system sound indicating an event.
263
    Sound,
264
    /// Represents the system's mouse pointer or other pointing device.
265
    Cursor,
266
    /// Represents the text insertion point indicator.
267
    Caret,
268
    /// Represents an alert or notification.
269
    Alert,
270
    /// Represents a window frame.
271
    Window,
272
    /// Represents a window's client area, where the main content is displayed.
273
    Client,
274
    /// Represents a pop-up menu.
275
    MenuPopup,
276
    /// Represents an individual item within a menu.
277
    MenuItem,
278
    /// Represents a small pop-up window that provides information.
279
    Tooltip,
280
    /// Represents the main window of an application.
281
    Application,
282
    /// Represents a document window within an application.
283
    Document,
284
    /// Represents a pane or a distinct section of a window.
285
    Pane,
286
    /// Represents a graphical chart or graph.
287
    Chart,
288
    /// Represents a dialog box or message box.
289
    Dialog,
290
    /// Represents a window's border.
291
    Border,
292
    /// Represents a group of related controls.
293
    Grouping,
294
    /// Represents a visual separator.
295
    Separator,
296
    /// Represents a toolbar containing a group of controls.
297
    Toolbar,
298
    /// Represents a status bar for displaying information.
299
    StatusBar,
300
    /// Represents a data table.
301
    Table,
302
    /// Represents a column header in a table.
303
    ColumnHeader,
304
    /// Represents a row header in a table.
305
    RowHeader,
306
    /// Represents a full column of cells in a table.
307
    Column,
308
    /// Represents a full row of cells in a table.
309
    Row,
310
    /// Represents a single cell within a table.
311
    Cell,
312
    /// Represents a hyperlink to a resource.
313
    Link,
314
    /// Represents a help balloon or pop-up.
315
    HelpBalloon,
316
    /// Represents an animated, character-like graphic object.
317
    Character,
318
    /// Represents a list of items.
319
    List,
320
    /// Represents an individual item within a list.
321
    ListItem,
322
    /// Represents an outline or tree structure.
323
    Outline,
324
    /// Represents an individual item within an outline or tree.
325
    OutlineItem,
326
    /// Represents a single tab in a tabbed interface.
327
    PageTab,
328
    /// Represents the content of a page in a property sheet.
329
    PropertyPage,
330
    /// Represents a visual indicator, like a slider thumb.
331
    Indicator,
332
    /// Represents a picture or graphical image.
333
    Graphic,
334
    /// Represents read-only text.
335
    StaticText,
336
    /// Represents editable text or a text area.
337
    Text,
338
    /// Represents a standard push button.
339
    PushButton,
340
    /// Represents a check box control.
341
    CheckButton,
342
    /// Represents a radio button.
343
    RadioButton,
344
    /// Represents a combination of a text field and a drop-down list.
345
    ComboBox,
346
    /// Represents a drop-down list box.
347
    DropList,
348
    /// Represents a progress bar.
349
    ProgressBar,
350
    /// Represents a dial or knob.
351
    Dial,
352
    /// Represents a control for entering a keyboard shortcut.
353
    HotkeyField,
354
    /// Represents a slider for selecting a value within a range.
355
    Slider,
356
    /// Represents a spin button (up/down arrows) for incrementing or decrementing a value.
357
    SpinButton,
358
    /// Represents a diagram or flowchart.
359
    Diagram,
360
    /// Represents an animation control.
361
    Animation,
362
    /// Represents a mathematical equation.
363
    Equation,
364
    /// Represents a button that drops down a list of items.
365
    ButtonDropdown,
366
    /// Represents a button that drops down a full menu.
367
    ButtonMenu,
368
    /// Represents a button that drops down a grid for selection.
369
    ButtonDropdownGrid,
370
    /// Represents blank space between other objects.
371
    Whitespace,
372
    /// Represents the container for a set of tabs.
373
    PageTabList,
374
    /// Represents a clock control.
375
    Clock,
376
    /// Represents a button with two parts: a default action and a dropdown.
377
    SplitButton,
378
    /// Represents a control for entering an IP address.
379
    IpAddress,
380
    /// Represents an element with no specific role.
381
    Nothing,
382
    /// Unknown or unspecified role.
383
    Unknown,
384
}
385

            
386
impl_option!(
387
    AccessibilityRole,
388
    OptionAccessibilityRole,
389
    [Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
390
);
391

            
392
/// Defines the current state of an element for accessibility APIs (e.g., focused, checked).
393
/// These states provide dynamic information to assistive technologies about the element's
394
/// condition.
395
///
396
/// See the [MSDN State Constants page](https://docs.microsoft.com/en-us/windows/win32/winauto/object-state-constants) for more details.
397
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
398
#[repr(C)]
399
pub enum AccessibilityState {
400
    /// The element is unavailable and cannot be interacted with.
401
    Unavailable,
402
    /// The element is selected.
403
    Selected,
404
    /// The element has the keyboard focus.
405
    Focused,
406
    /// The element is checked, toggled, or in an "on" state.
407
    CheckedTrue,
408
    /// The element is unchecked, untoggled, or in an "off" state.
409
    CheckedFalse,
410
    /// The element's content cannot be edited by the user.
411
    Readonly,
412
    /// The element is the default action in a dialog or form.
413
    Default,
414
    /// The element is expanded, showing its child items.
415
    Expanded,
416
    /// The element is collapsed, hiding its child items.
417
    Collapsed,
418
    /// The element is busy and cannot respond to user interaction.
419
    Busy,
420
    /// The element is not currently visible on the screen.
421
    Offscreen,
422
    /// The element can accept keyboard focus.
423
    Focusable,
424
    /// The element is a container whose children can be selected.
425
    Selectable,
426
    /// The element is a hyperlink.
427
    Linked,
428
    /// The element is a hyperlink that has been visited.
429
    Traversed,
430
    /// The element allows multiple of its children to be selected at once.
431
    Multiselectable,
432
    /// The element contains protected content that should not be read aloud.
433
    Protected,
434
}
435

            
436
impl_option!(
437
    AccessibilityState,
438
    OptionAccessibilityState,
439
    [Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash]
440
);
441

            
442
impl_vec!(
443
    AccessibilityState,
444
    AccessibilityStateVec,
445
    AccessibilityStateVecDestructor,
446
    AccessibilityStateVecDestructorType,
447
    AccessibilityStateVecSlice,
448
    OptionAccessibilityState
449
);
450
impl_vec_clone!(
451
    AccessibilityState,
452
    AccessibilityStateVec,
453
    AccessibilityStateVecDestructor
454
);
455
impl_vec_debug!(AccessibilityState, AccessibilityStateVec);
456
impl_vec_partialeq!(AccessibilityState, AccessibilityStateVec);
457
impl_vec_partialord!(AccessibilityState, AccessibilityStateVec);
458
impl_vec_eq!(AccessibilityState, AccessibilityStateVec);
459
impl_vec_ord!(AccessibilityState, AccessibilityStateVec);
460
impl_vec_hash!(AccessibilityState, AccessibilityStateVec);
461

            
462
/// Compact accessibility information for common use cases.
463
///
464
/// This is a lighter-weight alternative to `AccessibilityInfo` for cases where
465
/// only basic accessibility properties are needed. Developers must explicitly
466
/// pass `None` if they choose not to provide accessibility information.
467
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
468
#[repr(C)]
469
pub struct SmallAriaInfo {
470
    /// Accessible label/name
471
    pub label: OptionString,
472
    /// Element's role (button, link, etc.)
473
    pub role: OptionAccessibilityRole,
474
    /// Additional description
475
    pub description: OptionString,
476
}
477

            
478
impl_option!(
479
    SmallAriaInfo,
480
    OptionSmallAriaInfo,
481
    copy = false,
482
    [Debug, Clone, PartialEq, Eq, Hash]
483
);
484

            
485
impl SmallAriaInfo {
486
    /// Creates a `SmallAriaInfo` with the given accessible label.
487
857
    pub fn label<S: Into<AzString>>(text: S) -> Self {
488
857
        Self {
489
857
            label: OptionString::Some(text.into()),
490
857
            role: OptionAccessibilityRole::None,
491
857
            description: OptionString::None,
492
857
        }
493
857
    }
494

            
495
    /// Builder method for setting self.role
496
    #[must_use]
497
70
    pub const fn with_role(mut self, role: AccessibilityRole) -> Self {
498
70
        self.role = OptionAccessibilityRole::Some(role);
499
70
        self
500
70
    }
501

            
502
    /// Builder method for setting self.description
503
    #[must_use]
504
258
    pub fn with_description<S: Into<AzString>>(mut self, desc: S) -> Self {
505
258
        self.description = OptionString::Some(desc.into());
506
258
        self
507
258
    }
508

            
509
    /// Convert to full `AccessibilityInfo`
510
    #[must_use]
511
1205
    pub fn to_full_info(&self) -> AccessibilityInfo {
512
        AccessibilityInfo {
513
1205
            accessibility_name: self.label.clone(),
514
1205
            accessibility_value: OptionString::None,
515
1205
            description: self.description.clone(),
516
1205
            role: match self.role {
517
655
                OptionAccessibilityRole::Some(r) => r,
518
550
                OptionAccessibilityRole::None => AccessibilityRole::Unknown,
519
            },
520
1205
            states: Vec::new().into(),
521
1205
            accelerator: OptionVirtualKeyCodeCombo::None,
522
1205
            default_action: OptionString::None,
523
1205
            supported_actions: Vec::new().into(),
524
            is_live_region: false,
525
1205
            labelled_by: OptionDomNodeId::None,
526
1205
            described_by: OptionDomNodeId::None,
527
        }
528
1205
    }
529
}
530

            
531
/// Accessibility information for a `<progress>` indicator.
532
///
533
/// Mirrors HTML's `<progress value max>` plus an `indeterminate` flag for
534
/// progress bars whose end is unknown. Maps to `AccessibilityRole::ProgressBar`.
535
#[derive(Debug, Clone, PartialEq, Eq)]
536
#[repr(C)]
537
pub struct ProgressAriaInfo {
538
    /// Accessible label describing the task being measured.
539
    pub label: OptionString,
540
    /// Current progress value. `None` for indeterminate progress.
541
    pub current_value: OptionF32,
542
    /// Maximum value the progress bar can reach. `None` falls back to `1.0`.
543
    pub max: OptionF32,
544
    /// `true` for spinners / progress with no known endpoint. Overrides `current_value`.
545
    pub indeterminate: bool,
546
    /// Optional extended description (`aria-describedby` equivalent).
547
    pub description: OptionString,
548
}
549

            
550
impl_option!(
551
    ProgressAriaInfo,
552
    OptionProgressAriaInfo,
553
    copy = false,
554
    [Debug, Clone, PartialEq, Eq]
555
);
556

            
557
impl ProgressAriaInfo {
558
    /// Creates a `ProgressAriaInfo` with only an accessible label.
559
    #[must_use]
560
1803
    pub const fn create(label: AzString) -> Self {
561
1803
        Self {
562
1803
            label: OptionString::Some(label),
563
1803
            current_value: OptionF32::None,
564
1803
            max: OptionF32::None,
565
1803
            indeterminate: false,
566
1803
            description: OptionString::None,
567
1803
        }
568
1803
    }
569

            
570
    /// Returns a copy with the given current value.
571
    #[must_use]
572
1033
    pub const fn with_current_value(mut self, value: f32) -> Self {
573
1033
        self.current_value = OptionF32::Some(value);
574
1033
        self
575
1033
    }
576

            
577
    /// Returns a copy with the given maximum value.
578
    #[must_use]
579
513
    pub const fn with_max(mut self, max: f32) -> Self {
580
513
        self.max = OptionF32::Some(max);
581
513
        self
582
513
    }
583

            
584
    /// Returns a copy with the indeterminate flag set.
585
    #[must_use]
586
5
    pub const fn with_indeterminate(mut self, indeterminate: bool) -> Self {
587
5
        self.indeterminate = indeterminate;
588
5
        self
589
5
    }
590

            
591
    /// Returns a copy with the given description.
592
    #[must_use]
593
258
    pub fn with_description(mut self, desc: AzString) -> Self {
594
258
        self.description = OptionString::Some(desc);
595
258
        self
596
258
    }
597

            
598
    /// Convert to full `AccessibilityInfo` so the value can be installed on a node.
599
    #[must_use]
600
1033
    pub fn to_full_info(&self) -> AccessibilityInfo {
601
1033
        let value_string = if self.indeterminate {
602
1
            OptionString::None
603
        } else {
604
1032
            match self.current_value {
605
1031
                OptionF32::Some(v) => OptionString::Some(format!("{v}").into()),
606
1
                OptionF32::None => OptionString::None,
607
            }
608
        };
609
1033
        AccessibilityInfo {
610
1033
            accessibility_name: self.label.clone(),
611
1033
            accessibility_value: value_string,
612
1033
            description: self.description.clone(),
613
1033
            role: AccessibilityRole::ProgressBar,
614
1033
            states: Vec::new().into(),
615
1033
            accelerator: OptionVirtualKeyCodeCombo::None,
616
1033
            default_action: OptionString::None,
617
1033
            supported_actions: Vec::new().into(),
618
1033
            is_live_region: false,
619
1033
            labelled_by: OptionDomNodeId::None,
620
1033
            described_by: OptionDomNodeId::None,
621
1033
        }
622
1033
    }
623
}
624

            
625
/// Accessibility information for a `<meter>` gauge.
626
///
627
/// Unlike `<progress>`, `<meter>` always carries a known `value`/`min`/`max`
628
/// triple, so those fields are required at construction time. Maps to
629
/// `AccessibilityRole::Indicator`.
630
#[derive(Debug, Clone, PartialEq)]
631
#[repr(C)]
632
pub struct MeterAriaInfo {
633
    /// Accessible label describing what the meter measures.
634
    pub label: OptionString,
635
    /// Current value of the meter (within `[min, max]`).
636
    pub current_value: f32,
637
    /// Lower bound of the measurement range.
638
    pub min: f32,
639
    /// Upper bound of the measurement range.
640
    pub max: f32,
641
    /// Optional "low" threshold (values below this are considered low).
642
    pub low: OptionF32,
643
    /// Optional "high" threshold (values above this are considered high).
644
    pub high: OptionF32,
645
    /// Optional optimum value within the range.
646
    pub optimum: OptionF32,
647
    /// Optional extended description.
648
    pub description: OptionString,
649
}
650

            
651
impl_option!(
652
    MeterAriaInfo,
653
    OptionMeterAriaInfo,
654
    copy = false,
655
    [Debug, Clone, PartialEq]
656
);
657

            
658
impl MeterAriaInfo {
659
    /// Creates a `MeterAriaInfo` with the required label and value/range triple.
660
    #[must_use]
661
786
    pub const fn create(label: AzString, current_value: f32, min: f32, max: f32) -> Self {
662
786
        Self {
663
786
            label: OptionString::Some(label),
664
786
            current_value,
665
786
            min,
666
786
            max,
667
786
            low: OptionF32::None,
668
786
            high: OptionF32::None,
669
786
            optimum: OptionF32::None,
670
786
            description: OptionString::None,
671
786
        }
672
786
    }
673

            
674
    /// Returns a copy with the given low threshold.
675
    #[must_use]
676
262
    pub const fn with_low(mut self, low: f32) -> Self {
677
262
        self.low = OptionF32::Some(low);
678
262
        self
679
262
    }
680

            
681
    /// Returns a copy with the given high threshold.
682
    #[must_use]
683
259
    pub const fn with_high(mut self, high: f32) -> Self {
684
259
        self.high = OptionF32::Some(high);
685
259
        self
686
259
    }
687

            
688
    /// Returns a copy with the given optimum value.
689
    #[must_use]
690
259
    pub const fn with_optimum(mut self, optimum: f32) -> Self {
691
259
        self.optimum = OptionF32::Some(optimum);
692
259
        self
693
259
    }
694

            
695
    /// Returns a copy with the given description.
696
    #[must_use]
697
257
    pub fn with_description(mut self, desc: AzString) -> Self {
698
257
        self.description = OptionString::Some(desc);
699
257
        self
700
257
    }
701

            
702
    /// Convert to full `AccessibilityInfo` so the value can be installed on a node.
703
    #[must_use]
704
272
    pub fn to_full_info(&self) -> AccessibilityInfo {
705
272
        AccessibilityInfo {
706
272
            accessibility_name: self.label.clone(),
707
272
            accessibility_value: OptionString::Some(format!("{}", self.current_value).into()),
708
272
            description: self.description.clone(),
709
272
            role: AccessibilityRole::Indicator,
710
272
            states: Vec::new().into(),
711
272
            accelerator: OptionVirtualKeyCodeCombo::None,
712
272
            default_action: OptionString::None,
713
272
            supported_actions: Vec::new().into(),
714
272
            is_live_region: false,
715
272
            labelled_by: OptionDomNodeId::None,
716
272
            described_by: OptionDomNodeId::None,
717
272
        }
718
272
    }
719
}
720

            
721
/// Accessibility information for a `<dialog>` element.
722
///
723
/// Captures the modal/non-modal distinction and a reference to a separate
724
/// node that describes the dialog (`aria-describedby`). The `role` defaults
725
/// to `AccessibilityRole::Dialog` but can be overridden (e.g., for alert
726
/// dialogs).
727
#[derive(Debug, Clone, PartialEq, Eq)]
728
#[repr(C)]
729
pub struct DialogAriaInfo {
730
    /// Accessible label / title for the dialog.
731
    pub label: OptionString,
732
    /// Optional ID of another node that describes the dialog content.
733
    pub described_by: OptionString,
734
    /// Optional inline description.
735
    pub description: OptionString,
736
    /// Role for the dialog. Defaults to `Dialog`; use `Alert` for urgent dialogs.
737
    pub role: AccessibilityRole,
738
    /// `true` if the dialog is modal (focus trapped, background inert).
739
    pub modal: bool,
740
}
741

            
742
impl_option!(
743
    DialogAriaInfo,
744
    OptionDialogAriaInfo,
745
    copy = false,
746
    [Debug, Clone, PartialEq, Eq]
747
);
748

            
749
impl DialogAriaInfo {
750
    /// Creates a `DialogAriaInfo` with the given accessible label. Defaults
751
    /// to a non-modal dialog with role `Dialog`.
752
    #[must_use]
753
783
    pub const fn create(label: AzString) -> Self {
754
783
        Self {
755
783
            label: OptionString::Some(label),
756
783
            modal: false,
757
783
            described_by: OptionString::None,
758
783
            role: AccessibilityRole::Dialog,
759
783
            description: OptionString::None,
760
783
        }
761
783
    }
762

            
763
    /// Returns a copy with the given modality flag.
764
    #[must_use]
765
7
    pub const fn with_modal(mut self, modal: bool) -> Self {
766
7
        self.modal = modal;
767
7
        self
768
7
    }
769

            
770
    /// Returns a copy with `aria-describedby` pointing at the given node ID.
771
    #[must_use]
772
259
    pub fn with_described_by(mut self, described_by: AzString) -> Self {
773
259
        self.described_by = OptionString::Some(described_by);
774
259
        self
775
259
    }
776

            
777
    /// Returns a copy with the given role (defaults to `Dialog`).
778
    #[must_use]
779
11
    pub const fn with_role(mut self, role: AccessibilityRole) -> Self {
780
11
        self.role = role;
781
11
        self
782
11
    }
783

            
784
    /// Returns a copy with the given inline description.
785
    #[must_use]
786
257
    pub fn with_description(mut self, desc: AzString) -> Self {
787
257
        self.description = OptionString::Some(desc);
788
257
        self
789
257
    }
790

            
791
    /// Convert to full `AccessibilityInfo` so the value can be installed on a node.
792
    #[must_use]
793
5
    pub fn to_full_info(&self) -> AccessibilityInfo {
794
5
        AccessibilityInfo {
795
5
            accessibility_name: self.label.clone(),
796
5
            accessibility_value: OptionString::None,
797
5
            description: self.description.clone(),
798
5
            role: self.role,
799
5
            states: Vec::new().into(),
800
5
            accelerator: OptionVirtualKeyCodeCombo::None,
801
5
            default_action: OptionString::None,
802
5
            supported_actions: Vec::new().into(),
803
5
            is_live_region: false,
804
5
            labelled_by: OptionDomNodeId::None,
805
5
            described_by: OptionDomNodeId::None,
806
5
        }
807
5
    }
808
}
809

            
810
#[cfg(test)]
811
#[path = "a11y_test.rs"]
812
mod a11y_test;