-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystray.go
120 lines (105 loc) · 2.66 KB
/
systray.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//go:build !nosystray
// +build !nosystray
package main
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")
mEnabled := systray.AddMenuItemCheckbox("Enabled", "Enable twenty-twenty-twenty", true)
mPause := systray.AddMenuItemCheckbox(
fmt.Sprintf("Pause for %.f hour", twenty.Settings.Pause.Hours()),
fmt.Sprintf("Pause twenty-twenty-twenty for %.f hour", twenty.Settings.Pause.Hours()),
false,
)
mSound := new(systray.MenuItem)
if twenty.Features.Sound {
mSound = systray.AddMenuItemCheckbox("Sound", "Enable notification sound", twenty.Settings.Sound)
}
systray.AddSeparator()
mQuit := systray.AddMenuItem("Quit", "Quit the whole app")
var pauseCtx context.Context
var cancelPauseCtx context.CancelFunc
var mu sync.Mutex
for {
select {
case <-mEnabled.ClickedCh:
if mEnabled.Checked() {
slog.DebugContext(twenty.Ctx(), "Enable button unchecked")
twenty.Stop()
withMutex(&mu, func() {
mEnabled.Uncheck()
mPause.Disable()
})
} else {
slog.DebugContext(twenty.Ctx(), "Enable button checked")
twenty.Start()
withMutex(&mu, func() {
mEnabled.Check()
mPause.Enable()
})
}
case <-mPause.ClickedCh:
if pauseCtx != nil {
slog.DebugContext(twenty.Ctx(), "Cancelling current pause")
cancelPauseCtx()
}
if mPause.Checked() {
slog.DebugContext(twenty.Ctx(), "Pause button unchecked")
twenty.Start()
withMutex(&mu, func() {
mEnabled.Enable()
mPause.Uncheck()
})
} else {
slog.DebugContext(twenty.Ctx(), "Pause button checked")
pauseCtx, cancelPauseCtx = context.WithCancel(context.Background())
go func() {
defer cancelPauseCtx()
twenty.Pause(
pauseCtx,
func() {
withMutex(&mu, func() {
slog.DebugContext(twenty.Ctx(), "Calling pause callback")
mEnabled.Enable()
mPause.Uncheck()
})
},
nil,
)
}()
withMutex(&mu, func() {
mEnabled.Disable()
mPause.Check()
})
}
case <-mSound.ClickedCh:
if mSound.Checked() {
slog.DebugContext(twenty.Ctx(), "Sound button unchecked")
twenty.Settings.Sound = false
withMutex(&mu, func() { mSound.Uncheck() })
} else {
slog.DebugContext(twenty.Ctx(), "Sound button checked")
twenty.Settings.Sound = true
withMutex(&mu, func() { mSound.Check() })
}
case <-mQuit.ClickedCh:
slog.DebugContext(twenty.Ctx(), "Quit button clicked")
systray.Quit()
}
}
}
func onExit() {
twenty.Stop()
}