-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapplet.go
178 lines (168 loc) · 5.9 KB
/
applet.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
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
// Copyright (C) 2019 Evgeny Kuznetsov ([email protected])
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"io"
"os"
"github.com/andlabs/ui"
"github.com/getlantern/systray"
"github.com/nicksnyder/go-i18n/v2/i18n"
_ "embed"
)
const (
defaultIcon = "assets/matebook-applet.png"
)
var (
appQuit = make(chan struct{})
)
func onReady() {
logTrace.Println(localizer.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{ID: "PreparingTray", Other: "Setting up menu..."}}))
systray.SetIcon(getIcon(iconPath, defaultIcon))
mKbdlightTimeout := systray.AddMenuItem("", "")
systray.AddSeparator()
mStatus := systray.AddMenuItem("", "")
systray.AddSeparator()
mOff := systray.AddMenuItem(localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "StatusOff"}), "Switch off battery protection")
mTravel := systray.AddMenuItem(localizer.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{ID: "DoTravel", Other: "TRAVEL (95%-100%)"}}), "Set battery protection to TRAVEL")
mOffice := systray.AddMenuItem(localizer.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{ID: "DoOffice", Other: "OFFICE (70%-90%)"}}), "Set battery protection to OFFICE")
mHome := systray.AddMenuItem(localizer.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{ID: "DoHome", Other: "HOME (40%-70%)"}}), "Set battery protection to HOME")
mCustom := systray.AddMenuItem(localizer.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{ID: "DoCustom", Other: "CUSTOM"}}), "Set custom battery protection thresholds")
systray.AddSeparator()
mFnlock := systray.AddMenuItem("", "")
systray.AddSeparator()
mQuit := systray.AddMenuItem(localizer.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{ID: "Quit", Other: "Quit"}}), "Quit the applet")
if config.kdblightTimeout == nil {
mKbdlightTimeout.Hide()
logTrace.Println("no access to kbdlight_timeout setting, not showing its GUI")
} else {
mKbdlightTimeout.SetTitle(getKbdlightTimeoutStatus())
if !config.kdblightTimeout.isWritable() {
mKbdlightTimeout.Disable()
}
}
if config.thresh == nil {
mStatus.Hide()
logTrace.Println("no access to BP information, not showing it")
} else {
mStatus.SetTitle(getStatus())
}
if config.thresh == nil || !config.thresh.isWritable() {
mOff.Hide()
mTravel.Hide()
mOffice.Hide()
mHome.Hide()
mCustom.Hide()
logTrace.Println("no way to change BP settings, not showing the corresponding GUI")
}
if config.fnlock == nil {
mFnlock.Hide()
logTrace.Println("no access to Fn-Lock setting, not showing its GUI")
} else {
mFnlock.SetTitle(getFnlockStatus())
}
logTrace.Println("Menu is now ready")
go func() {
for {
select {
case <-mStatus.ClickedCh:
logTrace.Println("Got a click on BP status")
mStatus.SetTitle(getStatus())
case <-mOff.ClickedCh:
logTrace.Println("Got a click on BP OFF")
setThresholds(0, 100)
mStatus.SetTitle(getStatus())
case <-mTravel.ClickedCh:
logTrace.Println("Got a click on BP TRAVEL")
setThresholds(95, 100)
mStatus.SetTitle(getStatus())
case <-mOffice.ClickedCh:
logTrace.Println("Got a click on BP OFFICE")
setThresholds(70, 90)
mStatus.SetTitle(getStatus())
case <-mHome.ClickedCh:
logTrace.Println("Got a click on BP HOME")
setThresholds(40, 70)
mStatus.SetTitle(getStatus())
case <-mFnlock.ClickedCh:
logTrace.Println("Got a click on fnlock")
config.fnlock.toggle()
mFnlock.SetTitle(getFnlockStatus())
case <-appQuit:
logTrace.Println("Shutting down systray applet")
systray.Quit()
return
}
}
}()
logTrace.Println("Setting up GUI thread...")
if err := ui.Main(func() {
ui.OnShouldQuit(func() bool {
customWindow.Destroy()
kbdlightTimeoutWindow.Destroy()
logTrace.Println("ready to quit GUI thread")
return true
})
go func() {
for {
select {
case <-mCustom.ClickedCh:
logTrace.Println("Got a click on BP CUSTOM")
ch := make(chan struct{})
ui.QueueMain(func() { customThresholds(ch) })
<-ch
mStatus.SetTitle(getStatus())
case <-mKbdlightTimeout.ClickedCh:
logTrace.Println("Got a click on KbdlightTimeout")
ch := make(chan struct{})
ui.QueueMain(func() { kbdlightTimeout(ch) })
<-ch
mKbdlightTimeout.SetTitle(getKbdlightTimeoutStatus())
case <-appQuit:
return
}
}
}()
go func() {
<-mQuit.ClickedCh
logTrace.Println("Got a click on Quit")
ui.Quit()
close(appQuit)
}()
}); err != nil {
logError.Println(err)
}
logInfo.Println(localizer.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{ID: "AppletExit", Other: "Exiting the applet..."}}))
os.Exit(0)
}
func onExit() {
}
func getIcon(pth, dflt string) []byte {
b, err := os.ReadFile(pth)
if err != nil {
logInfo.Println(localizer.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{ID: "NoCustomIcon", Other: "Couldn't get custom icon, falling back to default"}}))
file, err := assets.Open(dflt)
if err != nil {
logError.Println(err)
}
defer file.Close()
b, err = io.ReadAll(file)
if err != nil {
logError.Println(err)
}
} else {
logInfo.Println(localizer.MustLocalize(&i18n.LocalizeConfig{DefaultMessage: &i18n.Message{ID: "GotCustomIcon", Other: "Successfully loaded custom icon from {{.Path}}"}, TemplateData: map[string]interface{}{"Path": pth}}))
}
return b
}