mirror of
https://github.com/wavetermdev/wails.git
synced 2026-08-05 13:53:43 -07:00
Support loading multiple tray icons
This commit is contained in:
@@ -4,55 +4,91 @@ package build
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/wailsapp/wails/v2/internal/fs"
|
||||
"github.com/leaanthony/slicer"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (d *DesktopBuilder) convertToHexLiteral(bytes []byte) string {
|
||||
result := ""
|
||||
for _, b := range bytes {
|
||||
result += fmt.Sprintf("0x%x, ", b)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// desktop_linux.go will compile the tray icon found at <projectdir>/trayicon.png into the application
|
||||
func (d *DesktopBuilder) processTrayIcons(assetDir string, options *Options) error {
|
||||
|
||||
// Determine icon file
|
||||
iconFile := filepath.Join(options.ProjectData.Path, "trayicon.png")
|
||||
|
||||
var err error
|
||||
|
||||
// Get all the tray icon filenames
|
||||
trayIconDirectory := filepath.Join(options.ProjectData.Path, "trayicons")
|
||||
trayIconFilenames, err := filepath.Glob(trayIconDirectory + "/*.png")
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Setup target
|
||||
targetFilename := "trayicon"
|
||||
targetFilename := "trayicons"
|
||||
targetFile := filepath.Join(assetDir, targetFilename+".c")
|
||||
d.addFileToDelete(targetFile)
|
||||
|
||||
var dataBytes []byte
|
||||
|
||||
// If the icon file exists, load it up
|
||||
if fs.FileExists(iconFile) {
|
||||
// Load the tray icon
|
||||
dataBytes, err = ioutil.ReadFile(iconFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Use a strings builder
|
||||
var cdata strings.Builder
|
||||
|
||||
// Write header
|
||||
header := `// trayicon.c
|
||||
header := `// trayicons.c
|
||||
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL.
|
||||
// This file was auto-generated. DO NOT MODIFY.
|
||||
|
||||
`
|
||||
cdata.WriteString(header)
|
||||
cdata.WriteString(fmt.Sprintf("const unsigned int trayIconLength = %d;\n", len(dataBytes)))
|
||||
cdata.WriteString("const unsigned char trayIcon[] = { ")
|
||||
|
||||
// Convert each byte to hex
|
||||
for _, b := range dataBytes {
|
||||
cdata.WriteString(fmt.Sprintf("0x%x, ", b))
|
||||
var variableList slicer.StringSlicer
|
||||
|
||||
// Loop over icons
|
||||
for count, filename := range trayIconFilenames {
|
||||
|
||||
// Load the tray icon
|
||||
dataBytes, err = ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
iconname := strings.TrimSuffix(filepath.Base(filename), ".png")
|
||||
trayIconName := fmt.Sprintf("trayIcon%dName", count)
|
||||
variableList.Add(trayIconName)
|
||||
cdata.WriteString(fmt.Sprintf("const unsigned char %s[] = { %s0x00 };\n", trayIconName, d.convertToHexLiteral([]byte(iconname))))
|
||||
|
||||
trayIconLength := fmt.Sprintf("trayIcon%dLength", count)
|
||||
variableList.Add(trayIconLength)
|
||||
lengthAsString := strconv.Itoa(len(dataBytes))
|
||||
cdata.WriteString(fmt.Sprintf("const unsigned char %s[] = { %s0x00 };\n", trayIconLength, d.convertToHexLiteral([]byte(lengthAsString))))
|
||||
|
||||
trayIconData := fmt.Sprintf("trayIcon%dData", count)
|
||||
variableList.Add(trayIconData)
|
||||
cdata.WriteString(fmt.Sprintf("const unsigned char %s[] = { ", trayIconData))
|
||||
|
||||
// Convert each byte to hex
|
||||
for _, b := range dataBytes {
|
||||
cdata.WriteString(fmt.Sprintf("0x%x, ", b))
|
||||
}
|
||||
|
||||
cdata.WriteString("0x00 };\n")
|
||||
}
|
||||
|
||||
cdata.WriteString("0x00 };\n")
|
||||
// Write out main trayIcons data
|
||||
cdata.WriteString("const unsigned char *trayIcons[] = { ")
|
||||
cdata.WriteString(variableList.Join(", "))
|
||||
cdata.WriteString(", 0x00 };\n")
|
||||
|
||||
err = ioutil.WriteFile(targetFile, []byte(cdata.String()), 0600)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user