Support left/right click override

This commit is contained in:
Lea Anthony 2022-10-11 21:07:01 +11:00
commit 828ebd8fb6
No known key found for this signature in database
GPG key ID: 33DAF7BB90A58405
4 changed files with 35 additions and 19 deletions

View file

@ -18,6 +18,8 @@ type SysTray interface {
SetMenu(menu *menu.Menu) error
SetIcons(lightModeIcon, darkModeIcon *options.SystemTrayIcon) error
Update() error
OnLeftClick(func())
OnRightClick(func())
}
func NewSysTray() SysTray {

View file

@ -63,9 +63,17 @@ func (p *Systray) Update() error {
func (p *Systray) SetTitle(_ string) {}
func New() (*Systray, error) {
ni := &Systray{
lclick: func() {},
rclick: func() {},
ni := &Systray{}
ni.lclick = func() {
if ni.menu != nil {
_ = ni.menu.ShowAtCursor()
}
}
ni.rclick = func() {
if ni.menu != nil {
_ = ni.menu.ShowAtCursor()
}
}
MainClassName := "WailsSystray"
@ -158,12 +166,16 @@ func (p *Systray) Stop() error {
return nil
}
func (p *Systray) Click(fn func()) {
p.lclick = fn
func (p *Systray) OnLeftClick(fn func()) {
if fn != nil {
p.lclick = fn
}
}
func (p *Systray) OnRightClick(fn func()) {
p.rclick = fn
if fn != nil {
p.rclick = fn
}
}
func (p *Systray) SetTooltip(tooltip string) error {
@ -266,20 +278,12 @@ func (p *Systray) WinProc(hwnd win32.HWND, msg uint32, wparam, lparam uintptr) u
switch msg {
case win32.NotifyIconMessageId:
if lparam == win32.WM_LBUTTONUP {
p.lclick()
if p.menu != nil {
err := p.menu.ShowAtCursor()
if err != nil {
return 0
}
if p.lclick != nil {
p.lclick()
}
} else if lparam == win32.WM_RBUTTONUP {
p.rclick()
if p.menu != nil {
err := p.menu.ShowAtCursor()
if err != nil {
return 0
}
if p.rclick != nil {
p.rclick()
}
}
case win32.WM_SETTINGCHANGE:

View file

@ -15,6 +15,8 @@ type SystemTray struct {
tooltip string
startHidden bool
menu *menu.Menu
onLeftClick func()
onRightClick func()
// The platform specific implementation
impl platform.SysTray
@ -28,6 +30,8 @@ func newSystemTray(options *options.SystemTray) *SystemTray {
tooltip: options.Tooltip,
startHidden: options.StartHidden,
menu: options.Menu,
onLeftClick: options.OnLeftClick,
onRightClick: options.OnRightClick,
}
}
@ -36,6 +40,8 @@ func (t *SystemTray) run() {
t.impl.SetTitle(t.title)
t.impl.SetIcons(t.lightModeIcon, t.darkModeIcon)
t.impl.SetTooltip(t.tooltip)
t.impl.OnLeftClick(t.onLeftClick)
t.impl.OnRightClick(t.onRightClick)
if !t.startHidden {
t.impl.Show()
}

View file

@ -1,6 +1,8 @@
package options
import "github.com/wailsapp/wails/v2/pkg/menu"
import (
"github.com/wailsapp/wails/v2/pkg/menu"
)
// SystemTray contains options for the system tray
type SystemTray struct {
@ -10,6 +12,8 @@ type SystemTray struct {
Tooltip string
StartHidden bool
Menu *menu.Menu
OnLeftClick func()
OnRightClick func()
}
// SystemTrayIcon represents a system tray icon