-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
218 lines (189 loc) · 5.15 KB
/
Copy pathmain.go
File metadata and controls
218 lines (189 loc) · 5.15 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
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
// ToggleTheme is a Windows system tray application that allows users to toggle between
// light and dark themes. It monitors the Windows registry for theme changes and updates
// the system tray menu accordingly. The application provides a simple interface for
// switching themes and includes an "About" dialog with information about the application.
//
// If you update winres/winres.json run: go-winres make
//
// build with: go build -ldflags "-s -w -H=windowsgui" -trimpath -o toggletheme.exe
//
package main
import (
_ "embed"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"unsafe"
"fyne.io/systray"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
)
//go:embed dark-theme.ico
var iconData []byte
var mToggleTheme *systray.MenuItem
var user32 = syscall.NewLazyDLL("user32.dll")
var procMessageBoxW = user32.NewProc("MessageBoxW")
const personalizePath = `Software\Microsoft\Windows\CurrentVersion\Themes\Personalize`
const newline = "\r\n"
func MessageBox(title, text string, style uint) int {
titlePtr, _ := syscall.UTF16PtrFromString(title)
textPtr, _ := syscall.UTF16PtrFromString(text)
ret, _, _ := procMessageBoxW.Call(
0,
uintptr(unsafe.Pointer(textPtr)),
uintptr(unsafe.Pointer(titlePtr)),
uintptr(style),
)
return int(ret)
}
func onReady() {
systray.SetIcon(iconData)
systray.SetTooltip("ToggleTheme")
systray.SetOnTapped(handleToggleAction)
// Menu items
mToggleTheme = systray.AddMenuItem(getToggleLabel(), "Switch between light and dark mode")
systray.AddSeparator()
mAbout := systray.AddMenuItem("About", "About ToggleTheme")
mQuit := systray.AddMenuItem("Quit", "Exit the application")
// Start registry watcher in background
go watchThemeChanges()
// Handle menu clicks
go func() {
for {
select {
case <-mToggleTheme.ClickedCh:
handleToggleAction()
case <-mAbout.ClickedCh:
var msg = fmt.Sprintf("Toggles Windows theme between light and dark mode%s%s"+
"Left-click the tray icon to toggle the theme.%s"+
"Right-click the tray icon to access the menu.%s%s"+
"https://github.com/Timthreetwelve/toggletheme",
newline, newline, newline, newline, newline)
MessageBox("About ToggleTheme", msg, 0)
case <-mQuit.ClickedCh:
systray.Quit()
return
}
}
}()
}
func handleToggleAction() {
if err := toggleTheme(); err != nil {
log.Printf("%s", "Failed to toggle theme: "+err.Error())
return
}
mToggleTheme.SetTitle(getToggleLabel())
curMode, _ := getCurrentTheme()
if curMode {
log.Println("Switched to light mode.")
fmt.Println("Switched to light mode.")
} else {
log.Println("Switched to dark mode.")
fmt.Println("Switched to dark mode.")
}
}
func onExit() {
// Cleanup if needed
fmt.Println("Done.")
log.Printf("ToggleTheme is shutting down.")
log.Printf("")
}
func getCurrentTheme() (bool, error) {
k, err := registry.OpenKey(registry.CURRENT_USER, personalizePath, registry.QUERY_VALUE)
if err != nil {
return false, err
}
defer k.Close()
val, _, err := k.GetIntegerValue("AppsUseLightTheme")
if err != nil {
return false, err
}
return val == 1, nil
}
func getToggleLabel() string {
isLight, err := getCurrentTheme()
if err != nil {
return "Toggle Light/Dark Mode"
}
if isLight {
return "Switch to Dark Mode"
}
return "Switch to Light Mode"
}
func toggleTheme() error {
k, err := registry.OpenKey(registry.CURRENT_USER, personalizePath, registry.QUERY_VALUE|registry.SET_VALUE)
if err != nil {
return err
}
defer k.Close()
current, _, err := k.GetIntegerValue("AppsUseLightTheme")
if err != nil {
return err
}
var newVal uint64
if current == 1 {
newVal = 0 // Dark mode
} else {
newVal = 1 // Light mode
}
if err := k.SetDWordValue("AppsUseLightTheme", uint32(newVal)); err != nil {
return err
}
if err := k.SetDWordValue("SystemUsesLightTheme", uint32(newVal)); err != nil {
return err
}
return nil
}
// watchThemeChanges listens for registry changes and updates the menu label
func watchThemeChanges() {
k, err := registry.OpenKey(registry.CURRENT_USER, personalizePath, registry.NOTIFY)
if err != nil {
log.Printf("Failed to open registry key for watching: %v", err)
return
}
defer k.Close()
h := windows.Handle(k)
for {
// Wait for registry change notification
err := windows.RegNotifyChangeKeyValue(
h,
false,
windows.REG_NOTIFY_CHANGE_LAST_SET,
0,
false,
)
if err != nil {
log.Printf("Registry watch error: %v", err)
return
}
// Update menu label after change
mToggleTheme.SetTitle(getToggleLabel())
}
}
func main() {
temp := os.TempDir()
logFile := temp + "\\toggletheme.log"
file, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Printf("error opening log file: %v", err)
}
log.SetOutput(file)
execPath, err := os.Executable()
if err != nil {
fmt.Println("Error getting executable path:", err)
return
}
log.Printf("ToggleTheme is starting up from %s.", execPath)
// Handle Ctrl+C / SIGTERM gracefully
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigChan
systray.Quit()
}()
// Run systray
fmt.Println("ToggleTheme is running in the system tray. ")
systray.Run(onReady, onExit)
}