mirror of
https://github.com/gabrielkheisa/auto_dark_light_windows.git
synced 2025-12-02 22:05:49 +00:00
Update main.go
This commit is contained in:
52
main.go
52
main.go
@@ -15,6 +15,42 @@ import (
|
||||
"golang.org/x/sys/windows/registry"
|
||||
)
|
||||
|
||||
// Define required structures for OS version check
|
||||
const (
|
||||
VER_MAJORVERSION = 0x001
|
||||
VER_MINORVERSION = 0x002
|
||||
VER_BUILDNUMBER = 0x004
|
||||
)
|
||||
|
||||
type RTL_OSVERSIONINFOW struct {
|
||||
OSVersionInfoSize uint32
|
||||
MajorVersion uint32
|
||||
MinorVersion uint32
|
||||
BuildNumber uint32
|
||||
PlatformId uint32
|
||||
CSDVersion [128]uint16
|
||||
}
|
||||
|
||||
// isWindows11OrNewer checks if the OS is Windows 11 (Build >= 22000) or later.
|
||||
func isWindows11OrNewer() bool {
|
||||
ntdll := syscall.NewLazyDLL("ntdll.dll")
|
||||
rtlGetVersion := ntdll.NewProc("RtlGetVersion")
|
||||
|
||||
var info RTL_OSVERSIONINFOW
|
||||
info.OSVersionInfoSize = uint32(unsafe.Sizeof(info))
|
||||
|
||||
ret, _, _ := rtlGetVersion.Call(uintptr(unsafe.Pointer(&info)))
|
||||
|
||||
// Check if the call was successful (NTSTATUS 0)
|
||||
if ret != 0 {
|
||||
return false // Assume older or error
|
||||
}
|
||||
|
||||
// Windows 11 is generally defined by Build Number >= 22000
|
||||
return info.MajorVersion >= 10 && info.BuildNumber >= 22000
|
||||
}
|
||||
|
||||
|
||||
// Set Windows dark/light mode via registry
|
||||
func setMode(light bool) {
|
||||
key, _ := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Themes\Personalize`, registry.SET_VALUE)
|
||||
@@ -80,6 +116,15 @@ func backgroundLoop() {
|
||||
lightImg := findImageFile("light")
|
||||
darkImg := findImageFile("dark")
|
||||
lastMode := getCurrentMode()
|
||||
|
||||
// Determine if explorer restart is needed once at startup
|
||||
needsExplorerRestart := isWindows11OrNewer()
|
||||
if !needsExplorerRestart {
|
||||
fmt.Println("Running on Windows 10 (or older), skipping explorer restart.")
|
||||
} else {
|
||||
fmt.Println("Running on Windows 11 (or newer), explorer restart enabled.")
|
||||
}
|
||||
|
||||
|
||||
for {
|
||||
hour := time.Now().Hour()
|
||||
@@ -95,7 +140,12 @@ func backgroundLoop() {
|
||||
|
||||
if newMode != lastMode {
|
||||
setMode(newMode == "light")
|
||||
restartExplorer()
|
||||
|
||||
// Conditional Explorer Restart ( Windows 10 or 11)
|
||||
if needsExplorerRestart {
|
||||
restartExplorer()
|
||||
}
|
||||
|
||||
lastMode = newMode
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user