Add warning about some changes requiring a reboot & fix focus loss after Explorer restart (#506)

This commit is contained in:
Jeffrey
2026-03-07 15:53:58 +01:00
committed by GitHub
parent 8956c41b4d
commit c72e4fcb54
5 changed files with 94 additions and 22 deletions

View File

@@ -7,6 +7,26 @@ function Show-ApplyModal {
)
Add-Type -AssemblyName PresentationFramework,PresentationCore,WindowsBase | Out-Null
# P/Invoke helpers for forcing focus back after Explorer restart
if (-not ([System.Management.Automation.PSTypeName]'Win11Debloat.FocusHelper').Type) {
Add-Type -Namespace Win11Debloat -Name FocusHelper -MemberDefinition @'
[DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr lpdwProcessId);
[DllImport("user32.dll")] public static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
[DllImport("kernel32.dll")] public static extern uint GetCurrentThreadId();
public static void ForceActivate(IntPtr hwnd) {
IntPtr fg = GetForegroundWindow();
uint fgThread = GetWindowThreadProcessId(fg, IntPtr.Zero);
uint myThread = GetCurrentThreadId();
if (fgThread != myThread) AttachThreadInput(myThread, fgThread, true);
SetForegroundWindow(hwnd);
if (fgThread != myThread) AttachThreadInput(myThread, fgThread, false);
}
'@
}
$usesDarkMode = GetSystemUsesDarkMode
@@ -55,6 +75,8 @@ function Show-ApplyModal {
$script:ApplyCompletionTitleEl = $applyWindow.FindName('ApplyCompletionTitle')
$script:ApplyCompletionMessageEl = $applyWindow.FindName('ApplyCompletionMessage')
$script:ApplyCompletionIconEl = $applyWindow.FindName('ApplyCompletionIcon')
$applyRebootPanel = $applyWindow.FindName('ApplyRebootPanel')
$applyRebootList = $applyWindow.FindName('ApplyRebootList')
$applyCloseBtn = $applyWindow.FindName('ApplyCloseBtn')
$applyKofiBtn = $applyWindow.FindName('ApplyKofiBtn')
$applyCancelBtn = $applyWindow.FindName('ApplyCancelBtn')
@@ -104,6 +126,13 @@ function Show-ApplyModal {
# Restart explorer if requested
if ($RestartExplorer -and -not $script:CancelRequested) {
RestartExplorer
# Wait for Explorer to finish relaunching, then reclaim focus.
Start-Sleep -Milliseconds 800
$applyWindow.Dispatcher.Invoke([action]{
$hwnd = (New-Object System.Windows.Interop.WindowInteropHelper($applyWindow)).Handle
[Win11Debloat.FocusHelper]::ForceActivate($hwnd)
})
}
Write-Host ""
@@ -125,7 +154,33 @@ function Show-ApplyModal {
$script:ApplyCompletionMessageEl.Text = "Script execution was cancelled by the user."
} else {
$script:ApplyCompletionTitleEl.Text = "Changes Applied"
$script:ApplyCompletionMessageEl.Text = "Your clean system is ready. Thanks for using Win11Debloat!"
# Show completion message with reboot instructions if any applied features require reboot
if ($RestartExplorer) {
$rebootFeatures = @()
foreach ($paramKey in $script:Params.Keys) {
if ($script:Features.ContainsKey($paramKey) -and $script:Features[$paramKey].RequiresReboot -eq $true) {
$feature = $script:Features[$paramKey]
$rebootFeatures += "$($feature.Action) $($feature.Label)"
}
}
if ($rebootFeatures.Count -gt 0) {
foreach ($featureName in $rebootFeatures) {
$tb = [System.Windows.Controls.TextBlock]::new()
$tb.Text = "$([char]0x2022) $featureName"
$tb.FontSize = 12
$tb.SetResourceReference([System.Windows.Controls.TextBlock]::ForegroundProperty, 'FgColor')
$tb.Opacity = 0.85
$tb.Margin = [System.Windows.Thickness]::new(0, 2, 0, 0)
$applyRebootList.Children.Add($tb) | Out-Null
}
$applyRebootPanel.Visibility = 'Visible'
}
else {
$script:ApplyCompletionMessageEl.Text = "Your clean system is ready. Thanks for using Win11Debloat!"
}
}
}
$applyWindow.Dispatcher.Invoke([System.Windows.Threading.DispatcherPriority]::Render, [action]{})
}