1
0
Fork 0

Be dynamic

This commit is contained in:
Andrew Tomaka 2024-06-05 21:59:07 -04:00
parent bb7f987d68
commit b70269a1ac
Signed by: atomaka
GPG key ID: 61209BF70A5B18BE
3 changed files with 52 additions and 32 deletions

BIN
default.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 120 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 120 KiB

After

Width:  |  Height:  |  Size: 119 KiB

68
main.go
View file

@ -1,7 +1,10 @@
package main package main
import ( import (
"errors"
"log" "log"
"os"
"strconv"
"sync" "sync"
"fyne.io/fyne/v2" "fyne.io/fyne/v2"
@ -12,6 +15,19 @@ import (
"golang.design/x/hotkey" "golang.design/x/hotkey"
) )
type Layer struct {
Hotkey hotkey.Key
Image *canvas.Image
}
var keymap = map[string]hotkey.Key{
"KeyF13": hotkey.KeyF13,
"KeyF14": hotkey.KeyF14,
"KeyF15": hotkey.KeyF15,
"KeyF16": hotkey.KeyF16,
"KeyF17": hotkey.KeyF17,
}
func main() { func main() {
a := app.New() a := app.New()
w := a.NewWindow("SysTray") w := a.NewWindow("SysTray")
@ -26,57 +42,61 @@ func main() {
desk.SetSystemTrayMenu(m) desk.SetSystemTrayMenu(m)
desk.SetSystemTrayIcon(icon) desk.SetSystemTrayIcon(icon)
} }
// image := canvas.NewImageFromResource(theme.ConfirmIcon())
layer_1 := canvas.NewImageFromFile("./layer_1.png")
layer_1.Translucency = 0.5
layer_2 := canvas.NewImageFromFile("./layer_2.png")
layer_2.Translucency = 0.5
w.SetContent(container.NewPadded(layer_1))
w.Resize(fyne.Size{ w.Resize(fyne.Size{
Width: 1656 / 2, Width: 1656 / 2,
Height: 546 / 2, Height: 546 / 2,
}) })
defaultImage := canvas.NewImageFromFile("./default.png")
layers := []Layer{}
for i := range 5 {
imagePath := "./layer_" + strconv.Itoa(i+1) + ".png"
if _, err := os.Stat(imagePath); errors.Is(err, os.ErrNotExist) {
continue
}
newLayer := Layer{
Image: canvas.NewImageFromFile(imagePath),
Hotkey: keymap["KeyF"+strconv.Itoa(12+i)],
}
layers = append(layers, newLayer)
}
w.SetContent(container.NewPadded(defaultImage))
// w.SetContent(widget.NewLabel("Fyne System Tray"))
w.SetCloseIntercept(func() { w.SetCloseIntercept(func() {
w.Hide() w.Hide()
}) })
go func() { go func() {
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
wg.Add(2) wg.Add(len(layers))
for _, v := range layers {
go func() { go func() {
defer wg.Done() defer wg.Done()
err := listenHotkey( log.Printf("Registering %+v", v)
func() { w.SetContent(container.NewPadded(layer_1)) },
func() { w.SetContent(container.NewPadded(layer_2)) },
hotkey.Key1,
)
if err != nil {
log.Println(err)
}
}()
go func() {
defer wg.Done()
err := listenHotkey( err := listenHotkey(
func() { w.SetContent(container.NewPadded(layer_1)) }, func() { w.SetContent(container.NewPadded(v.Image)) },
func() { log.Println("up") }, func() { w.SetContent(container.NewPadded(defaultImage)) },
hotkey.Key2, v.Hotkey,
) )
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return
} }
}() }()
}
wg.Wait() wg.Wait()
}() }()
w.ShowAndRun() w.ShowAndRun()
} }
func listenHotkey(upfn func(), downfn func(), key hotkey.Key, mods ...hotkey.Modifier) (err error) { func listenHotkey(downfn func(), upfn func(), key hotkey.Key, mods ...hotkey.Modifier) (err error) {
ms := []hotkey.Modifier{} ms := []hotkey.Modifier{}
ms = append(ms, mods...) ms = append(ms, mods...)
hk := hotkey.New(ms, key) hk := hotkey.New(ms, key)