-
Notifications
You must be signed in to change notification settings - Fork 59
/
trayhost.go
86 lines (71 loc) · 2.12 KB
/
trayhost.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
/*
Package trayhost is a library for placing a Go
application in the task bar (system tray,
notification area, or dock) in a consistent
manner across multiple platforms. Currently,
there is built-in support for Windows, Mac OSX,
and Linux systems that support GTK+ 3 status
icons (including Gnome 2, KDE 4, Cinnamon,
MATE and other desktop environments).
The indended usage is for applications that
utilize web technology for the user interface, but
require access to the client system beyond what
is offered in a browser sandbox (for instance,
an application that requires access to the user's
file system).
The library places a tray icon on the host system's
task bar that can be used to open a URL, giving users
easy access to the web-based user interface.
Further information can be found at the project's
home at http://github.com/cratonica/trayhost
Clint Caywood
http://github.com/cratonica/trayhost
*/
package trayhost
import (
"reflect"
"unsafe"
)
/*
#cgo linux pkg-config: gtk+-3.0
#cgo linux CFLAGS: -DLINUX
#cgo windows CFLAGS: -DWIN32
#cgo darwin CFLAGS: -DDARWIN -x objective-c
#cgo darwin LDFLAGS: -framework Cocoa
#include <stdlib.h>
#include "platform/platform.h"
*/
import "C"
var isExiting bool
var urlPtr unsafe.Pointer
// Run the host system's event loop
func EnterLoop(title string, imageData []byte) {
defer C.free(urlPtr)
cTitle := C.CString(title)
defer C.free(unsafe.Pointer(cTitle))
// Copy the image data into unmanaged memory
cImageData := C.malloc(C.size_t(len(imageData)))
defer C.free(cImageData)
var cImageDataSlice []C.uchar
sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&cImageDataSlice))
sliceHeader.Cap = len(imageData)
sliceHeader.Len = len(imageData)
sliceHeader.Data = uintptr(cImageData)
for i, v := range imageData {
cImageDataSlice[i] = C.uchar(v)
}
// Enter the loop
C.native_loop(cTitle, &cImageDataSlice[0], C.uint(len(imageData)))
// If reached, user clicked Exit
isExiting = true
}
// Set the URL that the tray icon will open in a browser
func SetUrl(url string) {
if isExiting {
return
}
cs := C.CString(url)
C.free(urlPtr)
urlPtr = unsafe.Pointer(cs)
C.set_url(cs)
}