This repository has been archived by the owner on Jan 14, 2024. It is now read-only.
forked from fyne-io/fyne
/
main.go
263 lines (238 loc) · 7.5 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
// Package main provides various examples of Fyne API capabilities.
package main
import (
"fmt"
"log"
"net/url"
"github.com/Anwert/fyne/v2"
"github.com/Anwert/fyne/v2/app"
"github.com/Anwert/fyne/v2/cmd/fyne_demo/data"
"github.com/Anwert/fyne/v2/cmd/fyne_demo/tutorials"
"github.com/Anwert/fyne/v2/cmd/fyne_settings/settings"
"github.com/Anwert/fyne/v2/container"
"github.com/Anwert/fyne/v2/driver/desktop"
"github.com/Anwert/fyne/v2/theme"
"github.com/Anwert/fyne/v2/widget"
)
const preferenceCurrentTutorial = "currentTutorial"
var topWindow fyne.Window
func main() {
a := app.NewWithID("io.fyne.demo")
a.SetIcon(data.FyneLogo)
makeTray(a)
logLifecycle(a)
w := a.NewWindow("Fyne Demo")
topWindow = w
w.SetMainMenu(makeMenu(a, w))
w.SetMaster()
content := container.NewStack()
title := widget.NewLabel("Component name")
intro := widget.NewLabel("An introduction would probably go\nhere, as well as a")
intro.Wrapping = fyne.TextWrapWord
setTutorial := func(t tutorials.Tutorial) {
if fyne.CurrentDevice().IsMobile() {
child := a.NewWindow(t.Title)
topWindow = child
child.SetContent(t.View(topWindow))
child.Show()
child.SetOnClosed(func() {
topWindow = w
})
return
}
title.SetText(t.Title)
intro.SetText(t.Intro)
content.Objects = []fyne.CanvasObject{t.View(w)}
content.Refresh()
}
tutorial := container.NewBorder(
container.NewVBox(title, widget.NewSeparator(), intro), nil, nil, nil, content)
if fyne.CurrentDevice().IsMobile() {
w.SetContent(makeNav(setTutorial, false))
} else {
split := container.NewHSplit(makeNav(setTutorial, true), tutorial)
split.Offset = 0.2
w.SetContent(split)
}
w.Resize(fyne.NewSize(640, 460))
w.ShowAndRun()
}
func logLifecycle(a fyne.App) {
a.Lifecycle().SetOnStarted(func() {
log.Println("Lifecycle: Started")
})
a.Lifecycle().SetOnStopped(func() {
log.Println("Lifecycle: Stopped")
})
a.Lifecycle().SetOnEnteredForeground(func() {
log.Println("Lifecycle: Entered Foreground")
})
a.Lifecycle().SetOnExitedForeground(func() {
log.Println("Lifecycle: Exited Foreground")
})
}
func makeMenu(a fyne.App, w fyne.Window) *fyne.MainMenu {
newItem := fyne.NewMenuItem("New", nil)
checkedItem := fyne.NewMenuItem("Checked", nil)
checkedItem.Checked = true
disabledItem := fyne.NewMenuItem("Disabled", nil)
disabledItem.Disabled = true
otherItem := fyne.NewMenuItem("Other", nil)
mailItem := fyne.NewMenuItem("Mail", func() { fmt.Println("Menu New->Other->Mail") })
mailItem.Icon = theme.MailComposeIcon()
otherItem.ChildMenu = fyne.NewMenu("",
fyne.NewMenuItem("Project", func() { fmt.Println("Menu New->Other->Project") }),
mailItem,
)
fileItem := fyne.NewMenuItem("File", func() { fmt.Println("Menu New->File") })
fileItem.Icon = theme.FileIcon()
dirItem := fyne.NewMenuItem("Directory", func() { fmt.Println("Menu New->Directory") })
dirItem.Icon = theme.FolderIcon()
newItem.ChildMenu = fyne.NewMenu("",
fileItem,
dirItem,
otherItem,
)
openSettings := func() {
w := a.NewWindow("Fyne Settings")
w.SetContent(settings.NewSettings().LoadAppearanceScreen(w))
w.Resize(fyne.NewSize(440, 520))
w.Show()
}
settingsItem := fyne.NewMenuItem("Settings", openSettings)
settingsShortcut := &desktop.CustomShortcut{KeyName: fyne.KeyComma, Modifier: fyne.KeyModifierShortcutDefault}
settingsItem.Shortcut = settingsShortcut
w.Canvas().AddShortcut(settingsShortcut, func(shortcut fyne.Shortcut) {
openSettings()
})
cutShortcut := &fyne.ShortcutCut{Clipboard: w.Clipboard()}
cutItem := fyne.NewMenuItem("Cut", func() {
shortcutFocused(cutShortcut, w)
})
cutItem.Shortcut = cutShortcut
copyShortcut := &fyne.ShortcutCopy{Clipboard: w.Clipboard()}
copyItem := fyne.NewMenuItem("Copy", func() {
shortcutFocused(copyShortcut, w)
})
copyItem.Shortcut = copyShortcut
pasteShortcut := &fyne.ShortcutPaste{Clipboard: w.Clipboard()}
pasteItem := fyne.NewMenuItem("Paste", func() {
shortcutFocused(pasteShortcut, w)
})
pasteItem.Shortcut = pasteShortcut
performFind := func() { fmt.Println("Menu Find") }
findItem := fyne.NewMenuItem("Find", performFind)
findItem.Shortcut = &desktop.CustomShortcut{KeyName: fyne.KeyF, Modifier: fyne.KeyModifierShortcutDefault | fyne.KeyModifierAlt | fyne.KeyModifierShift | fyne.KeyModifierControl | fyne.KeyModifierSuper}
w.Canvas().AddShortcut(findItem.Shortcut, func(shortcut fyne.Shortcut) {
performFind()
})
helpMenu := fyne.NewMenu("Help",
fyne.NewMenuItem("Documentation", func() {
u, _ := url.Parse("https://developer.fyne.io")
_ = a.OpenURL(u)
}),
fyne.NewMenuItem("Support", func() {
u, _ := url.Parse("https://fyne.io/support/")
_ = a.OpenURL(u)
}),
fyne.NewMenuItemSeparator(),
fyne.NewMenuItem("Sponsor", func() {
u, _ := url.Parse("https://fyne.io/sponsor/")
_ = a.OpenURL(u)
}))
// a quit item will be appended to our first (File) menu
file := fyne.NewMenu("File", newItem, checkedItem, disabledItem)
device := fyne.CurrentDevice()
if !device.IsMobile() && !device.IsBrowser() {
file.Items = append(file.Items, fyne.NewMenuItemSeparator(), settingsItem)
}
main := fyne.NewMainMenu(
file,
fyne.NewMenu("Edit", cutItem, copyItem, pasteItem, fyne.NewMenuItemSeparator(), findItem),
helpMenu,
)
checkedItem.Action = func() {
checkedItem.Checked = !checkedItem.Checked
main.Refresh()
}
return main
}
func makeTray(a fyne.App) {
if desk, ok := a.(desktop.App); ok {
h := fyne.NewMenuItem("Hello", func() {})
h.Icon = theme.HomeIcon()
menu := fyne.NewMenu("Hello World", h)
h.Action = func() {
log.Println("System tray menu tapped")
h.Label = "Welcome"
menu.Refresh()
}
desk.SetSystemTrayMenu(menu)
}
}
func unsupportedTutorial(t tutorials.Tutorial) bool {
return !t.SupportWeb && fyne.CurrentDevice().IsBrowser()
}
func makeNav(setTutorial func(tutorial tutorials.Tutorial), loadPrevious bool) fyne.CanvasObject {
a := fyne.CurrentApp()
tree := &widget.Tree{
ChildUIDs: func(uid string) []string {
return tutorials.TutorialIndex[uid]
},
IsBranch: func(uid string) bool {
children, ok := tutorials.TutorialIndex[uid]
return ok && len(children) > 0
},
CreateNode: func(branch bool) fyne.CanvasObject {
return widget.NewLabel("Collection Widgets")
},
UpdateNode: func(uid string, branch bool, obj fyne.CanvasObject) {
t, ok := tutorials.Tutorials[uid]
if !ok {
fyne.LogError("Missing tutorial panel: "+uid, nil)
return
}
obj.(*widget.Label).SetText(t.Title)
if unsupportedTutorial(t) {
obj.(*widget.Label).TextStyle = fyne.TextStyle{Italic: true}
} else {
obj.(*widget.Label).TextStyle = fyne.TextStyle{}
}
},
OnSelected: func(uid string) {
if t, ok := tutorials.Tutorials[uid]; ok {
if unsupportedTutorial(t) {
return
}
a.Preferences().SetString(preferenceCurrentTutorial, uid)
setTutorial(t)
}
},
}
if loadPrevious {
currentPref := a.Preferences().StringWithFallback(preferenceCurrentTutorial, "welcome")
tree.Select(currentPref)
}
themes := container.NewGridWithColumns(2,
widget.NewButton("Dark", func() {
a.Settings().SetTheme(theme.DarkTheme())
}),
widget.NewButton("Light", func() {
a.Settings().SetTheme(theme.LightTheme())
}),
)
return container.NewBorder(nil, themes, nil, nil, tree)
}
func shortcutFocused(s fyne.Shortcut, w fyne.Window) {
switch sh := s.(type) {
case *fyne.ShortcutCopy:
sh.Clipboard = w.Clipboard()
case *fyne.ShortcutCut:
sh.Clipboard = w.Clipboard()
case *fyne.ShortcutPaste:
sh.Clipboard = w.Clipboard()
}
if focused, ok := w.Canvas().Focused().(fyne.Shortcutable); ok {
focused.TypedShortcut(s)
}
}