-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtray.go
More file actions
183 lines (160 loc) · 4.52 KB
/
Copy pathtray.go
File metadata and controls
183 lines (160 loc) · 4.52 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
_ "embed"
"context"
"fmt"
"log"
"runtime"
"time"
"fyne.io/systray"
"github.com/msfoundry/commit/extraction"
"github.com/msfoundry/commit/store"
"github.com/msfoundry/commit/whatsapp"
)
//go:embed assets/tray.png
var trayIconPNG []byte
//go:embed assets/tray_dim.png
var trayIconDimPNG []byte
//go:embed assets/tray_attn.png
var trayIconAttnPNG []byte
//go:embed assets/tray.ico
var trayIconICO []byte
//go:embed assets/tray_attn.ico
var trayIconAttnICO []byte
const (
trayModelFast = store.FallbackModel // Haiku
trayModelSharp = store.DefaultModel // Sonnet
)
type trayState int
const (
stateConnected trayState = iota
stateDisconnected
stateAttention
)
// runTray owns the process main thread (required by AppKit). The server and
// WhatsApp loops run in goroutines started before this is called.
func runTray(ctx context.Context, cancel context.CancelFunc, db *store.DB, wa *whatsapp.Client, ext *extraction.Extractor, dashboardURL string) {
onReady := func() {
setTrayIcon(stateConnected)
systray.SetTooltip("Commit")
status := systray.AddMenuItem("Starting...", "")
status.Disable()
stats := systray.AddMenuItem("", "")
stats.Disable()
systray.AddSeparator()
openDash := systray.AddMenuItem("Open Dashboard", "Open Commit in your browser")
systray.AddSeparator()
model := systray.AddMenuItem("Model", "Claude model used for extraction")
modelFast := model.AddSubMenuItemCheckbox("Claude Haiku — fast", "", false)
modelSharp := model.AddSubMenuItemCheckbox("Claude Sonnet — sharper", "", false)
syncModelChecks := func() {
if db.GetModel() == trayModelSharp {
modelFast.Uncheck()
modelSharp.Check()
} else {
modelFast.Check()
modelSharp.Uncheck()
}
}
syncModelChecks()
login := systray.AddMenuItemCheckbox("Start at Login", "Launch Commit when you log in", loginItemEnabled())
systray.AddSeparator()
quit := systray.AddMenuItem("Quit Commit", "")
refresh := func() {
state := stateConnected
statusText := "Connected to WhatsApp"
if !wa.IsConnected() {
state = stateDisconnected
statusText = "WhatsApp disconnected"
}
// extraction failing recently outranks the connection state
if ds := ext.GetDebugStatus(); ds.LastErrorAt != "" {
if t, err := time.Parse(time.RFC3339, ds.LastErrorAt); err == nil && time.Since(t) < 10*time.Minute {
state = stateAttention
statusText = "Extraction failing — open dashboard"
}
}
setTrayIcon(state)
status.SetTitle(statusText)
today, err1 := db.GetDayStats(0)
yday, err2 := db.GetDayStats(1)
if err1 == nil && err2 == nil {
stats.SetTitle(fmt.Sprintf("Yesterday %d msgs · %d found — today %d · %d",
yday.Messages, yday.Commitments, today.Messages, today.Commitments))
}
}
refresh()
go func() {
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
refresh()
case <-openDash.ClickedCh:
openBrowser(dashboardURL)
case <-modelFast.ClickedCh:
if err := db.SetModel(trayModelFast); err != nil {
log.Printf("set model error: %v", err)
}
syncModelChecks()
case <-modelSharp.ClickedCh:
if err := db.SetModel(trayModelSharp); err != nil {
log.Printf("set model error: %v", err)
}
syncModelChecks()
case <-login.ClickedCh:
if login.Checked() {
if err := disableLoginItem(); err != nil {
log.Printf("disable login item: %v", err)
} else {
login.Uncheck()
}
} else {
if err := enableLoginItem(); err != nil {
log.Printf("enable login item: %v", err)
} else {
login.Check()
}
}
case <-quit.ClickedCh:
systray.Quit()
}
}
}()
}
onExit := func() {
log.Println("tray exit — shutting down")
cancel()
// give the HTTP server a moment to drain
time.Sleep(300 * time.Millisecond)
}
systray.Run(onReady, onExit)
}
// quitTray tears down the systray loop; safe to call more than once.
func quitTray() {
defer func() { recover() }()
systray.Quit()
}
func setTrayIcon(s trayState) {
if runtime.GOOS == "windows" {
switch s {
case stateAttention:
systray.SetIcon(trayIconAttnICO)
default:
systray.SetIcon(trayIconICO)
}
return
}
switch s {
case stateConnected:
systray.SetTemplateIcon(trayIconPNG, trayIconPNG)
case stateDisconnected:
systray.SetTemplateIcon(trayIconDimPNG, trayIconDimPNG)
case stateAttention:
// colored dot — deliberately not a template image
systray.SetIcon(trayIconAttnPNG)
}
}