Skip to content

Commit

Permalink
Remove useless mutex in systray
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagokokada committed Feb 9, 2024
1 parent a434d48 commit e54a7fa
Showing 1 changed file with 16 additions and 31 deletions.
47 changes: 16 additions & 31 deletions systray.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,12 @@ import (
"context"
"fmt"
"log/slog"
"sync"

"fyne.io/systray"
)

const systrayEnabled bool = true

func withMutex(mu *sync.Mutex, f func()) {
mu.Lock()
defer mu.Unlock()
f()
}

func onReady() {
systray.SetIcon(systrayIcon)
systray.SetTooltip("TwentyTwentyTwenty")
Expand All @@ -36,27 +29,25 @@ func onReady() {
systray.AddSeparator()
mQuit := systray.AddMenuItem("Quit", "Quit the whole app")

var mu sync.Mutex
// running `go run . -race` detects a bunch of data races here that are safe
// to ignore since the access to the actual critical regions in systray are
// protected with a mutex
for {
select {
case <-mEnabled.ClickedCh:
if mEnabled.Checked() {
slog.DebugContext(ctx, "Enable button unchecked")
ctxCancel()

withMutex(&mu, func() {
mEnabled.Uncheck()
mPause.Disable()
})
mEnabled.Uncheck()
mPause.Disable()
} else {
slog.DebugContext(ctx, "Enable button checked")
ctx, ctxCancel = context.WithCancel(context.Background())
go twenty.Start(ctx)

withMutex(&mu, func() {
mEnabled.Check()
mPause.Enable()
})
mEnabled.Check()
mPause.Enable()
}
case <-mPause.ClickedCh:
slog.DebugContext(ctx, "Cancelling current pause")
Expand All @@ -66,41 +57,35 @@ func onReady() {
ctx, ctxCancel = context.WithCancel(context.Background())
go twenty.Start(ctx)

withMutex(&mu, func() {
mEnabled.Enable()
mPause.Uncheck()
})
mEnabled.Enable()
mPause.Uncheck()
} else {
slog.DebugContext(ctx, "Pause button checked")
ctx, ctxCancel = context.WithCancel(context.Background())
go twenty.Pause(
ctx,
func() {
withMutex(&mu, func() {
slog.DebugContext(ctx, "Calling pause callback")
mEnabled.Enable()
mPause.Uncheck()
})
slog.DebugContext(ctx, "Calling pause callback")
mEnabled.Enable()
mPause.Uncheck()
},
nil,
)

withMutex(&mu, func() {
mEnabled.Disable()
mPause.Check()
})
mEnabled.Disable()
mPause.Check()
}
case <-mSound.ClickedCh:
if mSound.Checked() {
slog.DebugContext(ctx, "Sound button unchecked")
twenty.Settings.Sound = false

withMutex(&mu, func() { mSound.Uncheck() })
mSound.Uncheck()
} else {
slog.DebugContext(ctx, "Sound button checked")
twenty.Settings.Sound = true

withMutex(&mu, func() { mSound.Check() })
mSound.Check()
}
case <-mQuit.ClickedCh:
slog.DebugContext(ctx, "Quit button clicked")
Expand Down

0 comments on commit e54a7fa

Please sign in to comment.