Repository navigation
Expand file tree
/
Copy pathelements.cpp
More file actions
1465 lines (1402 loc) · 52.2 KB
/
Copy pathelements.cpp
File metadata and controls
1465 lines (1402 loc) · 52.2 KB
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
#include "props.h"
#include <algorithm>
#include <chrono>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include <huxerui/file.h>
#include <huxerui/resource.h>
#include <huxerui/theme.h>
#include <huxerui/text.h>
namespace huxerui::tsx::detail {
namespace {
std::string Lowercase(std::string_view text) {
std::string result(text);
std::transform(result.begin(), result.end(), result.begin(), [](unsigned char character) {
return static_cast<char>(std::tolower(character));
});
return result;
}
std::string AttributeName(std::string_view text) {
std::string result;
result.reserve(text.size());
for (const char character : text) {
if (character == '-' || character == '_') {
continue;
}
result.push_back(static_cast<char>(std::tolower(static_cast<unsigned char>(character))));
}
return result;
}
std::optional<ValidationResult> ValidationFromProps(const quickjs::Value& props) {
const auto validation = Property(props, "validation");
if (!validation || !validation->IsObject()) {
return std::nullopt;
}
const auto status = StringProperty(*validation, "status");
if (!status) {
return std::nullopt;
}
const auto name = AttributeName(*status);
const auto message = StringProperty(*validation, "message").value_or("");
if (name == "invalid") {
return ValidationResult::Invalid(message);
}
if (name == "valid") {
return ValidationResult::Valid();
}
if (name == "pending") {
return ValidationResult::Pending(message);
}
return ValidationResult::None();
}
std::optional<std::chrono::year_month_day> ParseDate(std::string_view text) {
int year = 0;
int month = 0;
int day = 0;
const std::string copy(text);
if (std::sscanf(copy.c_str(), "%d-%d-%d", &year, &month, &day) != 3) {
return std::nullopt;
}
const std::chrono::year_month_day date{
std::chrono::year{year},
std::chrono::month{static_cast<unsigned int>(month)},
std::chrono::day{static_cast<unsigned int>(day)},
};
if (!date.ok()) {
return std::nullopt;
}
return date;
}
std::string FormatDate(std::chrono::year_month_day date) {
char buffer[16]{};
std::snprintf(
buffer,
sizeof(buffer),
"%04d-%02u-%02u",
static_cast<int>(date.year()),
static_cast<unsigned int>(date.month()),
static_cast<unsigned int>(date.day())
);
return buffer;
}
std::optional<std::chrono::minutes> ParseTime(std::string_view text) {
int hours = 0;
int minutes = 0;
const std::string copy(text);
if (std::sscanf(copy.c_str(), "%d:%d", &hours, &minutes) != 2) {
return std::nullopt;
}
if (hours < 0 || hours > 23 || minutes < 0 || minutes > 59) {
return std::nullopt;
}
return std::chrono::minutes{hours * 60 + minutes};
}
std::string FormatTime(std::chrono::minutes value) {
const auto total = value.count();
char buffer[8]{};
std::snprintf(
buffer,
sizeof(buffer),
"%02d:%02d",
static_cast<int>(total / 60),
static_cast<int>(total % 60)
);
return buffer;
}
std::optional<TextRole> ParseRole(const quickjs::Value& props) {
const auto name = StringProperty(props, "role");
if (!name) {
return std::nullopt;
}
const auto role = Lowercase(*name);
if (role == "body") {
return TextRole::Body;
}
if (role == "label") {
return TextRole::Label;
}
if (role == "title") {
return TextRole::Title;
}
return std::nullopt;
}
std::optional<FontWeight> ParseFontWeight(const quickjs::Value& value) {
if (value.IsNumber()) {
const auto number = value.ToNumber();
if (!number.Succeeded()) {
return std::nullopt;
}
return static_cast<FontWeight>(std::clamp(static_cast<int>(number.Value()), 100, 900));
}
const auto text = value.ToString();
if (!text.Succeeded()) {
return std::nullopt;
}
const auto name = AttributeName(text.Value());
if (name == "thin") {
return FontWeight::Thin;
}
if (name == "extralight" || name == "ultralight") {
return FontWeight::ExtraLight;
}
if (name == "light") {
return FontWeight::Light;
}
if (name == "regular" || name == "normal") {
return FontWeight::Regular;
}
if (name == "medium") {
return FontWeight::Medium;
}
if (name == "semibold" || name == "demibold") {
return FontWeight::SemiBold;
}
if (name == "bold") {
return FontWeight::Bold;
}
if (name == "extrabold" || name == "ultrabold") {
return FontWeight::ExtraBold;
}
if (name == "black" || name == "heavy") {
return FontWeight::Black;
}
return std::nullopt;
}
std::optional<TextAlign> ParseTextAlign(const quickjs::Value& value) {
const auto text = value.ToString();
if (!text.Succeeded()) {
return std::nullopt;
}
const auto name = AttributeName(text.Value());
if (name == "leading" || name == "start" || name == "left") {
return TextAlign::Leading;
}
if (name == "center") {
return TextAlign::Center;
}
if (name == "trailing" || name == "end" || name == "right") {
return TextAlign::Trailing;
}
return std::nullopt;
}
std::optional<TextDecoration> ParseDecoration(const quickjs::Value& value) {
const auto text = value.ToString();
if (!text.Succeeded()) {
return std::nullopt;
}
const auto name = AttributeName(text.Value());
if (name == "none") {
return TextDecoration::None;
}
if (name == "underline") {
return TextDecoration::Underline;
}
if (name == "strikethrough" || name == "lineThrough") {
return TextDecoration::StrikeThrough;
}
return std::nullopt;
}
std::optional<TextStyle> TextStyleFromProps(const quickjs::Value& props) {
const bool has_size = HasProperty(props, "fontSize");
const bool has_weight = HasProperty(props, "fontWeight");
const bool has_italic = HasProperty(props, "italic");
const bool has_family = HasProperty(props, "fontFamily");
const bool has_color = HasProperty(props, "color");
const bool has_decoration = HasProperty(props, "decoration");
if (!has_size && !has_weight && !has_italic && !has_family && !has_color && !has_decoration) {
return std::nullopt;
}
float size = 14.0F;
if (const auto number = NumberProperty(props, "fontSize")) {
size = static_cast<float>(*number);
}
Font font = Font::System(size);
if (const auto family = StringProperty(props, "fontFamily")) {
const auto name = Lowercase(*family);
if (name == "system") {
font = Font::System(size);
} else if (name == "monospace") {
font = Font::Monospace(size);
} else {
font = Font::Named(*family, size);
}
}
if (const auto value = Property(props, "fontWeight")) {
if (const auto weight = ParseFontWeight(*value)) {
font = font.WithWeight(*weight);
}
}
if (const auto italic = BoolProperty(props, "italic"); italic && *italic) {
font = font.WithSlant(FontSlant::Italic);
}
TextStyle style{.font = font, .foreground = Color::Rgb(31, 35, 40)};
if (const auto value = Property(props, "color")) {
if (const auto color = ToColor(*value)) {
style.foreground = *color;
}
}
if (const auto value = Property(props, "decoration")) {
if (const auto decoration = ParseDecoration(*value)) {
style.decoration = *decoration;
}
}
return style;
}
std::string ChildText(const quickjs::Value& props) {
const auto children = Property(props, "children");
return children ? TextContent(*children) : std::string{};
}
View BuildLayout(RuntimeImpl& runtime, const quickjs::Value& props, int kind) {
auto children = runtime.RenderChildren(
Property(props, "children").value_or(runtime.Context().Undefined())
);
switch (kind) {
case 0:
return Column(std::move(children));
case 1:
return Row(std::move(children));
case 2:
return Flow(std::move(children));
default:
return Stack(std::move(children));
}
}
View BuildText(RuntimeImpl& runtime, const quickjs::Value& props) {
StringVariant text(ChildText(props));
if (auto localized = StringVariantProperty(props, "value")) {
text = std::move(*localized);
}
const auto role = ParseRole(props).value_or(TextRole::Body);
Text element(std::move(text), role);
if (const auto style = TextStyleFromProps(props)) {
element = std::move(element).Style(*style);
}
if (const auto value = Property(props, "textAlign")) {
if (const auto align = ParseTextAlign(*value)) {
element = std::move(element).Align(*align);
}
}
return element;
}
View BuildButton(RuntimeImpl& runtime, const quickjs::Value& props) {
StringVariant label(ChildText(props));
if (auto localized = StringVariantProperty(props, "label")) {
label = std::move(*localized);
}
return Button(std::move(label));
}
std::optional<ImageVariant> ImageFromValue(const quickjs::Value& source) {
if (source.IsString()) {
const auto path = source.ToString();
if (!path.Succeeded()) {
return std::nullopt;
}
try {
return ImageAsset::FromFile(File(path.Value()));
} catch (...) {
return std::nullopt;
}
}
if (!source.IsObject()) {
return std::nullopt;
}
if (const auto file = StringProperty(source, "file")) {
try {
return ImageAsset::FromFile(File(*file));
} catch (...) {
return std::nullopt;
}
}
if (const auto resource = ParseResourceId(source)) {
return ImageResource(resource->first, resource->second);
}
return std::nullopt;
}
std::optional<ImageVariant> ImageFromProps(const quickjs::Value& props) {
const auto source = Property(props, "src");
if (!source) {
return std::nullopt;
}
return ImageFromValue(*source);
}
View BuildIconButton(RuntimeImpl& runtime, const quickjs::Value& props) {
const auto image = ImageFromProps(props);
if (!image) {
return Text("[IconButton: missing src]");
}
StringVariant label("");
if (auto localized = StringVariantProperty(props, "label")) {
label = std::move(*localized);
}
return IconButton(*image, std::move(label));
}
View BuildImage(RuntimeImpl& runtime, const quickjs::Value& props) {
const auto image = ImageFromProps(props);
if (!image) {
return Text("[Image: missing src]");
}
Image element(*image);
if (const auto fit = StringProperty(props, "fit")) {
const auto name = AttributeName(*fit);
if (name == "none") {
element = std::move(element).Fit(ImageFit::None);
} else if (name == "contain") {
element = std::move(element).Fit(ImageFit::Contain);
} else if (name == "cover") {
element = std::move(element).Fit(ImageFit::Cover);
} else if (name == "fill") {
element = std::move(element).Fit(ImageFit::Fill);
} else if (name == "scaledown") {
element = std::move(element).Fit(ImageFit::ScaleDown);
}
}
if (const auto value = Property(props, "tint")) {
if (const auto color = ToColor(*value)) {
element = std::move(element).Tint(*color);
}
}
return element;
}
void SetOptionalFrameValue(
quickjs::Context& context,
quickjs::Value& object,
const char* name,
std::optional<float> value
) {
if (value) {
static_cast<void>(object.SetProperty(name, context.Number(*value)));
}
}
View BuildSelect(RuntimeImpl& runtime, const quickjs::Value& props) {
const std::string component = runtime.CurrentComponentPath();
const auto items_value = Property(props, "items");
if (!items_value || !items_value->IsArray()) {
return Text("Select requires an items array");
}
const auto length = items_value->Length();
if (!length.Succeeded() || length.Value() <= 0) {
return Text("Select requires a non-empty items array");
}
std::vector<quickjs::Value> items;
items.reserve(static_cast<std::size_t>(length.Value()));
for (std::int64_t index = 0; index < length.Value(); ++index) {
const auto element = items_value->GetElement(static_cast<std::uint32_t>(index));
if (element.Succeeded()) {
items.push_back(element.Value());
}
}
if (items.empty()) {
return Text("Select requires at least one valid item");
}
const auto selected = static_cast<std::size_t>(std::clamp(
NumberProperty(props, "selected").value_or(0.0),
0.0,
static_cast<double>(items.size() - 1)
));
Select element(items, selected, [&runtime](const quickjs::Value& value) {
return runtime.RenderValue(value);
});
if (auto label = StringVariantProperty(props, "label")) {
element = std::move(element).Label(std::move(*label));
}
if (const auto validation = ValidationFromProps(props)) {
element = std::move(element).Validation(*validation);
}
if (const auto handler = Property(props, "onChange"); handler && handler->IsFunction()) {
element = std::move(element).OnChanged(
[impl = &runtime, function = *handler, component](std::size_t next) {
const quickjs::Value arguments[]{
impl->Context().Number(static_cast<double>(next))
};
impl->InvokeHandler(function, arguments, DiagnosticPhase::Event, component);
}
);
}
if (const auto handler = Property(props, "onExpandedChange");
handler && handler->IsFunction()) {
element = std::move(element).OnExpandedChanged(
[impl = &runtime, function = *handler, component](bool expanded) {
const quickjs::Value arguments[]{impl->Context().Boolean(expanded)};
impl->InvokeHandler(function, arguments, DiagnosticPhase::Event, component);
}
);
}
return element;
}
View BuildComboBox(RuntimeImpl& runtime, const quickjs::Value& props) {
const std::string component = runtime.CurrentComponentPath();
TextEditingValue value;
value.text = StringProperty(props, "value").value_or("");
std::vector<std::string> suggestions;
if (const auto list = Property(props, "suggestions"); list && list->IsArray()) {
const auto length = list->Length();
if (length.Succeeded()) {
suggestions.reserve(static_cast<std::size_t>(length.Value()));
for (std::int64_t index = 0; index < length.Value(); ++index) {
const auto element = list->GetElement(static_cast<std::uint32_t>(index));
if (!element.Succeeded()) {
continue;
}
const auto text = element.Value().ToString();
if (text.Succeeded()) {
suggestions.push_back(text.Value());
}
}
}
}
ComboBox element(value, suggestions);
if (auto label = StringVariantProperty(props, "label")) {
element = std::move(element).Label(std::move(*label));
}
if (auto placeholder = StringVariantProperty(props, "placeholder")) {
element = std::move(element).Placeholder(std::move(*placeholder));
}
if (const auto max_length = NumberProperty(props, "maxLength")) {
element = std::move(element).MaxLength(
static_cast<std::size_t>(std::max(0.0, *max_length))
);
}
if (const auto validation = ValidationFromProps(props)) {
element = std::move(element).Validation(*validation);
}
if (const auto handler = Property(props, "onChange"); handler && handler->IsFunction()) {
element = std::move(element).OnChanged(
[impl = &runtime, function = *handler, component](const TextEditingValue& next) {
const auto text = impl->Context().NewString(next.text);
if (text.Succeeded()) {
const quickjs::Value arguments[]{text.Value()};
impl->InvokeHandler(function, arguments, DiagnosticPhase::Event, component);
}
}
);
}
if (const auto handler = Property(props, "onSelect"); handler && handler->IsFunction()) {
element = std::move(element).OnSelected(
[impl = &runtime, function = *handler, component](
std::size_t index, const TextEditingValue& next) {
const auto text = impl->Context().NewString(next.text);
if (text.Succeeded()) {
const quickjs::Value arguments[]{
impl->Context().Number(static_cast<double>(index)),
text.Value(),
};
impl->InvokeHandler(function, arguments, DiagnosticPhase::Event, component);
}
}
);
}
if (const auto handler = Property(props, "onSubmit"); handler && handler->IsFunction()) {
element = std::move(element).OnSubmitted(
[impl = &runtime, function = *handler, component] {
impl->InvokeHandler(function, {}, DiagnosticPhase::Event, component);
}
);
}
if (const auto handler = Property(props, "onExpandedChange");
handler && handler->IsFunction()) {
element = std::move(element).OnExpandedChanged(
[impl = &runtime, function = *handler, component](bool expanded) {
const quickjs::Value arguments[]{impl->Context().Boolean(expanded)};
impl->InvokeHandler(function, arguments, DiagnosticPhase::Event, component);
}
);
}
return element;
}
View BuildDatePicker(RuntimeImpl& runtime, const quickjs::Value& props) {
const std::string component = runtime.CurrentComponentPath();
const auto value = ParseDate(StringProperty(props, "value").value_or(""));
if (!value) {
return Text("DatePicker requires value as YYYY-MM-DD");
}
DatePicker element(*value);
if (auto label = StringVariantProperty(props, "label")) {
element = std::move(element).Label(std::move(*label));
}
if (const auto validation = ValidationFromProps(props)) {
element = std::move(element).Validation(*validation);
}
if (const auto minimum = ParseDate(StringProperty(props, "min").value_or(""))) {
element = std::move(element).Minimum(*minimum);
}
if (const auto maximum = ParseDate(StringProperty(props, "max").value_or(""))) {
element = std::move(element).Maximum(*maximum);
}
if (const auto handler = Property(props, "onChange"); handler && handler->IsFunction()) {
element = std::move(element).OnChanged(
[impl = &runtime, function = *handler, component](std::chrono::year_month_day next) {
const auto text = impl->Context().NewString(FormatDate(next));
if (text.Succeeded()) {
const quickjs::Value arguments[]{text.Value()};
impl->InvokeHandler(function, arguments, DiagnosticPhase::Event, component);
}
}
);
}
return element;
}
View BuildTimePicker(RuntimeImpl& runtime, const quickjs::Value& props) {
const std::string component = runtime.CurrentComponentPath();
const auto value = ParseTime(StringProperty(props, "value").value_or(""));
if (!value) {
return Text("TimePicker requires value as HH:MM");
}
TimePicker element(*value);
if (auto label = StringVariantProperty(props, "label")) {
element = std::move(element).Label(std::move(*label));
}
if (const auto validation = ValidationFromProps(props)) {
element = std::move(element).Validation(*validation);
}
if (const auto step = NumberProperty(props, "step"); step && *step > 0.0) {
element = std::move(element).Step(
std::chrono::minutes{static_cast<std::int64_t>(*step)}
);
}
if (const auto handler = Property(props, "onChange"); handler && handler->IsFunction()) {
element = std::move(element).OnChanged(
[impl = &runtime, function = *handler, component](std::chrono::minutes next) {
const auto text = impl->Context().NewString(FormatTime(next));
if (text.Succeeded()) {
const quickjs::Value arguments[]{text.Value()};
impl->InvokeHandler(function, arguments, DiagnosticPhase::Event, component);
}
}
);
}
return element;
}
View BuildRefreshBox(RuntimeImpl& runtime, const quickjs::Value& props) {
const std::string component = runtime.CurrentComponentPath();
auto children = runtime.RenderChildren(
Property(props, "children").value_or(runtime.Context().Undefined())
);
View content = children.size() == 1 ? children.front() : Column(std::move(children));
RefreshBox element(std::move(content), BoolProperty(props, "refreshing").value_or(false));
if (const auto handler = Property(props, "onRefresh"); handler && handler->IsFunction()) {
element = std::move(element).OnRefresh(
[impl = &runtime, function = *handler, component] {
impl->InvokeHandler(function, {}, DiagnosticPhase::Event, component);
}
);
}
return element;
}
View BuildPager(RuntimeImpl& runtime, const quickjs::Value& props) {
const std::string component = runtime.CurrentComponentPath();
auto children = runtime.RenderChildren(
Property(props, "children").value_or(runtime.Context().Undefined())
);
if (children.empty()) {
return Text("Pager requires at least one child");
}
const auto selected = static_cast<std::size_t>(std::clamp(
NumberProperty(props, "selected").value_or(0.0),
0.0,
static_cast<double>(children.size() - 1)
));
Pager element(std::move(children), selected);
if (const auto axis = StringProperty(props, "axis")) {
element = std::move(element).ScrollAxis(
AttributeName(*axis) == "vertical" ? Axis::Vertical : Axis::Horizontal
);
}
if (const auto reverse = BoolProperty(props, "reverse")) {
element = std::move(element).Reverse(*reverse);
}
if (const auto drag = BoolProperty(props, "dragEnabled")) {
element = std::move(element).DragEnabled(*drag);
}
if (const auto handler = Property(props, "onChange"); handler && handler->IsFunction()) {
element = std::move(element).OnChanged(
[impl = &runtime, function = *handler, component](std::size_t next) {
const quickjs::Value arguments[]{
impl->Context().Number(static_cast<double>(next))
};
impl->InvokeHandler(function, arguments, DiagnosticPhase::Event, component);
}
);
}
if (const auto handler = Property(props, "onSettled"); handler && handler->IsFunction()) {
element = std::move(element).OnSettled(
[impl = &runtime, function = *handler, component](std::size_t next) {
const quickjs::Value arguments[]{
impl->Context().Number(static_cast<double>(next))
};
impl->InvokeHandler(function, arguments, DiagnosticPhase::Event, component);
}
);
}
return element;
}
View BuildSplitLayout(RuntimeImpl& runtime, const quickjs::Value& props) {
const std::string component = runtime.CurrentComponentPath();
auto children = runtime.RenderChildren(
Property(props, "children").value_or(runtime.Context().Undefined())
);
if (children.size() < 2) {
return Text("SplitLayout requires at least two children");
}
SplitLayout element(std::move(children));
if (const auto axis = StringProperty(props, "axis")) {
element = std::move(element).Orientation(
AttributeName(*axis) == "vertical" ? Axis::Vertical : Axis::Horizontal
);
}
if (const auto handler = Property(props, "onChange"); handler && handler->IsFunction()) {
element = std::move(element).OnChanged(
[impl = &runtime, function = *handler, component](
std::vector<SplitChange> changes) {
auto array = impl->Context().NewArray();
if (!array.Succeeded()) {
return;
}
for (std::size_t index = 0; index < changes.size(); ++index) {
auto item = impl->Context().NewObject();
if (!item.Succeeded()) {
continue;
}
auto& object = item.Value();
static_cast<void>(object.SetProperty(
"index",
impl->Context().Number(static_cast<double>(changes[index].index))
));
static_cast<void>(object.SetProperty(
"grow",
impl->Context().Number(static_cast<double>(changes[index].grow.factor))
));
auto frame = impl->Context().NewObject();
if (frame.Succeeded()) {
SetOptionalFrameValue(impl->Context(), frame.Value(), "width", changes[index].frame.width);
SetOptionalFrameValue(impl->Context(), frame.Value(), "height", changes[index].frame.height);
SetOptionalFrameValue(impl->Context(), frame.Value(), "minWidth", changes[index].frame.min_width);
SetOptionalFrameValue(impl->Context(), frame.Value(), "maxWidth", changes[index].frame.max_width);
SetOptionalFrameValue(impl->Context(), frame.Value(), "minHeight", changes[index].frame.min_height);
SetOptionalFrameValue(impl->Context(), frame.Value(), "maxHeight", changes[index].frame.max_height);
static_cast<void>(object.SetProperty("frame", frame.Value()));
}
static_cast<void>(array.Value().SetElement(
static_cast<std::uint32_t>(index),
object
));
}
const quickjs::Value arguments[]{array.Value()};
impl->InvokeHandler(function, arguments, DiagnosticPhase::Event, component);
}
);
}
return element;
}
View BuildTopAppBar(RuntimeImpl& runtime, const quickjs::Value& props) {
StringVariant title(ChildText(props));
if (auto localized = StringVariantProperty(props, "title")) {
title = std::move(*localized);
}
std::optional<View> leading;
if (const auto value = Property(props, "leading");
value && !value->IsUndefined() && !value->IsNull()) {
leading = runtime.RenderValue(*value);
}
auto actions = runtime.RenderChildren(
Property(props, "actions").value_or(runtime.Context().Undefined())
);
TopAppBar element(std::move(title), std::move(leading), std::move(actions));
if (const auto align = StringProperty(props, "titleAlign");
align && AttributeName(*align) == "center") {
element = std::move(element).TitleAlignment(TopAppBarTitleAlignment::Center);
}
return element;
}
View BuildCanvas(RuntimeImpl& runtime, const quickjs::Value& props) {
const auto callback = Property(props, "onPaint");
if (!callback || !callback->IsFunction()) {
return Canvas([](PaintContext&, Size) {});
}
return Canvas(
[impl = &runtime, callback = *callback](PaintContext& paint, Size size) {
const auto id = impl->RegisterPaintContext(paint);
const quickjs::Value arguments[]{
impl->Context().Number(static_cast<double>(id)),
impl->Context().Number(size.width),
impl->Context().Number(size.height),
};
static_cast<void>(callback.Call(arguments));
if (auto* state = impl->FindPaintState(id)) {
while (state->clips > 0) {
paint.PopClip();
--state->clips;
}
while (state->transforms > 0) {
paint.PopTransform();
--state->transforms;
}
}
impl->UnregisterPaintContext(id);
}
);
}
View BuildDivider(RuntimeImpl& runtime, const quickjs::Value& props) {
const auto axis = StringProperty(props, "axis").value_or("horizontal");
return Divider(AttributeName(axis) == "vertical" ? Axis::Vertical : Axis::Horizontal);
}
View BuildScrollView(RuntimeImpl& runtime, const quickjs::Value& props) {
auto children = runtime.RenderChildren(
Property(props, "children").value_or(runtime.Context().Undefined())
);
View content = children.size() == 1 ? children.front() : Column(std::move(children));
ScrollView element(content);
const auto axis = StringProperty(props, "axis").value_or("vertical");
element = std::move(element).ScrollAxis(AttributeName(axis) == "horizontal" ? Axis::Horizontal : Axis::Vertical);
if (const auto ref = Property(props, "scrollRef"); ref && ref->IsObject()) {
if (const auto controller = runtime.FindScrollController(*ref)) {
element = std::move(element).Controller(*controller);
}
}
return element;
}
View BuildSelectionArea(RuntimeImpl& runtime, const quickjs::Value& props) {
auto children = runtime.RenderChildren(
Property(props, "children").value_or(runtime.Context().Undefined())
);
View content = children.size() == 1 ? children.front() : Column(std::move(children));
return SelectionArea(content);
}
View BuildTextField(RuntimeImpl& runtime, const quickjs::Value& props) {
const std::string component = runtime.CurrentComponentPath();
TextEditingValue value;
value.text = StringProperty(props, "value").value_or("");
if (const auto selection = Property(props, "selection"); selection && selection->IsObject()) {
const auto anchor = NumberProperty(*selection, "anchor");
const auto active = NumberProperty(*selection, "active");
if (anchor && active) {
value.selection.anchor = static_cast<TextOffset>(*anchor);
value.selection.active = static_cast<TextOffset>(*active);
if (const auto affinity = StringProperty(*selection, "affinity");
affinity && AttributeName(*affinity) == "upstream") {
value.selection.affinity = TextAffinity::Upstream;
}
}
}
TextField element(value);
if (const auto label = StringProperty(props, "label")) {
element = std::move(element).Label(*label);
}
if (const auto placeholder = StringProperty(props, "placeholder")) {
element = std::move(element).Placeholder(*placeholder);
}
if (const auto variant = StringProperty(props, "variant")) {
const auto name = AttributeName(*variant);
if (name == "filled") {
element = std::move(element).Variant(TextFieldVariant::Filled);
} else if (name == "outlined") {
element = std::move(element).Variant(TextFieldVariant::Outlined);
} else if (name == "standard") {
element = std::move(element).Variant(TextFieldVariant::Standard);
}
}
if (const auto multiline = BoolProperty(props, "multiline"); multiline && *multiline) {
element = std::move(element).LineLimits(TextFieldLineLimits::MultiLine());
}
if (const auto max_length = NumberProperty(props, "maxLength")) {
element = std::move(element).MaxLength(static_cast<std::size_t>(std::max(0.0, *max_length)));
}
if (const auto secure = BoolProperty(props, "secure"); secure && *secure) {
element = std::move(element).Secure();
}
if (const auto validation = ValidationFromProps(props)) {
element = std::move(element).Validation(*validation);
}
const auto on_change = Property(props, "onChange");
const auto on_edit = Property(props, "onEdit");
if ((on_change && on_change->IsFunction()) || (on_edit && on_edit->IsFunction())) {
element = std::move(element).OnChanged(
[impl = &runtime, on_change, on_edit, component](const TextEditingValue& next) {
if (on_change && on_change->IsFunction()) {
const auto argument = impl->Context().NewString(next.text);
if (argument.Succeeded()) {
const quickjs::Value arguments[]{argument.Value()};
impl->InvokeHandler(*on_change, arguments, DiagnosticPhase::Event, component);
}
}
if (on_edit && on_edit->IsFunction()) {
try {
const auto event = MakeTextEditingEvent(impl->Context(), next);
const quickjs::Value arguments[]{event};
impl->InvokeHandler(*on_edit, arguments, DiagnosticPhase::Event, component);
} catch (const std::exception& error) {
impl->ReportMessage(
DiagnosticPhase::Event,
"text edit event",
error.what(),
component
);
}
}
}
);
}
if (const auto handler = Property(props, "onSubmit"); handler && handler->IsFunction()) {
element = std::move(element).OnSubmitted([impl = &runtime, function = *handler, component] {
impl->InvokeHandler(function, {}, DiagnosticPhase::Event, component);
});
}
return element;
}
View BuildCheckbox(RuntimeImpl& runtime, const quickjs::Value& props) {
const std::string component = runtime.CurrentComponentPath();
const std::string label = StringProperty(props, "label").value_or(ChildText(props));
const bool checked = BoolProperty(props, "checked").value_or(false);
Checkbox element = label.empty() ? Checkbox(checked) : Checkbox(label, checked);
if (const auto handler = Property(props, "onChange"); handler && handler->IsFunction()) {
element = std::move(element).OnChanged([impl = &runtime, function = *handler, component](bool next) {
const quickjs::Value arguments[]{impl->Context().Boolean(next)};
impl->InvokeHandler(function, arguments, DiagnosticPhase::Event, component);
});
}
return element;
}
View BuildRadioButton(RuntimeImpl& runtime, const quickjs::Value& props) {
const std::string component = runtime.CurrentComponentPath();
const std::string label = StringProperty(props, "label").value_or(ChildText(props));
const bool selected = BoolProperty(props, "selected").value_or(false);
RadioButton element = label.empty() ? RadioButton(selected) : RadioButton(label, selected);
if (const auto handler = Property(props, "onChange"); handler && handler->IsFunction()) {
element = std::move(element).OnChanged([impl = &runtime, function = *handler, component](bool next) {
const quickjs::Value arguments[]{impl->Context().Boolean(next)};
impl->InvokeHandler(function, arguments, DiagnosticPhase::Event, component);
});
}
return element;
}
View BuildSwitch(RuntimeImpl& runtime, const quickjs::Value& props) {
const std::string component = runtime.CurrentComponentPath();
const std::string label = StringProperty(props, "label").value_or(ChildText(props));
const bool checked = BoolProperty(props, "checked").value_or(false);
Switch element = label.empty() ? Switch(checked) : Switch(label, checked);
if (const auto handler = Property(props, "onChange"); handler && handler->IsFunction()) {
element = std::move(element).OnChanged([impl = &runtime, function = *handler, component](bool next) {
const quickjs::Value arguments[]{impl->Context().Boolean(next)};
impl->InvokeHandler(function, arguments, DiagnosticPhase::Event, component);
});
}
return element;
}
View BuildSlider(RuntimeImpl& runtime, const quickjs::Value& props) {
const std::string component = runtime.CurrentComponentPath();
const float value = static_cast<float>(NumberProperty(props, "value").value_or(0.0));
Slider element(value);
const auto minimum = NumberProperty(props, "min");
const auto maximum = NumberProperty(props, "max");
if (minimum || maximum) {
element = std::move(element).Range(
static_cast<float>(minimum.value_or(0.0)),
static_cast<float>(maximum.value_or(1.0))
);
}
if (const auto step = NumberProperty(props, "step")) {
element = std::move(element).Step(static_cast<float>(*step));
}
if (const auto handler = Property(props, "onChange"); handler && handler->IsFunction()) {
element = std::move(element).OnChanged([impl = &runtime, function = *handler, component](float next) {
const quickjs::Value arguments[]{impl->Context().Number(next)};
impl->InvokeHandler(function, arguments, DiagnosticPhase::Event, component);
});
}
return element;
}
View BuildProgressBar(RuntimeImpl& runtime, const quickjs::Value& props) {
if (const auto progress = NumberProperty(props, "progress")) {
return ProgressBar(static_cast<float>(*progress));
}
return ProgressBar();
}
View BuildProgressCircle(RuntimeImpl& runtime, const quickjs::Value& props) {
if (const auto progress = NumberProperty(props, "progress")) {
return ProgressCircle(static_cast<float>(*progress));
}
return ProgressCircle();
}
View BuildChip(RuntimeImpl& runtime, const quickjs::Value& props) {
const std::string component = runtime.CurrentComponentPath();
const std::string label = StringProperty(props, "label").value_or(ChildText(props));
const bool selected = BoolProperty(props, "selected").value_or(false);
Chip element(label, selected);
if (const auto handler = Property(props, "onChange"); handler && handler->IsFunction()) {
element = std::move(element).OnChanged([impl = &runtime, function = *handler, component](bool next) {
const quickjs::Value arguments[]{impl->Context().Boolean(next)};
impl->InvokeHandler(function, arguments, DiagnosticPhase::Event, component);
});
}
return element;
}
std::vector<StringVariant> StringListProperty(const quickjs::Value& props, std::string_view name) {
std::vector<StringVariant> output;
const auto value = Property(props, name);
if (!value || !value->IsArray()) {
return output;
}
const auto length = value->Length();
if (!length.Succeeded()) {
return output;
}
output.reserve(static_cast<std::size_t>(length.Value()));
for (std::int64_t index = 0; index < length.Value(); ++index) {
const auto element = value->GetElement(static_cast<std::uint32_t>(index));
if (!element.Succeeded()) {