From ab8d28b4363521873175c5006710dcf815e85141 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E9=91=AB?= <438518244@qq.com> Date: Mon, 9 May 2022 15:29:44 +0800 Subject: [PATCH] systray click callbacks --- README.md | 12 +++++++++++- example/main.go | 2 +- systray.go | 20 +++++++++++++++++--- systray_windows.go | 11 +++++++++-- systray_windows_test.go | 12 +++++++++++- webview_example/main.go | 2 +- 6 files changed, 50 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 4cf166ed..8b2fa5bf 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ systray is a cross-platform Go library to place an icon and menu in the notifica ```go func main() { - systray.Run(onReady, onExit) + systray.Run(onReady, onExit, onLClick, onRClick) } func onReady() { @@ -26,6 +26,16 @@ func onReady() { func onExit() { // clean up here } + +func onLClick(shouMenu func() error) { + // deal with mouse left click events with shouMenu callback + _ = showMenu() +} + +func onRClick(shouMenu func() error) { + // deal with mouse right click events with shouMenu callback + _ = showMenu() +} ``` See [full API](https://pkg.go.dev/github.com/getlantern/systray?tab=doc) as well as [CHANGELOG](https://github.com/getlantern/systray/tree/master/CHANGELOG.md). diff --git a/example/main.go b/example/main.go index b0f350d5..20e1d4ff 100644 --- a/example/main.go +++ b/example/main.go @@ -16,7 +16,7 @@ func main() { ioutil.WriteFile(fmt.Sprintf(`on_exit_%d.txt`, now.UnixNano()), []byte(now.String()), 0644) } - systray.Run(onReady, onExit) + systray.Run(onReady, onExit, nil, nil) } func onReady() { diff --git a/systray.go b/systray.go index 8fe9d163..c66b46ab 100644 --- a/systray.go +++ b/systray.go @@ -17,6 +17,9 @@ var ( systrayReady func() systrayExit func() + systrayLClick func(showMenu func() error) + systrayRClick func(showMenu func() error) + menuItems = make(map[uint32]*MenuItem) menuItemsLock sync.RWMutex @@ -73,8 +76,8 @@ func newMenuItem(title string, tooltip string, parent *MenuItem) *MenuItem { // Run initializes GUI and starts the event loop, then invokes the onReady // callback. It blocks until systray.Quit() is called. -func Run(onReady func(), onExit func()) { - Register(onReady, onExit) +func Run(onReady func(), onExit func(), onLClick func(showMenu func() error), onRClick func(showMenu func() error)) { + Register(onReady, onExit, onLClick, onRClick) nativeLoop() } @@ -83,7 +86,7 @@ func Run(onReady func(), onExit func()) { // needs to show other UI elements, for example, webview. // To overcome some OS weirdness, On macOS versions before Catalina, calling // this does exactly the same as Run(). -func Register(onReady func(), onExit func()) { +func Register(onReady func(), onExit func(), onLClick func(showMenu func() error), onRClick func(showMenu func() error)) { // NOTE: 左键点击托盘图标 if onReady == nil { systrayReady = func() {} } else { @@ -103,6 +106,17 @@ func Register(onReady func(), onExit func()) { onExit = func() {} } systrayExit = onExit + + if onLClick == nil { + onLClick = func(showMenu func() error) {showMenu()} + } + systrayLClick = onLClick + + if onRClick == nil { + onRClick = func(showMenu func() error ) {showMenu()} + } + systrayRClick = onRClick + registerSystray() } diff --git a/systray_windows.go b/systray_windows.go index 6bdc803d..665cb86e 100644 --- a/systray_windows.go +++ b/systray_windows.go @@ -1,3 +1,4 @@ +//go:build windows // +build windows package systray @@ -277,8 +278,14 @@ func (t *winTray) wndProc(hWnd windows.Handle, message uint32, wParam, lParam ui systrayExit() case t.wmSystrayMessage: switch lParam { - case WM_RBUTTONUP, WM_LBUTTONUP: - t.showMenu() + case WM_LBUTTONUP: + systrayLClick(func() error { + return t.showMenu() + }) + case WM_RBUTTONUP: + systrayRClick(func() error { + return t.showMenu() + }) } case t.wmTaskbarCreated: // on explorer.exe restarts t.muNID.Lock() diff --git a/systray_windows_test.go b/systray_windows_test.go index 7cb6c757..18984b0e 100644 --- a/systray_windows_test.go +++ b/systray_windows_test.go @@ -128,5 +128,15 @@ func TestWindowsRun(t *testing.T) { t.Log("Exit success") } - Run(onReady, onExit) + onLClick := func(showMenu func()(error)) { + t.Log("Mouse Left clicked") + _ = showMenu() + } + + onRClick := func(showMenu func()(error)) { + t.Log("Mouse Right clicked") + _ = showMenu() + } + + Run(onReady, onExit, onLClick, onRClick) } diff --git a/webview_example/main.go b/webview_example/main.go index 160a8a51..5612b076 100644 --- a/webview_example/main.go +++ b/webview_example/main.go @@ -6,7 +6,7 @@ import ( ) func main() { - systray.Register(onReady, nil) + systray.Register(onReady, nil, nil, nil) configureWebview("Webview example", 1024, 768) }