xa11y-core 0.14.0

Core types, traits, and selector engine for xa11y cross-platform accessibility
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
use std::collections::HashMap;
use std::fmt;
use std::ops::Deref;
use std::sync::Arc;

use serde::{Deserialize, Serialize};

use crate::error::Error;
use crate::provider::Provider;
use crate::role::Role;

/// Declare a reader/writer struct pair from one field list.
///
/// `#[non_exhaustive]` gives *readers* a stability guarantee: adding a field
/// does not break a consumer that only reads one. *Writers* need the opposite
/// — a new field should stop their build until they have decided what it
/// means. Two types satisfy both, but only if their field sets cannot drift,
/// which is what this macro enforces: there is one list, so a field cannot be
/// added to one type and defaulted in the other.
///
/// The reader gets the doc comments, serde attributes, and `#[non_exhaustive]`.
/// The writer gets the bare fields and stays exhaustive, so a struct literal
/// in another crate fails to compile the moment the list grows.
macro_rules! reader_writer_pair {
    (
        $(#[$reader_meta:meta])*
        pub struct $reader:ident;

        $(#[$writer_meta:meta])*
        pub struct $writer:ident;

        fields {
            $(
                $(#[$field_meta:meta])*
                pub $field:ident : $ty:ty,
            )*
        }
    ) => {
        $(#[$reader_meta])*
        #[non_exhaustive]
        pub struct $reader {
            $(
                $(#[$field_meta])*
                pub $field: $ty,
            )*
        }

        $(#[$writer_meta])*
        #[doc(hidden)]
        pub struct $writer {
            $(pub $field: $ty,)*
        }

        impl From<$writer> for $reader {
            fn from(parts: $writer) -> Self {
                // Destructured, not field-by-field: both halves come from the
                // macro's single field list, so neither can gain a field the
                // other silently defaults.
                let $writer { $($field,)* } = parts;
                Self { $($field,)* }
            }
        }
    };
}

// Importable by path from sibling modules (`Event` uses it too).
pub(crate) use reader_writer_pair;

reader_writer_pair! {
    /// The raw data for a single element in an accessibility tree.
    ///
    /// This is the underlying data struct. Most consumers should use
    /// [`Element`], which wraps `ElementData` with a provider reference for
    /// lazy navigation. `ElementData` is used directly by provider
    /// implementors.
    ///
    /// `#[non_exhaustive]`: this is the type that grows every time the
    /// normalized element model learns a new property, so adding a field must
    /// not break the consumers that only ever *read* one. Providers, which
    /// *write* one, get the opposite guarantee from [`ElementParts`].
    ///
    /// Build a partial element (an event target, a test fixture) with
    /// [`ElementData::for_role`] and assign what you have:
    ///
    /// ```
    /// # use xa11y_core::{ElementData, Role};
    /// let mut data = ElementData::for_role(Role::Button);
    /// data.name = Some("Submit".to_string());
    /// ```
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct ElementData;

    /// Every field a provider must decide on when it builds a *complete*
    /// element from a platform node.
    ///
    /// Deliberately exhaustive: a struct literal in `xa11y-linux`,
    /// `xa11y-macos`, or `xa11y-windows` stops compiling the moment a field
    /// is added, which is the only thing that forces a per-platform decision
    /// instead of a silent `None` on every backend.
    ///
    /// Paths that are partial by nature — an event target with no bounds, a
    /// test fixture — should use [`ElementData::for_role`] instead and accept
    /// that a new field arrives there as its default.
    ///
    /// Not public API (`#[doc(hidden)]`). Because a new field here is a
    /// compile error in the sibling provider crates, they pin `xa11y-core`
    /// with `=` rather than a caret requirement — see the workspace
    /// `Cargo.toml`.
    #[allow(
        clippy::exhaustive_structs,
        reason = "This type IS the completeness guard. Literal construction \
                  from the provider crates is exactly what makes a new \
                  ElementData field fail their build until each platform maps \
                  it; #[non_exhaustive] here would delete the property it \
                  exists for."
    )]
    #[derive(Debug, Clone)]
    pub struct ElementParts;

    fields {
        /// Element role
        pub role: Role,

        /// Human-readable name (title, label).
        ///
        /// Stripped of Unicode bidi format controls (LRM, RLM, embeddings,
        /// overrides, isolates) so equality assertions match the logical text.
        /// The unstripped platform string is preserved in [`Self::raw`] under the
        /// platform-native key (e.g. `AXTitle` on macOS, `atspi_name` on Linux,
        /// `uia_name` on Windows). See [`crate::text::strip_bidi`].
        pub name: Option<String>,

        /// Current value (text content, slider position, etc.).
        ///
        /// Stripped of Unicode bidi format controls. The unstripped platform
        /// string is preserved in [`Self::raw`] (`AXValue` on macOS, `atspi_value`
        /// on Linux, `uia_value` on Windows). See [`crate::text::strip_bidi`].
        pub value: Option<String>,

        /// Supplementary description (tooltip, help text).
        ///
        /// Stripped of Unicode bidi format controls. The unstripped platform
        /// string is preserved in [`Self::raw`] (`AXDescription`/`AXHelp` on
        /// macOS, `atspi_description` on Linux, `uia_help_text` on Windows).
        /// See [`crate::text::strip_bidi`].
        pub description: Option<String>,

        /// Bounding rectangle in **logical** screen coordinates
        /// (device-independent points), origin at the top-left of the primary
        /// display. This is the same coordinate space accepted by
        /// [`crate::ScreenshotProvider::capture_region`] and by the input layer's
        /// [`crate::input::Point`], so bounds can be fed directly to
        /// `screenshot_element` / `click` without conversion.
        ///
        /// To map to physical device pixels (e.g. to index into a captured image),
        /// multiply by the [`crate::Screenshot::scale`] reported for that display:
        /// `physical = logical × scale`. See [`Rect::to_physical`] /
        /// [`Rect::to_logical`].
        pub bounds: Option<Rect>,

        /// Available actions reported by the platform.
        ///
        /// Names are `snake_case` strings — well-known actions use their standard
        /// names (`"press"`, `"toggle"`, `"expand"`, etc.) and platform-specific
        /// actions use their converted names (e.g. macOS `AXCustomThing` →
        /// `"custom_thing"`).
        pub actions: Vec<String>,

        /// Current state flags
        pub states: StateSet,

        /// Numeric value for range controls (sliders, progress bars, spinners).
        pub numeric_value: Option<f64>,

        /// Minimum value for range controls.
        pub min_value: Option<f64>,

        /// Maximum value for range controls.
        pub max_value: Option<f64>,

        /// Platform-assigned stable identifier for cross-snapshot correlation.
        /// - macOS: `AXIdentifier`
        /// - Windows: `AutomationId`
        /// - Linux: D-Bus `object_path`
        ///
        /// Not all elements have one.
        pub stable_id: Option<String>,

        /// Process ID of the application that owns this element.
        pub pid: Option<u32>,

        /// Platform-specific raw data
        pub raw: RawPlatformData,

        /// Opaque handle for the provider to look up the platform object.
        /// Not serialized — only valid within the provider that created it.
        #[serde(skip, default)]
        pub handle: u64,
    }
}

impl ElementData {
    /// An element with the given role and every other field empty.
    ///
    /// `states` starts at [`StateSet::default`] (enabled and visible, nothing
    /// else), and `handle` at `0` — providers assign their own.
    ///
    /// This is the *partial* construction path. A provider translating a real
    /// platform node should use [`ElementParts`] instead, so that a new field
    /// fails its build rather than arriving as a default.
    ///
    /// Named `for_role` rather than `new` because `ElementData` is flattened
    /// onto `Element` for the bindings-parity check, where a member called
    /// `new` would collide with the existing [`Element::new`].
    pub fn for_role(role: Role) -> Self {
        // Struct literal, not a builder: this lives in the defining crate, so
        // the compiler still checks it for completeness when a field is added.
        Self {
            role,
            name: None,
            value: None,
            description: None,
            bounds: None,
            actions: Vec::new(),
            states: StateSet::default(),
            numeric_value: None,
            min_value: None,
            max_value: None,
            stable_id: None,
            pid: None,
            raw: RawPlatformData::new(),
            handle: 0,
        }
    }
}

impl Default for ElementData {
    /// A [`Role::Unknown`] element with no properties.
    fn default() -> Self {
        Self::for_role(Role::Unknown)
    }
}

/// A live element with lazy navigation via a provider reference.
///
/// `Element` dereferences to [`ElementData`], so all properties (`role`, `name`,
/// `value`, `states`, etc.) are accessible via field access. Navigation
/// methods (`parent()`, `children()`) call the provider on demand.
///
/// Elements are cheap to clone (they share the provider via `Arc`).
#[derive(Clone)]
pub struct Element {
    data: ElementData,
    provider: Arc<dyn Provider>,
}

impl Deref for Element {
    type Target = ElementData;

    fn deref(&self) -> &ElementData {
        &self.data
    }
}

impl fmt::Debug for Element {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&self.data, f)
    }
}

impl fmt::Display for Element {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let name_part = self
            .data
            .name
            .as_ref()
            .map(|n| format!(" \"{}\"", n))
            .unwrap_or_default();
        let value_part = self
            .data
            .value
            .as_ref()
            .map(|v| format!(" value=\"{}\"", v))
            .unwrap_or_default();
        write!(
            f,
            "{}{}{}",
            self.data.role.to_snake_case(),
            name_part,
            value_part,
        )
    }
}

impl Serialize for Element {
    fn serialize<S: serde::Serializer>(
        &self,
        serializer: S,
    ) -> std::result::Result<S::Ok, S::Error> {
        self.data.serialize(serializer)
    }
}

impl Element {
    /// Create an Element from raw data and a provider reference.
    pub fn new(data: ElementData, provider: Arc<dyn Provider>) -> Self {
        Self { data, provider }
    }

    /// Get the underlying ElementData.
    pub fn data(&self) -> &ElementData {
        &self.data
    }

    /// Get the provider reference.
    pub fn provider(&self) -> &Arc<dyn Provider> {
        &self.provider
    }

    /// Get direct children of this element.
    ///
    /// Each call queries the provider — results are not cached.
    pub fn children(&self) -> crate::error::Result<Vec<Element>> {
        let children = self.provider.get_children(Some(&self.data))?;
        Ok(children
            .into_iter()
            .map(|d| Element::new(d, Arc::clone(&self.provider)))
            .collect())
    }

    /// Get the parent element, if any (root-level elements have no parent).
    ///
    /// Each call queries the provider — results are not cached.
    pub fn parent(&self) -> crate::error::Result<Option<Element>> {
        let parent = self.provider.get_parent(&self.data)?;
        Ok(parent.map(|d| Element::new(d, Arc::clone(&self.provider))))
    }

    /// Get the process ID from the element data.
    pub fn pid(&self) -> Option<u32> {
        self.data.pid
    }

    /// Capture the subtree rooted at this element as a recursive snapshot.
    ///
    /// `max_depth` limits traversal depth: `0` = only this node (no children),
    /// `1` = node + direct children, and so on. `None` traverses the full subtree.
    pub fn tree(&self, max_depth: Option<usize>) -> crate::error::Result<TreeNode> {
        build_tree_node(self, max_depth, 0)
    }

    /// Render the subtree rooted at this element as an indented string.
    ///
    /// Each line is `{indent}{role} "{name}" [value="{value}"]`. Returns the
    /// string without printing it. Same depth semantics as [`Element::tree`].
    pub fn dump(&self, max_depth: Option<usize>) -> crate::error::Result<String> {
        let node = self.tree(max_depth)?;
        let mut out = String::new();
        write_tree_node(&node, 0, &mut out);
        Ok(out)
    }

    // ── Actions ─────────────────────────────────────────────────────
    //
    // Element actions invoke the platform via the captured provider handle —
    // they do **not** re-resolve the selector. If the underlying element has
    // been destroyed since this snapshot was taken, the provider returns a
    // platform-specific "gone" error. For resilient retry-on-change semantics,
    // use the equivalent method on [`crate::Locator`] instead.

    /// Click / invoke this element via the accessibility action layer.
    pub fn press(&self) -> crate::error::Result<()> {
        self.provider.press(&self.data)
    }

    /// Set keyboard focus to this element.
    pub fn focus(&self) -> crate::error::Result<()> {
        self.provider.focus(&self.data)
    }

    /// Remove keyboard focus from this element.
    pub fn blur(&self) -> crate::error::Result<()> {
        self.provider.blur(&self.data)
    }

    /// Toggle a two- or three-state control (checkbox, switch).
    pub fn toggle(&self) -> crate::error::Result<()> {
        self.provider.toggle(&self.data)
    }

    /// Select this element (list item, tab, row).
    pub fn select(&self) -> crate::error::Result<()> {
        self.provider.select(&self.data)
    }

    /// Expand a disclosure, menu, combo box, or tree item.
    pub fn expand(&self) -> crate::error::Result<()> {
        self.provider.expand(&self.data)
    }

    /// Collapse an expanded element.
    pub fn collapse(&self) -> crate::error::Result<()> {
        self.provider.collapse(&self.data)
    }

    /// Open this element's context menu or dropdown.
    pub fn show_menu(&self) -> crate::error::Result<()> {
        self.provider.show_menu(&self.data)
    }

    /// Increment a numeric control (slider, spinner) by its platform step.
    pub fn increment(&self) -> crate::error::Result<()> {
        self.provider.increment(&self.data)
    }

    /// Decrement a numeric control (slider, spinner) by its platform step.
    pub fn decrement(&self) -> crate::error::Result<()> {
        self.provider.decrement(&self.data)
    }

    /// Scroll this element into the visible area.
    ///
    /// No-op on macOS — the macOS accessibility API has no equivalent.
    pub fn scroll_into_view(&self) -> crate::error::Result<()> {
        self.provider.scroll_into_view(&self.data)
    }

    /// Set the text value of this element. Replaces the entire value rather
    /// than inserting at the caret — use [`Element::type_text`] for insertion.
    pub fn set_value(&self, value: &str) -> crate::error::Result<()> {
        self.provider.set_value(&self.data, value)
    }

    /// Set the numeric value of this element (slider, spinner).
    ///
    /// Returns [`Error::InvalidActionData`] if `value` is NaN or infinite.
    pub fn set_numeric_value(&self, value: f64) -> crate::error::Result<()> {
        if !value.is_finite() {
            return Err(Error::InvalidActionData {
                message: format!("set_numeric_value requires a finite value, got {}", value),
            });
        }
        self.provider.set_numeric_value(&self.data, value)
    }

    /// Insert text at the current cursor position.
    ///
    /// Uses the platform accessibility API — never simulates keyboard events.
    pub fn type_text(&self, text: &str) -> crate::error::Result<()> {
        self.provider.type_text(&self.data, text)
    }

    /// Select the text range from `start` to `end` (0-based character offsets).
    ///
    /// Returns [`Error::InvalidActionData`] if `start > end`.
    pub fn select_text(&self, start: u32, end: u32) -> crate::error::Result<()> {
        if start > end {
            return Err(Error::InvalidActionData {
                message: format!("select_text start ({}) must be <= end ({})", start, end),
            });
        }
        self.provider.set_text_selection(&self.data, start, end)
    }

    /// Perform an action by its `snake_case` name.
    ///
    /// Use this for actions the element advertises in its [`actions`](ElementData::actions)
    /// list that don't have a dedicated method. Well-known names (`"press"`,
    /// `"focus"`, etc.) also work — providers delegate to the named methods.
    pub fn perform_action(&self, action: &str) -> crate::error::Result<()> {
        self.provider.perform_action(&self.data, action)
    }
}

fn build_tree_node(
    element: &Element,
    max_depth: Option<usize>,
    depth: usize,
) -> crate::error::Result<TreeNode> {
    let children = if max_depth.is_none_or(|d| depth < d) {
        element
            .children()?
            .into_iter()
            .map(|child| build_tree_node(&child, max_depth, depth + 1))
            .collect::<crate::error::Result<Vec<_>>>()?
    } else {
        vec![]
    };
    Ok(TreeNode {
        role: element.data.role.to_snake_case().to_string(),
        name: element.data.name.clone(),
        value: element.data.value.clone(),
        children,
    })
}

fn write_tree_node(node: &TreeNode, depth: usize, out: &mut String) {
    use fmt::Write as _;
    let indent = "  ".repeat(depth);
    write!(out, "{}{}", indent, node.role).unwrap();
    if let Some(ref n) = node.name {
        write!(out, " \"{}\"", n).unwrap();
    }
    if let Some(ref v) = node.value {
        write!(out, " value=\"{}\"", v).unwrap();
    }
    out.push('\n');
    for child in &node.children {
        write_tree_node(child, depth + 1, out);
    }
}

reader_writer_pair! {
    /// Boolean state flags for an element.
    ///
    /// **Semantics for non-applicable states:** When a state doesn't apply to
    /// an element's role, the backend uses the platform's reported value or
    /// defaults:
    /// - `enabled`: `true` (elements are enabled unless explicitly disabled)
    /// - `visible`: `true` (elements are visible unless explicitly hidden/offscreen)
    /// - `focused`, `active`, `focusable`, `modal`, `selected`, `editable`, `required`, `busy`: `false`
    ///
    /// States that are inherently inapplicable use `Option`: `checked` is
    /// `None` for non-checkable elements, `expanded` is `None` for
    /// non-expandable elements.
    ///
    /// `#[non_exhaustive]`: more states arrive in compatible releases and must
    /// not break readers. Providers building a complete state set use
    /// [`StateParts`], which is exhaustive — the documented defaults above are
    /// what a *partial* construction falls back to, not a licence for a
    /// backend to skip deciding.
    #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
    pub struct StateSet;

    /// Every state a provider must decide on when it translates a platform
    /// node's state bits.
    ///
    /// Deliberately exhaustive, for the same reason as [`ElementParts`]: a new
    /// state must fail each backend's build rather than silently inherit
    /// [`StateSet::default`]. That matters more here than the defaults
    /// suggest — the parity check requires every state to surface as a binding
    /// getter, so a silently-defaulted state ships as a documented API that no
    /// platform populates.
    ///
    /// Not public API (`#[doc(hidden)]`).
    #[allow(
        clippy::exhaustive_structs,
        reason = "This type IS the completeness guard for element state. See \
                  ElementParts; the same reasoning applies."
    )]
    #[derive(Debug, Clone)]
    pub struct StateParts;

    fields {
        pub enabled: bool,
        pub visible: bool,
        pub focused: bool,
        /// Whether this element is the active (foreground) window — the window that
        /// currently receives the user's input. Only meaningful for window-like
        /// elements (windows, dialogs); `false` elsewhere. Distinct from `focused`,
        /// which is element-level keyboard focus. Platform mappings: the AT-SPI
        /// `ACTIVE` state (Linux), `AXMain` (macOS), and the foreground `HWND`
        /// (Windows).
        #[serde(default)]
        pub active: bool,
        /// None = not checkable
        pub checked: Option<Toggled>,
        pub selected: bool,
        /// None = not expandable
        pub expanded: Option<bool>,
        pub editable: bool,
        /// Whether the element can receive keyboard focus
        pub focusable: bool,
        /// Whether the element is a modal dialog
        pub modal: bool,
        /// Form field required
        pub required: bool,
        /// Async operation in progress
        pub busy: bool,
    }
}

impl Default for StateSet {
    fn default() -> Self {
        Self {
            enabled: true,
            visible: true,
            focused: false,
            active: false,
            checked: None,
            selected: false,
            expanded: None,
            editable: false,
            focusable: false,
            modal: false,
            required: false,
            busy: false,
        }
    }
}

/// Tri-state toggle value.
#[allow(
    clippy::exhaustive_enums,
    reason = "Closed domain: a toggle is off, on, or indeterminate. Every \
              platform's tri-state checkbox is exactly these three values, \
              and a fourth would not be a toggle."
)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Toggled {
    Off,
    On,
    /// Indeterminate / tri-state
    Mixed,
}

/// Screen-pixel bounding rectangle (origin + size).
/// `x`/`y` are signed to support negative multi-monitor coordinates.
/// `width`/`height` are unsigned (always non-negative).
#[allow(
    clippy::exhaustive_structs,
    reason = "Closed domain: an axis-aligned rectangle is fully described by \
              an origin and a size. Literal construction is the point of the \
              type, and it will not gain a fifth field."
)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Rect {
    pub x: i32,
    pub y: i32,
    pub width: u32,
    pub height: u32,
}

impl Rect {
    /// Convert a **logical** rectangle to **physical** device pixels by
    /// multiplying every component by `scale` (the physical-to-logical ratio,
    /// e.g. `1.5` at 150% or `2.0` on a typical Retina display).
    ///
    /// This is the inverse of [`Rect::to_logical`]. Each field is rounded to
    /// the nearest integer independently; for a single rectangle the position
    /// and size therefore round separately, which can differ by 1px from
    /// scaling the far edge — acceptable for capture/hit-test use where a 1px
    /// slack is expected on fractional scales.
    ///
    /// A non-finite or non-positive `scale` is treated as `1.0` (identity):
    /// callers on platforms without a known scale factor pass `1.0`, and a
    /// bogus value must never produce garbage coordinates.
    #[must_use]
    pub fn to_physical(self, scale: f64) -> Rect {
        let s = sane_scale(scale);
        Rect {
            x: scale_i32(self.x, s),
            y: scale_i32(self.y, s),
            width: scale_u32(self.width, s),
            height: scale_u32(self.height, s),
        }
    }

    /// Convert a **physical** rectangle (device pixels) to **logical**
    /// coordinates by dividing every component by `scale`. Inverse of
    /// [`Rect::to_physical`]. See that method for rounding and `scale`
    /// validity semantics.
    #[must_use]
    pub fn to_logical(self, scale: f64) -> Rect {
        self.to_physical(1.0 / sane_scale(scale))
    }
}

/// Clamp a scale factor to a usable positive, finite value. Non-finite or
/// non-positive inputs collapse to `1.0` so a bad platform reading degrades
/// to identity rather than producing nonsense coordinates.
pub(crate) fn sane_scale(scale: f64) -> f64 {
    if scale.is_finite() && scale > 0.0 {
        scale
    } else {
        1.0
    }
}

fn scale_i32(v: i32, scale: f64) -> i32 {
    (f64::from(v) * scale).round() as i32
}

fn scale_u32(v: u32, scale: f64) -> u32 {
    let scaled = (f64::from(v) * scale).round();
    if scaled < 0.0 {
        0
    } else {
        scaled as u32
    }
}

#[cfg(test)]
mod rect_scale_tests {
    use super::Rect;

    const R: Rect = Rect {
        x: 100,
        y: 200,
        width: 300,
        height: 40,
    };

    #[test]
    fn scale_one_is_identity() {
        assert_eq!(R.to_physical(1.0), R);
        assert_eq!(R.to_logical(1.0), R);
    }

    #[test]
    fn to_physical_multiplies_all_fields() {
        assert_eq!(
            R.to_physical(2.0),
            Rect {
                x: 200,
                y: 400,
                width: 600,
                height: 80
            }
        );
    }

    #[test]
    fn to_logical_divides_all_fields() {
        // Physical bounds on a 150% display -> logical points.
        let physical = Rect {
            x: 150,
            y: 300,
            width: 450,
            height: 60,
        };
        assert_eq!(
            physical.to_logical(1.5),
            Rect {
                x: 100,
                y: 200,
                width: 300,
                height: 40
            }
        );
    }

    #[test]
    fn round_trip_preserves_within_one_px() {
        for &scale in &[1.25_f64, 1.5, 1.75, 2.0] {
            let back = R.to_physical(scale).to_logical(scale);
            assert!((back.x - R.x).abs() <= 1, "x drift at {scale}");
            assert!((back.y - R.y).abs() <= 1, "y drift at {scale}");
            assert!(
                (back.width as i64 - R.width as i64).abs() <= 1,
                "w drift at {scale}"
            );
            assert!(
                (back.height as i64 - R.height as i64).abs() <= 1,
                "h drift at {scale}"
            );
        }
    }

    #[test]
    fn negative_origin_scales_correctly() {
        // Multi-monitor: a window on a display left of the primary.
        let r = Rect {
            x: -1920,
            y: -100,
            width: 200,
            height: 100,
        };
        assert_eq!(
            r.to_physical(2.0),
            Rect {
                x: -3840,
                y: -200,
                width: 400,
                height: 200
            }
        );
    }

    #[test]
    fn fractional_scale_rounds_to_nearest() {
        let r = Rect {
            x: 3,
            y: 3,
            width: 5,
            height: 5,
        };
        // 3 * 1.5 = 4.5 -> 5 (round half away from zero via f64::round);
        // 5 * 1.5 = 7.5 -> 8.
        assert_eq!(
            r.to_physical(1.5),
            Rect {
                x: 5,
                y: 5,
                width: 8,
                height: 8
            }
        );
    }

    #[test]
    fn bad_scale_degrades_to_identity() {
        assert_eq!(R.to_physical(0.0), R);
        assert_eq!(R.to_physical(-2.0), R);
        assert_eq!(R.to_physical(f64::NAN), R);
        assert_eq!(R.to_physical(f64::INFINITY), R);
        assert_eq!(R.to_logical(0.0), R);
    }
}

/// Platform-specific raw data attached to every element.
///
/// An untyped key-value map containing the original platform-specific data
/// exactly as the platform reported it. Keys use `snake_case` naming. This is
/// the escape hatch for consumers who need full platform fidelity.
pub type RawPlatformData = HashMap<String, serde_json::Value>;

/// A node in a recursive snapshot of the accessibility subtree.
///
/// Returned by [`Element::tree`] and [`Locator::tree`]. Each node carries the
/// role, display name, and value of one element, plus its children recursively.
/// `children` is empty when `max_depth` was reached or the element is a leaf.
///
/// `#[non_exhaustive]`: a dump node grows alongside [`ElementData`] — bounds
/// and stable ids are both plausible additions. Build one with
/// [`TreeNode::new`].
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct TreeNode {
    pub role: String,
    pub name: Option<String>,
    pub value: Option<String>,
    pub children: Vec<TreeNode>,
}

impl TreeNode {
    /// A leaf node with the given role and no name, value, or children.
    pub fn new(role: impl Into<String>) -> Self {
        Self {
            role: role.into(),
            name: None,
            value: None,
            children: Vec::new(),
        }
    }
}

#[cfg(test)]
mod tests {
    //! Unit tests for `Element` action methods. Verifies each action records the
    //! expected entry in the mock provider's action log and that validation
    //! errors fire before the provider is ever called.

    use super::*;
    use crate::mock::{build_provider, MockProvider};
    use crate::selector::Selector;

    /// Resolve `selector` against the mock tree and return the first match
    /// wrapped in an `Element`. Panics on no match — these are unit tests, not
    /// production paths.
    fn find_element(provider: &Arc<MockProvider>, selector: &str) -> Element {
        let parsed = Selector::parse(selector).expect("selector must parse");
        let provider_dyn: Arc<dyn Provider> = provider.clone();
        let root = provider_dyn
            .list_apps()
            .expect("list_apps must succeed")
            .into_iter()
            .next()
            .expect("mock provider must expose an application root");
        let mut matches = provider_dyn
            .find_elements(&root, &parsed, Some(1), None)
            .expect("find_elements must succeed");
        let data = matches.pop().expect("selector matched no elements");
        Element::new(data, provider_dyn)
    }

    fn last_action(provider: &Arc<MockProvider>) -> (u64, String, Option<String>) {
        provider
            .actions()
            .last()
            .cloned()
            .expect("expected at least one recorded action")
    }

    #[test]
    fn nullary_actions_record_correct_name() {
        let provider = build_provider();
        let cases = [
            (r#"button[name="Back"]"#, "press" as &str),
            (r#"button[name="Back"]"#, "focus"),
            (r#"button[name="Back"]"#, "blur"),
            (r#"check_box[name="Agree"]"#, "toggle"),
            (r#"list_item[name="Item 1"]"#, "select"),
            (r#"list[name="Items"]"#, "expand"),
            (r#"list[name="Items"]"#, "collapse"),
            (r#"button[name="Back"]"#, "show_menu"),
            (r#"slider[name="Volume"]"#, "increment"),
            (r#"slider[name="Volume"]"#, "decrement"),
            (r#"button[name="Back"]"#, "scroll_into_view"),
        ];
        for (selector, action) in cases {
            provider.clear_actions();
            let el = find_element(&provider, selector);
            match action {
                "press" => el.press().unwrap(),
                "focus" => el.focus().unwrap(),
                "blur" => el.blur().unwrap(),
                "toggle" => el.toggle().unwrap(),
                "select" => el.select().unwrap(),
                "expand" => el.expand().unwrap(),
                "collapse" => el.collapse().unwrap(),
                "show_menu" => el.show_menu().unwrap(),
                "increment" => el.increment().unwrap(),
                "decrement" => el.decrement().unwrap(),
                "scroll_into_view" => el.scroll_into_view().unwrap(),
                _ => unreachable!(),
            }
            let (handle, name, data) = last_action(&provider);
            assert_eq!(
                name, action,
                "wrong action recorded for selector {selector}"
            );
            assert_eq!(data, None, "nullary action should not carry data");
            assert_eq!(handle, el.data.handle);
        }
    }

    #[test]
    fn set_value_records_text_payload() {
        let provider = build_provider();
        let el = find_element(&provider, r#"text_field[name="Search"]"#);
        el.set_value("world").unwrap();
        let (handle, name, data) = last_action(&provider);
        assert_eq!(handle, el.data.handle);
        assert_eq!(name, "set_value");
        assert_eq!(data.as_deref(), Some("world"));
    }

    #[test]
    fn set_numeric_value_records_payload() {
        let provider = build_provider();
        let el = find_element(&provider, r#"slider[name="Volume"]"#);
        el.set_numeric_value(42.0).unwrap();
        let (_, name, data) = last_action(&provider);
        assert_eq!(name, "set_numeric_value");
        assert_eq!(data.as_deref(), Some("42"));
    }

    #[test]
    fn set_numeric_value_rejects_non_finite() {
        let provider = build_provider();
        let el = find_element(&provider, r#"slider[name="Volume"]"#);
        for bad in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
            assert!(matches!(
                el.set_numeric_value(bad),
                Err(Error::InvalidActionData { .. })
            ));
        }
        // None of the validation failures should have reached the provider.
        assert!(provider.actions().is_empty());
    }

    #[test]
    fn type_text_records_payload() {
        let provider = build_provider();
        let el = find_element(&provider, r#"text_field[name="Search"]"#);
        el.type_text("abc").unwrap();
        let (_, name, data) = last_action(&provider);
        assert_eq!(name, "type_text");
        assert_eq!(data.as_deref(), Some("abc"));
    }

    #[test]
    fn select_text_records_range() {
        let provider = build_provider();
        let el = find_element(&provider, r#"text_field[name="Search"]"#);
        el.select_text(1, 4).unwrap();
        let (_, name, data) = last_action(&provider);
        assert_eq!(name, "set_text_selection");
        assert_eq!(data.as_deref(), Some("1..4"));
    }

    #[test]
    fn select_text_rejects_inverted_range() {
        let provider = build_provider();
        let el = find_element(&provider, r#"text_field[name="Search"]"#);
        assert!(matches!(
            el.select_text(5, 2),
            Err(Error::InvalidActionData { .. })
        ));
        assert!(provider.actions().is_empty());
    }

    #[test]
    fn perform_action_records_arbitrary_name() {
        let provider = build_provider();
        let el = find_element(&provider, r#"button[name="Back"]"#);
        el.perform_action("raise").unwrap();
        let (_, name, _) = last_action(&provider);
        assert_eq!(name, "raise");
    }

    #[test]
    fn locator_actions_desugar_to_element_actions() {
        // Locator's auto-wait wraps the resolved data in an Element and calls
        // its action — no duplication at the provider call site. This test
        // pins that behavior: pressing via the Locator should record exactly
        // the same entry as pressing via the Element it resolves to.
        let provider = build_provider();
        let provider_dyn: Arc<dyn Provider> = provider.clone();
        let locator = crate::locator::Locator::new(provider_dyn, None, r#"button[name="Back"]"#);
        locator.press().unwrap();
        let (_, name, data) = last_action(&provider);
        assert_eq!(name, "press");
        assert_eq!(data, None);
    }

    #[test]
    fn locator_validation_runs_before_auto_wait() {
        // Locator validates payloads before entering its 5s auto-wait poll.
        // We verify by passing invalid input against a never-matching selector:
        // if validation fired first we get InvalidActionData immediately, not
        // a Timeout 5 seconds later.
        let provider = build_provider();
        let provider_dyn: Arc<dyn Provider> = provider.clone();
        let locator =
            crate::locator::Locator::new(provider_dyn, None, r#"button[name="never-matches"]"#);
        let started = std::time::Instant::now();
        let err = locator.set_numeric_value(f64::NAN).unwrap_err();
        assert!(matches!(err, Error::InvalidActionData { .. }));
        assert!(
            started.elapsed() < std::time::Duration::from_secs(1),
            "validation must short-circuit auto-wait",
        );
    }
}