-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
systray.go
138 lines (120 loc) · 3.13 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"fmt"
"time"
"github.com/getlantern/systray"
"github.com/hako/durafmt"
)
func (a *app) onSystrayReady() {
systray.SetTitle("0m")
var getProClickedCh chan struct{}
if !a.isPro {
mGetPro := systray.AddMenuItem(
fmt.Sprintf("⭐️ Get %s Pro", appName),
"",
)
getProClickedCh = mGetPro.ClickedCh
systray.AddSeparator()
}
// Setup break entries
mBreaks := systray.AddMenuItem("Breaks", "")
mNoBreaks := mBreaks.AddSubMenuItem("No breaks yet", "")
mNoBreaks.Disable()
a.mBreaks = mBreaks
a.breakMenuItems = append(a.breakMenuItems, mNoBreaks)
if len(a.breaks) > 0 {
a.updateBreakMenuItems()
}
// Setup active time entries
mActivePeriods := systray.AddMenuItem("Active periods", "")
mNoActivePeriods := mActivePeriods.AddSubMenuItem(
"No active periods yet",
"",
)
mNoActivePeriods.Disable()
a.mActivePeriods = mActivePeriods
a.activePeriodMenuItems = append(a.activePeriodMenuItems, mNoActivePeriods)
if len(a.activePeriods) > 0 {
a.updateActivePeriodMenuItems()
}
mPreferences := systray.AddMenuItem("Preferences", "")
mGoIdleAfter := mPreferences.AddSubMenuItem(
"Reset after inactivity for",
"",
)
mKeepTimeLogsFor := mPreferences.AddSubMenuItem("Keep time logs for", "")
mOpenAtLogin := mPreferences.AddSubMenuItemCheckbox(
"Start at Login",
"",
a.startup.RunningAtStartup(),
)
createMinutesSelectionItems := func(
menuItem *systray.MenuItem,
selectedItem time.Duration,
optionsInSettings []uint32,
) (
itemSelected chan int, menuItems []*systray.MenuItem,
) {
selectedItemIndex := getMinutesSliceIndexFromDuration(
selectedItem,
optionsInSettings[:],
)
for index, minutes := range optionsInSettings {
durationString := durafmt.Parse(time.Duration(minutes) * time.Minute).
LimitFirstN(1).
String()
menuItems = append(
menuItems,
menuItem.AddSubMenuItemCheckbox(
durationString,
"",
index == selectedItemIndex,
),
)
}
itemSelected = make(chan int)
for i, mItem := range menuItems {
go func(c chan struct{}, index int) {
for range c {
itemSelected <- index
}
}(mItem.ClickedCh, i)
}
return itemSelected, menuItems
}
idleTimeSelected, mIdleTimes := createMinutesSelectionItems(
mGoIdleAfter,
a.maxAllowedIdleTime,
idleTimeOptionsInSettings[:],
)
keepTimeLogsForSelected, mKeepTimeLogsForOptions := createMinutesSelectionItems(
mKeepTimeLogsFor,
a.keepTimeLogsFor,
keepTimeLogsForOptionsInSettings[:],
)
systray.AddSeparator()
mAbout := systray.AddMenuItem(fmt.Sprintf("About %s", appName), "")
mQuit := systray.AddMenuItem(fmt.Sprintf("Quit %s", appName), "")
go func() {
for {
select {
case <-getProClickedCh:
openURL(getProURL)
case index := <-idleTimeSelected:
a.handleIdleItemSelected(mIdleTimes, index)
case index := <-keepTimeLogsForSelected:
a.handleKeepTimeLogsForOptionSelected(
mKeepTimeLogsForOptions,
index,
)
case <-mOpenAtLogin.ClickedCh:
a.handleOpenAtLoginClicked(mOpenAtLogin)
case <-mAbout.ClickedCh:
a.handleAboutClicked()
case <-mQuit.ClickedCh:
a.handleQuitClicked()
}
}
}()
a.isSystrayReady = true
}