-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialogs.go
172 lines (139 loc) · 4.88 KB
/
dialogs.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
package main
/* This file contains helper functions to show different types and combinations of dialogs */
/* ================================================================================ Imports */
import (
"image/color"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/widget"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/theme"
)
/* ================================================================================ Public functions */
func ShowConfirmDialog(title, text string, confirmedCallback func()) {
dialog.ShowConfirm(title, text,
func(confirmed bool) {
if confirmed && confirmedCallback != nil {
confirmedCallback()
}
}, window,
)
}
func ShowEntryDialog(title, placeholder, text string, confirmedCallback func(text string)) {
entry := widget.NewEntry()
entry.SetPlaceHolder(placeholder)
entry.SetText(text)
dialogContainer := container.NewVBox(entry, canvas.NewText("", color.Black))
dialog.ShowCustomConfirm(title, "OK", "Cancel", dialogContainer,
func(confirmed bool) {
if confirmed && confirmedCallback != nil {
confirmedCallback(entry.Text)
}
}, window,
)
window.Canvas().Focus(entry)
}
func ShowColorPickerDialog(title, message string, preselected color.RGBA, confirmedCallback func(selected color.RGBA)) {
colorPickerDialog := dialog.NewColorPicker(title, message,
func(c color.Color) {
if confirmedCallback != nil {
confirmedCallback(ColorToRGBA(c))
}
}, window,
)
colorPickerDialog.Advanced = true
colorPickerDialog.Show()
colorPickerDialog.SetColor(preselected)
}
func ShowItemDialog(dialogPrefix, title, tagEditString, description string, style ItemStyle, confirmedCallback func(title, tagEditString, description string, style ItemStyle)) {
titleEntry := widget.NewEntry()
titleEntry.SetPlaceHolder("Title ...")
titleEntry.SetText(title)
tagsEntry := widget.NewEntry()
tagsEntry.SetPlaceHolder("Tag1=Value1; Tag2=Value2; ...")
tagsEntry.SetText(tagEditString)
descriptionEntry := widget.NewMultiLineEntry()
descriptionEntry.SetPlaceHolder("Description ...")
descriptionEntry.SetText(description)
foregroundColor := style.Foreground
foregroundColorButton := widget.NewButtonWithIcon("Foregound", theme.ColorPaletteIcon(),
func() {
ShowColorPickerDialog("Choose Foreground Color", "Please choose the color for item text and tag frames.", foregroundColor,
func(selected color.RGBA) {
foregroundColor = selected
},
)
},
)
backgroundColor := style.Background
backgroundColorButton := widget.NewButtonWithIcon("Background", theme.ColorPaletteIcon(),
func() {
ShowColorPickerDialog("Choose Background Color", "Please choose the color for the item's background.", backgroundColor,
func(selected color.RGBA) {
backgroundColor = selected
},
)
},
)
buttonContainer := container.NewGridWithColumns(2, foregroundColorButton, backgroundColorButton)
dialogContainer := container.NewVBox(titleEntry, tagsEntry, descriptionEntry, buttonContainer, canvas.NewText("", color.Black))
dialog.ShowCustomConfirm(dialogPrefix + " Item", "OK", "Cancel", dialogContainer,
func(confirmed bool) {
if confirmed && confirmedCallback != nil {
confirmedCallback(titleEntry.Text, tagsEntry.Text, descriptionEntry.Text, ItemStyle{ foregroundColor, backgroundColor })
}
}, window,
)
window.Canvas().Focus(titleEntry)
}
func ShowFileOpenConfirmDialog(title, text string, defaultFileURI fyne.URI, confirmedCallback func(reader fyne.URIReadCloser)) {
fileDialog := dialog.NewFileOpen(
func(reader fyne.URIReadCloser, err error) {
if reader != nil && err == nil {
ShowConfirmDialog(title, text,
func() {
if confirmedCallback != nil {
confirmedCallback(reader)
}
},
)
}
}, window,
)
if defaultFileURI != nil {
fileDialog.SetLocation(getParentListableURI(defaultFileURI))
}
fileDialog.SetFilter(storage.NewExtensionFileFilter([]string{ ".json" }))
fileDialog.Show()
}
func ShowSaveAsDialog(defaultFileURI fyne.URI, confirmedCallback func(writer fyne.URIWriteCloser)) {
fileDialog := dialog.NewFileSave(
func(writer fyne.URIWriteCloser, err error) {
if writer != nil && err == nil && confirmedCallback != nil {
confirmedCallback(writer)
}
}, window,
)
if defaultFileURI != nil {
fileDialog.SetFileName(defaultFileURI.Name())
fileDialog.SetLocation(getParentListableURI(defaultFileURI))
} else {
fileDialog.SetFileName("bankan_board.json")
}
fileDialog.SetFilter(storage.NewExtensionFileFilter([]string{ ".json" }))
fileDialog.Show()
}
/* ================================================================================ Private functions */
func getParentListableURI(file fyne.URI) fyne.ListableURI {
dirURI, err := storage.Parent(file)
if err != nil {
return nil
}
dirListableURI, err := storage.ListerForURI(dirURI)
if err != nil {
return nil
}
return dirListableURI
}