-
Notifications
You must be signed in to change notification settings - Fork 54
/
lib.rs
2474 lines (2293 loc) · 81.7 KB
/
lib.rs
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
// Copyright 2021 The AccessKit Authors. All rights reserved.
// Licensed under the Apache License, Version 2.0 (found in
// the LICENSE-APACHE file) or the MIT license (found in
// the LICENSE-MIT file), at your option.
// Derived from Chromium's accessibility abstraction.
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE.chromium file.
#![cfg_attr(not(any(feature = "pyo3", feature = "schemars")), no_std)]
extern crate alloc;
use alloc::{boxed::Box, string::String, vec::Vec};
use core::fmt;
#[cfg(feature = "pyo3")]
use pyo3::pyclass;
#[cfg(feature = "schemars")]
use schemars::{
gen::SchemaGenerator,
schema::{InstanceType, ObjectValidation, Schema, SchemaObject},
JsonSchema, Map as SchemaMap,
};
#[cfg(feature = "serde")]
use serde::{
de::{Deserializer, IgnoredAny, MapAccess, Visitor},
ser::{SerializeMap, Serializer},
Deserialize, Serialize,
};
mod geometry;
pub use geometry::{Affine, Point, Rect, Size, Vec2};
/// The type of an accessibility node.
///
/// The majority of these roles come from the ARIA specification. Reference
/// the latest draft for proper usage.
///
/// Like the AccessKit schema as a whole, this list is largely taken
/// from Chromium. However, unlike Chromium's alphabetized list, this list
/// is ordered roughly by expected usage frequency (with the notable exception
/// of [`Role::Unknown`]). This is more efficient in serialization formats
/// where integers use a variable-length encoding.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "enumn", derive(enumn::N))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(
feature = "pyo3",
pyclass(module = "accesskit", rename_all = "SCREAMING_SNAKE_CASE")
)]
#[repr(u8)]
pub enum Role {
#[default]
Unknown,
TextRun,
Cell,
Label,
Image,
Link,
Row,
ListItem,
/// Contains the bullet, number, or other marker for a list item.
ListMarker,
TreeItem,
ListBoxOption,
MenuItem,
MenuListOption,
Paragraph,
/// A generic container that should be ignored by assistive technologies
/// and filtered out of platform accessibility trees. Equivalent to the ARIA
/// `none` or `presentation` role, or to an HTML `div` with no role.
GenericContainer,
CheckBox,
RadioButton,
TextInput,
Button,
DefaultButton,
Pane,
RowHeader,
ColumnHeader,
RowGroup,
List,
Table,
LayoutTableCell,
LayoutTableRow,
LayoutTable,
Switch,
Menu,
MultilineTextInput,
SearchInput,
DateInput,
DateTimeInput,
WeekInput,
MonthInput,
TimeInput,
EmailInput,
NumberInput,
PasswordInput,
PhoneNumberInput,
UrlInput,
Abbr,
Alert,
AlertDialog,
Application,
Article,
Audio,
Banner,
Blockquote,
Canvas,
Caption,
Caret,
Code,
ColorWell,
ComboBox,
EditableComboBox,
Complementary,
Comment,
ContentDeletion,
ContentInsertion,
ContentInfo,
Definition,
DescriptionList,
DescriptionListDetail,
DescriptionListTerm,
Details,
Dialog,
Directory,
DisclosureTriangle,
Document,
EmbeddedObject,
Emphasis,
Feed,
FigureCaption,
Figure,
Footer,
FooterAsNonLandmark,
Form,
Grid,
Group,
Header,
HeaderAsNonLandmark,
Heading,
Iframe,
IframePresentational,
ImeCandidate,
Keyboard,
Legend,
LineBreak,
ListBox,
Log,
Main,
Mark,
Marquee,
Math,
MenuBar,
MenuItemCheckBox,
MenuItemRadio,
MenuListPopup,
Meter,
Navigation,
Note,
PluginObject,
Portal,
Pre,
ProgressIndicator,
RadioGroup,
Region,
RootWebArea,
Ruby,
RubyAnnotation,
ScrollBar,
ScrollView,
Search,
Section,
Slider,
SpinButton,
Splitter,
Status,
Strong,
Suggestion,
SvgRoot,
Tab,
TabList,
TabPanel,
Term,
Time,
Timer,
TitleBar,
Toolbar,
Tooltip,
Tree,
TreeGrid,
Video,
WebView,
Window,
PdfActionableHighlight,
PdfRoot,
// ARIA Graphics module roles:
// https://rawgit.com/w3c/graphics-aam/master/#mapping_role_table
GraphicsDocument,
GraphicsObject,
GraphicsSymbol,
// DPub Roles:
// https://www.w3.org/TR/dpub-aam-1.0/#mapping_role_table
DocAbstract,
DocAcknowledgements,
DocAfterword,
DocAppendix,
DocBackLink,
DocBiblioEntry,
DocBibliography,
DocBiblioRef,
DocChapter,
DocColophon,
DocConclusion,
DocCover,
DocCredit,
DocCredits,
DocDedication,
DocEndnote,
DocEndnotes,
DocEpigraph,
DocEpilogue,
DocErrata,
DocExample,
DocFootnote,
DocForeword,
DocGlossary,
DocGlossRef,
DocIndex,
DocIntroduction,
DocNoteRef,
DocNotice,
DocPageBreak,
DocPageFooter,
DocPageHeader,
DocPageList,
DocPart,
DocPreface,
DocPrologue,
DocPullquote,
DocQna,
DocSubtitle,
DocTip,
DocToc,
/// Behaves similar to an ARIA grid but is primarily used by Chromium's
/// `TableView` and its subclasses, so they can be exposed correctly
/// on certain platforms.
ListGrid,
/// This is just like a multi-line document, but signals that assistive
/// technologies should implement behavior specific to a VT-100-style
/// terminal.
Terminal,
}
/// An action to be taken on an accessibility node.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "enumn", derive(enumn::N))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(
feature = "pyo3",
pyclass(module = "accesskit", rename_all = "SCREAMING_SNAKE_CASE")
)]
#[repr(u8)]
pub enum Action {
/// Do the equivalent of a single click or tap.
Click,
Focus,
Blur,
Collapse,
Expand,
/// Requires [`ActionRequest::data`] to be set to [`ActionData::CustomAction`].
CustomAction,
/// Decrement a numeric value by one step.
Decrement,
/// Increment a numeric value by one step.
Increment,
HideTooltip,
ShowTooltip,
/// Delete any selected text in the control's text value and
/// insert the specified value in its place, like when typing or pasting.
/// Requires [`ActionRequest::data`] to be set to [`ActionData::Value`].
ReplaceSelectedText,
// Scrolls by approximately one screen in a specific direction.
// TBD: Do we need a doc comment on each of the values below?
// Or does this awkwardness suggest a refactor?
ScrollBackward,
ScrollDown,
ScrollForward,
ScrollLeft,
ScrollRight,
ScrollUp,
/// Scroll any scrollable containers to make the target object visible
/// on the screen. Optionally set [`ActionRequest::data`] to
/// [`ActionData::ScrollTargetRect`].
ScrollIntoView,
/// Scroll the given object to a specified point in the tree's container
/// (e.g. window). Requires [`ActionRequest::data`] to be set to
/// [`ActionData::ScrollToPoint`].
ScrollToPoint,
/// Requires [`ActionRequest::data`] to be set to [`ActionData::SetScrollOffset`].
SetScrollOffset,
/// Requires [`ActionRequest::data`] to be set to [`ActionData::SetTextSelection`].
SetTextSelection,
/// Don't focus this node, but set it as the sequential focus navigation
/// starting point, so that pressing Tab moves to the next element
/// following this one, for example.
SetSequentialFocusNavigationStartingPoint,
/// Replace the value of the control with the specified value and
/// reset the selection, if applicable. Requires [`ActionRequest::data`]
/// to be set to [`ActionData::Value`] or [`ActionData::NumericValue`].
SetValue,
ShowContextMenu,
}
impl Action {
fn mask(self) -> u32 {
1 << (self as u8)
}
#[cfg(not(feature = "enumn"))]
fn n(value: u8) -> Option<Self> {
// Manually implement something similar to the enumn crate. We don't
// want to bring this crate by default though and we can't use a
// macro as it would break C bindings header file generation.
match value {
0 => Some(Action::Click),
1 => Some(Action::Focus),
2 => Some(Action::Blur),
3 => Some(Action::Collapse),
4 => Some(Action::Expand),
5 => Some(Action::CustomAction),
6 => Some(Action::Decrement),
7 => Some(Action::Increment),
8 => Some(Action::HideTooltip),
9 => Some(Action::ShowTooltip),
10 => Some(Action::ReplaceSelectedText),
11 => Some(Action::ScrollBackward),
12 => Some(Action::ScrollDown),
13 => Some(Action::ScrollForward),
14 => Some(Action::ScrollLeft),
15 => Some(Action::ScrollRight),
16 => Some(Action::ScrollUp),
17 => Some(Action::ScrollIntoView),
18 => Some(Action::ScrollToPoint),
19 => Some(Action::SetScrollOffset),
20 => Some(Action::SetTextSelection),
21 => Some(Action::SetSequentialFocusNavigationStartingPoint),
22 => Some(Action::SetValue),
23 => Some(Action::ShowContextMenu),
_ => None,
}
}
}
fn action_mask_to_action_vec(mask: u32) -> Vec<Action> {
let mut actions = Vec::new();
let mut i = 0;
while let Some(variant) = Action::n(i) {
if mask & variant.mask() != 0 {
actions.push(variant);
}
i += 1;
}
actions
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "enumn", derive(enumn::N))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(
feature = "pyo3",
pyclass(module = "accesskit", rename_all = "SCREAMING_SNAKE_CASE")
)]
#[repr(u8)]
pub enum Orientation {
/// E.g. most toolbars and separators.
Horizontal,
/// E.g. menu or combo box.
Vertical,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "enumn", derive(enumn::N))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(
feature = "pyo3",
pyclass(module = "accesskit", rename_all = "SCREAMING_SNAKE_CASE")
)]
#[repr(u8)]
pub enum TextDirection {
LeftToRight,
RightToLeft,
TopToBottom,
BottomToTop,
}
/// Indicates if a form control has invalid input or if a web DOM element has an
/// [`aria-invalid`] attribute.
///
/// [`aria-invalid`]: https://www.w3.org/TR/wai-aria-1.1/#aria-invalid
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "enumn", derive(enumn::N))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(
feature = "pyo3",
pyclass(module = "accesskit", rename_all = "SCREAMING_SNAKE_CASE")
)]
#[repr(u8)]
pub enum Invalid {
True,
Grammar,
Spelling,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "enumn", derive(enumn::N))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(
feature = "pyo3",
pyclass(module = "accesskit", rename_all = "SCREAMING_SNAKE_CASE")
)]
#[repr(u8)]
pub enum Toggled {
False,
True,
Mixed,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "enumn", derive(enumn::N))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(
feature = "pyo3",
pyclass(module = "accesskit", rename_all = "SCREAMING_SNAKE_CASE")
)]
#[repr(u8)]
pub enum SortDirection {
Ascending,
Descending,
Other,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "enumn", derive(enumn::N))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(
feature = "pyo3",
pyclass(module = "accesskit", rename_all = "SCREAMING_SNAKE_CASE")
)]
#[repr(u8)]
pub enum AriaCurrent {
False,
True,
Page,
Step,
Location,
Date,
Time,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "enumn", derive(enumn::N))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(
feature = "pyo3",
pyclass(module = "accesskit", rename_all = "SCREAMING_SNAKE_CASE")
)]
#[repr(u8)]
pub enum AutoComplete {
Inline,
List,
Both,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "enumn", derive(enumn::N))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(
feature = "pyo3",
pyclass(module = "accesskit", rename_all = "SCREAMING_SNAKE_CASE")
)]
#[repr(u8)]
pub enum Live {
Off,
Polite,
Assertive,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "enumn", derive(enumn::N))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(
feature = "pyo3",
pyclass(module = "accesskit", rename_all = "SCREAMING_SNAKE_CASE")
)]
#[repr(u8)]
pub enum HasPopup {
True,
Menu,
Listbox,
Tree,
Grid,
Dialog,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "enumn", derive(enumn::N))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(
feature = "pyo3",
pyclass(module = "accesskit", rename_all = "SCREAMING_SNAKE_CASE")
)]
#[repr(u8)]
pub enum ListStyle {
Circle,
Disc,
Image,
Numeric,
Square,
/// Language specific ordering (alpha, roman, cjk-ideographic, etc...)
Other,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "enumn", derive(enumn::N))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(
feature = "pyo3",
pyclass(module = "accesskit", rename_all = "SCREAMING_SNAKE_CASE")
)]
#[repr(u8)]
pub enum TextAlign {
Left,
Right,
Center,
Justify,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "enumn", derive(enumn::N))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(
feature = "pyo3",
pyclass(module = "accesskit", rename_all = "SCREAMING_SNAKE_CASE")
)]
#[repr(u8)]
pub enum VerticalOffset {
Subscript,
Superscript,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "enumn", derive(enumn::N))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(
feature = "pyo3",
pyclass(module = "accesskit", rename_all = "SCREAMING_SNAKE_CASE")
)]
#[repr(u8)]
pub enum TextDecoration {
Solid,
Dotted,
Dashed,
Double,
Wavy,
}
pub type NodeIdContent = u64;
/// The stable identity of a [`Node`], unique within the node's tree.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[repr(transparent)]
pub struct NodeId(pub NodeIdContent);
impl From<NodeIdContent> for NodeId {
#[inline]
fn from(inner: NodeIdContent) -> Self {
Self(inner)
}
}
impl From<NodeId> for NodeIdContent {
#[inline]
fn from(outer: NodeId) -> Self {
outer.0
}
}
/// Defines a custom action for a UI element.
///
/// For example, a list UI can allow a user to reorder items in the list by dragging the
/// items.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct CustomAction {
pub id: i32,
pub description: Box<str>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct TextPosition {
/// The node's role must be [`Role::TextRun`].
pub node: NodeId,
/// The index of an item in [`Node::character_lengths`], or the length
/// of that slice if the position is at the end of the line.
pub character_index: usize,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct TextSelection {
/// The position where the selection started, and which does not change
/// as the selection is expanded or contracted. If there is no selection
/// but only a caret, this must be equal to the value of [`TextSelection::focus`].
/// This is also known as a degenerate selection.
pub anchor: TextPosition,
/// The active end of the selection, which changes as the selection
/// is expanded or contracted, or the position of the caret if there is
/// no selection.
pub focus: TextPosition,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize, enumn::N))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[repr(u8)]
enum Flag {
Hidden,
Linked,
Multiselectable,
Required,
Visited,
Busy,
LiveAtomic,
Modal,
TouchTransparent,
ReadOnly,
Disabled,
Bold,
Italic,
ClipsChildren,
IsLineBreakingObject,
IsPageBreakingObject,
IsSpellingError,
IsGrammarError,
IsSearchMatch,
IsSuggestion,
}
impl Flag {
fn mask(self) -> u32 {
1 << (self as u8)
}
}
// The following is based on the technique described here:
// https://viruta.org/reducing-memory-consumption-in-librsvg-2.html
#[derive(Clone, Debug, PartialEq)]
enum PropertyValue {
None,
NodeIdVec(Vec<NodeId>),
NodeId(NodeId),
String(Box<str>),
F64(f64),
Usize(usize),
Color(u32),
TextDecoration(TextDecoration),
LengthSlice(Box<[u8]>),
CoordSlice(Box<[f32]>),
Bool(bool),
Invalid(Invalid),
Toggled(Toggled),
Live(Live),
TextDirection(TextDirection),
Orientation(Orientation),
SortDirection(SortDirection),
AriaCurrent(AriaCurrent),
AutoComplete(AutoComplete),
HasPopup(HasPopup),
ListStyle(ListStyle),
TextAlign(TextAlign),
VerticalOffset(VerticalOffset),
Affine(Box<Affine>),
Rect(Rect),
TextSelection(Box<TextSelection>),
CustomActionVec(Vec<CustomAction>),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize, enumn::N))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[repr(u8)]
enum PropertyId {
// NodeIdVec
Children,
Controls,
Details,
DescribedBy,
FlowTo,
LabelledBy,
Owns,
RadioGroup,
// NodeId
ActiveDescendant,
ErrorMessage,
InPageLinkTarget,
MemberOf,
NextOnLine,
PreviousOnLine,
PopupFor,
// String
Label,
Description,
Value,
AccessKey,
AuthorId,
ClassName,
FontFamily,
HtmlTag,
InnerHtml,
KeyboardShortcut,
Language,
Placeholder,
RoleDescription,
StateDescription,
Tooltip,
Url,
RowIndexText,
ColumnIndexText,
// f64
ScrollX,
ScrollXMin,
ScrollXMax,
ScrollY,
ScrollYMin,
ScrollYMax,
NumericValue,
MinNumericValue,
MaxNumericValue,
NumericValueStep,
NumericValueJump,
FontSize,
FontWeight,
// usize
RowCount,
ColumnCount,
RowIndex,
ColumnIndex,
RowSpan,
ColumnSpan,
Level,
SizeOfSet,
PositionInSet,
// Color
ColorValue,
BackgroundColor,
ForegroundColor,
// TextDecoration
Overline,
Strikethrough,
Underline,
// LengthSlice
CharacterLengths,
WordLengths,
// CoordSlice
CharacterPositions,
CharacterWidths,
// bool
Expanded,
Selected,
// Unique enums
Invalid,
Toggled,
Live,
TextDirection,
Orientation,
SortDirection,
AriaCurrent,
AutoComplete,
HasPopup,
ListStyle,
TextAlign,
VerticalOffset,
// Other
Transform,
Bounds,
TextSelection,
CustomActions,
// This MUST be last.
Unset,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(transparent)]
struct PropertyIndices([u8; PropertyId::Unset as usize]);
impl Default for PropertyIndices {
fn default() -> Self {
Self([PropertyId::Unset as u8; PropertyId::Unset as usize])
}
}
#[derive(Clone, Debug, PartialEq)]
struct FrozenProperties {
indices: PropertyIndices,
values: Box<[PropertyValue]>,
}
/// An accessibility node snapshot that can't be modified. This is not used by
/// toolkits or applications, but only by code that retains an AccessKit tree
/// in memory, such as the `accesskit_consumer` crate.
#[derive(Clone, PartialEq)]
pub struct FrozenNode {
role: Role,
actions: u32,
flags: u32,
properties: FrozenProperties,
}
#[derive(Clone, Debug, Default, PartialEq)]
struct Properties {
indices: PropertyIndices,
values: Vec<PropertyValue>,
}
/// A single accessible object. A complete UI is represented as a tree of these.
///
/// For brevity, and to make more of the documentation usable in bindings
/// to other languages, documentation of getter methods is written as if
/// documenting fields in a struct, and such methods are referred to
/// as properties.
#[derive(Clone, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct Node {
role: Role,
actions: u32,
flags: u32,
properties: Properties,
}
impl PropertyIndices {
fn get<'a>(&self, values: &'a [PropertyValue], id: PropertyId) -> &'a PropertyValue {
let index = self.0[id as usize];
if index == PropertyId::Unset as u8 {
&PropertyValue::None
} else {
&values[index as usize]
}
}
}
fn unexpected_property_type() -> ! {
panic!();
}
impl Properties {
fn get_mut(&mut self, id: PropertyId, default: PropertyValue) -> &mut PropertyValue {
let index = self.indices.0[id as usize] as usize;
if index == PropertyId::Unset as usize {
self.values.push(default);
let index = self.values.len() - 1;
self.indices.0[id as usize] = index as u8;
&mut self.values[index]
} else {
if matches!(self.values[index], PropertyValue::None) {
self.values[index] = default;
}
&mut self.values[index]
}
}
fn set(&mut self, id: PropertyId, value: PropertyValue) {
let index = self.indices.0[id as usize];
if index == PropertyId::Unset as u8 {
self.values.push(value);
self.indices.0[id as usize] = (self.values.len() - 1) as u8;
} else {
self.values[index as usize] = value;
}
}
fn clear(&mut self, id: PropertyId) {
let index = self.indices.0[id as usize];
if index != PropertyId::Unset as u8 {
self.values[index as usize] = PropertyValue::None;
}
}
}
impl From<Properties> for FrozenProperties {
fn from(props: Properties) -> Self {
Self {
indices: props.indices,
values: props.values.into_boxed_slice(),
}
}
}
macro_rules! flag_methods {
($($(#[$doc:meta])* ($id:ident, $getter:ident, $setter:ident, $clearer:ident)),+) => {
impl FrozenNode {
$($(#[$doc])*
#[inline]
pub fn $getter(&self) -> bool {
(self.flags & (Flag::$id).mask()) != 0
})*
fn debug_flag_properties(&self, fmt: &mut fmt::DebugStruct) {
$(
if self.$getter() {
fmt.field(stringify!($getter), &true);
}
)*
}