This repository has been archived by the owner on Apr 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tray_windows.go
248 lines (214 loc) · 6.18 KB
/
tray_windows.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
//go:generate windres -i PostgreSQLPortable.rc -O coff -o resource.syso
package main
import (
"fmt"
"log"
"os"
"github.com/lxn/walk"
)
var (
mw *walk.MainWindow
ni *walk.NotifyIcon
statusPgServerAction *walk.Action
startPgServerAction *walk.Action
stopPgServerAction *walk.Action
startPgShellAction *walk.Action
showSettingsAction *walk.Action
checkForUpdatesAction *walk.Action
err error
imgStatus = "resources/windows/postgresql-16.ico"
imgStart = "resources/windows/play-16.ico"
imgStop = "resources/windows/stop-16.ico"
imgConsole = "resources/windows/console-16.ico"
imgSettings = "resources/windows/settings-16.ico"
imgSync = "resources/windows/sync-16.ico"
imgExit = "resources/windows/exit-16.ico"
)
func ShowNotification(msg string) {
if ni != nil {
if err := ni.ShowCustom(strTitle, msg); err != nil {
log.Fatal(err)
}
}
}
func SetNotificationToolTip(msg string) {
if err := ni.SetToolTip(fmt.Sprintf("%s\n%s", strTitle, msg)); err != nil {
log.Fatal(err)
}
}
func CreateTray() {
mw, err = walk.NewMainWindow()
if err != nil {
log.Fatal(err)
}
mw.Hide()
icon, err := walk.NewIconFromResourceId(101)
if err != nil {
log.Fatal(err)
}
ni, err = walk.NewNotifyIcon()
if err != nil {
log.Fatal(err)
}
defer ni.Dispose()
if err := ni.SetIcon(icon); err != nil {
log.Fatal(err)
}
SetNotificationToolTip("")
ni.MouseDown().Attach(func(x, y int, button walk.MouseButton) {
if button != walk.LeftButton {
return
}
ShowNotification(strHelp)
})
// Start status item {{{
statusPgServerAction = walk.NewAction()
if err := statusPgServerAction.SetText(strStopped); err != nil {
log.Fatal(err)
}
statusPgServerImg, stsImgErr := walk.NewBitmapFromFile(imgStatus)
if stsImgErr != nil {
log.Printf("status resource - %s\n", stsImgErr.Error())
}
if err := statusPgServerAction.SetImage(statusPgServerImg); err != nil {
log.Printf("statusPgServerAction - %v\n", err.Error())
}
statusPgServerAction.SetEnabled(false)
if err := ni.ContextMenu().Actions().Add(statusPgServerAction); err != nil {
log.Fatal(err)
}
// }}} End status item
// Start separator item {{{
if err := ni.ContextMenu().Actions().Add(walk.NewSeparatorAction()); err != nil {
log.Fatal(err)
}
// }}} Start separator item
// Start start item {{{
startPgServerAction = walk.NewAction()
if err := startPgServerAction.SetText(strStart); err != nil {
log.Fatal(err)
}
startPgServerImg, _ := walk.NewBitmapFromFile(imgStart)
if err := startPgServerAction.SetImage(startPgServerImg); err != nil {
log.Fatal(err)
}
startPgServerAction.SetEnabled(!serverStatus && len(conf.UsedVersion) > 0)
startPgServerAction.Triggered().Attach(func() { go startPg() })
if err := ni.ContextMenu().Actions().Add(startPgServerAction); err != nil {
log.Fatal(err)
}
// }}} End start item
// Start stop item {{{
stopPgServerAction = walk.NewAction()
if err := stopPgServerAction.SetText(strStop); err != nil {
log.Fatal(err)
}
stopImg, _ := walk.NewBitmapFromFile(imgStop)
if err := stopPgServerAction.SetImage(stopImg); err != nil {
log.Fatal(err)
}
stopPgServerAction.SetEnabled(serverStatus)
stopPgServerAction.Triggered().Attach(func() { go stopPg() })
if err := ni.ContextMenu().Actions().Add(stopPgServerAction); err != nil {
log.Fatal(err)
}
// }}} End stop item
if err := ni.ContextMenu().Actions().Add(walk.NewSeparatorAction()); err != nil {
log.Fatal(err)
}
// Start shell item {{{
startPgShellAction = walk.NewAction()
if err := startPgShellAction.SetText(strStartShell); err != nil {
log.Fatal(err)
}
startPgShellImg, _ := walk.NewBitmapFromFile(imgConsole)
if err := startPgShellAction.SetImage(startPgShellImg); err != nil {
log.Fatal(err)
}
startPgShellAction.SetEnabled(false)
startPgShellAction.Triggered().Attach(func() { go startShell() })
if err := ni.ContextMenu().Actions().Add(startPgShellAction); err != nil {
log.Fatal(err)
}
// }}} End shell item
if err := ni.ContextMenu().Actions().Add(walk.NewSeparatorAction()); err != nil {
log.Fatal(err)
}
// Start settings item {{{
showSettingsAction = walk.NewAction()
if err := showSettingsAction.SetText(strSettings); err != nil {
log.Fatal(err)
}
settingsImg, _ := walk.NewBitmapFromFile(imgSettings)
if err := showSettingsAction.SetImage(settingsImg); err != nil {
log.Fatal(err)
}
showSettingsAction.Triggered().Attach(func() {
ShowSettingsDialog()
})
if err := ni.ContextMenu().Actions().Add(showSettingsAction); err != nil {
log.Fatal(err)
}
// }}} End settings item
// Start checkForUpdates item {{{
checkForUpdatesAction = walk.NewAction()
if err := checkForUpdatesAction.SetText(strCFU); err != nil {
log.Fatal(err)
}
cfuImg, _ := walk.NewBitmapFromFile(imgSync)
if err := checkForUpdatesAction.SetImage(cfuImg); err != nil {
log.Fatal(err)
}
checkForUpdatesAction.Triggered().Attach(func() {
checkNewestVersion()
})
if err := ni.ContextMenu().Actions().Add(checkForUpdatesAction); err != nil {
log.Fatal(err)
}
// }}} End checkForUpdates item
// Start exit item {{{
exitAction := walk.NewAction()
if err := exitAction.SetText(strExit); err != nil {
log.Fatal(err)
}
exitImg, _ := walk.NewBitmapFromFile(imgExit)
if err := exitAction.SetImage(exitImg); err != nil {
log.Fatal(err)
}
exitAction.Triggered().Attach(func() { quit() })
if err := ni.ContextMenu().Actions().Add(exitAction); err != nil {
log.Fatal(err)
}
// }}} End exit item
if err := ni.SetVisible(true); err != nil {
log.Fatal(err)
}
if conf.CheckForUpdates {
log.Println("Check for updates enabled")
checkNewestVersion()
}
mw.Run()
}
func SetStatus(msg string) {
statusPgServerAction.SetText(msg)
}
func EnableStart(enable bool) {
startPgServerAction.SetEnabled(enable)
}
func EnableStop(enable bool) {
stopPgServerAction.SetEnabled(enable)
}
func EnableShell(enable bool) {
startPgShellAction.SetEnabled(enable)
}
func ShowMessage(msg string) {
walk.MsgBox(nil, strTitle, msg, walk.MsgBoxIconInformation)
}
func AskQuestion(msg string) int {
return walk.MsgBox(nil, strTitle, msg, walk.MsgBoxIconQuestion+walk.MsgBoxOKCancel+walk.MsgBoxDefButton1)
}
func AppQuit() {
ni.SetVisible(false)
walk.App().Exit(0)
os.Exit(0)
}