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
use super::Expression;
use crate::langtype::{NativeClass, Type};
use std::collections::{BTreeMap, HashMap};
use std::num::NonZeroUsize;
use std::rc::Rc;
pub type PropertyIndex = usize;
#[derive(Debug, Clone)]
pub enum Animation {
Static(Expression),
Transition(Expression),
}
#[derive(Debug, Clone)]
pub struct BindingExpression {
pub expression: Expression,
pub animation: Option<Animation>,
pub is_constant: bool,
}
#[derive(Debug)]
pub struct GlobalComponent {
pub name: String,
pub properties: Vec<Property>,
pub init_values: Vec<Option<BindingExpression>>,
pub const_properties: Vec<bool>,
pub public_properties: PublicProperties,
pub exported: bool,
pub aliases: Vec<String>,
pub is_builtin: bool,
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum PropertyReference {
Local { sub_component_path: Vec<usize>, property_index: PropertyIndex },
InNativeItem { sub_component_path: Vec<usize>, item_index: usize, prop_name: String },
InParent { level: NonZeroUsize, parent_reference: Box<PropertyReference> },
Global { global_index: usize, property_index: usize },
}
#[derive(Debug)]
pub struct Property {
pub name: String,
pub ty: Type,
}
#[derive(Debug, Clone)]
pub struct ListViewInfo {
pub viewport_y: PropertyReference,
pub viewport_height: PropertyReference,
pub viewport_width: PropertyReference,
pub listview_height: PropertyReference,
pub listview_width: PropertyReference,
pub prop_y: PropertyReference,
pub prop_width: PropertyReference,
pub prop_height: PropertyReference,
}
#[derive(Debug)]
pub struct RepeatedElement {
pub model: Expression,
pub index_prop: Option<PropertyIndex>,
pub data_prop: Option<PropertyIndex>,
pub sub_tree: ItemTree,
pub index_in_tree: usize,
pub listview: Option<ListViewInfo>,
}
pub struct Item {
pub ty: Rc<NativeClass>,
pub name: String,
pub index_in_tree: usize,
pub is_flickable_viewport: bool,
}
impl std::fmt::Debug for Item {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Item")
.field("ty", &self.ty.class_name)
.field("name", &self.name)
.field("index_in_tree", &self.index_in_tree)
.field("is_flickable_viewport", &self.is_flickable_viewport)
.finish()
}
}
#[derive(Debug)]
pub struct TreeNode {
pub sub_component_path: Vec<usize>,
pub item_index: usize,
pub repeated: bool,
pub children: Vec<TreeNode>,
}
impl TreeNode {
fn children_count(&self) -> usize {
let mut count = self.children.len();
for c in &self.children {
count += c.children_count();
}
count
}
pub fn visit_in_array(
&self,
visitor: &mut dyn FnMut(
&TreeNode,
usize,
usize,
),
) {
visitor(self, 1, 0);
visit_in_array_recursive(self, 1, 0, visitor);
fn visit_in_array_recursive(
node: &TreeNode,
children_offset: usize,
current_index: usize,
visitor: &mut dyn FnMut(&TreeNode, usize, usize),
) {
let mut offset = children_offset + node.children.len();
for c in &node.children {
visitor(c, offset, current_index);
offset += c.children_count();
}
let mut offset = children_offset + node.children.len();
for (i, c) in node.children.iter().enumerate() {
visit_in_array_recursive(c, offset, children_offset + i, visitor);
offset += c.children_count();
}
}
}
}
#[derive(Debug)]
pub struct SubComponent {
pub name: String,
pub properties: Vec<Property>,
pub items: Vec<Item>,
pub repeated: Vec<RepeatedElement>,
pub popup_windows: Vec<ItemTree>,
pub sub_components: Vec<SubComponentInstance>,
pub property_init: Vec<(PropertyReference, BindingExpression)>,
pub animations: HashMap<PropertyReference, Expression>,
pub two_way_bindings: Vec<(PropertyReference, PropertyReference)>,
pub const_properties: Vec<PropertyReference>,
pub init_code: Vec<Expression>,
pub layout_info_h: Expression,
pub layout_info_v: Expression,
}
impl SubComponent {
pub fn repeater_count(&self) -> usize {
let mut count = self.repeated.len();
for x in self.sub_components.iter() {
count += x.ty.repeater_count();
}
count
}
}
pub struct SubComponentInstance {
pub ty: Rc<SubComponent>,
pub name: String,
pub index_in_tree: usize,
pub index_of_first_child_in_tree: usize,
pub repeater_offset: usize,
}
impl std::fmt::Debug for SubComponentInstance {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SubComponentInstance")
.field("ty", &self.ty.name)
.field("name", &self.name)
.field("index_in_tree", &self.index_in_tree)
.field("index_of_first_child_in_tree", &self.index_of_first_child_in_tree)
.field("repeater_offset", &self.repeater_offset)
.finish()
}
}
#[derive(Debug)]
pub struct ItemTree {
pub root: SubComponent,
pub tree: TreeNode,
pub parent_context: Option<String>,
}
#[derive(Debug)]
pub struct PublicComponent {
pub public_properties: PublicProperties,
pub item_tree: ItemTree,
pub sub_components: Vec<Rc<SubComponent>>,
pub globals: Vec<GlobalComponent>,
}
pub type PublicProperties = BTreeMap<String, (Type, PropertyReference)>;