Skip to content

Commit

Permalink
fix(app): fix tray clickCh
Browse files Browse the repository at this point in the history
  • Loading branch information
ilharp committed May 13, 2023
1 parent 6fe9c53 commit 398e246
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
45 changes: 19 additions & 26 deletions packages/app/ui/tray/tray.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tray
import (
"errors"
"fmt"
util2 "gopkg.ilharper.com/koi/core/util"
"io/fs"
"os"
"os/exec"
Expand Down Expand Up @@ -37,7 +38,7 @@ type TrayLock struct {

type TrayDaemon struct { //nolint:golint
i *do.Injector
chanReg []chan struct{}
chanReg util2.ChannelRegistry[struct{}]
manager *manage.KoiManager
}

Expand Down Expand Up @@ -123,7 +124,7 @@ func (tray *TrayDaemon) onReady() {

mStarting := systray.AddMenuItem(p.Sprintf("Starting..."), "")
mStarting.Disable()
tray.chanReg = append(tray.chanReg, mStarting.ClickedCh)
tray.chanReg.Register(mStarting.ClickedCh)

tray.addItemsAfter()

Expand Down Expand Up @@ -154,10 +155,6 @@ func (tray *TrayDaemon) rebuild() {
conn, err := tray.manager.Available()
if err != nil {
systray.ResetMenu()
for _, c := range tray.chanReg {
close(c)
}
tray.chanReg = []chan struct{}{}
tray.addItemsBefore()
systray.AddSeparator()
tray.addItemsAfter()
Expand Down Expand Up @@ -219,10 +216,6 @@ func (tray *TrayDaemon) rebuild() {

// Clear all menu items.
systray.ResetMenu()
for _, c := range tray.chanReg {
close(c)
}
tray.chanReg = []chan struct{}{}

tray.addItemsBefore()

Expand Down Expand Up @@ -250,10 +243,10 @@ func (tray *TrayDaemon) rebuild() {
mStop.Disable()
}

tray.chanReg = append(tray.chanReg, mOpen.ClickedCh)
tray.chanReg = append(tray.chanReg, mStart.ClickedCh)
tray.chanReg = append(tray.chanReg, mRestart.ClickedCh)
tray.chanReg = append(tray.chanReg, mStop.ClickedCh)
tray.chanReg.Register(mOpen.ClickedCh)
tray.chanReg.Register(mStart.ClickedCh)
tray.chanReg.Register(mRestart.ClickedCh)
tray.chanReg.Register(mStop.ClickedCh)

go func(name string) {
for {
Expand Down Expand Up @@ -530,8 +523,8 @@ func (tray *TrayDaemon) addItemsBefore() {
mVersion := systray.AddMenuItem(version, "")
mVersion.Disable()

tray.chanReg = append(tray.chanReg, mTitle.ClickedCh)
tray.chanReg = append(tray.chanReg, mVersion.ClickedCh)
tray.chanReg.Register(mTitle.ClickedCh)
tray.chanReg.Register(mVersion.ClickedCh)
}

func (tray *TrayDaemon) addItemsAfter() {
Expand All @@ -557,16 +550,16 @@ func (tray *TrayDaemon) addItemsAfter() {
mQuit := systray.AddMenuItem(p.Sprintf("Hide"), "")
mQuit.SetTemplateIcon(icon.Hide, icon.Hide)

tray.chanReg = append(tray.chanReg, mAdvanced.ClickedCh)
tray.chanReg = append(tray.chanReg, mRefresh.ClickedCh)
tray.chanReg = append(tray.chanReg, mStartDaemon.ClickedCh)
tray.chanReg = append(tray.chanReg, mStopDaemon.ClickedCh)
tray.chanReg = append(tray.chanReg, mKillDaemon.ClickedCh)
tray.chanReg = append(tray.chanReg, mOpenDataFolder.ClickedCh)
tray.chanReg = append(tray.chanReg, mOpenTerminal.ClickedCh)
tray.chanReg = append(tray.chanReg, mExit.ClickedCh)
tray.chanReg = append(tray.chanReg, mAbout.ClickedCh)
tray.chanReg = append(tray.chanReg, mQuit.ClickedCh)
tray.chanReg.Register(mAdvanced.ClickedCh)
tray.chanReg.Register(mRefresh.ClickedCh)
tray.chanReg.Register(mStartDaemon.ClickedCh)
tray.chanReg.Register(mStopDaemon.ClickedCh)
tray.chanReg.Register(mKillDaemon.ClickedCh)
tray.chanReg.Register(mOpenDataFolder.ClickedCh)
tray.chanReg.Register(mOpenTerminal.ClickedCh)
tray.chanReg.Register(mExit.ClickedCh)
tray.chanReg.Register(mAbout.ClickedCh)
tray.chanReg.Register(mQuit.ClickedCh)

go func() {
for {
Expand Down
14 changes: 14 additions & 0 deletions packages/core/util/chanreg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package util

type ChannelRegistry[T any] struct {
reg [256]chan<- T
next uint8
}

func (chanReg *ChannelRegistry[T]) Register(c chan<- T) {
if chanReg.reg[chanReg.next] != nil {
close(chanReg.reg[chanReg.next])
}
chanReg.reg[chanReg.next] = c
chanReg.next++
}

0 comments on commit 398e246

Please sign in to comment.