mirror of
https://github.com/wailsapp/wails.git
synced 2026-03-14 14:45:49 +01:00
Support left/right click override
This commit is contained in:
parent
bca58afc04
commit
828ebd8fb6
4 changed files with 35 additions and 19 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue