From ff91167af4a9d1471fc31ff11cf0fd6f06505d31 Mon Sep 17 00:00:00 2001 From: MyDrift Date: Thu, 7 Nov 2024 19:36:43 +0100 Subject: [PATCH] update Explorerrefresh - change Invoke-WinUtilExplorerRefresh to handle refresh and restart - add restart logic to window snapping Flyout & Suggestions - rename Invoke-WinUtilExplorerRefresh to Invoke-WinUtilExplorerUpdate --- config/tweaks.json | 24 ++++++++++- .../private/Invoke-WinUtilExplorerRefresh.ps1 | 33 -------------- .../private/Invoke-WinUtilExplorerUpdate.ps1 | 43 +++++++++++++++++++ 3 files changed, 65 insertions(+), 35 deletions(-) delete mode 100644 functions/private/Invoke-WinUtilExplorerRefresh.ps1 create mode 100644 functions/private/Invoke-WinUtilExplorerUpdate.ps1 diff --git a/config/tweaks.json b/config/tweaks.json index 200e39d8..10e32103 100644 --- a/config/tweaks.json +++ b/config/tweaks.json @@ -3311,12 +3311,12 @@ ], "InvokeScript": [ " - Invoke-WinUtilExplorerRefresh + Invoke-WinUtilExplorerUpdate " ], "UndoScript": [ " - Invoke-WinUtilExplorerRefresh + Invoke-WinUtilExplorerUpdate " ], "link": "https://christitustech.github.io/winutil/dev/tweaks/Customize-Preferences/DarkMode" @@ -3416,6 +3416,16 @@ "Type": "DWord" } ], + "InvokeScript": [ + " + Invoke-WinUtilExplorerUpdate -action \"restart\" + " + ], + "UndoScript": [ + " + Invoke-WinUtilExplorerUpdate -action \"restart\" + " + ], "link": "https://christitustech.github.io/winutil/dev/tweaks/Customize-Preferences/SnapFlyout" }, "WPFToggleSnapSuggestion": { @@ -3434,6 +3444,16 @@ "Type": "DWord" } ], + "InvokeScript": [ + " + Invoke-WinUtilExplorerUpdate -action \"restart\" + " + ], + "UndoScript": [ + " + Invoke-WinUtilExplorerUpdate -action \"restart\" + " + ], "link": "https://christitustech.github.io/winutil/dev/tweaks/Customize-Preferences/SnapSuggestion" }, "WPFToggleMouseAcceleration": { diff --git a/functions/private/Invoke-WinUtilExplorerRefresh.ps1 b/functions/private/Invoke-WinUtilExplorerRefresh.ps1 deleted file mode 100644 index 76041169..00000000 --- a/functions/private/Invoke-WinUtilExplorerRefresh.ps1 +++ /dev/null @@ -1,33 +0,0 @@ -function Invoke-WinUtilExplorerRefresh { - <# - .SYNOPSIS - Refreshes the Windows Explorer - #> - - Invoke-WPFRunspace -DebugPreference $DebugPreference -ScriptBlock { - # Send the WM_SETTINGCHANGE message to all windows - Add-Type -TypeDefinition @" -using System; -using System.Runtime.InteropServices; -public class Win32 { - [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] - public static extern IntPtr SendMessageTimeout( - IntPtr hWnd, - uint Msg, - IntPtr wParam, - string lParam, - uint fuFlags, - uint uTimeout, - out IntPtr lpdwResult); -} -"@ - - $HWND_BROADCAST = [IntPtr]0xffff - $WM_SETTINGCHANGE = 0x1A - $SMTO_ABORTIFHUNG = 0x2 - $timeout = 100 - - # Send the broadcast message to all windows - [Win32]::SendMessageTimeout($HWND_BROADCAST, $WM_SETTINGCHANGE, [IntPtr]::Zero, "ImmersiveColorSet", $SMTO_ABORTIFHUNG, $timeout, [ref]([IntPtr]::Zero)) - } -} diff --git a/functions/private/Invoke-WinUtilExplorerUpdate.ps1 b/functions/private/Invoke-WinUtilExplorerUpdate.ps1 new file mode 100644 index 00000000..5984f7a8 --- /dev/null +++ b/functions/private/Invoke-WinUtilExplorerUpdate.ps1 @@ -0,0 +1,43 @@ +function Invoke-WinUtilExplorerUpdate { + <# + .SYNOPSIS + Refreshes the Windows Explorer + #> + + param ( + [string]$action = "refresh" + ) + + if ($action -eq "refresh") { + Invoke-WPFRunspace -DebugPreference $DebugPreference -ScriptBlock { + # Send the WM_SETTINGCHANGE message to all windows + Add-Type -TypeDefinition @" +using System; +using System.Runtime.InteropServices; +public class Win32 { + [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] + public static extern IntPtr SendMessageTimeout( + IntPtr hWnd, + uint Msg, + IntPtr wParam, + string lParam, + uint fuFlags, + uint uTimeout, + out IntPtr lpdwResult); +} +"@ + + $HWND_BROADCAST = [IntPtr]0xffff + $WM_SETTINGCHANGE = 0x1A + $SMTO_ABORTIFHUNG = 0x2 + $timeout = 100 + + # Send the broadcast message to all windows + [Win32]::SendMessageTimeout($HWND_BROADCAST, $WM_SETTINGCHANGE, [IntPtr]::Zero, "ImmersiveColorSet", $SMTO_ABORTIFHUNG, $timeout, [ref]([IntPtr]::Zero)) + } + } elseif ($action -eq "restart") { + # Restart the Windows Explorer + taskkill.exe /F /IM "explorer.exe" + Start-Process "explorer.exe" + } +}