-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
247 lines (205 loc) · 4.93 KB
/
main.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package main
import (
_ "embed"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"syscall"
"time"
"github.com/getlantern/systray"
"golang.org/x/sys/windows/registry"
)
const (
REGKEY_THEME_PERSONALIZE = `Software\Microsoft\Windows\CurrentVersion\Themes\Personalize` // in HKCU
REGKEY_AUTORUN = `SOFTWARE\Microsoft\Windows\CurrentVersion\Run`
REGNAME_TASKBAR_TRAY = `SystemUsesLightTheme`
REGNAME_APP_LIGHT_THEME = `AppsUseLightTheme`
APPNAME = `Windows Theme Switcher`
)
//go:embed assets/dark_mode.ico
var dark_mode []byte
//go:embed assets/light_mode.ico
var light_mode []byte
func main() {
fmt.Println("Dark Mode on:", isDark())
go monitor(react)
systray.Run(onReady, onExit)
}
func onReady() {
if !isSetAutoRun() {
SetAutoRun(true)
}
systray.SetTitle("Theme Switch")
systray.SetTooltip("Theme switcher")
mLightMode := systray.AddMenuItem("Light Mode", "Switch to Light Mode")
mDarkMode := systray.AddMenuItem("Dark Mode", "Switch to Dark Mode")
mExit := systray.AddMenuItem("Exit", "Quit the program")
if isDark() {
// systray.SetIcon(getIcon("assets/light_mode.ico"))
systray.SetIcon(light_mode)
mDarkMode.Disable()
} else {
// systray.SetIcon(getIcon("assets/dark_mode.ico"))
systray.SetIcon(dark_mode)
mLightMode.Disable()
}
go func() {
for {
select {
case <-mLightMode.ClickedCh:
fmt.Println("Set light mode")
mDarkMode.Enable()
mLightMode.Disable()
setLightModeTheme()
case <-mDarkMode.ClickedCh:
fmt.Println("Set dark mode")
mLightMode.Enable()
mDarkMode.Disable()
setDarkModeTheme()
case <-mExit.ClickedCh:
systray.Quit()
return
}
}
}()
}
func onExit() {
systray.Quit()
}
func getIcon(s string) []byte {
b, err := ioutil.ReadFile(s)
if err != nil {
fmt.Print(err)
}
return b
}
// react to the change
func react(isDark bool) {
if isDark {
systray.SetIcon(light_mode)
} else {
systray.SetIcon(dark_mode)
}
}
func isDark() bool {
k, err := registry.OpenKey(registry.CURRENT_USER, REGKEY_THEME_PERSONALIZE, registry.QUERY_VALUE)
if err != nil {
log.Fatal(err)
}
defer k.Close()
val, _, err := k.GetIntegerValue(REGNAME_TASKBAR_TRAY)
if err != nil {
log.Fatal(err)
}
return val == 0
}
func setDarkModeTheme() {
setTheme(0)
}
func setLightModeTheme() {
setTheme(1)
}
func setTheme(themeMode uint32) {
k, err := registry.OpenKey(registry.CURRENT_USER, REGKEY_THEME_PERSONALIZE, registry.QUERY_VALUE|registry.SET_VALUE)
if err != nil {
log.Fatal(err)
}
if err := k.SetDWordValue(REGNAME_TASKBAR_TRAY, themeMode); err != nil {
log.Fatal(err)
}
if err := k.SetDWordValue(REGNAME_APP_LIGHT_THEME, themeMode); err != nil {
log.Fatal(err)
}
if err := k.Close(); err != nil {
log.Fatal(err)
}
}
func monitor(fn func(bool)) {
var regNotifyChangeKeyValue *syscall.Proc
changed := make(chan bool)
if advapi32, err := syscall.LoadDLL("Advapi32.dll"); err == nil {
if p, err := advapi32.FindProc("RegNotifyChangeKeyValue"); err == nil {
regNotifyChangeKeyValue = p
} else {
log.Fatal("Could not find function RegNotifyChangeKeyValue in Advapi32.dll")
}
}
if regNotifyChangeKeyValue != nil {
go func() {
k, err := registry.OpenKey(registry.CURRENT_USER, REGKEY_THEME_PERSONALIZE, syscall.KEY_NOTIFY|registry.QUERY_VALUE)
if err != nil {
log.Fatal(err)
}
var wasDark uint64
for {
regNotifyChangeKeyValue.Call(uintptr(k), 0, 0x00000001|0x00000004, 0, 0)
val, _, err := k.GetIntegerValue(REGNAME_TASKBAR_TRAY)
if err != nil {
log.Fatal(err)
}
if val != wasDark {
wasDark = val
changed <- val == 0
}
}
}()
}
for {
val := <-changed
fn(val)
}
}
// auto dark mode light mode switch
func getClockTime(tz string) string {
t := time.Now()
utc, _ := time.LoadLocation(tz)
hour, min, sec := t.In(utc).Clock()
return ItoaTwoDigits(hour) + ":" + ItoaTwoDigits(min) + ":" + ItoaTwoDigits(sec)
}
// ItoaTwoDigits time.Clock returns one digit on values, so we make sure to convert to two digits
func ItoaTwoDigits(i int) string {
b := "0" + strconv.Itoa(i)
return b[len(b)-2:]
}
// add to autorun
func SetAutoRun(run bool) error {
ex, err := os.Executable()
if err != nil {
panic(err)
}
// executable_path=filepath.Dir(ex)
// get the real path if it is a symlink
exReal, err := filepath.EvalSymlinks(ex)
if err != nil {
panic(err)
}
k, err := registry.OpenKey(registry.CURRENT_USER, REGKEY_AUTORUN, registry.QUERY_VALUE|registry.SET_VALUE)
if err != nil {
return err
}
defer k.Close()
if run {
if err := k.SetStringValue(APPNAME, exReal); err != nil {
return err
}
} else {
k.DeleteValue(APPNAME)
}
return nil
}
func isSetAutoRun() bool {
k, err := registry.OpenKey(registry.CURRENT_USER, REGKEY_AUTORUN, registry.QUERY_VALUE)
if err != nil {
log.Fatal(err)
}
defer k.Close()
_, _, err = k.GetStringValue(APPNAME)
if err == registry.ErrNotExist {
// the key value doesn't exit
return false
}
return true
}