-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
129 lines (114 loc) · 3.45 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
package main
import (
"fmt"
"io/ioutil"
"time"
"cazzoo.me/godrive/process"
"github.com/getlantern/systray"
"github.com/getlantern/systray/example/icon"
"github.com/skratchdot/open-golang/open"
)
var odriveagentPid int
func main() {
onExit := func() {
now := time.Now()
ioutil.WriteFile(fmt.Sprintf(`on_exit_%d.txt`, now.UnixNano()), []byte(now.String()), 0644)
}
if pid, s, err := process.FindProcess("odriveagent"); err == nil {
fmt.Printf("Pid:%d, Pname:%s\n", pid, s)
odriveagentPid = pid
systray.Run(onReady, onExit)
} else {
fmt.Println("No process found for [odriveagent]. Please ensure it is started.")
}
}
func onReady() {
systray.SetTemplateIcon(icon.Data, icon.Data)
systray.SetTitle("Awesome App")
systray.SetTooltip("Lantern")
mQuitOrig := systray.AddMenuItem("Quit", "Quit the whole app")
go func() {
<-mQuitOrig.ClickedCh
fmt.Println("Requesting quit")
process.TerminateProcess(odriveagentPid)
systray.Quit()
fmt.Println("Finished quitting")
}()
// We can manipulate the systray in other goroutines
go func() {
if odriveagentPid == 0 {
startOdrive := systray.AddMenuItem("Start odrive agent", "Start odrive agent")
go func() {
<-<-startOdrive.ClickedCh
fmt.Println("Requesting quit")
process.TerminateProcess(odriveagentPid)
systray.Quit()
fmt.Println("Finished quitting")
}()
}
systray.SetTemplateIcon(icon.Data, icon.Data)
systray.SetTitle("Awesome App")
systray.SetTooltip("Pretty awesome")
mChange := systray.AddMenuItem("Change Me", "Change Me")
mChecked := systray.AddMenuItemCheckbox("Unchecked", "Check Me", true)
mEnabled := systray.AddMenuItem("Enabled", "Enabled")
// Sets the icon of a menu item. Only available on Mac.
mEnabled.SetTemplateIcon(icon.Data, icon.Data)
systray.AddMenuItem("Ignored", "Ignored")
subMenuTop := systray.AddMenuItem("SubMenuTop", "SubMenu Test (top)")
subMenuMiddle := subMenuTop.AddSubMenuItem("SubMenuMiddle", "SubMenu Test (middle)")
subMenuBottom := subMenuMiddle.AddSubMenuItemCheckbox("SubMenuBottom - Toggle Panic!", "SubMenu Test (bottom) - Hide/Show Panic!", false)
subMenuBottom2 := subMenuMiddle.AddSubMenuItem("SubMenuBottom - Panic!", "SubMenu Test (bottom)")
mUrl := systray.AddMenuItem("Open UI", "my home")
mQuit := systray.AddMenuItem("Quit", "Quit the whole app")
// Sets the icon of a menu item. Only available on Mac.
mQuit.SetIcon(icon.Data)
systray.AddSeparator()
mToggle := systray.AddMenuItem("Toggle", "Toggle the Quit button")
shown := true
toggle := func() {
if shown {
subMenuBottom.Check()
subMenuBottom2.Hide()
mQuitOrig.Hide()
mEnabled.Hide()
shown = false
} else {
subMenuBottom.Uncheck()
subMenuBottom2.Show()
mQuitOrig.Show()
mEnabled.Show()
shown = true
}
}
for {
select {
case <-mChange.ClickedCh:
mChange.SetTitle("I've Changed")
case <-mChecked.ClickedCh:
if mChecked.Checked() {
mChecked.Uncheck()
mChecked.SetTitle("Unchecked")
} else {
mChecked.Check()
mChecked.SetTitle("Checked")
}
case <-mEnabled.ClickedCh:
mEnabled.SetTitle("Disabled")
mEnabled.Disable()
case <-mUrl.ClickedCh:
open.Run("https://www.getlantern.org")
case <-subMenuBottom2.ClickedCh:
panic("panic button pressed")
case <-subMenuBottom.ClickedCh:
toggle()
case <-mToggle.ClickedCh:
toggle()
case <-mQuit.ClickedCh:
systray.Quit()
fmt.Println("Quit2 now...")
return
}
}
}()
}