1    type WrapPanel @mut =
2        inherit Panel
3    
4        var distance : u32 = 0
5    
6        def Control.measure w h =
7            let mut res_w = 0
8            let mut res_h = 0
9            let mut row_height = 0
10           let mut x = 0
11   
12           for item in items do
13               if not item.is_visible then
14                   continue
15   
16               measure_clamped item w h
17               let item_width = item.measure_width
18               let item_height = item.measure_height
19               let next_width = if x == 0
20                                then item_width
21                                else x + distance + item_width
22   
23               if next_width > w then
24                   res_w = res_w.max x
25                   x = item_width
26                   res_h += row_height + distance
27                   row_height = item_height
28               else
29                   row_height = row_height.max item_height
30                   if x <> 0 then
31                       x += distance
32   
33                   x += item_width
34   
35           if x <> 0 then
36               res_w = res_w.max x
37               res_h += row_height
38   
39           measure_width = res_w
40           measure_height = res_h
41   
42       def Control.arrange self =
43           let width = self.width
44           let mut x = 0
45           let mut y = 0
46           let mut row_height = 0
47   
48           let distance = self.distance
49           for item in self.items do
50               if not item.is_visible then
51                   continue
52   
53               arrange_measured item
54               let item_width = item.width
55               if x + item_width + distance > width && x <> 0 then
56                   x = 0
57                   y += row_height + distance
58                   row_height = 0
59   
60               if x <> 0 then
61                   x += distance
62   
63               let v = Vector3 x.as<f32> y.as<f32> Control.offset_z
64               item.position = v
65               x += item_width
66               row_height = row_height.max item.height
67   
68           let height = if row_height <> 0 then
69                            y + row_height
70                        else if y > 0 then
71                            y - distance
72                        else
73                            0
74   
75           self.height = height
76