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

84
main.go
View file

@ -1,7 +1,10 @@
package main
import (
"errors"
"log"
"os"
"strconv"
"sync"
"fyne.io/fyne/v2"
@ -12,6 +15,19 @@ import (
"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() {
a := app.New()
w := a.NewWindow("SysTray")
@ -26,57 +42,61 @@ func main() {
desk.SetSystemTrayMenu(m)
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{
Width: 1656 / 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.Hide()
})
go func() {
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
wg.Add(len(layers))
err := listenHotkey(
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()
for _, v := range layers {
go func() {
defer wg.Done()
err := listenHotkey(
func() { w.SetContent(container.NewPadded(layer_1)) },
func() { log.Println("up") },
hotkey.Key2,
)
if err != nil {
log.Println(err)
}
}()
log.Printf("Registering %+v", v)
err := listenHotkey(
func() { w.SetContent(container.NewPadded(v.Image)) },
func() { w.SetContent(container.NewPadded(defaultImage)) },
v.Hotkey,
)
if err != nil {
log.Println(err)
return
}
}()
}
wg.Wait()
}()
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 = append(ms, mods...)
hk := hotkey.New(ms, key)