-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.rs
642 lines (586 loc) · 21.8 KB
/
view.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
use crate::color::Color;
use crate::parser::view::models::layout::Orientation;
use crate::parser::view::models::{image, layout, text, AndroidView, SpinnerMode};
use crate::parser::view::Layout;
use thiserror::Error;
/// A model that represents a single view
///
/// This struct contains fields that are common to all views, a field that holds an enum of every
/// (sketchware original) view types with its used fields (`view`) (if the view type isn't
/// recognized, it will be set as None) and another field that holds the raw view, just in case you
/// needed it.
#[derive(Debug, Clone, PartialEq)]
pub struct View {
/// The id of this view, must be unique in the layout it belongs to; this is used to identify
/// and find views in your java code
pub id: String,
/// The background color of this view
pub background_color: Color,
pub height: layout::Size,
pub width: layout::Size,
pub padding: SidesValue,
pub margin: SidesValue,
/// The weight of this view; Weight is a value that defines how much of the parent layout this
/// view should occupy.
///
/// For example, if a layout contains two views and both views has the weight of `1`, both views
/// will get divided in half.
pub weight: u32,
/// The weight sum of this view; Weight sum is a value that defines the maximum sum of weight
/// of all children of this layout.
pub weight_sum: u32,
/// The layout gravity of this view; Layout gravity is a value that defines the gravity of this
/// view in the parent layout.
pub layout_gravity: layout::gravity::Gravity,
/// The view-type-specific fields are stored in this enum in this result.
///
/// This is stored in a result type because it doesn't really matter in the tree parsing process
///
/// Special note: the resulting reconstructed view's type field is dependent this field; unless
/// it's an `Err`, in which case the type field on the raw field is used.
pub view: Result<ViewType, ViewTypeConversionError>,
/// The children of this view
pub children: Vec<View>,
/// The raw view of this View. This may be used to access every fields of this view in its raw
/// form. Changes made on fields are then reflected on the view reconstruction result, except
/// for fields that are already exposed on the API (such as a specific view type field, or stuff
/// like padding and margin (stuff that are already exposed in this struct)).
///
/// Special note for the type field: the resulting reconstructed view's type field is dependent
/// on the view field of this struct; unless it's an `Err`, in which case the type field in here
/// is used.
pub raw: AndroidView,
}
impl View {
pub fn find_id(&self, id: &str) -> Option<&View> {
if self.id == id {
Some(self)
} else {
// recurse on children
self.children.iter().find_map(|i| i.find_id(id))
}
}
pub fn find_id_mut(&mut self, id: &str) -> Option<&mut View> {
if self.id == id {
Some(self)
} else {
// recurse on children
self.children.iter_mut().find_map(|i| i.find_id_mut(id))
}
}
}
impl From<AndroidView> for View {
fn from(value: AndroidView) -> Self {
View {
id: value.id.clone(),
background_color: value.layout.background_color,
height: value.layout.height,
width: value.layout.width,
padding: SidesValue {
top: value.layout.padding_top,
right: value.layout.padding_right,
bottom: value.layout.padding_bottom,
left: value.layout.padding_left,
},
margin: SidesValue {
top: value.layout.margin_top,
right: value.layout.margin_right,
bottom: value.layout.margin_bottom,
left: value.layout.margin_left,
},
weight: value.layout.weight,
weight_sum: value.layout.weight_sum,
layout_gravity: value.layout.layout_gravity,
view: ViewType::from_view(&value),
children: vec![],
raw: value,
}
}
}
/// A struct that stores 4 `u32` values (top, right, bottom, and left). Used as a model of
/// padding and margin
#[derive(Debug, Clone, PartialEq)]
pub struct SidesValue {
pub top: u32,
pub right: u32,
pub bottom: u32,
pub left: u32,
}
/// An enum that contains every sketchware original view types and its necessary fields, any other
/// fields that aren't used in the specific view type will be neglected.
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum ViewType {
LinearLayout {
orientation: layout::Orientation,
gravity: layout::gravity::Gravity,
},
ScrollView {
orientation: layout::Orientation,
gravity: layout::gravity::Gravity,
},
Button {
text: String,
text_color: Color,
text_size: u32,
text_style: text::TextType,
},
TextView {
text: String,
text_color: Color,
text_size: u32,
single_line: bool,
text_font: String,
text_style: text::TextType,
lines: u32,
},
EditText {
text: String,
text_color: Color,
text_size: u32,
single_line: bool,
text_font: String,
text_style: text::TextType,
lines: u32,
hint: String,
hint_color: Color,
ime_option: text::ImeOption,
input_type: text::InputType,
},
ImageView {
image_res_name: String,
image_scale_type: image::ImageScaleType,
},
WebView,
ProgressBar {
max_progress: u32,
progress: u32,
indeterminate: bool,
progress_style: String,
},
ListView {
divider_height: u32,
custom_view: String,
},
Spinner {
spinner_mode: SpinnerMode,
},
CheckBox {
checked: bool,
text: String,
text_color: Color,
text_size: u32,
text_font: String,
text_style: text::TextType,
},
Switch {
checked: bool,
text: String,
text_color: Color,
text_size: u32,
text_font: String,
text_style: text::TextType,
},
SeekBar {
max_progress: u32,
progress: u32,
},
CalendarView {
first_day_of_week: u8,
},
Fab {
image_res_name: String,
},
AdView {
adview_size: String,
},
MapView,
}
impl ViewType {
/// Converts an [`AndroidView`] into [`ViewType`]
pub fn from_view(android_view: &AndroidView) -> Result<Self, ViewTypeConversionError> {
// https://github.com/Iyxan23/sketchware-data/blob/main/data/view-types.md
Ok(match android_view.r#type {
0 => ViewType::LinearLayout {
orientation: android_view.layout.orientation,
gravity: android_view.layout.gravity,
},
2 => ViewType::ScrollView {
orientation: Orientation::Horizontal,
gravity: android_view.layout.gravity,
},
3 => ViewType::Button {
text: android_view.text.text.clone(),
text_color: android_view.text.text_color,
text_size: android_view.text.text_size,
text_style: android_view.text.text_type,
},
4 => ViewType::TextView {
text: android_view.text.text.clone(),
text_color: android_view.text.text_color,
text_size: android_view.text.text_size,
single_line: android_view.text.single_line,
text_font: android_view.text.text_font.clone(),
text_style: android_view.text.text_type,
lines: android_view.text.line,
},
5 => ViewType::EditText {
text: android_view.text.text.clone(),
text_color: android_view.text.text_color,
text_size: android_view.text.text_size,
single_line: android_view.text.single_line,
text_font: android_view.text.text_font.clone(),
text_style: android_view.text.text_type,
lines: android_view.text.line,
hint: android_view.text.hint.clone(),
hint_color: android_view.text.hint_color,
ime_option: android_view.text.ime_option,
input_type: android_view.text.input_type,
},
6 => ViewType::ImageView {
image_res_name: android_view
.image
.res_name
.as_ref()
.ok_or_else(|| ViewTypeConversionError::ResNameNotPresent {
view_id: android_view.id.to_owned(),
})?
.clone(),
image_scale_type: android_view.image.scale_type,
},
7 => ViewType::WebView,
8 => ViewType::ProgressBar {
max_progress: android_view.max,
progress: android_view.progress,
indeterminate: android_view.indeterminate,
progress_style: android_view.progress_style.clone(),
},
9 => ViewType::ListView {
divider_height: android_view.divider_height,
custom_view: android_view.custom_view.clone(),
},
10 => ViewType::Spinner {
spinner_mode: android_view.spinner_mode,
},
11 => ViewType::CheckBox {
checked: android_view.checked,
text: android_view.text.text.clone(),
text_color: android_view.text.text_color,
text_size: android_view.text.text_size,
text_font: android_view.text.text_font.clone(),
text_style: android_view.text.text_type,
},
12 => ViewType::ScrollView {
orientation: Orientation::Vertical,
gravity: android_view.layout.gravity,
},
13 => ViewType::Switch {
checked: android_view.checked,
text: android_view.text.text.clone(),
text_color: android_view.text.text_color,
text_size: android_view.text.text_size,
text_font: android_view.text.text_font.clone(),
text_style: android_view.text.text_type,
},
14 => ViewType::SeekBar {
max_progress: android_view.max,
progress: android_view.progress,
},
15 => ViewType::CalendarView {
first_day_of_week: android_view.first_day_of_week,
},
16 => ViewType::Fab {
image_res_name: android_view
.image
.res_name
.as_ref()
.ok_or_else(|| ViewTypeConversionError::ResNameNotPresent {
view_id: android_view.id.to_owned(),
})?
.clone(),
},
17 => ViewType::AdView {
adview_size: android_view.ad_size.clone(),
},
18 => ViewType::MapView,
_ => Err(ViewTypeConversionError::UnknownViewType {
view_type: android_view.r#type,
view_id: android_view.id.to_owned(),
})?,
})
}
/// Gets the type id of this view
pub fn get_type_id(&self) -> u8 {
match self {
ViewType::LinearLayout { .. } => 0,
ViewType::ScrollView { .. } => 2,
ViewType::Button { .. } => 3,
ViewType::TextView { .. } => 4,
ViewType::EditText { .. } => 5,
ViewType::ImageView { .. } => 6,
ViewType::WebView => 7,
ViewType::ProgressBar { .. } => 8,
ViewType::ListView { .. } => 9,
ViewType::Spinner { .. } => 10,
ViewType::CheckBox { .. } => 11,
ViewType::Switch { .. } => 12,
ViewType::SeekBar { .. } => 13,
ViewType::CalendarView { .. } => 14,
ViewType::Fab { .. } => 15,
ViewType::AdView { .. } => 16,
ViewType::MapView => 17,
}
}
/// Applies the specific values of a ViewType to the given view
pub fn apply_values_to_view(self, view: &mut AndroidView) {
match self {
ViewType::LinearLayout {
orientation,
gravity,
} => {
view.layout.orientation = orientation;
view.layout.gravity = gravity;
}
ViewType::ScrollView {
orientation,
gravity,
} => {
view.layout.orientation = orientation;
view.layout.gravity = gravity;
}
ViewType::Button {
text,
text_color,
text_size,
text_style,
} => {
view.text.text = text;
view.text.text_color = text_color;
view.text.text_size = text_size;
view.text.text_type = text_style;
}
ViewType::TextView {
text,
text_color,
text_size,
single_line,
text_font,
text_style,
lines,
} => {
view.text.text = text;
view.text.text_color = text_color;
view.text.text_size = text_size;
view.text.single_line = single_line;
view.text.text_font = text_font;
view.text.text_type = text_style;
view.text.line = lines;
}
ViewType::EditText {
text,
text_color,
text_size,
single_line,
text_font,
text_style,
lines,
hint,
hint_color,
ime_option,
input_type,
} => {
view.text.text = text;
view.text.text_color = text_color;
view.text.text_size = text_size;
view.text.single_line = single_line;
view.text.text_font = text_font;
view.text.text_type = text_style;
view.text.line = lines;
view.text.hint = hint;
view.text.hint_color = hint_color;
view.text.ime_option = ime_option;
view.text.input_type = input_type;
}
ViewType::ImageView {
image_res_name,
image_scale_type,
} => {
view.image.res_name = Some(image_res_name);
view.image.scale_type = image_scale_type;
}
ViewType::WebView => {}
ViewType::ProgressBar {
max_progress,
progress,
indeterminate,
progress_style,
} => {
view.max = max_progress;
view.progress = progress;
view.indeterminate = indeterminate;
view.progress_style = progress_style;
}
ViewType::ListView {
divider_height,
custom_view,
} => {
view.divider_height = divider_height;
view.custom_view = custom_view;
}
ViewType::Spinner { spinner_mode } => {
view.spinner_mode = spinner_mode;
}
ViewType::CheckBox {
checked,
text,
text_color,
text_size,
text_font,
text_style,
} => {
view.checked = checked;
view.text.text = text;
view.text.text_color = text_color;
view.text.text_size = text_size;
view.text.text_font = text_font;
view.text.text_type = text_style;
}
ViewType::Switch {
checked,
text,
text_color,
text_size,
text_font,
text_style,
} => {
view.checked = checked;
view.text.text = text;
view.text.text_color = text_color;
view.text.text_size = text_size;
view.text.text_font = text_font;
view.text.text_type = text_style;
}
ViewType::SeekBar {
max_progress,
progress,
} => {
view.max = max_progress;
view.progress = progress;
}
ViewType::CalendarView { first_day_of_week } => {
view.first_day_of_week = first_day_of_week;
}
ViewType::Fab { image_res_name } => {
view.image.res_name = Some(image_res_name);
}
ViewType::AdView { adview_size } => {
view.ad_size = adview_size;
}
ViewType::MapView => {}
}
}
}
#[derive(Error, Debug, Clone, PartialEq)]
pub enum ViewTypeConversionError {
#[error("unknown view type `{view_type}` on view with id `{view_id}`")]
UnknownViewType { view_type: u8, view_id: String },
#[error("the field res_name on view with id `{view_id}` is not present when needed as this view is either a FAB or an ImageView")]
ResNameNotPresent { view_id: String },
}
/// Converts a parser's raw [`Layout`] into a tree of views, the returned vector of views is the
/// children of the root view.
///
/// You might expect this function to return a single view because a layout only has one single
/// root view, but no, sketchware hardcodes the root view and it only stores its children.
pub fn parse_raw_layout(screen_view: Layout) -> Result<Vec<View>, ParseLayoutError> {
let mut result = Vec::<View>::new();
for view in screen_view.0 {
let parent_id = view
.parent
.as_ref()
.ok_or_else(|| ParseLayoutError::NoParentField {
view_id: view.id.to_owned(),
})?;
if parent_id == "root" {
result.push(view.into());
} else {
let parent = result
.iter_mut()
.find_map(|i| i.find_id_mut(&parent_id))
.ok_or_else(|| ParseLayoutError::NonexistentParent {
view_id: view.id.to_owned(),
parent_id: parent_id.to_owned(),
})?;
parent.children.push(view.into());
}
}
Ok(result)
}
#[derive(Error, Debug)]
pub enum ParseLayoutError {
#[error("view with id {view_id} doesn't have a parent field")]
NoParentField { view_id: String },
#[error("couldn't find parent of view with id `{view_id}` (parent id: `{parent_id}`)")]
NonexistentParent { view_id: String, parent_id: String },
}
/// Flattens a list of API [`View`] models into a single list of parser models
pub fn flatten_views(
views: Vec<View>,
parent_id: Option<String>,
parent_type: Option<u8>,
) -> Vec<AndroidView> {
// default parent is the root view
let parent_id = parent_id.unwrap_or_else(|| "root".to_string());
let parent_type = parent_type.unwrap_or_else(|| 0);
let mut result = Vec::<AndroidView>::new();
let mut children_result = Vec::<AndroidView>::new();
for (idx, view) in views.into_iter().enumerate() {
// get the raw view old
let mut result_view = view.raw;
// set our id, type and parent just to make sure those cheeky hackers tries to change it
result_view.id = view.id.to_owned();
result_view.parent = Some(parent_id.clone());
result_view.parent_type = parent_type as i8;
result_view.index = idx as u32; // <- here we need to set the index of the children
// Gets the type id from the view type that is in the `view` field. If it's an Err, will
// fallback to the type field from the raw view.
//
// also overwrite values from the view-specific fields to the raw view
let view_type_id = if let Ok(view_type) = view.view {
// set the type id to the one from view
let type_id = view_type.get_type_id();
view_type.apply_values_to_view(&mut result_view);
type_id
} else {
// use the one in the raw view
result_view.r#type
};
// and then just map the variables
result_view.layout.height = view.height;
result_view.layout.width = view.width;
result_view.layout.padding_bottom = view.padding.bottom;
result_view.layout.padding_left = view.padding.left;
result_view.layout.padding_right = view.padding.right;
result_view.layout.padding_top = view.padding.top;
result_view.layout.margin_bottom = view.margin.bottom;
result_view.layout.margin_left = view.margin.left;
result_view.layout.margin_right = view.margin.right;
result_view.layout.margin_top = view.margin.top;
result_view.layout.background_color = view.background_color;
result_view.layout.layout_gravity = view.layout_gravity;
result_view.layout.weight = view.weight;
result_view.layout.weight_sum = view.weight_sum;
// done, push it to the result
result.push(result_view);
// and also recursively flatten the children if there is any
if !view.children.is_empty() {
children_result.append(&mut flatten_views(
view.children,
Some(view.id),
Some(view_type_id),
))
}
}
// finally add the children views into the final result
result.append(&mut children_result);
result
}