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
// 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.

pub(crate) mod tree;
pub use tree::{ChangeHandler as TreeChangeHandler, State as TreeState, Tree};

pub(crate) mod node;
pub use node::{DetachedNode, Node, NodeState};

pub(crate) mod filters;
pub use filters::{
    common_filter, common_filter_detached, common_filter_with_root_exception, FilterResult,
};

pub(crate) mod iterators;

pub(crate) mod text;
pub use text::{
    AttributeValue as TextAttributeValue, Position as TextPosition, Range as TextRange,
    WeakRange as WeakTextRange,
};

#[cfg(test)]
mod tests {
    use accesskit::{
        Affine, NodeBuilder, NodeClassSet, NodeId, Rect, Role, Tree, TreeUpdate, Vec2,
    };

    use crate::FilterResult;

    pub const ROOT_ID: NodeId = NodeId(0);
    pub const PARAGRAPH_0_ID: NodeId = NodeId(1);
    pub const STATIC_TEXT_0_0_IGNORED_ID: NodeId = NodeId(2);
    pub const PARAGRAPH_1_IGNORED_ID: NodeId = NodeId(3);
    pub const STATIC_TEXT_1_0_ID: NodeId = NodeId(4);
    pub const PARAGRAPH_2_ID: NodeId = NodeId(5);
    pub const STATIC_TEXT_2_0_ID: NodeId = NodeId(6);
    pub const PARAGRAPH_3_IGNORED_ID: NodeId = NodeId(7);
    pub const EMPTY_CONTAINER_3_0_IGNORED_ID: NodeId = NodeId(8);
    pub const LINK_3_1_IGNORED_ID: NodeId = NodeId(9);
    pub const STATIC_TEXT_3_1_0_ID: NodeId = NodeId(10);
    pub const BUTTON_3_2_ID: NodeId = NodeId(11);
    pub const EMPTY_CONTAINER_3_3_IGNORED_ID: NodeId = NodeId(12);

    pub fn test_tree() -> crate::tree::Tree {
        let mut classes = NodeClassSet::new();
        let root = {
            let mut builder = NodeBuilder::new(Role::RootWebArea);
            builder.set_children(vec![
                PARAGRAPH_0_ID,
                PARAGRAPH_1_IGNORED_ID,
                PARAGRAPH_2_ID,
                PARAGRAPH_3_IGNORED_ID,
            ]);
            builder.build(&mut classes)
        };
        let paragraph_0 = {
            let mut builder = NodeBuilder::new(Role::Paragraph);
            builder.set_children(vec![STATIC_TEXT_0_0_IGNORED_ID]);
            builder.build(&mut classes)
        };
        let static_text_0_0_ignored = {
            let mut builder = NodeBuilder::new(Role::StaticText);
            builder.set_name("static_text_0_0_ignored");
            builder.build(&mut classes)
        };
        let paragraph_1_ignored = {
            let mut builder = NodeBuilder::new(Role::Paragraph);
            builder.set_transform(Affine::translate(Vec2::new(10.0, 40.0)));
            builder.set_bounds(Rect {
                x0: 0.0,
                y0: 0.0,
                x1: 800.0,
                y1: 40.0,
            });
            builder.set_children(vec![STATIC_TEXT_1_0_ID]);
            builder.build(&mut classes)
        };
        let static_text_1_0 = {
            let mut builder = NodeBuilder::new(Role::StaticText);
            builder.set_bounds(Rect {
                x0: 10.0,
                y0: 10.0,
                x1: 90.0,
                y1: 30.0,
            });
            builder.set_name("static_text_1_0");
            builder.build(&mut classes)
        };
        let paragraph_2 = {
            let mut builder = NodeBuilder::new(Role::Paragraph);
            builder.set_children(vec![STATIC_TEXT_2_0_ID]);
            builder.build(&mut classes)
        };
        let static_text_2_0 = {
            let mut builder = NodeBuilder::new(Role::StaticText);
            builder.set_name("static_text_2_0");
            builder.build(&mut classes)
        };
        let paragraph_3_ignored = {
            let mut builder = NodeBuilder::new(Role::Paragraph);
            builder.set_children(vec![
                EMPTY_CONTAINER_3_0_IGNORED_ID,
                LINK_3_1_IGNORED_ID,
                BUTTON_3_2_ID,
                EMPTY_CONTAINER_3_3_IGNORED_ID,
            ]);
            builder.build(&mut classes)
        };
        let empty_container_3_0_ignored =
            NodeBuilder::new(Role::GenericContainer).build(&mut classes);
        let link_3_1_ignored = {
            let mut builder = NodeBuilder::new(Role::Link);
            builder.set_children(vec![STATIC_TEXT_3_1_0_ID]);
            builder.set_linked();
            builder.build(&mut classes)
        };
        let static_text_3_1_0 = {
            let mut builder = NodeBuilder::new(Role::StaticText);
            builder.set_name("static_text_3_1_0");
            builder.build(&mut classes)
        };
        let button_3_2 = {
            let mut builder = NodeBuilder::new(Role::Button);
            builder.set_name("button_3_2");
            builder.build(&mut classes)
        };
        let empty_container_3_3_ignored =
            NodeBuilder::new(Role::GenericContainer).build(&mut classes);
        let initial_update = TreeUpdate {
            nodes: vec![
                (ROOT_ID, root),
                (PARAGRAPH_0_ID, paragraph_0),
                (STATIC_TEXT_0_0_IGNORED_ID, static_text_0_0_ignored),
                (PARAGRAPH_1_IGNORED_ID, paragraph_1_ignored),
                (STATIC_TEXT_1_0_ID, static_text_1_0),
                (PARAGRAPH_2_ID, paragraph_2),
                (STATIC_TEXT_2_0_ID, static_text_2_0),
                (PARAGRAPH_3_IGNORED_ID, paragraph_3_ignored),
                (EMPTY_CONTAINER_3_0_IGNORED_ID, empty_container_3_0_ignored),
                (LINK_3_1_IGNORED_ID, link_3_1_ignored),
                (STATIC_TEXT_3_1_0_ID, static_text_3_1_0),
                (BUTTON_3_2_ID, button_3_2),
                (EMPTY_CONTAINER_3_3_IGNORED_ID, empty_container_3_3_ignored),
            ],
            tree: Some(Tree::new(ROOT_ID)),
            focus: ROOT_ID,
        };
        crate::tree::Tree::new(initial_update, false)
    }

    pub fn test_tree_filter(node: &crate::Node) -> FilterResult {
        let id = node.id();
        if id == STATIC_TEXT_0_0_IGNORED_ID
            || id == PARAGRAPH_1_IGNORED_ID
            || id == PARAGRAPH_3_IGNORED_ID
            || id == EMPTY_CONTAINER_3_0_IGNORED_ID
            || id == LINK_3_1_IGNORED_ID
            || id == EMPTY_CONTAINER_3_3_IGNORED_ID
        {
            FilterResult::ExcludeNode
        } else {
            FilterResult::Include
        }
    }
}