-
Notifications
You must be signed in to change notification settings - Fork 20
/
menuet.go
214 lines (186 loc) · 4.63 KB
/
menuet.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
210
211
212
213
214
package menuet
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa
#import <Cocoa/Cocoa.h>
#ifndef __MENUET_H_H__
#import "menuet.h"
#endif
*/
import "C"
import (
"context"
"encoding/json"
"log"
"reflect"
"sync"
"time"
"unsafe"
)
// Application represents the OSX application
type Application struct {
Name string
Label string
// Children returns the top level children
Children func() []MenuItem
// If Version and Repo are set, checks for updates every day
AutoUpdate struct {
Version string
Repo string // For example "caseymrm/menuet"
}
// NotificationResponder is a handler called when notification respond
NotificationResponder func(id, response string)
alertChannel chan AlertClicked
currentState *MenuState
nextState *MenuState
hideStartupItem bool
pendingStateChange bool
debounceMutex sync.Mutex
visibleMenuItemsMutex sync.RWMutex
visibleMenuItems map[string]internalItem
// Used to coordinate graceful shutdown
wg sync.WaitGroup
ctx context.Context
cancel context.CancelFunc
}
var appInstance *Application
var appOnce sync.Once
var shutdownOnce sync.Once
// App returns the application singleton
func App() *Application {
appOnce.Do(func() {
appInstance = &Application{
visibleMenuItems: make(map[string]internalItem),
}
})
return appInstance
}
// RunApplication does not return
func (a *Application) RunApplication() {
if a.AutoUpdate.Version != "" && a.AutoUpdate.Repo != "" {
go a.checkForUpdates()
}
C.createAndRunApplication()
}
// SetMenuState changes what is shown in the dropdown
func (a *Application) SetMenuState(state *MenuState) {
if reflect.DeepEqual(a.currentState, state) {
return
}
go a.sendState(state)
}
// MenuChanged refreshes any open menus
func (a *Application) MenuChanged() {
C.menuChanged()
}
// GracefulShutdownHandles returns a WaitGroup and Context that can
// be used to manage graceful shutdown of go resources when the
// menuabar app is terminated.
// Use the WaitGroup to track your running goroutines, then shut them
// down when the context is Done.
func (a *Application) GracefulShutdownHandles() (*sync.WaitGroup, context.Context) {
shutdownOnce.Do(func() {
a.ctx, a.cancel = context.WithCancel(context.Background())
})
return &a.wg, a.ctx
}
// HideStartup prevents the Start at Login menu item from being displayed
func (a *Application) HideStartup() {
a.hideStartupItem = true
}
// MenuState represents the title and drop down,
type MenuState struct {
Title string
Image string // // In Resources dir or URL, should have height 22
}
func (a *Application) sendState(state *MenuState) {
a.debounceMutex.Lock()
a.nextState = state
if a.pendingStateChange {
a.debounceMutex.Unlock()
return
}
a.pendingStateChange = true
a.debounceMutex.Unlock()
time.Sleep(100 * time.Millisecond)
a.debounceMutex.Lock()
a.pendingStateChange = false
if reflect.DeepEqual(a.currentState, a.nextState) {
a.debounceMutex.Unlock()
return
}
a.currentState = a.nextState
a.debounceMutex.Unlock()
b, err := json.Marshal(a.currentState)
if err != nil {
log.Printf("Marshal: %v (%+v)", err, a.currentState)
return
}
cstr := C.CString(string(b))
C.setState(cstr)
C.free(unsafe.Pointer(cstr))
}
func (a *Application) clicked(unique string) {
a.visibleMenuItemsMutex.RLock()
item, ok := a.visibleMenuItems[unique]
a.visibleMenuItemsMutex.RUnlock()
if !ok {
log.Printf("Item not found for click: %s", unique)
}
if item.Clicked != nil {
go item.Clicked()
}
}
//export itemClicked
func itemClicked(uniqueCString *C.char) {
unique := C.GoString(uniqueCString)
App().clicked(unique)
}
//export children
func children(uniqueCString *C.char) *C.char {
unique := C.GoString(uniqueCString)
items := App().children(unique)
if items == nil {
return nil
}
b, err := json.Marshal(items)
if err != nil {
log.Printf("Marshal: %v", err)
return nil
}
return C.CString(string(b))
}
//export menuClosed
func menuClosed(uniqueCString *C.char) {
unique := C.GoString(uniqueCString)
App().menuClosed(unique)
}
//export notificationRespond
func notificationRespond(id *C.char, response *C.char) {
App().NotificationResponder(C.GoString(id), C.GoString(response))
}
//export hideStartup
func hideStartup() bool {
return App().hideStartupItem
}
//export runningAtStartup
func runningAtStartup() bool {
return App().runningAtStartup()
}
//export toggleStartup
func toggleStartup() {
a := App()
if a.runningAtStartup() {
a.removeStartupItem()
} else {
a.addStartupItem()
}
go a.sendState(a.currentState)
}
//export shutdownWait
func shutdownWait() {
if App().cancel != nil {
App().cancel()
}
App().wg.Wait()
}