Update main.go

This commit is contained in:
Gabriel Kheisa
2025-11-26 08:09:26 +07:00
committed by GitHub
parent b0c38f4fda
commit e6fba958b3

52
main.go
View File

@@ -15,6 +15,42 @@ import (
"golang.org/x/sys/windows/registry" "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 // Set Windows dark/light mode via registry
func setMode(light bool) { func setMode(light bool) {
key, _ := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Themes\Personalize`, registry.SET_VALUE) key, _ := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Themes\Personalize`, registry.SET_VALUE)
@@ -81,6 +117,15 @@ func backgroundLoop() {
darkImg := findImageFile("dark") darkImg := findImageFile("dark")
lastMode := getCurrentMode() 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 { for {
hour := time.Now().Hour() hour := time.Now().Hour()
newMode := "dark" newMode := "dark"
@@ -95,7 +140,12 @@ func backgroundLoop() {
if newMode != lastMode { if newMode != lastMode {
setMode(newMode == "light") setMode(newMode == "light")
restartExplorer()
// Conditional Explorer Restart ( Windows 10 or 11)
if needsExplorerRestart {
restartExplorer()
}
lastMode = newMode lastMode = newMode
} }