Skip to content

Commit

Permalink
Merge pull request #23 from thiagokokada/add-pause-to-systray
Browse files Browse the repository at this point in the history
Add 1 hour pause to systray
  • Loading branch information
thiagokokada authored Jan 17, 2024
2 parents f163d5c + 191b564 commit 4203180
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

var (
version = "development"
ctx context.Context
ctxCancel context.CancelFunc
mainCtx context.Context
mainCtxCancel context.CancelFunc
duration = new(time.Duration)
frequency = new(time.Duration)
notificationSound = new(bool)
Expand Down Expand Up @@ -144,8 +144,8 @@ func runTwentyTwentyTwenty() {
)
}

ctx, ctxCancel = context.WithCancel(context.Background())
go twentyTwentyTwenty(ctx, notifier, duration, frequency, notificationSound)
mainCtx, mainCtxCancel = context.WithCancel(context.Background())
go twentyTwentyTwenty(mainCtx, notifier, duration, frequency, notificationSound)
}

func main() {
Expand Down
34 changes: 33 additions & 1 deletion systray.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
package main

import (
"context"
_ "embed"
"log"
"time"

"fyne.io/systray"
)
Expand All @@ -17,22 +19,52 @@ func onReady() {
systray.SetIcon(data)
systray.SetTooltip("TwentyTwentyTwenty")
mEnabled := systray.AddMenuItemCheckbox("Enabled", "Enable twenty-twenty-twenty", true)
mPause := systray.AddMenuItemCheckbox("Pause for 1 hour", "Pause twenty-twenty-twenty for 1 hour", false)
mSound := new(systray.MenuItem)
if notificationSoundEnabled {
mSound = systray.AddMenuItemCheckbox("Sound", "Enable notification sound", *notificationSound)
}
systray.AddSeparator()
mQuit := systray.AddMenuItem("Quit", "Quit the whole app")
var ctx context.Context
var cancel context.CancelFunc

for {
select {
case <-mEnabled.ClickedCh:
if mEnabled.Checked() {
ctxCancel()
mainCtxCancel()
mEnabled.Uncheck()
mPause.Disable()
} else {
runTwentyTwentyTwenty()
mEnabled.Check()
mPause.Enable()
}
case <-mPause.ClickedCh:
if mPause.Checked() {
mainCtxCancel() // make sure the loop stopped
cancel()
runTwentyTwentyTwenty()
mEnabled.Enable()
mPause.Uncheck()
} else {
log.Println("Pausing twenty-twenty-twenty for 1 hour...")
ctx, cancel = context.WithCancel(context.Background())
go func(ctx context.Context) {
mainCtxCancel()
timer := time.NewTimer(time.Hour)
select {
case <-timer.C:
runTwentyTwentyTwenty()
mEnabled.Enable()
mPause.Uncheck()
case <-ctx.Done():
}
cancel()
}(ctx)
mEnabled.Disable()
mPause.Check()
}
case <-mSound.ClickedCh:
if mSound.Checked() {
Expand Down

0 comments on commit 4203180

Please sign in to comment.