-
Notifications
You must be signed in to change notification settings - Fork 0
/
menus.go
149 lines (134 loc) · 3.29 KB
/
menus.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
package ui
import (
"errors"
"image"
"image/png"
"os"
"strconv"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/widget"
"zerotomastery.io/pixl/util"
)
func saveFileDialog(app *AppInit) {
dialog.ShowFileSave(func(uri fyne.URIWriteCloser, e error) {
if uri == nil {
return
} else {
err := png.Encode(uri, app.PixlCanvas.PixelData)
if err != nil {
dialog.ShowError(err, app.PixlWindow)
return
}
app.State.SetFilePath(uri.URI().Path())
}
}, app.PixlWindow)
}
func BuildSaveAsMenu(app *AppInit) *fyne.MenuItem {
return fyne.NewMenuItem("Save As...", func() {
saveFileDialog(app)
})
}
func BuildSaveMenu(app *AppInit) *fyne.MenuItem {
return fyne.NewMenuItem("Save", func() {
if app.State.FilePath == "" {
saveFileDialog(app)
} else {
tryClose := func(fh *os.File) {
err := fh.Close()
if err != nil {
dialog.ShowError(err, app.PixlWindow)
}
}
fh, err := os.Create(app.State.FilePath)
defer tryClose(fh)
if err != nil {
dialog.ShowError(err, app.PixlWindow)
return
}
err = png.Encode(fh, app.PixlCanvas.PixelData)
if err != nil {
dialog.ShowError(err, app.PixlWindow)
return
}
}
})
}
func BuildNewMenu(app *AppInit) *fyne.MenuItem {
return fyne.NewMenuItem("New", func() {
sizeValidator := func(s string) error {
width, err := strconv.Atoi(s)
if err != nil {
return errors.New("must be a positive integer")
}
if width <= 0 {
return errors.New("must be > 0")
}
return nil
}
widthEntry := widget.NewEntry()
widthEntry.Validator = sizeValidator
heightEntry := widget.NewEntry()
heightEntry.Validator = sizeValidator
widthFormEntry := widget.NewFormItem("Width", widthEntry)
heightFormEntry := widget.NewFormItem("Height", heightEntry)
formItems := []*widget.FormItem{widthFormEntry, heightFormEntry}
dialog.ShowForm("New Image", "Create", "Cancel", formItems, func(ok bool) {
if ok {
pixelWidth := 0
pixelHeight := 0
if widthEntry.Validate() != nil {
dialog.ShowError(errors.New("invalid width"), app.PixlWindow)
} else {
pixelWidth, _ = strconv.Atoi(widthEntry.Text)
}
if heightEntry.Validate() != nil {
dialog.ShowError(errors.New("invalid height"), app.PixlWindow)
} else {
pixelHeight, _ = strconv.Atoi(heightEntry.Text)
}
app.PixlCanvas.NewDrawing(pixelWidth, pixelHeight)
}
}, app.PixlWindow)
})
}
func BuildOpenMenu(app *AppInit) *fyne.MenuItem {
return fyne.NewMenuItem("Open...", func() {
dialog.ShowFileOpen(func(uri fyne.URIReadCloser, e error) {
if uri == nil {
return
} else {
image, _, err := image.Decode(uri)
if err != nil {
dialog.ShowError(err, app.PixlWindow)
return
}
app.PixlCanvas.LoadImage(image)
app.State.SetFilePath(uri.URI().Path())
imgColors := util.GetImageColors(image)
i := 0
for c := range imgColors {
if i == len(app.Swatches) {
break
}
app.Swatches[i].SetColor(c)
i++
}
}
}, app.PixlWindow)
})
}
func BuildMenus(app *AppInit) *fyne.Menu {
return fyne.NewMenu(
"File",
BuildNewMenu(app),
BuildOpenMenu(app),
BuildSaveMenu(app),
BuildSaveAsMenu(app),
)
}
func SetupMenus(app *AppInit) {
menus := BuildMenus(app)
mainMenu := fyne.NewMainMenu(menus)
app.PixlWindow.SetMainMenu(mainMenu)
}