forked from g3n/engine
/
builder_layout.go
200 lines (160 loc) · 5.14 KB
/
builder_layout.go
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
// Copyright 2016 The G3N Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gui
//
// BuilderLayoutHBox is builder for HBox layout
//
type BuilderLayoutHBox struct{}
// BuildLayout builds and returns an HBoxLayout with the specified attributes
func (bl *BuilderLayoutHBox) BuildLayout(b *Builder, am map[string]interface{}) (ILayout, error) {
// Creates layout and sets optional spacing
l := NewHBoxLayout()
var spacing float32
if sp := am[AttribSpacing]; sp != nil {
spacing = sp.(float32)
}
l.SetSpacing(spacing)
// Sets optional horizontal alignment
if ah := am[AttribAlignh]; ah != nil {
l.SetAlignH(ah.(Align))
}
// Sets optional minheight flag
if mh := am[AttribAutoHeight]; mh != nil {
l.SetAutoHeight(mh.(bool))
}
// Sets optional minwidth flag
if mw := am[AttribAutoWidth]; mw != nil {
l.SetAutoWidth(mw.(bool))
}
return l, nil
}
// BuildParams builds and returns a pointer to HBoxLayoutParams with the specified attributes
func (bl *BuilderLayoutHBox) BuildParams(b *Builder, am map[string]interface{}) (interface{}, error) {
// Creates layout parameters with default values
params := HBoxLayoutParams{Expand: 0, AlignV: AlignNone}
// Sets optional expand parameter
if expand := am[AttribExpand]; expand != nil {
params.Expand = expand.(float32)
}
// Sets optional align parameter
if alignv := am[AttribAlignv]; alignv != nil {
params.AlignV = alignv.(Align)
}
return ¶ms, nil
}
//
// BuilderLayoutVBox is builder for VBox layout
//
type BuilderLayoutVBox struct{}
// BuildLayout builds and returns an VBoxLayout with the specified attributes
func (bl *BuilderLayoutVBox) BuildLayout(b *Builder, am map[string]interface{}) (ILayout, error) {
// Creates layout and sets optional spacing
l := NewVBoxLayout()
var spacing float32
if sp := am[AttribSpacing]; sp != nil {
spacing = sp.(float32)
}
l.SetSpacing(spacing)
// Sets optional vertical alignment
if av := am[AttribAlignh]; av != nil {
l.SetAlignV(av.(Align))
}
// Sets optional minheight flag
if mh := am[AttribAutoHeight]; mh != nil {
l.SetAutoHeight(mh.(bool))
}
// Sets optional minwidth flag
if mw := am[AttribAutoWidth]; mw != nil {
l.SetAutoWidth(mw.(bool))
}
return l, nil
}
// BuildParams builds and returns a pointer to VBoxLayoutParams with the specified attributes
func (bl *BuilderLayoutVBox) BuildParams(b *Builder, am map[string]interface{}) (interface{}, error) {
// Creates layout parameters with default values
params := VBoxLayoutParams{Expand: 0, AlignH: AlignNone}
// Sets optional expand parameter
if expand := am[AttribExpand]; expand != nil {
params.Expand = expand.(float32)
}
// Sets optional align parameter
if alignh := am[AttribAlignh]; alignh != nil {
params.AlignH = alignh.(Align)
}
return ¶ms, nil
}
//
// BuilderLayoutGrid is builder for Grid layout
//
type BuilderLayoutGrid struct{}
// BuildLayout builds and returns a GridLayout with the specified attributes
func (bl *BuilderLayoutGrid) BuildLayout(b *Builder, am map[string]interface{}) (ILayout, error) {
// Get number of columns
v := am[AttribCols]
if v == nil {
return nil, b.err(am, AttribCols, "Number of columns must be supplied")
}
cols := v.(int)
if cols <= 0 {
return nil, b.err(am, AttribCols, "Invalid number of columns")
}
// Creates layout
l := NewGridLayout(cols)
// Sets optional horizontal alignment
if ah := am[AttribAlignh]; ah != nil {
l.SetAlignH(ah.(Align))
}
// Sets optional vertical alignment
if av := am[AttribAlignv]; av != nil {
l.SetAlignV(av.(Align))
}
// Sets optional horizontal expand flag
if eh := am[AttribExpandh]; eh != nil {
l.SetExpandH(eh.(bool))
}
// Sets optional vertical expand flag
if ev := am[AttribExpandv]; ev != nil {
l.SetExpandV(ev.(bool))
}
return l, nil
}
// BuildParams builds and returns a pointer to GridLayoutParams with the specified attributes
func (bl *BuilderLayoutGrid) BuildParams(b *Builder, am map[string]interface{}) (interface{}, error) {
// Creates layout parameters with default values
params := GridLayoutParams{
ColSpan: 0,
AlignH: AlignNone,
AlignV: AlignNone,
}
// Sets optional colspan
if cs := am[AttribColSpan]; cs != nil {
params.ColSpan = cs.(int)
}
// Sets optional alignh parameter
if align := am[AttribAlignh]; align != nil {
params.AlignH = align.(Align)
}
// Sets optional alignv parameter
if align := am[AttribAlignv]; align != nil {
params.AlignV = align.(Align)
}
return ¶ms, nil
}
//
// BuilderLayoutDock is builder for Dock layout
//
type BuilderLayoutDock struct{}
// BuildLayout builds and returns a DockLayout with the specified attributes
func (bl *BuilderLayoutDock) BuildLayout(b *Builder, am map[string]interface{}) (ILayout, error) {
return NewDockLayout(), nil
}
// BuildParams builds and returns a pointer to DockLayoutParams with the specified attributes
func (bl *BuilderLayoutDock) BuildParams(b *Builder, am map[string]interface{}) (interface{}, error) {
edge := am[AttribEdge]
if edge == nil {
return nil, b.err(am, AttribEdge, "Edge name not found")
}
params := DockLayoutParams{Edge: edge.(int)}
return ¶ms, nil
}