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
| // LayoutConfig provides `create` below with configuration options.
pub const LayoutConfig = union(enum) {
pub const Amount = union(enum) {
pixels: f64,
fraction: f64,
};
Horizontal: struct {
width: Amount,
height: Amount,
spacing_pixels: f64,
align_contents: enum { left, right, center } = .center,
align_children: enum { top, bottom, center } = .center,
},
Vertical: struct {
width: Amount,
height: Amount,
spacing_pixels: f64,
align_contents: enum { top, bottom, center } = .center,
align_children: enum { left, right, center } = .center,
},
Box: struct {
width: Amount,
height: Amount,
child_origin: struct {
x: Amount,
y: Amount,
},
},
Stack: struct {
width: Amount,
height: Amount,
},
};
// Here is the function signature I had in mind.
pub fn create(config: LayoutConfig, children: anytype) !Layout;
// And finally, here is the above example as a bunch of calls to `create`:
const example_layout = try create(
// to put the thin column next to the column of rows,
// we use a Horizontal layout
.{ .Horizontal = .{
.width = .{ .fraction = 1.0 },
.height = .{ .fraction = 1.0 },
.spacing_pixels = 5.0,
// .align_contents = .center
// the above is unnecessary because we provided a default value.
.align_children = .top,
} },
.{
// we give children as a tuple of Layouts
// first the lefthand column
try create(
.{ .Vertical = .{
.width = .{ .fraction = 0.3 },
.height = .{ .fraction = 1.0 },
.spacing_pixels = 5.0,
.align_contents = .top,
} },
.{
// children, like the squiggles suggesting text,
// would go here!
},
),
// next the righthand column of rows
try create(
.{ .Vertical = .{
.width = .{ .fraction = 0.7 },
.height = .{ .fraction = 1.0 },
.spacing_pixels = 3.0,
} },
.{
// there are three children,
// two of which are straightforward
try create(.{ .Horizontal = .{
.width = .{ .fraction = 1.0 },
.height = .{ .pixels = 100 },
.spacing_pixels = 10.0,
} }, .{
// the shapes go here...
},
),
try create(.{ .Horizontal = .{
.width = .{ .fraction = 1.0 },
.height = .{ .pixels = 100 },
.spacing_pixels = 5,
} }, .{
// the text goes here...
},
),
// in this call, the height parameter uses up all the
// remaining height
try create(.{ .Stack = .{
.width = .{ .fraction = 1.0 },
.height = .{ .fraction = 1.0 },
} }, .{
// these children will be stacked on top of each other
// from back to front
try create(.{ .Box = .{
.width = .{ .fraction = 1.0 },
.height = .{ .fraction = 1.0 },
.child_origin = .{
.x = .{ .pixels = 0 },
.y = .{ .pixels = 0 },
},
} }, .{
// the image goes here...
}),
try create(.{ .Vertical = .{
.width = .{ .fraction = 0.3 },
.height = .{ .fraction = 1.0 },
.spacing_pixels = 3.0,
} }, .{
// the circles go here...
}),
try create(.{ .Box = .{
.width = .{ .fraction = 1.0 },
.height = .{ .fraction = 1.0 },
.child_origin = .{
.x = .{ .fraction = 0.7 },
.y = .{ .fraction = 0.6 },
},
} }, .{
// the red rotated rectangle goes here...
}),
}),
},
),
},
);
|