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
/
main.go
283 lines (256 loc) · 6.72 KB
/
main.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package main
import (
"fmt"
"go/format"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
var (
editForm *widget.Form
widType *widget.Label
paletteList *fyne.Container
)
func buildLibrary() fyne.CanvasObject {
var selected *widgetInfo
tempNames := []string{}
widgetLowerNames := []string{}
for _, name := range widgetNames {
widgetLowerNames = append(widgetLowerNames, strings.ToLower(name))
tempNames = append(tempNames, name)
}
list := widget.NewList(func() int {
return len(tempNames)
}, func() fyne.CanvasObject {
return widget.NewLabel("")
}, func(i widget.ListItemID, obj fyne.CanvasObject) {
obj.(*widget.Label).SetText(widgets[tempNames[i]].name)
})
list.OnSelected = func(i widget.ListItemID) {
if match, ok := widgets[tempNames[i]]; ok {
selected = &match
}
}
list.OnUnselected = func(widget.ListItemID) {
selected = nil
}
searchBox := widget.NewEntry()
searchBox.SetPlaceHolder("Search Widgets")
searchBox.OnChanged = func(s string) {
s = strings.ToLower(s)
tempNames = []string{}
for i := 0; i < len(widgetLowerNames); i++ {
if strings.Contains(widgetLowerNames[i], s) {
tempNames = append(tempNames, widgetNames[i])
}
}
list.Refresh()
list.Select(0) // Needed for new selection
list.Unselect(0) // Without this (and with the above), list is behaving in a weird way
}
return container.NewBorder(searchBox, widget.NewButtonWithIcon("Insert", theme.ContentAddIcon(), func() {
if c, ok := current.(*overlayContainer); ok {
if selected != nil {
c.c.Objects = append(c.c.Objects, wrapContent(selected.create(), c.c))
c.c.Refresh()
}
return
}
log.Println("Please select a container")
}), nil, nil, list)
}
func buildUI(win fyne.Window) fyne.CanvasObject {
content := previewUI().(*fyne.Container)
overlay := wrapContent(content, nil)
wrap := container.NewMax(overlay)
toolbar := widget.NewToolbar(
widget.NewToolbarAction(theme.FolderOpenIcon(), func() {
d := dialog.NewFileOpen(func(r fyne.URIReadCloser, err error) {
if err != nil {
dialog.ShowError(err, win)
}
if r == nil {
return
}
obj := DecodeJSON(r)
_ = r.Close()
overlay = wrapContent(obj, nil)
wrap.Objects[0] = overlay
wrap.Refresh()
}, win)
d.SetFilter(storage.NewExtensionFileFilter([]string{".json"}))
d.Show()
}),
widget.NewToolbarAction(theme.DocumentSaveIcon(), func() {
d := dialog.NewFileSave(func(w fyne.URIWriteCloser, err error) {
if err != nil {
dialog.ShowError(err, win)
}
if w == nil {
return
}
err = EncodeJSON(overlay, w)
if err != nil {
dialog.ShowError(err, win)
}
_ = w.Close()
}, win)
d.SetFilter(storage.NewExtensionFileFilter([]string{".json"}))
d.SetFileName("main.ui.json")
d.Show()
}),
widget.NewToolbarAction(theme.DownloadIcon(), func() {
packagesList := packagesRequired(overlay)
code := exportCode(packagesList, overlay)
fmt.Println(code)
}),
widget.NewToolbarAction(theme.MailForwardIcon(), func() {
packagesList := append(packagesRequired(overlay), "app")
code := exportCode(packagesList, overlay)
code += `
func main() {
myApp := app.New()
myWindow := myApp.NewWindow("Hello")
myWindow.SetContent(makeUI())
myWindow.ShowAndRun()
}
`
path := filepath.Join(os.TempDir(), "fynebuilder")
os.MkdirAll(path, 0711)
path = filepath.Join(path, "main.go")
_ = ioutil.WriteFile(path, []byte(code), 0600)
cmd := exec.Command("go", "run", path)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Start()
}))
widType = widget.NewLabelWithStyle("(None Selected)", fyne.TextAlignCenter, fyne.TextStyle{Bold: true})
paletteList = container.NewVBox()
palette := container.NewBorder(widType, nil, nil, nil,
container.NewGridWithRows(2, widget.NewCard("Properties", "", paletteList),
widget.NewCard("Component List", "", buildLibrary()),
))
split := container.NewHSplit(wrap, palette)
split.Offset = 0.8
return container.New(layout.NewBorderLayout(toolbar, nil, nil, nil), toolbar,
split)
}
func packagesRequired(obj fyne.CanvasObject) []string {
if w, ok := obj.(*overlayWidget); ok {
return w.Packages()
}
ret := []string{"container"}
var objs []fyne.CanvasObject
if c, ok := obj.(*fyne.Container); ok {
objs = c.Objects
} else if c, ok := obj.(*overlayContainer); ok {
objs = c.c.Objects
}
for _, w := range objs {
for _, p := range packagesRequired(w) {
added := false
for _, exists := range ret {
if p == exists {
added = true
break
}
}
if !added {
ret = append(ret, p)
}
}
}
return ret
}
func choose(o fyne.CanvasObject) {
typeName := reflect.TypeOf(o).Elem().Name()
widName := reflect.TypeOf(o).String()
l := reflect.ValueOf(o).Elem()
if typeName == "Entry" {
if l.FieldByName("Password").Bool() {
typeName = "PasswordEntry"
} else if l.FieldByName("MultiLine").Bool() {
typeName = "MultiLineEntry"
}
widName = "*widget." + typeName
}
widType.SetText(typeName)
var items []*widget.FormItem
if match, ok := widgets[widName]; ok {
items = match.edit(o)
}
editForm = widget.NewForm(items...)
remove := widget.NewButton("Remove", func() {
var parent *fyne.Container
var obj fyne.CanvasObject
if c, ok := current.(*overlayContainer); ok {
parent = c.parent
obj = c
} else if w, ok := current.(*overlayWidget); ok {
parent = w.parent
for _, o := range parent.Objects { // match our widget in the container wrapping us
if c, ok := o.(*fyne.Container); ok && c.Objects[0] == w.child {
obj = c
break
}
}
}
if parent == nil {
log.Println("Nothing to remove")
return
}
parent.Remove(obj)
parent.Refresh()
})
paletteList.Objects = []fyne.CanvasObject{editForm, remove}
paletteList.Refresh()
}
func exportCode(pkgs []string, obj fyne.CanvasObject) string {
for i := 0; i < len(pkgs); i++ {
pkgs[i] = fmt.Sprintf(` "fyne.io/fyne/v2/%s"`, pkgs[i])
}
code := fmt.Sprintf(`
package main
import (
"fyne.io/fyne/v2"
%s
)
func makeUI() fyne.CanvasObject {
return %#v
}
`,
strings.Join(pkgs, "\n"),
obj)
formatted, err := format.Source([]byte(code))
if err != nil {
log.Fatal(err)
}
return string(formatted)
}
func main() {
a := app.NewWithID("xyz.andy.fynebuilder")
initIcons()
initWidgets()
w := a.NewWindow("Fyne Builder")
w.SetContent(buildUI(w))
w.Resize(fyne.NewSize(600, 400))
w.ShowAndRun()
}
func previewUI() fyne.CanvasObject {
return container.New(layout.NewVBoxLayout(),
widget.NewIcon(theme.ContentAddIcon()),
widget.NewLabel("label"),
widget.NewButton("Button", func() {}))
}