forked from alexflint/gallium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cocoa.go
239 lines (210 loc) · 5.77 KB
/
cocoa.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package gallium
/*
#include <stdlib.h>
#include "gallium/cocoa.h"
// It does not seem that we can import "_cgo_export.h" from here
extern void cgo_onMenuClicked(void*);
// This is a wrapper around NSMenu_AddMenuItem that adds the function pointer
// argument, since this does not seem to be possible from Go directly.
static inline gallium_nsmenuitem_t* helper_NSMenu_AddMenuItem(
gallium_nsmenu_t* menu,
const char* title,
const char* shortcutKey,
gallium_modifier_t shortcutModifier,
void *callbackArg) {
return NSMenu_AddMenuItem(
menu,
title,
shortcutKey,
shortcutModifier,
&cgo_onMenuClicked,
callbackArg);
}
*/
import "C"
import (
"errors"
"fmt"
"log"
"strings"
"unsafe"
)
// MenuEntry is the interface for menus and menu items.
type MenuEntry interface {
menu()
}
// Separator displays a horizontal separator within a menu
var Separator MenuEntry
// A MenuItem has a title and can be clicked on. It is a leaf node in the menu tree.
type MenuItem struct {
Title string
Shortcut KeyCombination
OnClick func()
}
func (MenuItem) menu() {}
// A Menu has a title and a list of entries. It is a non-leaf node in the menu tree.
type Menu struct {
Title string
Entries []MenuEntry
}
func (Menu) menu() {}
// menuMgr is the singleton that owns the menu
var menuMgr *menuManager
// menuManager translates Menus and MenuItems to their native equivalent (e.g. NSMenuItem on macOS)
type menuManager struct {
items map[int]MenuItem
}
func newMenuManager() *menuManager {
return &menuManager{make(map[int]MenuItem)}
}
func (m *menuManager) add(menu MenuEntry, parent *C.gallium_nsmenu_t) {
switch menu := menu.(type) {
case Menu:
item := C.NSMenu_AddMenuItem(parent, C.CString(menu.Title), nil, 0, nil, nil)
submenu := C.NSMenu_New(C.CString(menu.Title))
C.NSMenuItem_SetSubmenu(item, submenu)
for _, entry := range menu.Entries {
m.add(entry, submenu)
}
case MenuItem:
id := len(m.items)
m.items[id] = menu
callbackArg := C.malloc(C.sizeof_int)
*(*C.int)(callbackArg) = C.int(id)
C.helper_NSMenu_AddMenuItem(
parent,
C.CString(menu.Title),
C.CString(menu.Shortcut.Key),
C.gallium_modifier_t(menu.Shortcut.Modifiers),
callbackArg)
case nil:
// nil means add a separator
C.NSMenu_AddSeparator(parent)
default:
log.Printf("unexpected menu entry: %T", menu)
}
}
func parseShortcut(s string) (key string, modifiers int, err error) {
parts := strings.Split(s, "+")
if len(parts) == 0 {
return "", 0, fmt.Errorf("empty shortcut")
}
key = parts[len(parts)-1]
if len(key) == 0 {
return "", 0, fmt.Errorf("empty key")
}
for _, part := range parts[:len(parts)-1] {
switch strings.ToLower(part) {
case "cmd":
modifiers |= int(C.GalliumCmdModifier)
case "ctrl":
modifiers |= int(C.GalliumCtrlModifier)
case "cmdctrl":
modifiers |= int(C.GalliumCmdOrCtrlModifier)
case "alt":
modifiers |= int(C.GalliumAltOrOptionModifier)
case "option":
modifiers |= int(C.GalliumAltOrOptionModifier)
case "fn":
modifiers |= int(C.GalliumFnModifier)
case "shift":
modifiers |= int(C.GalliumShiftModifier)
default:
return "", 0, fmt.Errorf("unknown modifier: %s", part)
}
}
return
}
func (app *App) SetMenu(menus []Menu) {
if menuMgr == nil {
menuMgr = newMenuManager()
}
root := C.NSMenu_New(C.CString("<root>"))
for _, m := range menus {
menuMgr.add(m, root)
}
C.NSApplication_SetMainMenu(root)
}
type StatusItemOptions struct {
Image *Image // image to show in the status bar, must be non-nil
Width float64 // width of item in pixels (zero means automatic size)
Highlight bool // whether to highlight the item when clicked
Menu []MenuEntry // the menu to display when the item is clicked
}
func (app *App) AddStatusItem(opts StatusItemOptions) {
if menuMgr == nil {
menuMgr = newMenuManager()
}
if opts.Image == nil {
panic("status item image must not be nil")
}
menu := C.NSMenu_New(C.CString("<statusbar>"))
for _, m := range opts.Menu {
menuMgr.add(m, menu)
}
C.NSStatusBar_AddItem(
opts.Image.c,
C.float(opts.Width),
C.bool(opts.Highlight),
menu)
}
// Image holds a handle to a platform-specific image structure (e.g. NSImage on macOS).
type Image struct {
c *C.gallium_nsimage_t
}
var (
ErrImageDecodeFailed = errors.New("image could not be decoded")
)
// ImageFromPNG creates an image from a buffer containing a PNG-encoded image.
func ImageFromPNG(buf []byte) (*Image, error) {
cbuf := C.CBytes(buf)
defer C.free(cbuf)
cimg := C.NSImage_NewFromPNG(cbuf, C.int(len(buf)))
if cimg == nil {
return nil, ErrImageDecodeFailed
}
return &Image{cimg}, nil
}
// Notification represents a desktop notification
type Notification struct {
Title string
Subtitle string
InformativeText string
Image *Image
Identifier string
ActionButtonTitle string
OtherButtonTitle string
}
// Post shows the given desktop notification
func (app *App) Post(n Notification) {
var cimg *C.gallium_nsimage_t
if n.Image != nil {
cimg = n.Image.c
}
cn := C.NSUserNotification_New(
C.CString(n.Title),
C.CString(n.Subtitle),
C.CString(n.InformativeText),
cimg,
C.CString(n.Identifier),
len(n.ActionButtonTitle) > 0,
len(n.OtherButtonTitle) > 0,
C.CString(n.ActionButtonTitle),
C.CString(n.OtherButtonTitle))
C.NSUserNotificationCenter_DeliverNotification(cn)
}
// BundleInfo looks up an entry in the Info.plist for the current bundle.
func BundleInfo(key string) string {
cstr := C.MainBundle_ObjectForKey(C.CString(key))
if cstr == nil {
return ""
}
defer C.free(unsafe.Pointer(cstr))
return C.GoString(cstr)
}
// RunApplication is for debugging only. It allows creation of menus and
// desktop notifications without firing up any parts of chromium. It will
// be removed before the 1.0 release.
func RunApplication() {
C.NSApplication_Run()
}