5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-10 01:50:36 +08:00

[v3 windows] Fix systray icon size

This commit is contained in:
Lea Anthony 2023-05-07 20:19:37 +10:00
parent 19a654a2b1
commit 1ed270fe05
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
4 changed files with 46 additions and 19 deletions

File diff suppressed because one or more lines are too long

View File

@ -22,12 +22,14 @@ type systemTrayImpl interface {
setIconPosition(position int)
setTemplateIcon(icon []byte)
destroy()
setDarkModeIcon(icon []byte)
}
type SystemTray struct {
id uint
label string
icon []byte
darkModeIcon []byte
iconPosition int
leftButtonClickHandler func()
@ -80,6 +82,17 @@ func (s *SystemTray) SetIcon(icon []byte) *SystemTray {
return s
}
func (s *SystemTray) SetDarkModeIcon(icon []byte) *SystemTray {
if s.impl == nil {
s.darkModeIcon = icon
} else {
invokeSync(func() {
s.impl.setDarkModeIcon(icon)
})
}
return s
}
func (s *SystemTray) SetMenu(menu *Menu) *SystemTray {
if s.impl == nil {
s.menu = menu

View File

@ -3,6 +3,7 @@
package application
import (
"github.com/samber/lo"
"github.com/wailsapp/wails/v3/pkg/events"
"github.com/wailsapp/wails/v3/pkg/w32"
"syscall"
@ -75,12 +76,16 @@ func (s *windowsSystemTray) run() {
panic(syscall.GetLastError())
}
defaultIcon, err := w32.CreateHIconFromPNG(s.parent.icon)
if err != nil {
panic(err)
if s.parent.icon != nil {
s.lightModeIcon = lo.Must(w32.CreateHIconFromPNG(s.parent.icon))
} else {
s.lightModeIcon = lo.Must(w32.CreateHIconFromPNG(DefaultApplicationIcon))
}
if s.parent.darkModeIcon != nil {
s.darkModeIcon = lo.Must(w32.CreateHIconFromPNG(s.parent.darkModeIcon))
} else {
s.darkModeIcon = s.lightModeIcon
}
s.lightModeIcon = defaultIcon
s.darkModeIcon = defaultIcon
s.uid = nid.UID
// TODO: Set Menu
@ -133,18 +138,25 @@ func (s *windowsSystemTray) newNotifyIconData() w32.NOTIFYICONDATA {
}
func (s *windowsSystemTray) setIcon(icon []byte) {
// TODO:
var err error
if w32.IsCurrentlyDarkMode() {
s.darkModeIcon, err = w32.CreateHIconFromPNG(icon)
if err != nil {
panic(syscall.GetLastError())
}
} else {
s.lightModeIcon, err = w32.CreateHIconFromPNG(icon)
if err != nil {
panic(syscall.GetLastError())
}
s.lightModeIcon, err = w32.CreateHIconFromPNG(icon)
if err != nil {
panic(syscall.GetLastError())
}
if s.darkModeIcon == 0 {
s.darkModeIcon = s.lightModeIcon
}
// Update the icon
s.updateIcon()
}
func (s *windowsSystemTray) setDarkModeIcon(icon []byte) {
var err error
s.darkModeIcon, err = w32.CreateHIconFromPNG(icon)
if err != nil {
panic(syscall.GetLastError())
}
if s.lightModeIcon == 0 {
s.lightModeIcon = s.darkModeIcon
}
// Update the icon
s.updateIcon()

View File

@ -29,13 +29,15 @@ func CreateIconFromResourceEx(presbits uintptr, dwResSize uint32, isIcon bool, v
// CreateHIconFromPNG creates a HICON from a PNG file
func CreateHIconFromPNG(pngData []byte) (HICON, error) {
iconWidth := GetSystemMetrics(SM_CXSMICON)
iconHeight := GetSystemMetrics(SM_CYSMICON)
icon, err := CreateIconFromResourceEx(
uintptr(unsafe.Pointer(&pngData[0])),
uint32(len(pngData)),
true,
0x00030000,
0,
0,
iconWidth,
iconHeight,
LR_DEFAULTSIZE)
return HICON(icon), err
}