|
| 1 | +// +build !cli |
| 2 | + |
| 3 | +package systray |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + "github.com/arduino/arduino-create-agent/icon" |
| 11 | + "github.com/getlantern/systray" |
| 12 | + "github.com/go-ini/ini" |
| 13 | + "github.com/kardianos/osext" |
| 14 | + "github.com/skratchdot/open-golang/open" |
| 15 | +) |
| 16 | + |
| 17 | +// Start sets up the systray icon with its menus |
| 18 | +func (s *Systray) Start() { |
| 19 | + if s.Hibernate { |
| 20 | + systray.Run(s.startHibernate, s.end) |
| 21 | + } else { |
| 22 | + systray.Run(s.start, s.end) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// Quit simply exits the program |
| 27 | +func (s *Systray) Quit() { |
| 28 | + systray.Quit() |
| 29 | +} |
| 30 | + |
| 31 | +// start creates a systray icon with menu options to go to arduino create, open debug, pause/quit the agent |
| 32 | +func (s *Systray) start() { |
| 33 | + systray.SetIcon(icon.GetIcon()) |
| 34 | + |
| 35 | + // Add version |
| 36 | + menuVer := systray.AddMenuItem("Agent version "+s.Version, "") |
| 37 | + menuVer.Disable() |
| 38 | + |
| 39 | + // Add links |
| 40 | + mUrl := systray.AddMenuItem("Go to Arduino Create", "Arduino Create") |
| 41 | + mDebug := systray.AddMenuItem("Open Debug Console", "Debug console") |
| 42 | + |
| 43 | + // Add pause/quit |
| 44 | + mPause := systray.AddMenuItem("Pause Plugin", "") |
| 45 | + systray.AddSeparator() |
| 46 | + mQuit := systray.AddMenuItem("Quit Plugin", "") |
| 47 | + |
| 48 | + // Add configs |
| 49 | + s.addConfigs() |
| 50 | + |
| 51 | + // listen for events |
| 52 | + go func() { |
| 53 | + for { |
| 54 | + select { |
| 55 | + case <-mUrl.ClickedCh: |
| 56 | + _ = open.Start("https://create.arduino.cc") |
| 57 | + case <-mDebug.ClickedCh: |
| 58 | + _ = open.Start(s.DebugURL()) |
| 59 | + case <-mPause.ClickedCh: |
| 60 | + s.Pause() |
| 61 | + case <-mQuit.ClickedCh: |
| 62 | + s.Quit() |
| 63 | + } |
| 64 | + } |
| 65 | + }() |
| 66 | +} |
| 67 | + |
| 68 | +// starthibernate creates a systray icon with menu options to resume/quit the agent |
| 69 | +func (s *Systray) startHibernate() { |
| 70 | + systray.SetIcon(icon.GetIconHiber()) |
| 71 | + |
| 72 | + mResume := systray.AddMenuItem("Resume Plugin", "") |
| 73 | + systray.AddSeparator() |
| 74 | + mQuit := systray.AddMenuItem("Quit Plugin", "") |
| 75 | + |
| 76 | + // listen for events |
| 77 | + go func() { |
| 78 | + for { |
| 79 | + select { |
| 80 | + case <-mResume.ClickedCh: |
| 81 | + s.Resume() |
| 82 | + case <-mQuit.ClickedCh: |
| 83 | + s.Quit() |
| 84 | + } |
| 85 | + } |
| 86 | + }() |
| 87 | +} |
| 88 | + |
| 89 | +// end simply exits the program |
| 90 | +func (s *Systray) end() { |
| 91 | + os.Exit(0) |
| 92 | +} |
| 93 | + |
| 94 | +func (s *Systray) addConfigs() { |
| 95 | + var mConfigCheckbox []*systray.MenuItem |
| 96 | + |
| 97 | + configs := getConfigs() |
| 98 | + if len(configs) > 1 { |
| 99 | + for _, config := range configs { |
| 100 | + entry := systray.AddMenuItem(config.Name, "") |
| 101 | + mConfigCheckbox = append(mConfigCheckbox, entry) |
| 102 | + // decorate configs |
| 103 | + gliph := " β " |
| 104 | + if s.AdditionalConfig == config.Location { |
| 105 | + gliph = " πΉ " |
| 106 | + } |
| 107 | + entry.SetTitle(gliph + config.Name) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // It would be great to use the select channel here, |
| 112 | + // 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 |
| 113 | + for i := range mConfigCheckbox { |
| 114 | + go func(v int) { |
| 115 | + <-mConfigCheckbox[v].ClickedCh |
| 116 | + s.AdditionalConfig = configs[v].Location |
| 117 | + s.Restart() |
| 118 | + }(i) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +type configIni struct { |
| 123 | + Name string |
| 124 | + Location string |
| 125 | +} |
| 126 | + |
| 127 | +// getconfigs parses all config files in the executable folder |
| 128 | +func getConfigs() []configIni { |
| 129 | + // config.ini must be there, so call it Default |
| 130 | + src, _ := osext.Executable() |
| 131 | + dest := filepath.Dir(src) |
| 132 | + |
| 133 | + var configs []configIni |
| 134 | + |
| 135 | + err := filepath.Walk(dest, func(path string, f os.FileInfo, _ error) error { |
| 136 | + if !f.IsDir() { |
| 137 | + if filepath.Ext(path) == ".ini" { |
| 138 | + cfg, err := ini.LoadSources(ini.LoadOptions{IgnoreInlineComment: true}, filepath.Join(dest, f.Name())) |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + defaultSection, err := cfg.GetSection("") |
| 143 | + name := defaultSection.Key("name").String() |
| 144 | + if name == "" || err != nil { |
| 145 | + name = "Default config" |
| 146 | + } |
| 147 | + conf := configIni{Name: name, Location: f.Name()} |
| 148 | + configs = append(configs, conf) |
| 149 | + } |
| 150 | + } |
| 151 | + return nil |
| 152 | + }) |
| 153 | + |
| 154 | + if err != nil { |
| 155 | + fmt.Println("error walking through executable configuration: %w", err) |
| 156 | + } |
| 157 | + |
| 158 | + return configs |
| 159 | +} |
0 commit comments