diff --git a/v2/internal/platform/systray.go b/v2/internal/platform/systray.go index 9625f1a7e..611d74c48 100644 --- a/v2/internal/platform/systray.go +++ b/v2/internal/platform/systray.go @@ -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 { diff --git a/v2/internal/platform/systray/windows.go b/v2/internal/platform/systray/windows.go index 965b08d54..032067a13 100644 --- a/v2/internal/platform/systray/windows.go +++ b/v2/internal/platform/systray/windows.go @@ -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: diff --git a/v2/pkg/application/systray.go b/v2/pkg/application/systray.go index 00965e41f..0447e27b9 100644 --- a/v2/pkg/application/systray.go +++ b/v2/pkg/application/systray.go @@ -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() } diff --git a/v2/pkg/options/systemtray.go b/v2/pkg/options/systemtray.go index 14639040e..3e694ce85 100644 --- a/v2/pkg/options/systemtray.go +++ b/v2/pkg/options/systemtray.go @@ -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