Skip to content

Commit

Permalink
working
Browse files Browse the repository at this point in the history
  • Loading branch information
ohidurbappy committed Oct 8, 2021
0 parents commit a79e6e4
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.exe
*.syso
*.rar
Binary file added assets/dark_mode.ico
Binary file not shown.
Binary file added assets/light_mode.ico
Binary file not shown.
2 changes: 2 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go-winres simply --icon icon.png --manifest gui
go build -ldflags -H=windowsgui
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module ohidur.com/windows-theme-switcher

go 1.16

require (
github.com/getlantern/systray v1.1.0
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac
)
28 changes: 28 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/getlantern/context v0.0.0-20190109183933-c447772a6520 h1:NRUJuo3v3WGC/g5YiyF790gut6oQr5f3FBI88Wv0dx4=
github.com/getlantern/context v0.0.0-20190109183933-c447772a6520/go.mod h1:L+mq6/vvYHKjCX2oez0CgEAJmbq1fbb/oNJIWQkBybY=
github.com/getlantern/errors v0.0.0-20190325191628-abdb3e3e36f7 h1:6uJ+sZ/e03gkbqZ0kUG6mfKoqDb4XMAzMIwlajq19So=
github.com/getlantern/errors v0.0.0-20190325191628-abdb3e3e36f7/go.mod h1:l+xpFBrCtDLpK9qNjxs+cHU6+BAdlBaxHqikB6Lku3A=
github.com/getlantern/golog v0.0.0-20190830074920-4ef2e798c2d7 h1:guBYzEaLz0Vfc/jv0czrr2z7qyzTOGC9hiQ0VC+hKjk=
github.com/getlantern/golog v0.0.0-20190830074920-4ef2e798c2d7/go.mod h1:zx/1xUUeYPy3Pcmet8OSXLbF47l+3y6hIPpyLWoR9oc=
github.com/getlantern/hex v0.0.0-20190417191902-c6586a6fe0b7 h1:micT5vkcr9tOVk1FiH8SWKID8ultN44Z+yzd2y/Vyb0=
github.com/getlantern/hex v0.0.0-20190417191902-c6586a6fe0b7/go.mod h1:dD3CgOrwlzca8ed61CsZouQS5h5jIzkK9ZWrTcf0s+o=
github.com/getlantern/hidden v0.0.0-20190325191715-f02dbb02be55 h1:XYzSdCbkzOC0FDNrgJqGRo8PCMFOBFL9py72DRs7bmc=
github.com/getlantern/hidden v0.0.0-20190325191715-f02dbb02be55/go.mod h1:6mmzY2kW1TOOrVy+r41Za2MxXM+hhqTtY3oBKd2AgFA=
github.com/getlantern/ops v0.0.0-20190325191751-d70cb0d6f85f h1:wrYrQttPS8FHIRSlsrcuKazukx/xqO/PpLZzZXsF+EA=
github.com/getlantern/ops v0.0.0-20190325191751-d70cb0d6f85f/go.mod h1:D5ao98qkA6pxftxoqzibIBBrLSUli+kYnJqrgBf9cIA=
github.com/getlantern/systray v1.1.0 h1:U0wCEqseLi2ok1fE6b88gJklzriavPJixZysZPkZd/Y=
github.com/getlantern/systray v1.1.0/go.mod h1:AecygODWIsBquJCJFop8MEQcJbWFfw/1yWbVabNgpCM=
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c h1:rp5dCmg/yLR3mgFuSOe4oEnDDmGLROTvMragMUXpTQw=
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c/go.mod h1:X07ZCGwUbLaax7L0S3Tw4hpejzu63ZrrQiUe6W0hcy0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac h1:oN6lz7iLW/YC7un8pq+9bOLyXrprv2+DKfkJY+2LJJw=
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
171 changes: 171 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"strconv"
"syscall"
"time"

"github.com/getlantern/systray"
"golang.org/x/sys/windows/registry"
)

const (
REGKEY = `Software\Microsoft\Windows\CurrentVersion\Themes\Personalize` // in HKCU
REGNAME_TASKBAR_TRAY = `SystemUsesLightTheme`
REGNAME_APP_LIGHT_THEME = `AppsUseLightTheme`
)

func main() {
fmt.Println("Dark Mode on:", isDark())
go monitor(react)
systray.Run(onReady, onExit)

}

func onReady() {

if isDark() {
systray.SetIcon(getIcon("assets/light_mode.ico"))
} else {
systray.SetIcon(getIcon("assets/dark_mode.ico"))
}

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")

go func() {
for {
select {
case <-mLightMode.ClickedCh:
fmt.Println("Set light mode")
setLightModeTheme()
case <-mDarkMode.ClickedCh:
fmt.Println("Set dark mode")
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(getIcon("assets/light_mode.ico"))

} else {
systray.SetIcon(getIcon("assets/dark_mode.ico"))

}
}

func isDark() bool {
k, err := registry.OpenKey(registry.CURRENT_USER, REGKEY, 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, 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.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, 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:]
}
19 changes: 19 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## Windows theme switcher

This program will appear on the system tray and help
switch the taskbar theme on windows.


Install the necessary dependencies

```
go install github.com/tc-hib/go-winres@latest
```


To build run:

```
./build.sh
```

0 comments on commit a79e6e4

Please sign in to comment.