-
Notifications
You must be signed in to change notification settings - Fork 0
/
menubar.go
181 lines (150 loc) · 4.74 KB
/
menubar.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
package menubar
import (
"fmt"
"net/url"
"os"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/cmd/fyne_settings/settings"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/theme"
)
func fileMenu(a fyne.App, w fyne.Window) *fyne.Menu {
mailItem := fyne.NewMenuItem("Mail", func() { fmt.Println("Menu New->Other->Mail") })
mailItem.Icon = theme.MailComposeIcon()
// *********************************
// Prepare the New item and its submenus
fileItem := fyne.NewMenuItem("File", func() {
fmt.Println("Menu New->File")
dialog.ShowFileOpen(func(uri fyne.URIReadCloser, err error) {
fmt.Println("URI:", uri)
}, w)
})
fileItem.Icon = theme.FileIcon()
dirItem := fyne.NewMenuItem("Directory", func() { fmt.Println("Menu New->Directory") })
dirItem.Icon = theme.FolderIcon()
otherItem := fyne.NewMenuItem("Other", nil)
otherItem.ChildMenu = fyne.NewMenu("",
fyne.NewMenuItem("Project", func() { fmt.Println("Menu New->Other->Project") }),
mailItem,
)
newItem := fyne.NewMenuItem("New", nil)
newItem.ChildMenu = fyne.NewMenu("",
fileItem,
dirItem,
otherItem,
)
// end of New item and submenus
// **************************************
fd := dialog.NewFileOpen(func(uri fyne.URIReadCloser, err error) {
fmt.Println("URI:", uri.URI())
}, w)
wd, err := os.Getwd()
if err != nil {
fyne.LogError("Could not get current dir", err)
}
lister, err := storage.ListerForURI(storage.NewFileURI(wd))
if err != nil {
fyne.LogError("Could not create lister for working dir", err)
}
fd.SetLocation(lister)
fileOpenItem := fyne.NewMenuItem("Open...", func() {
fmt.Println("Menu New->File", wd)
fd.Show()
})
// ***************************************
// The top-level File menu item and sub-items
// a quit item will be appended to our first (File) menu
file := fyne.NewMenu("File", fileOpenItem, newItem)
// If it is not mobile, add a Settings menu item
if device := fyne.CurrentDevice(); !device.IsMobile() && !device.IsBrowser() {
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()
})
file.Items = append(file.Items, fyne.NewMenuItemSeparator(), settingsItem)
}
return file
}
func editMenu(a fyne.App, w fyne.Window) *fyne.Menu {
// Cut
cutShortcut := &fyne.ShortcutCut{Clipboard: w.Clipboard()}
cutItem := fyne.NewMenuItem("Cut", func() {
shortcutFocused(cutShortcut, w)
})
cutItem.Shortcut = cutShortcut
// Copy
copyShortcut := &fyne.ShortcutCopy{Clipboard: w.Clipboard()}
copyItem := fyne.NewMenuItem("Copy", func() {
shortcutFocused(copyShortcut, w)
})
copyItem.Shortcut = copyShortcut
// Paste
pasteShortcut := &fyne.ShortcutPaste{Clipboard: w.Clipboard()}
pasteItem := fyne.NewMenuItem("Paste", func() {
shortcutFocused(pasteShortcut, w)
})
pasteItem.Shortcut = pasteShortcut
// Find
performFind := func() { fmt.Println("Menu Find") }
findItem := fyne.NewMenuItem("Find", performFind)
findItem.Shortcut = &desktop.CustomShortcut{KeyName: fyne.KeyF, Modifier: fyne.KeyModifierShortcutDefault | fyne.KeyModifierControl}
w.Canvas().AddShortcut(findItem.Shortcut, func(shortcut fyne.Shortcut) {
performFind()
})
// ***************************************
// The top-level File menu item and sub-items
// a quit item will be appended to our first (File) menu
edit := fyne.NewMenu("Edit",
cutItem,
copyItem,
pasteItem,
fyne.NewMenuItemSeparator(),
findItem)
return edit
}
func helpMenu(a fyne.App, w fyne.Window) *fyne.Menu {
helpMenu := fyne.NewMenu("Help",
fyne.NewMenuItem("Documentation", func() {
u, _ := url.Parse("https://dome-marketplace.eu")
_ = a.OpenURL(u)
}),
fyne.NewMenuItem("Support", func() {
u, _ := url.Parse("https://dome-marketplace.eu")
_ = a.OpenURL(u)
}),
)
return helpMenu
}
// makeMenu creates the main menu bar of the application
func MakeMenu(a fyne.App, w fyne.Window) *fyne.MainMenu {
// The mainMenuBar menu bar of the application
mainMenuBar := fyne.NewMainMenu(
fileMenu(a, w),
editMenu(a, w),
helpMenu(a, w),
)
return mainMenuBar
}
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)
}
}