vdi-cloud/tools/set-desktop-responsiveness.ps1

53 lines
2.7 KiB
PowerShell

param([switch]$Restore)
# Run in the logged-in desktop user session, not the Guest Agent's Session 0.
$ErrorActionPreference = 'Stop'
$directory = Join-Path $env:LOCALAPPDATA 'VDI'
New-Item -ItemType Directory -Force $directory | Out-Null
$backup = Join-Path $directory 'desktop-responsiveness-before-20261103.json'
Add-Type @'
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
public static class VDIVisualResponse {
[StructLayout(LayoutKind.Sequential)] struct Animation { public uint size; public int enabled; }
[DllImport("user32.dll", EntryPoint="SystemParametersInfoW", SetLastError=true)]
static extern bool SpiAnimation(uint action, uint size, ref Animation data, uint flags);
[DllImport("user32.dll", EntryPoint="SystemParametersInfoW", SetLastError=true)]
static extern bool SpiGet(uint action, uint unused, out uint value, uint flags);
[DllImport("user32.dll", EntryPoint="SystemParametersInfoW", SetLastError=true)]
static extern bool SpiSet(uint action, uint value, IntPtr unused, uint flags);
static void Check(bool result) { if(!result) throw new Win32Exception(Marshal.GetLastWin32Error()); }
public static int AnimationEnabled() {
Animation data = new Animation { size=8 };
Check(SpiAnimation(0x0048,8,ref data,0)); return data.enabled;
}
public static uint MenuDelay() { uint value; Check(SpiGet(0x006A,0,out value,0)); return value; }
public static void Set(int animation, uint delay) {
Animation data = new Animation { size=8, enabled=animation };
Check(SpiAnimation(0x0049,8,ref data,3));
Check(SpiSet(0x006B,delay,IntPtr.Zero,3));
}
}
'@
$before = @{ animation=[VDIVisualResponse]::AnimationEnabled(); menuDelay=[VDIVisualResponse]::MenuDelay() }
if ($Restore) {
$target = Get-Content -LiteralPath $backup -Raw | ConvertFrom-Json
} else {
if (-not (Test-Path -LiteralPath $backup)) {
$before | ConvertTo-Json | Set-Content -LiteralPath $backup -Encoding UTF8
}
$target = @{ animation=0; menuDelay=100 }
}
try {
[VDIVisualResponse]::Set([int]$target.animation,[uint32]$target.menuDelay)
$after = @{ animation=[VDIVisualResponse]::AnimationEnabled(); menuDelay=[VDIVisualResponse]::MenuDelay() }
if ($after.animation -ne $target.animation -or $after.menuDelay -ne $target.menuDelay) {
throw 'Desktop preferences were not retained'
}
@{ before=$before; after=$after; backup=$backup; restored=[bool]$Restore } |
ConvertTo-Json -Depth 4 | Set-Content -LiteralPath (Join-Path $directory 'desktop-responsiveness-result.json') -Encoding UTF8
} catch {
[VDIVisualResponse]::Set([int]$before.animation,[uint32]$before.menuDelay)
throw
}