From 63d9cd5013fb93df76f2246a758f77cdae291d5c Mon Sep 17 00:00:00 2001 From: kerbalwzy Date: Sat, 24 Aug 2024 17:48:06 +0800 Subject: [PATCH 1/5] feat: Support custom left mouse click events --- systray.go | 5 +++++ systray_windows.go | 21 ++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/systray.go b/systray.go index cd5ae283..ddc0258a 100644 --- a/systray.go +++ b/systray.go @@ -111,6 +111,11 @@ func Quit() { quitOnce.Do(quit) } +// Set the callback function for left mouse click events +func OnLButtomup(callbake func()) { + wt.setLButtonup(callbake) +} + // AddMenuItem adds a menu item with the designated title and tooltip. // It can be safely invoked from different goroutines. // Created menu items are checkable on Windows and OSX by default. For Linux you have to use AddMenuItemCheckbox diff --git a/systray_windows.go b/systray_windows.go index 13b4411d..38f2bba2 100644 --- a/systray_windows.go +++ b/systray_windows.go @@ -1,3 +1,4 @@ +//go:build windows // +build windows package systray @@ -202,6 +203,8 @@ type winTray struct { wmSystrayMessage, wmTaskbarCreated uint32 + // + onLeftButtomUp []func() } // Loads an image from file and shows it in tray. @@ -241,6 +244,14 @@ func (t *winTray) setTooltip(src string) error { return t.nid.modify() } +// Set the callback function for left mouse click events +func (t *winTray) setLButtonup(callback func()) { + if t.onLeftButtomUp == nil { + t.onLeftButtomUp = make([]func(), 0) + } + t.onLeftButtomUp = append(t.onLeftButtomUp, callback) +} + var wt winTray // WindowProc callback function that processes messages sent to a window. @@ -277,8 +288,16 @@ func (t *winTray) wndProc(hWnd windows.Handle, message uint32, wParam, lParam ui systrayExit() case t.wmSystrayMessage: switch lParam { - case WM_RBUTTONUP, WM_LBUTTONUP: + case WM_RBUTTONUP: t.showMenu() + case WM_LBUTTONUP: + if t.onLeftButtomUp == nil { + t.showMenu() + } else { + for _, callback := range t.onLeftButtomUp { + callback() + } + } } case t.wmTaskbarCreated: // on explorer.exe restarts t.muNID.Lock() From b1e5ed94b069e7448bc319091533fc969b4dd568 Mon Sep 17 00:00:00 2001 From: kerbalwzy Date: Sat, 24 Aug 2024 17:51:05 +0800 Subject: [PATCH 2/5] fix: update go.mod module name --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 6b6db020..973610db 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/getlantern/systray +module github.com/kebalwzy/systray go 1.13 From f2a036282b402e040bd1640b87c5df01735580a5 Mon Sep 17 00:00:00 2001 From: kerbalwzy Date: Sat, 24 Aug 2024 18:07:16 +0800 Subject: [PATCH 3/5] feat: Support custom left mouse click events --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 973610db..6b6db020 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/kebalwzy/systray +module github.com/getlantern/systray go 1.13 From 7ada290d8cec26699f6a55b4c7b845beeab86da4 Mon Sep 17 00:00:00 2001 From: kerbalwzy Date: Sat, 24 Aug 2024 18:56:10 +0800 Subject: [PATCH 4/5] feat: Support left and right mouse button click event callback function for tray icons --- systray.go | 13 ++++++++++--- systray_windows.go | 30 ++++++++++++++++++------------ 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/systray.go b/systray.go index ddc0258a..eaf55b18 100644 --- a/systray.go +++ b/systray.go @@ -111,9 +111,16 @@ func Quit() { quitOnce.Do(quit) } -// Set the callback function for left mouse click events -func OnLButtomup(callbake func()) { - wt.setLButtonup(callbake) +// Set the left mouse click event callback for the tray icon, which can be called repeatedly. +// The callback function is executed according to the first in, first out rule +func OnLButtomUp(callbake func()) { + wt.onLButtonUp(callbake) +} + +// Set the left mouse click event callback for the tray icon, which can be called repeatedly. +// The callback function is executed according to the first in, first out rule +func OnRButtomUp(callbake func()) { + wt.onRButtonUp(callbake) } // AddMenuItem adds a menu item with the designated title and tooltip. diff --git a/systray_windows.go b/systray_windows.go index 38f2bba2..963b5a39 100644 --- a/systray_windows.go +++ b/systray_windows.go @@ -203,8 +203,9 @@ type winTray struct { wmSystrayMessage, wmTaskbarCreated uint32 - // - onLeftButtomUp []func() + // tray icon click event callback functions + onLButtomUpCallbacks []func() + onRButtomUpCallbacks []func() } // Loads an image from file and shows it in tray. @@ -244,12 +245,14 @@ func (t *winTray) setTooltip(src string) error { return t.nid.modify() } -// Set the callback function for left mouse click events -func (t *winTray) setLButtonup(callback func()) { - if t.onLeftButtomUp == nil { - t.onLeftButtomUp = make([]func(), 0) - } - t.onLeftButtomUp = append(t.onLeftButtomUp, callback) +// Set the callback function for `WM_LBUTTONUP` +func (t *winTray) onLButtonUp(callback func()) { + t.onLButtomUpCallbacks = append(t.onLButtomUpCallbacks, callback) +} + +// Set the callback function for `WM_RBUTTONUP` +func (t *winTray) onRButtonUp(callback func()) { + t.onRButtomUpCallbacks = append(t.onRButtomUpCallbacks, callback) } var wt winTray @@ -288,16 +291,19 @@ func (t *winTray) wndProc(hWnd windows.Handle, message uint32, wParam, lParam ui systrayExit() case t.wmSystrayMessage: switch lParam { - case WM_RBUTTONUP: - t.showMenu() case WM_LBUTTONUP: - if t.onLeftButtomUp == nil { + if len(t.onLButtomUpCallbacks) == 0 { t.showMenu() } else { - for _, callback := range t.onLeftButtomUp { + for _, callback := range t.onLButtomUpCallbacks { callback() } } + case WM_RBUTTONUP: + t.showMenu() + for _, callback := range t.onRButtomUpCallbacks { + callback() + } } case t.wmTaskbarCreated: // on explorer.exe restarts t.muNID.Lock() From 6759704fef7dea33d0f0d8f0e56aa50444cb3db1 Mon Sep 17 00:00:00 2001 From: kerbalwzy Date: Fri, 13 Sep 2024 17:06:31 +0800 Subject: [PATCH 5/5] fix: Multi platform functionality repair --- systray.go | 12 ------------ systray_nonwindows.go | 12 ++++++++++++ systray_windows.go | 12 ++++++++++++ 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/systray.go b/systray.go index eaf55b18..cd5ae283 100644 --- a/systray.go +++ b/systray.go @@ -111,18 +111,6 @@ func Quit() { quitOnce.Do(quit) } -// Set the left mouse click event callback for the tray icon, which can be called repeatedly. -// The callback function is executed according to the first in, first out rule -func OnLButtomUp(callbake func()) { - wt.onLButtonUp(callbake) -} - -// Set the left mouse click event callback for the tray icon, which can be called repeatedly. -// The callback function is executed according to the first in, first out rule -func OnRButtomUp(callbake func()) { - wt.onRButtonUp(callbake) -} - // AddMenuItem adds a menu item with the designated title and tooltip. // It can be safely invoked from different goroutines. // Created menu items are checkable on Windows and OSX by default. For Linux you have to use AddMenuItemCheckbox diff --git a/systray_nonwindows.go b/systray_nonwindows.go index 12eacdfa..df1c58e4 100644 --- a/systray_nonwindows.go +++ b/systray_nonwindows.go @@ -1,4 +1,6 @@ +//go:build !windows // +build !windows + // go:build !windows package systray @@ -99,3 +101,13 @@ func systray_on_exit() { func systray_menu_item_selected(cID C.int) { systrayMenuItemSelected(uint32(cID)) } + +// only support on windows +func OnLButtomUp(callbake func()) { + // do nothing +} + +// only support on windows +func OnRButtomUp(callbake func()) { + // do nothing +} diff --git a/systray_windows.go b/systray_windows.go index 963b5a39..76e6b6ca 100644 --- a/systray_windows.go +++ b/systray_windows.go @@ -972,3 +972,15 @@ func hideMenuItem(item *MenuItem) { func showMenuItem(item *MenuItem) { addOrUpdateMenuItem(item) } + +// Set the left mouse click event callback for the tray icon, which can be called repeatedly. +// The callback function is executed according to the first in, first out rule +func OnLButtomUp(callbake func()) { + wt.onLButtonUp(callbake) +} + +// Set the left mouse click event callback for the tray icon, which can be called repeatedly. +// The callback function is executed according to the first in, first out rule +func OnRButtomUp(callbake func()) { + wt.onRButtonUp(callbake) +}