This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
forked from caseymrm/menuet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menuet.go
168 lines (147 loc) · 3.4 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
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 (
"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 "sasbury/menuet"
}
alertChannel chan AlertClicked
currentState *MenuState
nextState *MenuState
pendingStateChange bool
debounceMutex sync.Mutex
visibleMenuItemsMutex sync.RWMutex
visibleMenuItems map[string]internalItem
}
var appInstance *Application
var appOnce 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()
}
// 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 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)
}