Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support left and right mouse button click event callback function for tray icon #278

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions systray_nonwindows.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//go:build !windows
// +build !windows

// go:build !windows

package systray
Expand Down Expand Up @@ -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
}
39 changes: 38 additions & 1 deletion systray_windows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build windows
// +build windows

package systray
Expand Down Expand Up @@ -202,6 +203,9 @@ type winTray struct {

wmSystrayMessage,
wmTaskbarCreated uint32
// tray icon click event callback functions
onLButtomUpCallbacks []func()
onRButtomUpCallbacks []func()
}

// Loads an image from file and shows it in tray.
Expand Down Expand Up @@ -241,6 +245,16 @@ func (t *winTray) setTooltip(src string) error {
return t.nid.modify()
}

// 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

// WindowProc callback function that processes messages sent to a window.
Expand Down Expand Up @@ -277,8 +291,19 @@ 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_LBUTTONUP:
if len(t.onLButtomUpCallbacks) == 0 {
t.showMenu()
} else {
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()
Expand Down Expand Up @@ -947,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)
}