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

Feature: Click callbacks #234

Open
wants to merge 1 commit 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: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
20 changes: 17 additions & 3 deletions systray.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
}

Expand All @@ -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 {
Expand All @@ -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()
}

Expand Down
11 changes: 9 additions & 2 deletions 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 @@ -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()
Expand Down
12 changes: 11 additions & 1 deletion systray_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion webview_example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func main() {
systray.Register(onReady, nil)
systray.Register(onReady, nil, nil, nil)
configureWebview("Webview example", 1024, 768)
}

Expand Down