Skip to content

Commit

Permalink
Actively follow windows theme (#2781)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marterich authored Sep 21, 2024
1 parent 395ac94 commit 42febae
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions functions/private/Invoke-WinUtilDarkMode.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ Function Invoke-WinUtilDarkMode {
Set-ItemProperty -Path $Path -Name AppsUseLightTheme -Value $DarkMoveValue
Set-ItemProperty -Path $Path -Name SystemUsesLightTheme -Value $DarkMoveValue
Invoke-WinUtilExplorerRefresh
# Update Winutil Theme if the Theme Button shows the Icon for Auto
if ($sync.ThemeButton.Content -eq [char]0xF08C) {
Invoke-WinutilThemeChange -theme "Auto"
}
} catch [System.Security.SecurityException] {
Write-Warning "Unable to set $Path\$Name to $Value due to a Security Exception"
} catch [System.Management.Automation.ItemNotFoundException] {
Expand Down
29 changes: 29 additions & 0 deletions scripts/main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,35 @@ if (-NOT ($readerOperationSuccessful)) {
[System.GC]::Collect()
exit 1
}

# Setup the Window to follow listen for windows Theme Change events and update the winutil theme
# throttle logic needed, because windows seems to send more than one theme change event per change
$lastThemeChangeTime = [datetime]::MinValue
$debounceInterval = [timespan]::FromSeconds(2)
$sync.Form.Add_Loaded({
$interopHelper = New-Object System.Windows.Interop.WindowInteropHelper $sync.Form
$hwndSource = [System.Windows.Interop.HwndSource]::FromHwnd($interopHelper.Handle)
$hwndSource.AddHook({
param (
[System.IntPtr]$hwnd,
[int]$msg,
[System.IntPtr]$wParam,
[System.IntPtr]$lParam,
[ref]$handled
)
# Check for the Event WM_SETTINGCHANGE (0x1001A) and validate that Button shows the icon for "Auto" => [char]0xF08C
if (($msg -eq 0x001A) -and $sync.ThemeButton.Content -eq [char]0xF08C) {
$currentTime = [datetime]::Now
if ($currentTime - $lastThemeChangeTime -gt $debounceInterval) {
Invoke-WinutilThemeChange -theme "Auto"
$script:lastThemeChangeTime = $currentTime
$handled = $true
}
}
return 0
})
})

Invoke-WinutilThemeChange -init $true
# Load the configuration files
#Invoke-WPFUIElements -configVariable $sync.configs.nav -targetGridName "WPFMainGrid"
Expand Down

0 comments on commit 42febae

Please sign in to comment.