-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtray.go
209 lines (193 loc) · 4.62 KB
/
tray.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
package main
import (
"bufio"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"os"
"os/signal"
"reflect"
"strings"
"syscall"
"github.com/getlantern/systray"
)
func main() {
// Should be called at the very beginning of main().
systray.Run(onReady, onExit)
}
func onExit() {
os.Exit(0)
}
// Item represents an item in the menu
type Item struct {
Title string `json:"title"`
Tooltip string `json:"tooltip"`
Enabled bool `json:"enabled"`
Checked bool `json:"checked"`
}
// Menu has an icon, title and list of items
type Menu struct {
Icon string `json:"icon"`
Title string `json:"title"`
Tooltip string `json:"tooltip"`
Items []Item `json:"items"`
}
// Action for an item?..
type Action struct {
Type string `json:"type"`
Item Item `json:"item"`
Menu Menu `json:"menu"`
SeqID int `json:"seq_id"`
}
func readLine(reader *bufio.Reader) io.Reader {
input, err := reader.ReadString('\n')
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
if len(input) < 1 {
return strings.NewReader("")
}
return strings.NewReader(input[0 : len(input)-1])
}
func readAction(reader *bufio.Reader) Action {
var action Action
if err := json.NewDecoder(readLine(reader)).Decode(&action); err != nil {
fmt.Fprintln(os.Stderr, err)
}
return action
}
func onReady() {
signalChannel := make(chan os.Signal, 2)
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)
go func() {
for sig := range signalChannel {
switch sig {
case os.Interrupt, syscall.SIGTERM:
//handle SIGINT, SIGTERM
fmt.Println("Quit")
systray.Quit()
default:
fmt.Println("Unhandled signal:", sig)
}
}
}()
// We can manipulate the systray in other goroutines
go func() {
items := make([]*systray.MenuItem, 0)
// fmt.Println(items)
fmt.Println(`{"type": "ready"}`)
reader := bufio.NewReader(os.Stdin)
// println(readLine(reader))
var menu Menu
err := json.NewDecoder(readLine(reader)).Decode(&menu)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
// fmt.Println("menu", menu)
icon, err := base64.StdEncoding.DecodeString(menu.Icon)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
systray.SetIcon(icon)
systray.SetTitle(menu.Title)
systray.SetTooltip(menu.Tooltip)
updateItem := func(action Action) {
item := action.Item
menuItem := items[action.SeqID]
menu.Items[action.SeqID] = item
if item.Checked {
menuItem.Check()
} else {
menuItem.Uncheck()
}
if item.Enabled {
menuItem.Enable()
} else {
menuItem.Disable()
}
menuItem.SetTitle(item.Title)
menuItem.SetTooltip(item.Tooltip)
// fmt.Println("Done")
// fmt.Printf("Read from channel %#v and received %s\n", items[chosen], value.String())
}
updateMenu := func(action Action) {
m := action.Menu
if menu.Title != m.Title {
menu.Title = m.Title
systray.SetTitle(menu.Title)
}
if menu.Icon != m.Icon {
menu.Icon = m.Icon
icon, err := base64.StdEncoding.DecodeString(menu.Icon)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
systray.SetIcon(icon)
}
if menu.Tooltip != m.Tooltip {
menu.Tooltip = m.Tooltip
systray.SetTooltip(menu.Tooltip)
}
}
update := func(action Action) {
switch action.Type {
case "update-item":
updateItem(action)
case "update-menu":
updateMenu(action)
case "update-item-and-menu":
updateItem(action)
updateMenu(action)
}
}
go func(reader *bufio.Reader) {
for {
action := readAction(reader)
update(action)
}
}(reader)
for i := 0; i < len(menu.Items); i++ {
item := menu.Items[i]
menuItem := systray.AddMenuItem(item.Title, item.Tooltip)
if item.Checked {
menuItem.Check()
} else {
menuItem.Uncheck()
}
if item.Enabled {
menuItem.Enable()
} else {
menuItem.Disable()
}
items = append(items, menuItem)
}
stdoutEnc := json.NewEncoder(os.Stdout)
// {"type": "update-item", "item": {"Title":"aa3","Tooltip":"bb","Enabled":true,"Checked":true}, "seqID": 0}
for {
cases := make([]reflect.SelectCase, len(items))
for i, ch := range items {
cases[i] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(ch.ClickedCh)}
}
remaining := len(cases)
for remaining > 0 {
chosen, _, ok := reflect.Select(cases)
if !ok {
// The chosen channel has been closed, so zero out the channel to disable the case
cases[chosen].Chan = reflect.ValueOf(nil)
remaining--
continue
}
// menuItem := items[chosen]
err := stdoutEnc.Encode(Action{
Type: "clicked",
Item: menu.Items[chosen],
SeqID: chosen,
})
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
}
}()
}