227 lines
12 KiB
PowerShell
227 lines
12 KiB
PowerShell
param(
|
|
[string]$Executable = "$PSScriptRoot/../build/files-cmake/KSwitcher.Files.exe",
|
|
[string]$Screenshot
|
|
)
|
|
$ErrorActionPreference = 'Stop'
|
|
Add-Type @'
|
|
using System;
|
|
using System.Text;
|
|
using System.Runtime.InteropServices;
|
|
public static class FilesShortcutWindow {
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public struct Rect { public int Left, Top, Right, Bottom; }
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public struct Point { public int X, Y; }
|
|
[DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow();
|
|
[DllImport("user32.dll")] static extern bool SetForegroundWindow(IntPtr window);
|
|
[DllImport("user32.dll")] static extern void keybd_event(byte key, byte scan, uint flags, UIntPtr extra);
|
|
[DllImport("user32.dll")] public static extern IntPtr GetWindow(IntPtr window, uint command);
|
|
[DllImport("user32.dll")] public static extern IntPtr GetDlgItem(IntPtr window, int id);
|
|
[DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr window);
|
|
[DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr window, out Rect rect);
|
|
[DllImport("user32.dll")] public static extern uint GetDpiForWindow(IntPtr window);
|
|
[DllImport("user32.dll")] public static extern IntPtr SetThreadDpiAwarenessContext(IntPtr context);
|
|
[DllImport("user32.dll")] public static extern bool ScreenToClient(IntPtr window, ref Point point);
|
|
[DllImport("user32.dll", EntryPoint="SendMessageW")]
|
|
public static extern IntPtr Send(IntPtr window, uint message, IntPtr w, IntPtr l);
|
|
[DllImport("user32.dll", CharSet=CharSet.Unicode)]
|
|
public static extern IntPtr SendMessage(IntPtr window, uint msg, IntPtr w, StringBuilder text);
|
|
public static bool Activate(IntPtr window) {
|
|
if (GetForegroundWindow() == window) return true;
|
|
keybd_event(0x12, 0, 0, UIntPtr.Zero);
|
|
keybd_event(0x12, 0, 2, UIntPtr.Zero);
|
|
SetForegroundWindow(window);
|
|
return GetForegroundWindow() == window;
|
|
}
|
|
}
|
|
'@
|
|
function Wait-Until([scriptblock]$Condition) {
|
|
for ($attempt = 0; $attempt -lt 100; $attempt++) {
|
|
if (& $Condition) { return }
|
|
Start-Sleep -Milliseconds 50
|
|
}
|
|
throw "Timed out waiting for: $Condition"
|
|
}
|
|
function Read-Text($Control, $Message = 13, $Parameter = 2048) {
|
|
$buffer = [Text.StringBuilder]::new(2048)
|
|
[void][FilesShortcutWindow]::SendMessage($Control, $Message, [IntPtr]$Parameter, $buffer)
|
|
$buffer.ToString()
|
|
}
|
|
function Read-ShortcutDialog {
|
|
$foreground = [FilesShortcutWindow]::GetForegroundWindow()
|
|
if ([FilesShortcutWindow]::GetWindow($foreground, 4) -eq $window -and
|
|
[FilesShortcutWindow]::GetDlgItem($foreground, 310) -ne [IntPtr]::Zero) { return $foreground }
|
|
return [IntPtr]::Zero
|
|
}
|
|
|
|
$exe = (Resolve-Path $Executable).Path
|
|
$key = [Microsoft.Win32.Registry]::CurrentUser.CreateSubKey('Software\KSwitcher\Files')
|
|
$saved = @{}
|
|
$names = 'FolderShortcuts', 'LeftWide', 'RightWide', 'LeftPath', 'RightPath', 'LeftCursor', 'RightCursor', 'WindowPlacement'
|
|
foreach ($name in $names) {
|
|
if ($name -in $key.GetValueNames()) { $saved[$name] = @($key.GetValue($name), $key.GetValueKind($name)) }
|
|
}
|
|
$folder = [IO.Directory]::CreateTempSubdirectory('ks-files-shortcuts-').FullName
|
|
$previousDpi = [FilesShortcutWindow]::SetThreadDpiAwarenessContext([IntPtr](-4))
|
|
try {
|
|
$groups = [object[]]::new(10)
|
|
for ($digit = 0; $digit -lt 10; $digit++) { $groups[$digit] = @() }
|
|
foreach ($entry in @(@(3, 'Three'), @(4, 'Four'), @(8, 'Eight A'), @(8, 'Eight B'))) {
|
|
$path = [IO.Directory]::CreateDirectory((Join-Path $folder $entry[1])).FullName
|
|
$groups[$entry[0]] += @{ path = $path; peer = ''; name = ''; title = $entry[1] }
|
|
}
|
|
$groups[9] = @(1..200 | ForEach-Object {
|
|
@{ path = (Join-Path $folder "Folder $_"); peer = ''; name = ''; title = "Folder $_" }
|
|
})
|
|
$key.SetValue('FolderShortcuts', [Text.Encoding]::UTF8.GetBytes((ConvertTo-Json -InputObject $groups -Depth 4)), 'Binary')
|
|
foreach ($name in 'LeftWide', 'RightWide') { $key.SetValue($name, 1, 'DWord') }
|
|
foreach ($name in 'LeftPath', 'RightPath') { $key.SetValue($name, $folder, 'String') }
|
|
$key.DeleteValue('WindowPlacement', $false)
|
|
|
|
$process = Start-Process -FilePath $exe -PassThru
|
|
$keyboard = New-Object -ComObject WScript.Shell
|
|
Wait-Until { $process.Refresh(); $process.MainWindowHandle -ne [IntPtr]::Zero }
|
|
$window = $process.MainWindowHandle
|
|
Wait-Until { [FilesShortcutWindow]::Activate($window) }
|
|
$heading = [FilesShortcutWindow]::GetDlgItem($window, 140)
|
|
$rightHeading = [FilesShortcutWindow]::GetDlgItem($window, 141)
|
|
$leftPath = [FilesShortcutWindow]::GetDlgItem($window, 142)
|
|
Wait-Until { (Read-Text $leftPath) -eq $folder }
|
|
|
|
$keyboard.SendKeys('^8')
|
|
Wait-Until { (Read-ShortcutDialog) -ne [IntPtr]::Zero }
|
|
$dialog = Read-ShortcutDialog
|
|
$list = [FilesShortcutWindow]::GetDlgItem($dialog, 310)
|
|
$count = [FilesShortcutWindow]::SendMessage($list, 395, [IntPtr]::Zero, $null).ToInt32()
|
|
$rows = for ($row = 0; $row -lt $count; $row++) { Read-Text $list 393 $row }
|
|
if (($rows -join '|') -cne 'Eight A|Eight B') {
|
|
throw "Ctrl+8 should display only its two assigned folders. Actual rows: $($rows -join ' | ')"
|
|
}
|
|
if ($Screenshot) {
|
|
Add-Type -AssemblyName System.Drawing
|
|
$bounds = [FilesShortcutWindow+Rect]::new()
|
|
if (![FilesShortcutWindow]::GetWindowRect($dialog, [ref]$bounds)) { throw 'Cannot locate the rendered popup.' }
|
|
$bitmap = [Drawing.Bitmap]::new(($bounds.Right - $bounds.Left), ($bounds.Bottom - $bounds.Top))
|
|
try {
|
|
$graphics = [Drawing.Graphics]::FromImage($bitmap)
|
|
try { $graphics.CopyFromScreen($bounds.Left, $bounds.Top, 0, 0, $bitmap.Size) }
|
|
finally { $graphics.Dispose() }
|
|
$bitmap.Save($Screenshot, [Drawing.Imaging.ImageFormat]::Png)
|
|
} finally { $bitmap.Dispose() }
|
|
}
|
|
$keyboard.SendKeys('{ESC}')
|
|
Wait-Until { [FilesShortcutWindow]::GetForegroundWindow() -eq $window }
|
|
$keyboard.SendKeys('^4')
|
|
Wait-Until { (Read-Text $leftPath) -eq (Join-Path $folder 'Four') }
|
|
if (![FilesShortcutWindow]::IsWindowVisible($heading)) { throw 'Ctrl+4 changed panel mode.' }
|
|
$keyboard.SendKeys('^{TAB}')
|
|
$keyboard.SendKeys('^{F1}')
|
|
Wait-Until {
|
|
![FilesShortcutWindow]::IsWindowVisible($heading) -or
|
|
![FilesShortcutWindow]::IsWindowVisible($rightHeading)
|
|
}
|
|
if (![FilesShortcutWindow]::IsWindowVisible($rightHeading)) {
|
|
throw 'Ctrl+Tab changed the active panel.'
|
|
}
|
|
$keyboard.SendKeys('{TAB}')
|
|
$keyboard.SendKeys('^{F1}')
|
|
Wait-Until { ![FilesShortcutWindow]::IsWindowVisible($rightHeading) }
|
|
$keyboard.SendKeys('^{F2}')
|
|
Wait-Until { [FilesShortcutWindow]::IsWindowVisible($rightHeading) }
|
|
$keyboard.SendKeys('{TAB}')
|
|
Wait-Until { ![FilesShortcutWindow]::IsWindowVisible($heading) }
|
|
|
|
$rightPath = [FilesShortcutWindow]::GetDlgItem($window, 143)
|
|
$leftList = [FilesShortcutWindow]::GetDlgItem($window, 102)
|
|
$rightList = [FilesShortcutWindow]::GetDlgItem($window, 103)
|
|
$keyboard.SendKeys('^u')
|
|
Wait-Until {
|
|
(Read-Text ([FilesShortcutWindow]::GetDlgItem($window, 142))) -eq $folder -and
|
|
(Read-Text ([FilesShortcutWindow]::GetDlgItem($window, 143))) -eq (Join-Path $folder 'Four') -and
|
|
[FilesShortcutWindow]::IsWindowVisible($rightHeading)
|
|
}
|
|
$movedLeft = [FilesShortcutWindow+Rect]::new()
|
|
$movedRight = [FilesShortcutWindow+Rect]::new()
|
|
if (![FilesShortcutWindow]::GetWindowRect($rightList, [ref]$movedLeft) -or
|
|
![FilesShortcutWindow]::GetWindowRect($leftList, [ref]$movedRight) -or
|
|
$movedLeft.Right -gt $movedRight.Left -or
|
|
[FilesShortcutWindow]::IsWindowVisible($heading)) {
|
|
throw 'Ctrl+U did not move the visible panel lists with their modes.'
|
|
}
|
|
$keyboard.SendKeys('^3')
|
|
Wait-Until { (Read-Text $leftPath) -eq (Join-Path $folder 'Three') }
|
|
if ((Read-Text $rightPath) -ne $folder) { throw 'Ctrl+U did not preserve the active panel.' }
|
|
$keyboard.SendKeys('^u')
|
|
Wait-Until { [FilesShortcutWindow]::GetDlgItem($window, 142) -eq $leftPath }
|
|
$keyboard.SendKeys('^{F2}')
|
|
Wait-Until { [FilesShortcutWindow]::IsWindowVisible($heading) }
|
|
|
|
$keyboard.SendKeys('^9')
|
|
Wait-Until { (Read-ShortcutDialog) -ne [IntPtr]::Zero }
|
|
$dialog = Read-ShortcutDialog
|
|
$list = [FilesShortcutWindow]::GetDlgItem($dialog, 310)
|
|
Add-Type -AssemblyName System.Drawing
|
|
$bounds = [FilesShortcutWindow+Rect]::new()
|
|
if (![FilesShortcutWindow]::GetWindowRect($list, [ref]$bounds)) { throw 'Missing overflow list.' }
|
|
Start-Sleep -Milliseconds 150
|
|
$barWidth = [int][Math]::Round(12 * [FilesShortcutWindow]::GetDpiForWindow($list) / 96)
|
|
$bitmap = [Drawing.Bitmap]::new($barWidth, ($bounds.Bottom - $bounds.Top))
|
|
try {
|
|
$graphics = [Drawing.Graphics]::FromImage($bitmap)
|
|
try { $graphics.CopyFromScreen($bounds.Right, $bounds.Top, 0, 0, $bitmap.Size) }
|
|
finally { $graphics.Dispose() }
|
|
$background = $bitmap.GetPixel(0, 0)
|
|
$thumb = $bitmap.GetPixel(0, $barWidth + 1)
|
|
$trackColors = @(0..4 | ForEach-Object {
|
|
$x = $_
|
|
0..7 | ForEach-Object { $bitmap.GetPixel($x, [int]($bitmap.Height / 2) + $_).ToArgb() }
|
|
} | Sort-Object -Unique)
|
|
$hasArrow = {
|
|
param($y)
|
|
$center = [int][Math]::Floor($barWidth / 2)
|
|
@(0..($barWidth - 1) | Where-Object {
|
|
$bitmap.GetPixel($center, $y + $_).ToArgb() -eq $thumb.ToArgb()
|
|
}).Count -gt 0
|
|
}
|
|
if ($thumb.GetBrightness() -le $background.GetBrightness() -or $trackColors.Count -ne 2 -or
|
|
!(& $hasArrow 0) -or !(& $hasArrow ($bitmap.Height - $barWidth))) {
|
|
throw "Unexpected scrollbar pixels. Background $background, thumb $thumb, track colors $($trackColors.Count), arrows $(& $hasArrow 0)/$(& $hasArrow ($bitmap.Height - $barWidth))."
|
|
}
|
|
} finally { $bitmap.Dispose() }
|
|
$arrow = [FilesShortcutWindow+Point]::new()
|
|
$arrow.X = $bounds.Right + [int]($barWidth / 2)
|
|
$arrow.Y = $bounds.Bottom - [int]($barWidth / 2)
|
|
if (![FilesShortcutWindow]::ScreenToClient($dialog, [ref]$arrow)) { throw 'Cannot locate the down arrow.' }
|
|
$arrowPoint = [IntPtr](($arrow.Y -shl 16) -bor $arrow.X)
|
|
[void][FilesShortcutWindow]::Send($dialog, 0x0201, [IntPtr]1, $arrowPoint)
|
|
if ([FilesShortcutWindow]::Send($list, 0x018E, [IntPtr]::Zero, [IntPtr]::Zero).ToInt32() -ne 1) {
|
|
throw 'The down arrow should scroll the list by one row.'
|
|
}
|
|
$grip = [FilesShortcutWindow+Point]::new()
|
|
$grip.X = $bounds.Right + [int]($barWidth / 2)
|
|
$grip.Y = $bounds.Top + $barWidth + 4
|
|
if (![FilesShortcutWindow]::ScreenToClient($dialog, [ref]$grip)) { throw 'Cannot locate the thumb.' }
|
|
$gripPoint = [IntPtr](($grip.Y -shl 16) -bor $grip.X)
|
|
$dragPoint = [IntPtr]((($grip.Y + 3 * $barWidth) -shl 16) -bor $grip.X)
|
|
[void][FilesShortcutWindow]::Send($dialog, 0x0201, [IntPtr]1, $gripPoint)
|
|
[void][FilesShortcutWindow]::Send($dialog, 0x0200, [IntPtr]1, $dragPoint)
|
|
[void][FilesShortcutWindow]::Send($dialog, 0x0202, [IntPtr]::Zero, $dragPoint)
|
|
if ([FilesShortcutWindow]::Send($list, 0x018E, [IntPtr]::Zero, [IntPtr]::Zero).ToInt32() -le 1) {
|
|
throw 'Dragging the scrollbar thumb should advance the list.'
|
|
}
|
|
$keyboard.SendKeys('{ESC}')
|
|
Write-Output 'Verified folder shortcuts, panel swapping, and the rendered overflow scrollbar.'
|
|
} finally {
|
|
if ($previousDpi) { [void][FilesShortcutWindow]::SetThreadDpiAwarenessContext($previousDpi) }
|
|
if ($process -and !$process.HasExited) {
|
|
$keyboard.SendKeys('{ESC}')
|
|
if (!$process.CloseMainWindow() -or !$process.WaitForExit(2500)) { $process.Kill(); $process.WaitForExit() }
|
|
}
|
|
foreach ($name in $names) {
|
|
if ($saved.ContainsKey($name)) { $key.SetValue($name, $saved[$name][0], $saved[$name][1]) }
|
|
else { $key.DeleteValue($name, $false) }
|
|
}
|
|
$key.Dispose()
|
|
[IO.Directory]::Delete($folder, $true)
|
|
}
|