-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathsystray_real.go
160 lines (136 loc) · 3.64 KB
/
systray_real.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
// +build !cli
// Systray_real gets compiled when the tag 'cli' is missing. This is the default case
package systray
import (
"fmt"
"os"
"path/filepath"
"github.com/arduino/arduino-create-agent/icon"
"github.com/getlantern/systray"
"github.com/go-ini/ini"
"github.com/kardianos/osext"
"github.com/skratchdot/open-golang/open"
)
// Start sets up the systray icon with its menus
func (s *Systray) Start() {
if s.Hibernate {
systray.Run(s.startHibernate, s.end)
} else {
systray.Run(s.start, s.end)
}
}
// Quit simply exits the program
func (s *Systray) Quit() {
systray.Quit()
}
// start creates a systray icon with menu options to go to arduino create, open debug, pause/quit the agent
func (s *Systray) start() {
systray.SetIcon(icon.GetIcon())
// Add version
menuVer := systray.AddMenuItem("Agent version "+s.Version, "")
menuVer.Disable()
// Add links
mUrl := systray.AddMenuItem("Go to Arduino Create", "Arduino Create")
mDebug := systray.AddMenuItem("Open Debug Console", "Debug console")
// Add pause/quit
mPause := systray.AddMenuItem("Pause Plugin", "")
systray.AddSeparator()
mQuit := systray.AddMenuItem("Quit Plugin", "")
// Add configs
s.addConfigs()
// listen for events
go func() {
for {
select {
case <-mUrl.ClickedCh:
_ = open.Start("https://create.arduino.cc")
case <-mDebug.ClickedCh:
_ = open.Start(s.DebugURL())
case <-mPause.ClickedCh:
s.Pause()
case <-mQuit.ClickedCh:
s.Quit()
}
}
}()
}
// starthibernate creates a systray icon with menu options to resume/quit the agent
func (s *Systray) startHibernate() {
systray.SetIcon(icon.GetIconHiber())
mResume := systray.AddMenuItem("Resume Plugin", "")
systray.AddSeparator()
mQuit := systray.AddMenuItem("Quit Plugin", "")
// listen for events
go func() {
for {
select {
case <-mResume.ClickedCh:
s.Resume()
case <-mQuit.ClickedCh:
s.Quit()
}
}
}()
}
// end simply exits the program
func (s *Systray) end() {
os.Exit(0)
}
func (s *Systray) addConfigs() {
var mConfigCheckbox []*systray.MenuItem
configs := getConfigs()
if len(configs) > 1 {
for _, config := range configs {
entry := systray.AddMenuItem(config.Name, "")
mConfigCheckbox = append(mConfigCheckbox, entry)
// decorate configs
gliph := " ☐ "
if s.AdditionalConfig == config.Location {
gliph = " 🗹 "
}
entry.SetTitle(gliph + config.Name)
}
}
// It would be great to use the select channel here,
// but unfortunately there's no clean way to do it with an array of channels, so we start a single goroutine for each of them
for i := range mConfigCheckbox {
go func(v int) {
<-mConfigCheckbox[v].ClickedCh
s.AdditionalConfig = configs[v].Location
s.Restart()
}(i)
}
}
type configIni struct {
Name string
Location string
}
// getconfigs parses all config files in the executable folder
func getConfigs() []configIni {
// config.ini must be there, so call it Default
src, _ := osext.Executable()
dest := filepath.Dir(src)
var configs []configIni
err := filepath.Walk(dest, func(path string, f os.FileInfo, _ error) error {
if !f.IsDir() {
if filepath.Ext(path) == ".ini" {
cfg, err := ini.LoadSources(ini.LoadOptions{IgnoreInlineComment: true}, filepath.Join(dest, f.Name()))
if err != nil {
return err
}
defaultSection, err := cfg.GetSection("")
name := defaultSection.Key("name").String()
if name == "" || err != nil {
name = "Default config"
}
conf := configIni{Name: name, Location: f.Name()}
configs = append(configs, conf)
}
}
return nil
})
if err != nil {
fmt.Println("error walking through executable configuration: %w", err)
}
return configs
}