-
Notifications
You must be signed in to change notification settings - Fork 26
/
project.go
249 lines (217 loc) · 5.35 KB
/
project.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
package main
import (
"io"
"os"
"os/exec"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/widget"
)
func (d *defyne) showNewProjectDialog(w fyne.Window) {
parent := widget.NewButton("Choose directory", nil)
dir := defaultDir()
if dir != nil {
parent.SetText(dir.Name())
}
parent.OnTapped = func() {
dialog.ShowFolderOpen(func(u fyne.ListableURI, err error) {
if err != nil {
dialog.ShowError(err, d.win)
return
}
if u == nil {
return
}
dir = u
parent.SetText(u.Name())
}, w)
}
name := widget.NewEntry()
dialog.ShowForm("Create project", "Create", "Cancel", []*widget.FormItem{
widget.NewFormItem("Parent directory", parent),
widget.NewFormItem("Project name", name),
}, func(ok bool) {
if !ok {
return
}
dir, err := createProject(dir, name.Text)
if err != nil {
dialog.ShowError(err, w)
} else {
d.setProject(dir)
d.win.Show()
w.Close()
addRecent(dir, fyne.CurrentApp().Preferences())
}
}, w)
}
func (d *defyne) showOpenProjectDialog(w fyne.Window) {
dialog.ShowFolderOpen(func(u fyne.ListableURI, err error) {
if err != nil {
dialog.ShowError(err, d.win)
return
}
if u == nil {
return
}
d.setProject(u)
d.win.Show()
w.Close()
addRecent(u, fyne.CurrentApp().Preferences())
}, w)
}
func (d *defyne) showProjectSelect() {
a := fyne.CurrentApp()
w := a.NewWindow("Defyne : Open Project")
if d.projectRoot == nil { // this is our welcome screen
w.SetMainMenu(fyne.NewMainMenu(
fyne.NewMenu("File",
fyne.NewMenuItem("Open Project...", w.Show),
),
d.makeHelpMenu(w),
))
}
img := canvas.NewImageFromResource(resourceIconPng)
img.FillMode = canvas.ImageFillContain
img.SetMinSize(fyne.NewSize(192, 192))
openProject := widget.NewButton("Open Project", func() {
d.showOpenProjectDialog(w)
})
openProject.Importance = widget.HighImportance
open := container.NewBorder(widget.NewLabelWithStyle("Defyne", fyne.TextAlignCenter, fyne.TextStyle{Bold: true}),
openProject, nil, nil, container.NewCenter(img))
create := widget.NewButton("New Project", func() {
d.showNewProjectDialog(w)
})
w.SetContent(container.NewGridWithColumns(2,
container.NewBorder(widget.NewLabel("Recent projects"), create, nil, nil,
makeRecentList(a.Preferences(), func(u fyne.URI) {
d.setProject(u)
d.win.Show()
w.Close()
addRecent(u, fyne.CurrentApp().Preferences())
})),
open))
w.Resize(fyne.NewSize(620, 440))
w.Show()
}
func createProject(parent fyne.URI, name string) (fyne.URI, error) {
dir, err := storage.Child(parent, name)
if err != nil {
return nil, err
}
err = storage.CreateListable(dir)
if err != nil {
return nil, err
}
err = writeFile(dir, "main.go", `package main
import "fyne.io/fyne/v2/app"
func main() {
a := app.New()
w := a.NewWindow("`+name+`")
g := newGUI()
w.SetContent(g.makeUI())
w.ShowAndRun()
}
`)
if err != nil {
fyne.LogError("Failed to write main.go", err) // we can just return the partial project
return dir, nil
}
err = writeFile(dir, "main.gui.go", `// auto-generated
// Code generated by Defyne GUI builder.
package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
type gui struct {}
func newGUI() *gui {
return &gui{}
}
func (g *gui) makeUI() fyne.CanvasObject {
return container.NewVBox(
widget.NewLabel("Hello `+name+`!"))
}
`)
if err != nil {
fyne.LogError("Failed to write main.go", err) // we can just return the partial project
return dir, nil
}
err = writeFile(dir, "main.gui.json", `{
"Type": "*fyne.Container",
"Layout": "VBox",
"Name": "",
"Objects": [
{
"Type": "*widget.Label",
"Name": "",
"Struct": {
"Hidden": false,
"Text": "Hello `+name+`!",
"Alignment": 0,
"Wrapping": 0,
"TextStyle": {
"Bold": false,
"Italic": false,
"Monospace": false,
"Symbol": false,
"TabWidth": 0
},
"Truncation": 0,
"Importance": 0
}
}
]
}
`)
if err != nil {
fyne.LogError("Failed to write main.gui.json", err) // we can just return the partial project
return dir, nil
}
err = writeFile(dir, "go.mod", `module `+name+`
require fyne.io/fyne/v2 v2.4.0
`)
if err != nil {
fyne.LogError("Failed to write go.mod", err) // we can just return the partial project
return dir, nil
}
cmd := exec.Command("go", "mod", "tidy")
cmd.Dir = dir.Path()
err = cmd.Start() // run in background - may take a little while but should not block file editing
if err != nil { // just print, can just continue to open project
fyne.LogError("Could not run go mod tidy", err)
}
return dir, nil
}
func defaultDir() fyne.ListableURI {
homeDir, err := os.UserHomeDir()
if err != nil {
fyne.LogError("Failed to get user home directory", err)
return nil
}
defaultDir := storage.NewFileURI(homeDir)
newDir, err := storage.ListerForURI(defaultDir)
if err != nil {
fyne.LogError("Failed to list home directory", err)
return nil
}
return newDir
}
func writeFile(dir fyne.URI, name, content string) error {
modURI, _ := storage.Child(dir, name)
w, err := storage.Writer(modURI)
if err != nil {
return err
}
_, err = io.WriteString(w, content)
if err != nil {
fyne.LogError("Failed to write go.mod", err)
return err
}
return w.Close()
}