This repository has been archived by the owner on Oct 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
layout.go
198 lines (184 loc) · 4.31 KB
/
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
package main
import (
"sort"
"strconv"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
)
type layoutInfo struct {
create func(map[string]string) fyne.Layout
edit func(*fyne.Container, map[string]string) []*widget.FormItem
}
var (
// layoutNames is an array with the list of names of all the layouts
layoutNames = extractLayoutNames()
layoutProps = make(map[*fyne.Container]map[string]string)
layouts = map[string]layoutInfo{
"Center": {
func(map[string]string) fyne.Layout {
return layout.NewCenterLayout()
},
nil,
},
"Form": {
func(map[string]string) fyne.Layout {
return layout.NewFormLayout()
},
nil,
},
"Grid": {
func(props map[string]string) fyne.Layout {
rowCol := props["grid_type"]
if rowCol == "" {
rowCol = "Columns"
}
count := props["count"]
if count == "" {
count = "2"
}
num, err := strconv.ParseInt(count, 0, 0)
if err != nil {
num = 2
}
if rowCol == "Rows" {
return layout.NewGridLayoutWithRows(int(num))
}
return layout.NewGridLayoutWithColumns(int(num))
},
func(c *fyne.Container, props map[string]string) []*widget.FormItem {
rowCol := props["grid_type"]
if rowCol == "" {
rowCol = "Columns"
}
count := props["count"]
if count == "" {
count = "2"
}
cols := widget.NewEntry()
cols.SetText(count)
vert := widget.NewSelect([]string{"Columns", "Rows"}, nil)
vert.SetSelected(rowCol)
change := func(string) {
if cols.Text == "" {
return
}
num, err := strconv.ParseInt(cols.Text, 0, 0)
if err != nil {
return
}
props["grid_type"] = vert.Selected
props["count"] = cols.Text
if vert.Selected == "Rows" {
c.Layout = layout.NewGridLayoutWithRows(int(num))
} else {
c.Layout = layout.NewGridLayoutWithColumns(int(num))
}
c.Refresh()
}
cols.OnChanged = change
vert.OnChanged = change
return []*widget.FormItem{
widget.NewFormItem("Count", cols),
widget.NewFormItem("Arrange in", vert),
}
},
},
"GridWrap": {
func(props map[string]string) fyne.Layout {
width := props["width"]
if width == "" {
width = "100"
}
height := props["height"]
if height == "" {
height = "100"
}
w, err := strconv.ParseInt(width, 0, 0)
if err != nil {
w = 100
}
h, err := strconv.ParseInt(height, 0, 0)
if err != nil {
h = 100
}
return layout.NewGridWrapLayout(fyne.NewSize(float32(w), float32(h)))
},
func(c *fyne.Container, props map[string]string) []*widget.FormItem {
width := props["width"]
if width == "" {
width = "100"
}
height := props["height"]
if height == "" {
height = "100"
}
widthEnt := widget.NewEntry()
widthEnt.SetText(width)
heightEnt := widget.NewEntry()
heightEnt.SetText(height)
change := func(string) {
if widthEnt.Text == "" {
return
}
w, err := strconv.ParseInt(widthEnt.Text, 0, 0)
if err != nil {
return
}
if widthEnt.Text == "" {
return
}
h, err := strconv.ParseInt(heightEnt.Text, 0, 0)
if err != nil {
return
}
props["width"] = widthEnt.Text
props["height"] = heightEnt.Text
c.Layout = layout.NewGridWrapLayout(fyne.NewSize(float32(w), float32(h)))
c.Refresh()
}
widthEnt.OnChanged = change
heightEnt.OnChanged = change
return []*widget.FormItem{
widget.NewFormItem("Item Width", widthEnt),
widget.NewFormItem("Item Height", heightEnt),
}
},
},
"HBox": {
func(props map[string]string) fyne.Layout {
return layout.NewHBoxLayout()
},
nil,
},
"Max": {
func(props map[string]string) fyne.Layout {
return layout.NewMaxLayout()
},
nil,
},
"Padded": {
func(props map[string]string) fyne.Layout {
return layout.NewPaddedLayout()
},
nil,
},
"VBox": {
func(props map[string]string) fyne.Layout {
return layout.NewVBoxLayout()
},
nil,
},
}
)
// extractLayoutNames returns all the list of names of all the layouts known
func extractLayoutNames() []string {
var layoutsNamesFromData = make([]string, len(layouts))
i := 0
for k := range layouts {
layoutsNamesFromData[i] = k
i++
}
sort.Strings(layoutsNamesFromData)
return layoutsNamesFromData
}