-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
120 lines (89 loc) · 2.66 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
package main
import (
"os"
"path/filepath"
"flag"
"github.com/getlantern/systray"
"image"
"image/draw"
"image/color"
ico "git.shangtai.net/staffan/go-ico"
)
func defaultPath() string {
homedir, err := os.UserHomeDir()
// default to SOMETHING
if err != nil {
homedir = "/"
}
// FromSlash because filepath somehow doesn't handle windows directory
// separators directly.
path := filepath.FromSlash(filepath.Join(homedir, ".docker", "config.json"))
return path
}
func main() {
var fn = flag.String("path", defaultPath(), "Path to the docker context config file")
var custom customColors
flag.Var(&custom, "color", "Custom color configuration, values should be in the form color=#rrggbb, can be repeated")
flag.Parse()
state := NewState(*fn)
state.palette.Set("default", color.RGBA{0, 255, 0, 255})
for _, entry := range custom {
state.palette.Set(entry.name, entry.color)
}
systray.Run(state.onSystrayReady, nil)
}
type state struct {
Filename string
palette *Palette
context *Context
}
func NewState(fn string) *state {
state := new(state)
state.Filename = fn
state.palette = new(Palette)
return state
}
func (self *state) onSystrayReady() {
// an icon must exist before a menu item is added, so lets just use the default
systray.SetIcon(self.generateIconFromContextString("default"))
// display the current context as a disabled entry in the menu,
// default to "default" here, it will be immediately overwritten by the
// loop below, but there has to be something.
current := systray.AddMenuItem("default", "Current context")
current.Disable()
// launch a goroutine to handle clicks
go menuClickHandler()
// file watcher goroutine, feeds the current context into its channel
self.context = NewContext()
go self.context.watch(self.Filename)
// wait for a context from the channel and set it in the tray
for {
var context = <-self.context.Channel
systray.SetIcon(self.generateIconFromContextString(context))
current.SetTitle(context)
systray.SetTitle(context)
systray.SetTooltip(context)
}
}
func menuClickHandler() {
quit := systray.AddMenuItem("Quit", "Stop monitoring context")
for {
select {
case <-quit.ClickedCh:
systray.Quit()
return
}
}
}
func (self *state) generateIconFromContextString(context string) []byte {
var color = self.palette.GetColor(context)
icon := ico.NewIcon()
img := image.NewRGBA(image.Rect(0,0,64,64))
draw.Draw(img, img.Bounds(), &image.Uniform{color}, image.ZP, draw.Src)
icon.AddPng(img)
ico, err := icon.Encode()
if err != nil {
return nil
}
return ico
}