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