Installer/fyne-theme_windows.go

92 lines
2.2 KiB
Go
Raw Normal View History

2024-07-07 10:07:11 +00:00
//go:build windows
// +build windows
package main
import (
"log"
"fyne.io/fyne/v2"
"golang.org/x/sys/windows/registry"
)
// GetRegistryColorValue retrieves a DWORD value from the Windows registry.
func GetRegistryColorValue(keyPath, valueName string) (uint32, error) {
key, err := registry.OpenKey(registry.CURRENT_USER, keyPath, registry.QUERY_VALUE)
if err != nil {
return 0, err
}
defer key.Close()
value, _, err := key.GetIntegerValue(valueName)
if err != nil {
return 0, err
}
return uint32(value), nil
}
// ParseRegistryColorValue converts a DWORD value to RGB format.
func ParseRegistryColorValue(value uint32) (uint8, uint8, uint8) {
return uint8(value & 0xFF), uint8((value >> 8) & 0xFF), uint8((value >> 16) & 0xFF)
}
// GetDarkMode checks if dark mode is enabled.
func GetDarkMode() (bool, error) {
const keyPath = `Software\Microsoft\Windows\CurrentVersion\Themes\Personalize`
const valueName = "AppsUseLightTheme"
darkModeValue, err := GetRegistryColorValue(keyPath, valueName)
if err != nil {
return false, err
}
isDark := darkModeValue == 0
return isDark, nil
}
// GetThemeWin retrieves the background and theme colors from the registry.
func GetThemeWin() (colBackground, colTheme [3]uint8, isDark bool, err error) {
registryEntries := []struct {
keyPath string
valueName string
}{
{`Software\Microsoft\Windows\CurrentVersion\Explorer\Accent`, "AccentColorMenu"},
{`Software\Microsoft\Windows\DWM`, "AccentColor"},
}
isDark, err = GetDarkMode()
if err != nil {
return
}
blackRGB := [3]uint8{10, 10, 10}
whiteRGB := [3]uint8{245, 245, 245}
if isDark {
colBackground = blackRGB
} else {
colBackground = whiteRGB
}
for _, entry := range registryEntries {
registryValue, err := GetRegistryColorValue(entry.keyPath, entry.valueName)
if err == nil {
r, g, b := ParseRegistryColorValue(registryValue)
if entry.valueName == "AccentColorMenu" || entry.valueName == "AccentColor" {
colTheme = [3]uint8{r, g, b}
}
}
}
return
}
func ApplyTheme(a fyne.App) {
colBackground, colTheme, isDark, err := GetThemeWin()
if err != nil {
log.Fatalf("Error: %v\n", err)
}
applyThemeToApp(a, colBackground, colTheme, isDark)
}