Repository navigation
Expand file tree
/
Copy pathExplorerGridLayout.cs
More file actions
335 lines (311 loc) · 17.9 KB
/
Copy pathExplorerGridLayout.cs
File metadata and controls
335 lines (311 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Windows.Automation;
namespace QuickPreview;
internal sealed record ExplorerGridSnapshot(
int FocusedIndex,
int Rows,
int Columns,
int FocusedRow,
int FocusedColumn,
int? TargetIndex,
string SourceName,
string? TargetName,
bool OffscreenStrideSafe = true,
bool IndicesAreGlobal = true,
string? SourceGroupName = null);
/// <summary>
/// Reads the active Explorer view's exposed grid layout. Selection is committed
/// separately through the native Shell API; UI Automation is never used for input.
/// </summary>
internal static class ExplorerGridLayout
{
private static readonly TimeSpan ProbeTimeout = TimeSpan.FromMilliseconds(1000);
private static int _probeInFlight;
internal static ExplorerGridSnapshot? Read(IntPtr shellViewWindow, int focusedIndex, NavigationDirection direction,
string? expectedSourceName = null)
{
if (focusedIndex < 0 || direction is not (NavigationDirection.Left or NavigationDirection.Up or
NavigationDirection.Right or NavigationDirection.Down) ||
!TryGetExternalWindowProcess(shellViewWindow, out var processId))
return null;
// A provider call cannot be forcibly cancelled. If one hangs, retain the
// gate until it actually finishes rather than accumulating blocked tasks.
if (Interlocked.CompareExchange(ref _probeInFlight, 1, 0) != 0)
return null;
var elapsed = Stopwatch.StartNew();
Task<ExplorerGridSnapshot?> probe;
try
{
probe = Task.Run(() =>
{
try
{
// The thread pool is MTA. Keep every UIA object on this worker
// and return only a small immutable data snapshot to the caller.
return ReadCore(shellViewWindow, processId, focusedIndex, direction, expectedSourceName, elapsed);
}
catch (Exception exception)
{
// Stale elements, unsupported patterns, COM/provider failures
// and unavailable accessibility providers all fail closed.
Debug.WriteLine($"QuickPreview Explorer grid read: {exception.GetType().Name}");
return null;
}
finally { Volatile.Write(ref _probeInFlight, 0); }
});
}
catch (Exception exception)
{
Volatile.Write(ref _probeInFlight, 0);
Debug.WriteLine($"QuickPreview Explorer grid worker: {exception.GetType().Name}");
return null;
}
try
{
var remaining = ProbeTimeout - elapsed.Elapsed;
return remaining > TimeSpan.Zero && probe.Wait(remaining) ? probe.GetAwaiter().GetResult() : null;
}
catch (Exception exception)
{
Debug.WriteLine($"QuickPreview Explorer grid wait: {exception.GetType().Name}");
return null;
}
}
private static ExplorerGridSnapshot? ReadCore(IntPtr shellViewWindow, uint processId, int focusedIndex,
NavigationDirection direction, string? expectedSourceName, Stopwatch elapsed)
{
if (Thread.CurrentThread.GetApartmentState() != ApartmentState.MTA || Expired() ||
!TryGetExternalWindowProcess(shellViewWindow, out var currentProcess) || currentProcess != processId)
return null;
var root = AutomationElement.FromHandle(shellViewWindow);
if (root is null || Expired()) return null;
var rootInfo = root.Current;
if (rootInfo.ProcessId != processId || rootInfo.ProcessId == Environment.ProcessId ||
rootInfo.NativeWindowHandle != unchecked((int)shellViewWindow.ToInt64()))
return null;
var selectedCondition = new AndCondition(
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ListItem),
new PropertyCondition(SelectionItemPattern.IsSelectedProperty, true),
new PropertyCondition(AutomationElement.ProcessIdProperty, (int)processId));
var selected = FindCached(root, selectedCondition);
AutomationElement? source = null;
var sourceMatches = 0;
for (var index = 0; index < Math.Min(selected.Count, 256); index++)
{
if (Expired()) return null;
var candidate = selected[index];
var name = candidate.Cached.Name;
if (!string.IsNullOrWhiteSpace(expectedSourceName) &&
!string.Equals(name, expectedSourceName, StringComparison.OrdinalIgnoreCase)) continue;
source = candidate;
sourceMatches++;
}
// Grouped Explorer resets AutomationId within each group. A unique selected
// name, validated against the native focused item by the caller, is the anchor.
if (sourceMatches != 1 || selected.Count > 256) return null;
if (source is null || Expired()) return null;
var sourceInfo = source.Cached;
if (sourceInfo.ControlType != ControlType.ListItem || sourceInfo.ProcessId != processId ||
string.IsNullOrWhiteSpace(sourceInfo.Name) ||
!source.TryGetCurrentPattern(SelectionItemPattern.Pattern, out var selectionObject) ||
selectionObject is not SelectionItemPattern selection || !selection.Current.IsSelected || Expired())
return null;
var hasSourceIndex = TryIndex(sourceInfo.AutomationId, out var actualIndex);
if (!source.TryGetCurrentPattern(GridItemPattern.Pattern, out var itemObject) ||
itemObject is not GridItemPattern item || Expired()) return null;
var cell = item.Current;
var gridElement = cell.ContainingGrid;
if (Expired()) return null;
if (gridElement is null)
return ReadVisibleFallback(root, processId, focusedIndex, direction, sourceInfo.Name,
knownGrouped: false, sourceGroupName: null, elapsed);
var gridInfo = gridElement.Current;
var grouped = gridInfo.ControlType == ControlType.Group;
var sourceGroupName = grouped ? gridInfo.Name : null;
if (gridInfo.ProcessId != processId ||
!gridElement.TryGetCurrentPattern(GridPattern.Pattern, out var gridObject) ||
gridObject is not GridPattern grid || Expired())
return ReadVisibleFallback(root, processId, focusedIndex, direction, sourceInfo.Name,
grouped, sourceGroupName, elapsed);
var dimensions = grid.Current;
if (dimensions.RowCount <= 0 || dimensions.ColumnCount <= 0 ||
cell.Row < 0 || cell.Row >= dimensions.RowCount || cell.Column < 0 || cell.Column >= dimensions.ColumnCount)
return null;
var targetRow = cell.Row;
var targetColumn = cell.Column;
switch (direction)
{
case NavigationDirection.Left: targetColumn--; break;
case NavigationDirection.Up: targetRow--; break;
case NavigationDirection.Right: targetColumn++; break;
case NavigationDirection.Down: targetRow++; break;
}
int? targetIndex = null;
string? targetName = null;
if (targetRow >= 0 && targetRow < dimensions.RowCount && targetColumn >= 0 && targetColumn < dimensions.ColumnCount && !Expired())
{
try
{
// GetItem is a query only. Do not Realize/ScrollIntoView/Select or
// SetFocus: those actions can activate Explorer or move its view.
var target = grid.GetItem(targetRow, targetColumn);
if (target is not null && !Expired())
{
var targetInfo = target.Current;
if (targetInfo.ControlType == ControlType.ListItem && targetInfo.ProcessId == processId &&
TryIndex(targetInfo.AutomationId, out var index) && (!hasSourceIndex || index != actualIndex) &&
!string.IsNullOrWhiteSpace(targetInfo.Name) &&
target.TryGetCurrentPattern(SelectionItemPattern.Pattern, out var targetSelection) &&
targetSelection is SelectionItemPattern &&
target.TryGetCurrentPattern(GridItemPattern.Pattern, out var targetCellObject) &&
targetCellObject is GridItemPattern targetCell && !Expired())
{
var targetPosition = targetCell.Current;
if (targetPosition.Row == targetRow && targetPosition.Column == targetColumn &&
Automation.Compare(targetPosition.ContainingGrid, gridElement))
{
targetIndex = index;
targetName = targetInfo.Name;
}
}
}
}
catch (Exception exception) when (exception is ElementNotAvailableException or InvalidOperationException or
ArgumentException or COMException or NotSupportedException)
{
// Virtualized/unrealized cells can throw instead of returning an
// element. Preserve the credible lane dimensions for native fallback.
}
}
if (Expired() || !TryGetExternalWindowProcess(shellViewWindow, out currentProcess) || currentProcess != processId)
return null;
if (grouped && targetIndex is null)
{
// At a group boundary the next visible row can belong to another grid.
var adjacentGroup = ReadVisibleFallback(root, processId, focusedIndex, direction, sourceInfo.Name,
knownGrouped: true, sourceGroupName, elapsed);
if (adjacentGroup?.TargetIndex is not null) return adjacentGroup;
}
var nativeIndices = !grouped && hasSourceIndex && actualIndex == focusedIndex;
return new ExplorerGridSnapshot(focusedIndex, dimensions.RowCount, dimensions.ColumnCount,
cell.Row, cell.Column, targetIndex, sourceInfo.Name, targetName,
OffscreenStrideSafe: nativeIndices, IndicesAreGlobal: nativeIndices, SourceGroupName: sourceGroupName);
bool Expired() => elapsed.Elapsed >= ProbeTimeout;
}
private static AutomationElementCollection FindCached(AutomationElement root, System.Windows.Automation.Condition condition)
{
var cache = new CacheRequest { TreeScope = TreeScope.Element, AutomationElementMode = AutomationElementMode.Full };
cache.Add(AutomationElement.ControlTypeProperty);
cache.Add(AutomationElement.AutomationIdProperty);
cache.Add(AutomationElement.NameProperty);
cache.Add(AutomationElement.ProcessIdProperty);
cache.Add(AutomationElement.BoundingRectangleProperty);
cache.Add(AutomationElement.IsSelectionItemPatternAvailableProperty);
cache.Add(SelectionItemPattern.IsSelectedProperty);
cache.Add(GridItemPattern.RowProperty);
cache.Add(GridItemPattern.ColumnProperty);
using (cache.Activate()) return root.FindAll(TreeScope.Descendants, condition);
}
private static ExplorerGridSnapshot? ReadVisibleFallback(AutomationElement root, uint processId, int focusedIndex,
NavigationDirection direction, string sourceName, bool knownGrouped, string? sourceGroupName, Stopwatch elapsed)
{
if (elapsed.Elapsed >= ProbeTimeout) return null;
var elements = FindCached(root, new AndCondition(
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ListItem),
new PropertyCondition(AutomationElement.ProcessIdProperty, (int)processId)));
if (elapsed.Elapsed >= ProbeTimeout || elements.Count > 4096) return null;
var cells = new List<VisibleCell>();
for (var index = 0; index < elements.Count; index++)
{
if (elapsed.Elapsed >= ProbeTimeout) return null;
var element = elements[index];
var info = element.Cached;
if (info.ControlType != ControlType.ListItem || info.ProcessId != processId ||
!TryIndex(info.AutomationId, out var itemIndex) || string.IsNullOrWhiteSpace(info.Name) ||
element.GetCachedPropertyValue(AutomationElement.IsSelectionItemPatternAvailableProperty, true) is not true ||
element.GetCachedPropertyValue(GridItemPattern.RowProperty, true) is not int row || row < 0 || row == int.MaxValue ||
element.GetCachedPropertyValue(GridItemPattern.ColumnProperty, true) is not int column || column < 0 || column == int.MaxValue)
continue;
var isSelected = element.GetCachedPropertyValue(SelectionItemPattern.IsSelectedProperty, true) is true;
cells.Add(new VisibleCell(itemIndex, row, column, info.Name, info.BoundingRectangle, isSelected));
}
var sources = cells.Where(cell => cell.IsSelected &&
string.Equals(cell.Name, sourceName, StringComparison.OrdinalIgnoreCase)).Take(2).ToArray();
if (sources.Length != 1) return null;
var source = sources[0];
var duplicateIds = cells.Select(cell => cell.Index).Distinct().Count() != cells.Count;
var duplicateCoordinates = cells.Select(cell => (cell.Row, cell.Column)).Distinct().Count() != cells.Count;
var ambiguous = knownGrouped || duplicateIds || duplicateCoordinates;
var targetRow = source.Row + (direction == NavigationDirection.Up ? -1 : direction == NavigationDirection.Down ? 1 : 0);
var targetColumn = source.Column + (direction == NavigationDirection.Left ? -1 : direction == NavigationDirection.Right ? 1 : 0);
VisibleCell? target = null;
if (!ambiguous)
{
target = cells.FirstOrDefault(cell => cell.Row == targetRow && cell.Column == targetColumn &&
IsInDirection(source.Bounds, cell.Bounds, direction));
}
// Repeated group-local row/column coordinates cannot identify another file.
// Use only visible physical neighbors, never UIA selection/scroll/realization.
target ??= FindSpatialNeighbor(cells, source, direction);
var globalIndices = !ambiguous && source.Index == focusedIndex;
return new ExplorerGridSnapshot(focusedIndex, cells.Max(cell => cell.Row) + 1, cells.Max(cell => cell.Column) + 1,
source.Row, source.Column, target?.Index, source.Name, target?.Name,
OffscreenStrideSafe: globalIndices, IndicesAreGlobal: globalIndices, SourceGroupName: sourceGroupName);
}
private static VisibleCell? FindSpatialNeighbor(IReadOnlyList<VisibleCell> cells, VisibleCell source, NavigationDirection direction)
{
if (!UsableBounds(source.Bounds)) return null;
var horizontal = direction is NavigationDirection.Left or NavigationDirection.Right;
var candidates = cells.Where(cell => !ReferenceEquals(cell, source) &&
IsInDirection(source.Bounds, cell.Bounds, direction)).ToArray();
if (horizontal)
{
return candidates.Where(cell => Math.Abs(CenterY(cell.Bounds) - CenterY(source.Bounds)) <=
Math.Max(4, Math.Min(source.Bounds.Height, cell.Bounds.Height) * 0.4))
.OrderBy(cell => Math.Abs(CenterX(cell.Bounds) - CenterX(source.Bounds)))
.ThenBy(cell => Math.Abs(CenterY(cell.Bounds) - CenterY(source.Bounds)))
.FirstOrDefault();
}
if (candidates.Length == 0) return null;
var nearestRow = candidates.Min(cell => Math.Abs(CenterY(cell.Bounds) - CenterY(source.Bounds)));
return candidates.Where(cell => Math.Abs(CenterY(cell.Bounds) - CenterY(source.Bounds)) <= nearestRow +
Math.Max(4, Math.Min(source.Bounds.Height, cell.Bounds.Height) * 0.35))
.OrderBy(cell => Math.Abs(CenterX(cell.Bounds) - CenterX(source.Bounds)))
.ThenBy(cell => Math.Abs(CenterY(cell.Bounds) - CenterY(source.Bounds)))
.FirstOrDefault();
}
private static bool IsInDirection(System.Windows.Rect source, System.Windows.Rect target, NavigationDirection direction)
{
if (!UsableBounds(source) || !UsableBounds(target)) return false;
var dx = CenterX(target) - CenterX(source);
var dy = CenterY(target) - CenterY(source);
return direction switch
{
NavigationDirection.Left => dx < -Math.Max(1, Math.Min(source.Width, target.Width) * 0.1),
NavigationDirection.Right => dx > Math.Max(1, Math.Min(source.Width, target.Width) * 0.1),
NavigationDirection.Up => dy < -Math.Max(4, Math.Min(source.Height, target.Height) * 0.4),
NavigationDirection.Down => dy > Math.Max(4, Math.Min(source.Height, target.Height) * 0.4),
_ => false
};
}
private static bool UsableBounds(System.Windows.Rect bounds) => !bounds.IsEmpty && bounds.Width > 0 && bounds.Height > 0 &&
double.IsFinite(bounds.X) && double.IsFinite(bounds.Y) && double.IsFinite(bounds.Width) && double.IsFinite(bounds.Height);
private static double CenterX(System.Windows.Rect bounds) => bounds.X + bounds.Width / 2;
private static double CenterY(System.Windows.Rect bounds) => bounds.Y + bounds.Height / 2;
private sealed record VisibleCell(int Index, int Row, int Column, string Name, System.Windows.Rect Bounds, bool IsSelected);
private static bool TryIndex(string? value, out int index) =>
int.TryParse(value, NumberStyles.None, CultureInfo.InvariantCulture, out index) && index >= 0;
private static bool TryGetExternalWindowProcess(IntPtr window, out uint processId)
{
processId = 0;
return window != IntPtr.Zero && IsWindow(window) &&
GetWindowThreadProcessId(window, out processId) != 0 && processId != 0 && processId != Environment.ProcessId;
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWindow(IntPtr window);
[DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(IntPtr window, out uint processId);
}