-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.v
994 lines (940 loc) · 26.2 KB
/
stack.v
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
// Copyright (c) 2020-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by a GPL license
// that can be found in the LICENSE file.
module ui
import eventbus
// import gg
enum Direction {
row
column
}
/*
Column & Row are identical except everything is reversed:
Row is treated like a column turned by 90 degrees, so values for row are reversed.
Width -> Height
Height -> Width
X -> Y
Y -> X
*/
/********** different size's definitions ************
* container_size is simply: (width, height)
* adjusted_size is (adj_width, adj_height) corresponding of the compact/fitted size inherited from children sizes
* size() returns full_size, i.e. container_size + margin_size
* total_spacing() returns spacing
* free_size() returns free_size_direct and free_size_opposite (in the proper order) where:
* free_size_direct = container_size - total_spacing()
* free_size_opposite = container_size
N.B.:
* direct size is the size in the main direction of the stack: height for .column and width for .row
* opposite size is the converse
* no needs of functions: container_size() and adjusted_size()
***********************************/
struct StackConfig {
width int // To remove soon
height int // To remove soon
vertical_alignment VerticalAlignment
horizontal_alignment HorizontalAlignment
spacing Spacing = Spacing(0) // int
stretch bool
direction Direction
margin MarginConfig
// children related
widths []f32 // children sizes
heights []f32
align Alignments
vertical_alignments VerticalAlignments
horizontal_alignments HorizontalAlignments
}
struct Stack {
cache CachedSizes
mut:
x int
y int
width int
height int
z_index int
parent Layout
ui &UI
vertical_alignment VerticalAlignment
horizontal_alignment HorizontalAlignment
spacing []int // int
stretch bool
direction Direction
margin Margin
adj_width int
adj_height int
// children related
children []Widget
drawing_children []Widget
widths []f32 // children sizes
heights []f32
vertical_alignments VerticalAlignments // Flexible alignments by index overriding alignment.
horizontal_alignments HorizontalAlignments
alignments Alignments
}
fn stack(c StackConfig, children []Widget) &Stack {
// w, h := sizes_f32_to_int(c.width, c.height)
mut s := &Stack{
height: c.height // TODO to remove
width: c.width // TODO to remove
vertical_alignment: c.vertical_alignment
horizontal_alignment: c.horizontal_alignment
spacing: c.spacing.as_int_array(children.len - 1)
stretch: c.stretch
direction: c.direction
margin: c.margin.as_margin()
children: children
widths: c.widths
heights: c.heights
vertical_alignments: c.vertical_alignments
horizontal_alignments: c.horizontal_alignments
alignments: c.align
ui: 0
}
return s
}
fn (mut s Stack) init(parent Layout) {
s.parent = parent
mut ui := parent.get_ui()
s.ui = ui
s.init_size()
if parent is Window {
ui.window = parent
// Only once for all children recursively
// 1) find all the adjusted sizes
s.set_adjusted_size(0, true, s.ui)
// 2) set cache sizes
s.set_cache_sizes()
$if cache ? {
s.debug_show_cache(0, '')
}
// 3) set all the sizes (could be updated possibly for resizing)
s.set_children_sizes()
// All sizes have to be set before positionning widgets
// 4) Set the position of this stack (anchor could possibly be defined inside set_pos later as suggested by Kahsa)
s.set_pos(s.x, s.y)
// 5) children z_index
s.set_drawing_children()
}
// Init all children recursively
for mut child in s.children {
child.init(s)
}
// Set all children's positions recursively
if parent is Window {
s.set_children_pos()
$if android {
// window_size := gg.window_size()
// w := window_size.width
// h := window_size.height
// mut window := ui.window
// window.width, window.height = w, h
s.resize(parent.width, parent.height)
} $else {
if parent.mode in [.fullscreen, .max_size] {
// println('mode: ${parent.mode}')
s.resize(parent.width, parent.height)
}
}
}
}
fn (mut s Stack) init_size() {
parent := s.parent
parent_width, parent_height := parent.size()
// s.debug_show_sizes("decode before -> ")
if parent is Window {
// Default: like stretch = strue
s.height = parent_height - s.margin.top - s.margin.right
s.width = parent_width - s.margin.left - s.margin.right
} else if s.stretch {
if s.direction == .row {
s.height = parent_height - s.margin.top - s.margin.right
} else {
s.width = parent_width - s.margin.left - s.margin.right
}
}
}
fn (mut s Stack) set_children_sizes() {
$if scs ? {
s.debug_show_sizes('BEGIN set_children_size ')
}
//* size of children from *
c := &s.cache
widths, heights := s.children_sizes()
// set children sizes
$if scs ? {
println('s.widths: $s.widths s.heights: $s.heights widths: $widths heights: $heights')
}
for i, mut child in s.children {
mut w, mut h := child.size()
$if scs ? {
println('before propose_size $i) $child.type_name() ($w,$h) ')
}
if child is Stack || child is Group {
w, h = widths[i], heights[i]
} else {
$if scs ? {
// tmp := (s.widths[i] == ui.stretch)
// tmp2 := (c.weight_widths[i] <= 0)
// tmp3 := (s.heights[i] == ui.stretch)
// tmp4 := (c.weight_heights[i] <= 0)
// println("tmp=$tmp tmp2=$tmp2 tmp3=$tmp3 tmp4=$tmp4")
}
if c.width_type[i] in [.stretch, .weighted] {
w = widths[i]
}
if c.height_type[i] in [.stretch, .weighted] {
h = heights[i]
}
}
$if scs ? {
println('propose_size $i) $child.type_name() ($w,$h)')
}
child.propose_size(w, h)
if child is Stack {
child.set_children_sizes()
}
}
$if scs ? {
s.debug_show_sizes('END set_children_size ')
}
}
fn (s &Stack) children_sizes() ([]int, []int) {
mut mcw, mut mch := [0].repeat(s.children.len), [0].repeat(s.children.len)
// free size without margin and spacing
mut free_width, mut free_height := s.free_size()
mut c := &s.cache
free_width -= c.fixed_width
free_height -= c.fixed_height
$if cs ? {
println(' children_size: ${typeof(s).name} s.widths: $s.widths s.heights: $s.heights ')
println(' w weight: ($c.weight_widths, $c.width_mass) fixed: ($c.fixed_widths, $c.fixed_width, $c.min_width)')
println(' h weight: ($c.weight_heights, $c.height_mass) fixed: ($c.fixed_heights, $c.fixed_height, $c.min_height)')
println(' type w: $c.width_type h: $c.height_type')
println(' free w: $free_width h: $free_height ')
}
for i, child in s.children {
// child_w, child_h := child.size()
$if cs ? {
println('$i) $child.type_name()')
}
match c.width_type[i] {
.stretch {
if s.direction == .row {
$if cs2 ? {
println('$i) .stretch width row: weight = ${c.weight_widths[i]} / $c.width_mass')
}
weight := c.weight_widths[i] / c.width_mass
mcw[i] = int(weight * free_width)
} else {
$if cs2 ? {
println('$i) .stretch width col: free_w=$free_width')
}
mcw[i] = free_width
}
}
.weighted, .weighted_minsize {
weight := c.weight_widths[i] / c.width_mass
mcw[i] = int(weight * free_width)
}
.propose, .compact, .fixed {
mcw[i] = c.fixed_widths[i]
}
}
match c.height_type[i] {
.stretch {
if s.direction == .column {
$if cs2 ? {
println('$i) .stretch height col: weight = ${c.weight_heights[i]} / $c.height_mass')
}
weight := c.weight_heights[i] / c.height_mass
mch[i] = int(weight * free_height)
} else {
$if cs2 ? {
println('$i) .stretch height row : $free_width')
}
mch[i] = free_height
}
$if cs2 ? {
println('.Stretch height: $i) $child.type_name() ${mch[i]}')
}
}
.weighted, .weighted_minsize, .propose {
weight := c.weight_heights[i] / c.height_mass
mch[i] = int(weight * free_height)
}
.compact, .fixed {
mch[i] = c.fixed_heights[i]
}
}
}
$if cs ? {
println('mcw: $mcw mch: $mch')
}
return mcw, mch
}
/*********************
How to interpret weight ?
if weight_<size>s[i] is :
== 0 then fixed size
in ]0,1] then weighted size with two different cases: 1) fixed < 0 (updatable widget with propose_size) and 2) fixed ==0 (static)
== -2 then adjusted size
***********************/
fn (mut s Stack) set_cache_sizes() {
//
s.default_sizes()
//
len := s.children.len
mut c := &s.cache
// size preallocated
c.fixed_width, c.fixed_height = 0, 0
c.min_width, c.min_height = 0, 0
c.width_mass, c.height_mass = 0., 0.
// fixed_<size>s and weight_<size>s can be cached in the Stack struct as private fields
// since once they are determined, they would never be updated
// above all, they would be used when resizing
c.fixed_widths, c.fixed_heights = [0].repeat(len), [0].repeat(len)
c.weight_widths, c.weight_heights = [0.].repeat(len), [0.].repeat(len)
c.width_type, c.height_type = [ChildSize(0)].repeat(len), [ChildSize(0)].repeat(len)
for i, mut child in s.children {
mut cw := s.widths[i] or { 0. }
mut ch := s.heights[i] or { 0. }
if child is Stack {
child.adjustable_size()
}
// adjusted (natural size) child size
adj_child_width, adj_child_height := child.size()
// if ! (child is Stack) {
if adj_child_width == 0 && cw == 0 {
$if ui_stack_c0 ? {
println('WARNINNGS222: Bad compact widths for ${typeof(s).name} $s.widths')
}
s.widths[i] = stretch
cw = stretch
}
if adj_child_height == 0 && ch == 0 {
$if ui_stack_c0 ? {
println('WARNINNGS222: Bad compact widths for $child.type_name() $s.widths')
}
s.heights[i] = stretch
ch = stretch
}
// }
// adj_child_width, adj_child_height := child.size()
// TODO: here test if widget is zero sized????
// cw as child width with type f64
if cw > 1 {
// fixed size ?
if cw == int(cw) {
c.width_type[i] = .fixed
c.fixed_widths[i] = int(cw)
if s.direction == .row {
c.fixed_width += c.fixed_widths[i]
c.min_width += c.fixed_widths[i]
} else {
if c.fixed_widths[i] > c.fixed_width {
c.fixed_width = c.fixed_widths[i]
}
if c.fixed_widths[i] > c.min_width {
c.min_width = c.fixed_widths[i]
}
}
} else {
// Possibly useful for Stack children: 200.6 as 200 as minimal size and .6 as weight
c.width_type[i] = .weighted_minsize
c.fixed_widths[i] = int(cw)
c.weight_widths[i] = cw - int(cw)
if s.direction == .row { // sum rule
c.fixed_width += c.fixed_widths[i]
c.min_width += c.fixed_widths[i]
c.width_mass += c.weight_widths[i]
} else { // max rule
if c.fixed_widths[i] > c.fixed_width {
c.fixed_width = c.fixed_widths[i]
}
if c.fixed_widths[i] > c.min_width {
c.min_width = c.fixed_widths[i]
}
}
}
} else if cw > 0 {
// weighted size
c.width_type[i] = .weighted
c.weight_widths[i] = cw
// Internally, fixed_widths[i] is set to minimal fixed size
c.fixed_widths[i] = adj_child_width
if s.direction == .row {
c.width_mass += cw
c.min_width += c.fixed_widths[i]
} else {
if c.fixed_widths[i] > c.min_width {
c.min_width = c.fixed_widths[i]
}
}
} else if cw == 0 {
// width for Widget and adj_width for Layout
c.width_type[i] = .compact
c.fixed_widths[i] = adj_child_width
if s.direction == .row {
c.fixed_width += c.fixed_widths[i]
c.min_width += c.fixed_widths[i]
} else {
if c.fixed_widths[i] > c.fixed_width {
c.fixed_width = c.fixed_widths[i]
}
if c.fixed_widths[i] > c.min_width {
c.min_width = c.fixed_widths[i]
}
}
} else if cw >= -1 {
// weight_widths is now means that the children can have their size updated from the parent
// even reducing their size!!!
c.width_type[i] = .propose
c.weight_widths[i] = -cw
// This is the initial size
c.fixed_widths[i] = adj_child_width
if s.direction == .row {
c.width_mass += c.weight_widths[i]
c.min_width += c.fixed_widths[i]
} else {
if c.fixed_widths[i] > c.min_width {
c.min_width = c.fixed_widths[i]
}
}
} else if cw == stretch {
c.width_type[i] = .stretch
c.weight_widths[i] = 1.0
c.fixed_widths[i] = adj_child_width
if s.direction == .row {
c.width_mass += c.weight_widths[i]
c.min_width += c.fixed_widths[i]
} else {
if c.fixed_widths[i] > c.min_width {
c.min_width = c.fixed_widths[i]
}
}
}
// ch as child height with type f64
if ch > 1 {
// fixed size ?
if ch == int(ch) {
c.height_type[i] = .fixed
c.fixed_heights[i] = int(ch)
if s.direction == .column {
c.fixed_height += c.fixed_heights[i]
c.min_height += c.fixed_heights[i]
} else {
if c.fixed_heights[i] > c.fixed_height {
c.fixed_height = c.fixed_heights[i]
}
if c.fixed_heights[i] > c.min_height {
c.min_height = c.fixed_heights[i]
}
}
} else {
// Possibly useful for Stack children: 200.6 as 200 as minimal size and .6 as weight
c.height_type[i] = .weighted_minsize
c.fixed_heights[i] = int(ch)
c.weight_heights[i] = ch - int(ch)
if s.direction == .column {
c.fixed_height += c.fixed_heights[i]
c.min_height += c.fixed_heights[i]
c.height_mass += c.weight_heights[i]
} else {
if c.fixed_heights[i] > c.fixed_height {
c.fixed_height = c.fixed_heights[i]
}
if c.fixed_heights[i] > c.min_height {
c.min_height = c.fixed_heights[i]
}
}
}
} else if ch > 0 {
// weighted size
c.height_type[i] = .weighted
c.weight_heights[i] = ch
// Internally, fixed_heights[i] is set to minimal fixed size
c.fixed_heights[i] = adj_child_height
if s.direction == .column {
c.height_mass += ch
c.min_height += c.fixed_heights[i]
} else {
if c.fixed_heights[i] > c.min_height {
c.min_height = c.fixed_heights[i]
}
}
} else if ch == 0 {
// height for Widget and adj_height for Layout
c.height_type[i] = .compact
c.fixed_heights[i] = adj_child_height
if s.direction == .column {
c.fixed_height += c.fixed_heights[i]
c.min_height += c.fixed_heights[i]
} else {
if c.fixed_heights[i] > c.fixed_height {
c.fixed_height = c.fixed_heights[i]
}
if c.fixed_heights[i] > c.min_height {
c.min_height = c.fixed_heights[i]
}
}
} else if ch >= -1 {
// weight_heights is now means that the children can have their size updated from the parent
// even reducing their size!!!
c.height_type[i] = .propose
c.weight_heights[i] = -cw
// This is the initial size
c.fixed_heights[i] = adj_child_height
if s.direction == .column {
c.height_mass += c.weight_heights[i]
c.min_height += c.fixed_heights[i]
} else {
if c.fixed_heights[i] > c.min_height {
c.min_height = c.fixed_heights[i]
}
}
} else if ch == stretch {
c.height_type[i] = .stretch
c.weight_heights[i] = 1.
c.fixed_heights[i] = adj_child_height
if s.direction == .column {
c.height_mass += c.weight_heights[i]
c.min_height += c.fixed_heights[i]
} else {
if c.fixed_heights[i] > c.min_height {
c.min_height = c.fixed_heights[i]
}
}
}
// recursively do the same for Stack children
if child is Stack {
child.set_cache_sizes()
} else {
set_text_fixed(mut child, c.width_type[i], c.height_type[i])
}
}
}
// default values for s.widths and s.heights
fn (mut s Stack) default_sizes() {
st := f32(stretch)
// comp := f32(ui.compact)
p_equi := f32(1) / f32(s.children.len)
if s.direction == .row {
mut nb := s.heights.len
if nb < s.children.len {
for i, _ in s.children {
// println("i=$i nb=$nb child_len=${s.children.len} h_len= ${s.heights.len} ")
if i < nb {
continue
}
s.heights << st // if child is Stack || child is Group { st } else { comp }
}
}
// println("1) nb=$nb child_len=${s.children.len} h_len= ${s.heights.len} ")
nb = s.widths.len
if nb < s.children.len {
for i, _ in s.children {
// println("i=$i nb=$nb child_len=${s.children.len} w_len= ${s.widths.len} ")
if i < nb {
continue
}
p := if is_children_have_widget(s.children) {
compact
} else {
// equispaced
p_equi
}
s.widths << p
}
}
// println("2) nb=$nb child_len=${s.children.len} w_len= ${s.widths.len} ")
} else {
mut nb := s.widths.len
if nb < s.children.len {
for i, _ in s.children {
// println("i=$i nb=$nb child_len=${s.children.len} w_len= ${s.widths.len} ")
if i < nb {
continue
}
s.widths << st // if child is Stack || child is Group { st } else { comp }
}
}
// println("3) nb=$nb child_len=${s.children.len} w_len= ${s.widths.len} ")
nb = s.heights.len
if nb < s.children.len {
for i, _ in s.children {
// println("i=$i nb=$nb child_len=${s.children.len} h_len= ${s.heights.len} ")
if i < nb {
continue
}
p := if is_children_have_widget(s.children) {
compact
} else {
// equispaced
p_equi
}
s.heights << p
}
}
// println("4) nb=$nb child_len=${s.children.len} h_len= ${s.heights.len} ")
}
}
fn (mut s Stack) adjustable_size() {
if s.height == 0 {
$if adj ? {
print('stack ${typeof(s).name} ')
C.printf(' %p', s)
println(' adjusted height $s.height <- $s.adj_height')
}
s.height = s.adj_height
}
if s.width == 0 {
$if adj ? {
print('stack ${typeof(s).name} ')
C.printf(' %p', s)
println(' adjusted width $s.width <- $s.adj_width')
}
s.width = s.adj_width
}
}
fn (mut s Stack) propose_size(w int, h int) (int, int) {
s.width, s.height = w - s.margin.left - s.margin.right, h - s.margin.top - s.margin.bottom
return s.width, s.height
}
fn (s &Stack) size() (int, int) {
mut w := s.width
mut h := s.height
// TODO: this has to disappear (not depending on adjusted_size)
// if s.width < s.adj_width {
// w = s.adj_width
// }
// if s.height < s.adj_height {
// h = s.adj_height
// }
w += s.margin.left + s.margin.right
h += s.margin.top + s.margin.bottom
return w, h
}
fn (s &Stack) free_size() (int, int) {
mut w := s.width
mut h := s.height
if s.direction == .row {
w -= s.total_spacing()
} else {
h -= s.total_spacing()
}
return w, h
}
fn (mut s Stack) set_adjusted_size(i int, force bool, ui &UI) {
mut h := 0
mut w := 0
for mut child in s.children {
mut child_width, mut child_height := 0, 0
if child is Stack {
if force || child.adj_width == 0 {
child.set_adjusted_size(i + 1, force, ui)
}
child_width, child_height = child.adj_width + child.margin.left + child.margin.right,
child.adj_height + child.margin.top + child.margin.bottom
} else if child is Group {
if force || child.adj_width == 0 {
child.set_adjusted_size(i + 1, ui)
}
child_width, child_height = child.adj_width + child.margin_left + child.margin_right,
child.adj_height + child.margin_top + child.margin_bottom
} else {
if child is Label {
child.set_ui(ui)
} else if child is Button {
child.set_ui(ui)
}
child_width, child_height = child.size()
$if adj_size ? {
println('adj size child $child.type_name(): ($child_width, $child_height) ')
}
}
if s.direction == .column {
h += child_height // height of vertical stack means adding children's height
if child_width > w { // width of vertical stack means greatest children's width
w = child_width
}
} else {
w += child_width // width of horizontal stack means adding children's width
if child_height > h { // height of horizontal stack means greatest children's height
h = child_height
}
}
}
// adding total spacing between children
if s.direction == .column {
h += s.total_spacing()
} else {
w += s.total_spacing()
}
s.adj_width = w
s.adj_height = h
}
fn (mut s Stack) set_pos(x int, y int) {
// could depend on anchor in the future
// Default is anchor=.top_left here (and could be .top_right, .bottom_left, .bottom_right)
s.x = x + s.margin.left
s.y = y + s.margin.top
}
fn (mut s Stack) set_children_pos() {
mut x := s.x
mut y := s.y
for i, mut child in s.children {
child_width, child_height := child.size()
s.set_child_pos(child, i, x, y)
if s.direction == .row {
x += child_width
if i < s.children.len - 1 {
x += s.spacing[i]
}
} else {
y += child_height
if i < s.children.len - 1 {
y += s.spacing[i]
}
}
if child is Stack {
child.set_children_pos()
}
}
}
fn (s &Stack) set_child_pos(mut child Widget, i int, x int, y int) {
// Only alignment along the opposite direction (ex: .row if direction is .column and vice-versa) is considered
// TODO: alignment in the direct direction
// (for these different cases, container size in the direct direction is more complicated to compute)
$if scp ? {
println('set_children_pos: $i) ${typeof(s).name}-$child.type_name()')
}
child_width, child_height := child.size()
if s.direction == .column {
container_width := s.width
mut x_offset := 0
match s.get_horizontal_alignment(i) {
.left {
x_offset = 0
}
.center {
if container_width > child_width {
x_offset = (container_width - child_width) / 2
} else {
x_offset = 0
}
}
.right {
if container_width > child_width {
x_offset = (container_width - child_width)
} else {
x_offset = 0
}
}
}
child.set_pos(x + x_offset, y)
} else {
container_height := s.height
mut y_offset := 0
match s.get_vertical_alignment(i) {
.top {
y_offset = 0
}
.center {
if container_height > child_height {
y_offset = (container_height - child_height) / 2
} else {
y_offset = 0
}
}
.bottom {
if container_height > child_height {
y_offset = container_height - child_height
} else {
y_offset = 0
}
}
}
$if scp ? {
println(' set_pos ($x,$y + $y_offset)')
}
child.set_pos(x, y + y_offset)
}
}
fn (s &Stack) get_subscriber() &eventbus.Subscriber {
parent := s.parent
return parent.get_subscriber()
}
fn (mut s Stack) set_drawing_children() {
for mut child in s.children {
if child is Stack {
child.set_drawing_children()
}
// println("z_index: ${child.type_name()} $child.z_index")
if child.z_index > s.z_index {
s.z_index = child.z_index
}
}
// println("Stack: z_index $s.z_index ")
s.drawing_children = s.children.clone()
s.drawing_children.sort(a.z_index < b.z_index)
}
fn (mut s Stack) draw() {
// DEBUG MODE: Uncomment to display the bounding boxes
$if bb ? {
s.draw_bb()
}
for child in s.drawing_children {
// println("$child.type_name()")
child.draw()
}
}
fn (s &Stack) total_spacing() int {
mut total_spacing := 0
// println('len $s.children.len $s.spacing')
if s.spacing.len > 0 && s.children.len > 1 {
for i in 0 .. (s.children.len - 1) {
total_spacing += s.spacing[i]
}
}
// println('len $total_spacing')
return total_spacing
}
fn (s &Stack) get_ui() &UI {
return s.ui
}
fn (s &Stack) unfocus_all() {
for child in s.children {
child.unfocus()
}
}
fn (s &Stack) get_state() voidptr {
parent := s.parent
return parent.get_state()
}
fn (s &Stack) point_inside(x f64, y f64) bool {
return false // x >= s.x && x <= s.x + s.width && y >= s.y && y <= s.y + s.height
}
fn (mut s Stack) focus() {
// s.is_focused = true
// println('')
}
fn (mut s Stack) unfocus() {
s.unfocus_all()
// s.is_focused = false
// println('')
}
fn (s &Stack) is_focused() bool {
return false // s.is_focused
}
fn (mut s Stack) resize(width int, height int) {
// println("Stack resize $width, $height")
mut window := s.ui.window
window.update_text_scale()
// println('resize scale $window.text_scale')
s.init_size()
// s.set_adjusted_size(0, true, s.ui)
s.set_children_sizes()
s.set_children_pos()
}
pub fn (s &Stack) get_children() []Widget {
return s.children
}
pub fn (mut s Stack) set_children(c []Widget) {
s.children = c
}
fn (s &Stack) get_vertical_alignment(i int) VerticalAlignment {
mut align := s.vertical_alignment
if i in s.vertical_alignments.top {
align = .top
} else if i in s.vertical_alignments.center {
align = .center
} else if i in s.vertical_alignments.bottom {
align = .bottom
}
return align
}
fn (s &Stack) get_horizontal_alignment(i int) HorizontalAlignment {
mut align := s.horizontal_alignment
if i in s.horizontal_alignments.left {
align = .left
} else if i in s.horizontal_alignments.center {
align = .center
} else if i in s.horizontal_alignments.right {
align = .right
}
return align
}
fn (s &Stack) set_child_pos_aligned(mut child Widget, i int, x int, y int) {
child_width, child_height := child.size()
horizontal_alignment, vertical_alignment := s.get_alignments(i)
// set x_offset
container_width := s.width
mut x_offset := 0
match horizontal_alignment {
.left {
x_offset = 0
}
.center {
if container_width > child_width {
x_offset = (container_width - child_width) / 2
} else {
x_offset = 0
}
}
.right {
if container_width > child_width {
x_offset = (container_width - child_width)
} else {
x_offset = 0
}
}
}
// set y_offset
container_height := s.height
mut y_offset := 0
match vertical_alignment {
.top {
y_offset = 0
}
.center {
if container_height > child_height {
y_offset = (container_height - child_height) / 2
} else {
y_offset = 0
}
}
.bottom {
if container_height > child_height {
y_offset = container_height - child_height
} else {
y_offset = 0
}
}
}
child.set_pos(x + x_offset, y + y_offset)
}
fn (s &Stack) get_alignments(i int) (HorizontalAlignment, VerticalAlignment) {
mut hor_align := s.horizontal_alignment
mut ver_align := s.vertical_alignment
if i in s.alignments.center {
hor_align, ver_align = .center, .center
} else if i in s.alignments.left_top {
hor_align, ver_align = .left, .top
} else if i in s.alignments.top {
hor_align, ver_align = .center, .top
} else if i in s.alignments.right_top {
hor_align, ver_align = .right, .top
} else if i in s.alignments.right {
hor_align, ver_align = .right, .center
} else if i in s.alignments.right_bottom {
hor_align, ver_align = .right, .bottom
} else if i in s.alignments.bottom {
hor_align, ver_align = .center, .bottom
} else if i in s.alignments.left_bottom {
hor_align, ver_align = .left, .bottom
} else if i in s.alignments.left {
hor_align, ver_align = .left, .center
}
return hor_align, ver_align
}