From 0f459768ff159adb288e5ca51eb7718801dad4dc Mon Sep 17 00:00:00 2001 From: Ox Cart Date: Thu, 12 Feb 2015 20:39:57 -0600 Subject: Windows using dll --- systray.go | 62 ++++++++++---------------------------------------------------- 1 file changed, 10 insertions(+), 52 deletions(-) (limited to 'systray.go') diff --git a/systray.go b/systray.go index a647975..5d753cc 100644 --- a/systray.go +++ b/systray.go @@ -5,20 +5,12 @@ Methods can be called from any goroutine except Run(), which should be called at */ package systray -/* -#cgo linux pkg-config: gtk+-3.0 appindicator3-0.1 -#cgo windows CFLAGS: -DWIN32 -DUNICODE -D_UNICODE -#cgo darwin CFLAGS: -DDARWIN -x objective-c -fobjc-arc -#cgo darwin LDFLAGS: -framework Cocoa - -#include "systray.h" -*/ -import "C" import ( - "code.google.com/p/go-uuid/uuid" "runtime" "sync" - "unsafe" + + "code.google.com/p/go-uuid/uuid" + "github.com/getlantern/golog" ) // MenuItem is used to keep track each menu item of systray @@ -40,6 +32,8 @@ type MenuItem struct { } var ( + log = golog.LoggerFor("systray") + readyCh = make(chan interface{}) clickedCh = make(chan interface{}) menuItems = make(map[string]*MenuItem) @@ -57,31 +51,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 @@ -142,31 +117,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 string) { menuItemsLock.RLock() item := menuItems[id] menuItemsLock.RUnlock() -- cgit v1.2.3 From 42246e6e5bea55bf70a4c07a22cd4e2a4b47589f Mon Sep 17 00:00:00 2001 From: Ox Cart Date: Thu, 12 Feb 2015 21:34:43 -0600 Subject: Switching to integer ids --- systray.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'systray.go') diff --git a/systray.go b/systray.go index 5d753cc..38fcd6c 100644 --- a/systray.go +++ b/systray.go @@ -8,8 +8,8 @@ package systray import ( "runtime" "sync" + "sync/atomic" - "code.google.com/p/go-uuid/uuid" "github.com/getlantern/golog" ) @@ -20,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 @@ -36,8 +36,10 @@ var ( 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 @@ -67,7 +69,7 @@ func Quit() { // // 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() @@ -124,7 +126,7 @@ func systrayReady() { readyCh <- nil } -func systrayMenuItemSelected(id string) { +func systrayMenuItemSelected(id int32) { menuItemsLock.RLock() item := menuItems[id] menuItemsLock.RUnlock() -- cgit v1.2.3