diff options
Diffstat (limited to 'systray.go')
| -rw-r--r-- | systray.go | 71 |
1 files changed, 15 insertions, 56 deletions
@@ -5,21 +5,12 @@ Methods can be called from any goroutine except Run(), which should be called at */ package systray -/* -#cgo LDFLAGS: -L /Users/merlin/workspace/go/src/github.com/getlantern/systray -#cgo linux pkg-config: gtk+-3.0 appindicator3-0.1 -#cgo windows CFLAGS: -DWIN32 -DUNICODE -D_UNICODE -#cgo darwin 386 LDFLAGS: -framework Cocoa -lsystray_darwin_386 -#cgo darwin amd64 LDFLAGS: -framework Cocoa -lsystray_darwin_amd64 - -#include "systray.h" -*/ -import "C" import ( - "code.google.com/p/go-uuid/uuid" "runtime" "sync" - "unsafe" + "sync/atomic" + + "github.com/getlantern/golog" ) // MenuItem is used to keep track each menu item of systray @@ -29,7 +20,7 @@ type MenuItem struct { ClickedCh chan interface{} // id uniquely identify a menu item, not supposed to be modified - id string + id int32 // title is the text shown on menu item title string // tooltip is the text shown when pointing to menu item @@ -41,10 +32,14 @@ type MenuItem struct { } var ( + log = golog.LoggerFor("systray") + readyCh = make(chan interface{}) clickedCh = make(chan interface{}) - menuItems = make(map[string]*MenuItem) + menuItems = make(map[int32]*MenuItem) menuItemsLock sync.RWMutex + + currentId int32 ) // Run initializes GUI and starts the event loop, then invokes the onReady @@ -58,31 +53,12 @@ func Run(onReady func()) { onReady() }() - C.nativeLoop() + nativeLoop() } // Quit the systray and whole app func Quit() { - C.quit() -} - -// SetIcon sets the systray icon. -// iconBytes should be the content of .ico for windows and .ico/.jpg/.png -// for other platforms. -func SetIcon(iconBytes []byte) { - cstr := (*C.char)(unsafe.Pointer(&iconBytes[0])) - C.setIcon(cstr, (C.int)(len(iconBytes))) -} - -// SetTitle sets the systray title, only available on Mac. -func SetTitle(title string) { - C.setTitle(C.CString(title)) -} - -// SetTitle sets the systray tooltip to display on mouse hover of the tray icon, -// only available on Mac. -func SetTooltip(tooltip string) { - C.setTooltip(C.CString(tooltip)) + quit() } // Add menu item with designated title and tooltip, returning a channel that @@ -93,7 +69,7 @@ func SetTooltip(tooltip string) { // // AddMenuItem can be safely invoked from different goroutines. func AddMenuItem(title string, tooltip string) *MenuItem { - id := uuid.New() + id := atomic.AddInt32(¤tId, 1) item := &MenuItem{nil, id, title, tooltip, false, false} item.ClickedCh = make(chan interface{}) item.update() @@ -143,31 +119,14 @@ func (item *MenuItem) update() { menuItemsLock.Lock() defer menuItemsLock.Unlock() menuItems[item.id] = item - var disabled C.short = 0 - if item.disabled { - disabled = 1 - } - var checked C.short = 0 - if item.checked { - checked = 1 - } - C.add_or_update_menu_item( - C.CString(item.id), - C.CString(item.title), - C.CString(item.tooltip), - disabled, - checked, - ) + addOrUpdateMenuItem(item) } -//export systray_ready -func systray_ready() { +func systrayReady() { readyCh <- nil } -//export systray_menu_item_selected -func systray_menu_item_selected(cId *C.char) { - id := C.GoString(cId) +func systrayMenuItemSelected(id int32) { menuItemsLock.RLock() item := menuItems[id] menuItemsLock.RUnlock() |
