-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
162 lines (140 loc) Β· 2.78 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
// +build windows
package main
import (
"bytes"
_ "embed"
"image/png"
"log"
"os/exec"
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
)
type Config struct {
textEdit *walk.TextEdit
binPath string
binDir string
jsonPath string
pwd string
cmd *exec.Cmd
mainWin *walk.MainWindow
tray *walk.NotifyIcon
}
var (
tagName = "dev"
sha = "0000000"
)
//go:embed assets/icon.png
var appIcon []byte
func (config *Config) logToTextarea(text string) {
log.Println(text)
config.textEdit.AppendText(text + "\r\n")
}
func main() {
var config Config
mainW, err := walk.NewMainWindow()
if err != nil {
log.Fatal(err)
}
img, err := png.Decode(bytes.NewReader(appIcon))
if err != nil {
log.Fatal(err)
}
icon, err := walk.NewIconFromImageForDPI(img, 96)
if err != nil {
log.Fatal(err)
}
mainWConfig := MainWindow{
AssignTo: &mainW,
Title: " Kcptun Walk ",
Size: Size{300, 500},
Layout: VBox{},
Icon: icon,
Children: []Widget{
TextLabel{
Text: "Version " + tagName + "(" + sha + ")",
},
PushButton{
Text: "Run",
OnClicked: func() {
if config.cmd != nil {
config.logToTextarea("current pid is " + string(rune(config.cmd.Process.Pid)))
config.logToTextarea("[kcptun] already running.")
return
}
go func() {
startCmd(&config)
}()
},
},
PushButton{
Text: "Stop",
OnClicked: func() {
go func() {
killCmd(&config)
}()
},
},
TextEdit{
AssignTo: &config.textEdit,
ReadOnly: true,
VScroll: true,
},
},
}
tray, err := walk.NewNotifyIcon(mainW)
if err != nil {
log.Fatal(err)
}
defer tray.Dispose()
if err := tray.SetIcon(icon); err != nil {
log.Fatal(err)
}
if err := tray.SetToolTip("App started"); err != nil {
log.Fatal(err)
}
// When the left mouse button is pressed, bring up our balloon.
tray.MouseUp().Attach(func(x, y int, button walk.MouseButton) {
if button != walk.LeftButton {
return
}
toggleVisible(mainW)
})
// toggle visible
addTrayAction(tray, "T&oggle visible", func() {
toggleVisible(mainW)
})
// exit
addTrayAction(tray, "E&xit", func() {
walk.App().Exit(0)
})
if err := tray.SetVisible(true); err != nil {
log.Fatal(err)
}
config.tray = tray
config.mainWin = mainW
if _, err := mainWConfig.Run(); err != nil {
log.Fatal(err)
}
}
func toggleVisible(mainWin *walk.MainWindow) {
if mainWin.Visible() {
mainWin.Hide()
} else {
mainWin.Show()
}
}
func addTrayAction(tray *walk.NotifyIcon, text string, cb func()) {
if tray == nil {
return
}
action := walk.NewAction()
if err := action.SetText(text); err != nil {
log.Fatal(err)
}
action.Triggered().Attach(func() {
cb()
})
if err := tray.ContextMenu().Actions().Add(action); err != nil {
log.Fatal(err)
}
}