Repository navigation
Expand file tree
/
Copy pathMainPage.Paths.cs
More file actions
562 lines (471 loc) · 14.7 KB
/
Copy pathMainPage.Paths.cs
File metadata and controls
562 lines (471 loc) · 14.7 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Windows.Storage.Pickers;
using WinRT.Interop;
using SDeleteGUI.Models;
using SDeleteGUI.Services;
namespace SDeleteGUI;
public sealed partial class MainPage
{
private async void AddFiles_Click(object sender, RoutedEventArgs e)
{
var picker = new FileOpenPicker { SuggestedStartLocation = PickerLocationId.ComputerFolder };
picker.FileTypeFilter.Add("*");
InitializeWithWindow.Initialize(picker, GetWindowHandle());
var files = await picker.PickMultipleFilesAsync();
foreach (var file in files)
{
if (_pathEntries.Any(p => string.Equals(p.Path, file.Path, StringComparison.OrdinalIgnoreCase)))
continue;
AddPathRow(file.Path, isDirectory: false);
}
await ShowBlockedPathsWarningAsync();
}
private async void AddFolder_Click(object sender, RoutedEventArgs e)
{
var picker = new FolderPicker { SuggestedStartLocation = PickerLocationId.ComputerFolder };
picker.FileTypeFilter.Add("*");
InitializeWithWindow.Initialize(picker, GetWindowHandle());
var folder = await picker.PickSingleFolderAsync();
if (folder != null)
{
if (!_pathEntries.Any(p => string.Equals(p.Path, folder.Path, StringComparison.OrdinalIgnoreCase)))
AddPathRow(folder.Path, isDirectory: true);
await ShowBlockedPathsWarningAsync();
}
}
private bool AddPathRow(string path, bool isDirectory, bool contentsOnly = false)
{
if (SystemPathGuard.IsProtected(path))
{
_blockedPaths.Add(path);
return false;
}
var entry = new PathEntry { Path = path, IsDirectory = isDirectory, ContentsOnly = contentsOnly };
_pathEntries.Add(entry);
var row = BuildPathRow(entry);
PathsPanel.Children.Add(row);
UpdateEmptyPathsHint();
RefreshCommandPreview();
RecalculatePathStats();
return true;
}
private async Task ShowBlockedPathsWarningAsync()
{
if (_blockedPaths.Count == 0 || XamlRoot is null)
return;
var blocked = _blockedPaths.Distinct(StringComparer.OrdinalIgnoreCase).ToList();
_blockedPaths.Clear();
const int maxListed = 5;
var details = string.Join(Environment.NewLine, blocked.Take(maxListed));
if (blocked.Count > maxListed)
details += Environment.NewLine + Loc.Format("BlockedPathsMore", blocked.Count - maxListed);
var content = new StackPanel { Spacing = 12 };
content.Children.Add(new TextBlock
{
Text = Loc.Get("BlockedPathsText"),
TextWrapping = TextWrapping.Wrap
});
content.Children.Add(new TextBlock
{
Text = details,
TextWrapping = TextWrapping.Wrap,
FontFamily = new FontFamily("Consolas"),
FontSize = 12,
IsTextSelectionEnabled = true,
Foreground = (Brush)Application.Current.Resources["TextFillColorSecondaryBrush"]
});
var dialog = new ContentDialog
{
XamlRoot = XamlRoot,
Title = Loc.Get("BlockedPathsTitle"),
Content = content,
CloseButtonText = Loc.Get("BlockedPathsClose"),
DefaultButton = ContentDialogButton.Close
};
await dialog.ShowAsync();
}
private void ClearPathsButton_Click(object sender, RoutedEventArgs e)
{
_pathEntries.Clear();
_pathRowControls.Clear();
for (var i = PathsPanel.Children.Count - 1; i >= 0; i--)
{
if (!ReferenceEquals(PathsPanel.Children[i], EmptyPathsHint))
PathsPanel.Children.RemoveAt(i);
}
UpdateEmptyPathsHint();
RefreshCommandPreview();
RecalculatePathStats();
}
private void RootGrid_DragOver(object sender, DragEventArgs e)
{
if (_isRunning ||
!e.DataView.Contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.StorageItems))
{
e.AcceptedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.None;
return;
}
e.AcceptedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Copy;
e.DragUIOverride.Caption = Loc.Get("DropToAddCaption");
}
private async void RootGrid_Drop(object sender, DragEventArgs e)
{
if (_isRunning ||
!e.DataView.Contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.StorageItems))
return;
var deferral = e.GetDeferral();
try
{
var items = await e.DataView.GetStorageItemsAsync();
var added = false;
foreach (var item in items)
{
if (string.IsNullOrEmpty(item.Path))
continue;
if (_pathEntries.Any(p => string.Equals(p.Path, item.Path, StringComparison.OrdinalIgnoreCase)))
continue;
AddPathRow(item.Path, isDirectory: item.IsOfType(Windows.Storage.StorageItemTypes.Folder));
added = true;
}
if (added)
MainPivot.SelectedIndex = 0;
}
finally
{
deferral.Complete();
}
await ShowBlockedPathsWarningAsync();
}
private FrameworkElement BuildPathRow(PathEntry entry)
{
var grid = new Grid { ColumnSpacing = 8 };
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
var iconFallback = new FontIcon
{
Glyph = entry.IsDirectory ? "\uE8B7" : "\uE8A5",
FontSize = 20,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
var iconImage = new Image { Width = 24, Height = 24, Visibility = Visibility.Collapsed };
var iconHost = new Grid { Width = 24, Height = 24, VerticalAlignment = VerticalAlignment.Center };
iconHost.Children.Add(iconFallback);
iconHost.Children.Add(iconImage);
var iconButton = new Button
{
Style = (Style)Resources["PathIconButtonStyle"],
Content = iconHost
};
ToolTipService.SetToolTip(iconButton, Loc.Get("OpenPathToolTip"));
iconButton.Click += (_, _) => OpenPathInExplorer(entry);
Grid.SetColumn(iconButton, 0);
_ = LoadPathIconAsync(entry, iconImage, iconFallback);
var pathTextBox = new TextBox
{
Text = entry.Path,
IsReadOnly = true,
VerticalAlignment = VerticalAlignment.Center
};
Grid.SetColumn(pathTextBox, 1);
var contentsOnlyCheckBox = new CheckBox
{
Content = Loc.Get("ContentsOnlyCheckBox"),
IsChecked = entry.ContentsOnly,
VerticalAlignment = VerticalAlignment.Center,
Visibility = entry.IsDirectory ? Visibility.Visible : Visibility.Collapsed
};
contentsOnlyCheckBox.Checked += (_, _) => { entry.ContentsOnly = true; RefreshCommandPreview(); };
contentsOnlyCheckBox.Unchecked += (_, _) => { entry.ContentsOnly = false; RefreshCommandPreview(); };
Grid.SetColumn(contentsOnlyCheckBox, 2);
var changeButton = new Button { Content = Loc.Get("ChangePathButton") };
changeButton.Click += async (_, _) =>
await ChangePathAsync(entry, pathTextBox, contentsOnlyCheckBox, iconImage, iconFallback);
Grid.SetColumn(changeButton, 3);
var removeButton = new Button { Content = "✕" };
removeButton.Click += (_, _) => RemovePathRow(entry, grid);
Grid.SetColumn(removeButton, 4);
grid.Children.Add(iconButton);
grid.Children.Add(pathTextBox);
grid.Children.Add(contentsOnlyCheckBox);
grid.Children.Add(changeButton);
grid.Children.Add(removeButton);
_pathRowControls[entry] = new PathRowControls
{
Row = grid,
PathTextBox = pathTextBox,
ContentsOnlyCheckBox = contentsOnlyCheckBox,
ChangeButton = changeButton,
IconButton = iconButton
};
if (entry.IsDeleted)
SetRowDeletedVisual(entry, true);
return grid;
}
private async Task ChangePathAsync(
PathEntry entry,
TextBox pathTextBox,
CheckBox contentsOnlyCheckBox,
Image iconImage,
FontIcon iconFallback)
{
string? newPath;
bool isDirectory = entry.IsDirectory;
if (isDirectory)
{
var picker = new FolderPicker { SuggestedStartLocation = PickerLocationId.ComputerFolder };
picker.FileTypeFilter.Add("*");
InitializeWithWindow.Initialize(picker, GetWindowHandle());
var folder = await picker.PickSingleFolderAsync();
if (folder is null) return;
newPath = folder.Path;
}
else
{
var picker = new FileOpenPicker { SuggestedStartLocation = PickerLocationId.ComputerFolder };
picker.FileTypeFilter.Add("*");
InitializeWithWindow.Initialize(picker, GetWindowHandle());
var file = await picker.PickSingleFileAsync();
if (file is null) return;
newPath = file.Path;
}
if (SystemPathGuard.IsProtected(newPath))
{
_blockedPaths.Add(newPath);
await ShowBlockedPathsWarningAsync();
return;
}
if (_pathEntries.Any(p => !ReferenceEquals(p, entry) &&
string.Equals(p.Path, newPath, StringComparison.OrdinalIgnoreCase)))
{
return;
}
entry.Path = newPath;
entry.ContentsOnly = false;
pathTextBox.Text = newPath;
contentsOnlyCheckBox.IsChecked = false;
contentsOnlyCheckBox.Visibility = isDirectory ? Visibility.Visible : Visibility.Collapsed;
iconImage.Source = null;
iconImage.Visibility = Visibility.Collapsed;
iconFallback.Glyph = isDirectory ? "\uE8B7" : "\uE8A5";
iconFallback.Visibility = Visibility.Visible;
_ = LoadPathIconAsync(entry, iconImage, iconFallback);
RefreshCommandPreview();
RecalculatePathStats();
}
private static readonly SemaphoreSlim IconLoadGate = new(3);
private static async Task LoadPathIconAsync(PathEntry entry, Image image, FontIcon fallback)
{
var path = entry.Path;
var isDirectory = entry.IsDirectory;
const int attempts = 3;
for (var attempt = 1; attempt <= attempts; attempt++)
{
var bitmap = await TryLoadThumbnailAsync(path, isDirectory);
if (bitmap is not null)
{
if (entry.Path == path)
{
image.Source = bitmap;
image.Visibility = Visibility.Visible;
fallback.Visibility = Visibility.Collapsed;
}
return;
}
if (attempt < attempts)
await Task.Delay(attempt * 250);
}
}
private static async Task<Microsoft.UI.Xaml.Media.Imaging.BitmapImage?> TryLoadThumbnailAsync(
string path, bool isDirectory)
{
await IconLoadGate.WaitAsync();
try
{
Windows.Storage.IStorageItemProperties item;
if (isDirectory)
item = await Windows.Storage.StorageFolder.GetFolderFromPathAsync(path);
else
item = await Windows.Storage.StorageFile.GetFileFromPathAsync(path);
using var thumbnail = await item.GetThumbnailAsync(
Windows.Storage.FileProperties.ThumbnailMode.ListView,
32,
Windows.Storage.FileProperties.ThumbnailOptions.UseCurrentScale);
if (thumbnail is null || thumbnail.Size == 0)
return null;
var bitmap = new Microsoft.UI.Xaml.Media.Imaging.BitmapImage();
await bitmap.SetSourceAsync(thumbnail);
return bitmap;
}
catch (Exception ex)
{
Debug.WriteLine($"[PathIcon] failed for '{path}': {ex.Message}");
return null;
}
finally
{
IconLoadGate.Release();
}
}
[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
private static extern int SHParseDisplayName(
string pszName, IntPtr pbc, out IntPtr ppidl, uint sfgaoIn, out uint psfgaoOut);
[DllImport("shell32.dll")]
private static extern int SHOpenFolderAndSelectItems(
IntPtr pidlFolder, uint cidl, IntPtr[]? apidl, uint dwFlags);
[DllImport("ole32.dll")]
private static extern void CoTaskMemFree(IntPtr pv);
private static void OpenPathInExplorer(PathEntry entry)
{
try
{
if (entry.IsDirectory)
{
if (Directory.Exists(entry.Path))
Process.Start(new ProcessStartInfo(entry.Path) { UseShellExecute = true });
return;
}
if (!File.Exists(entry.Path))
return;
if (!TrySelectInShell(entry.Path))
{
var parent = Path.GetDirectoryName(entry.Path);
if (!string.IsNullOrEmpty(parent))
Process.Start(new ProcessStartInfo(parent) { UseShellExecute = true });
}
}
catch
{
}
}
private static bool TrySelectInShell(string path)
{
var pidl = IntPtr.Zero;
try
{
if (SHParseDisplayName(path, IntPtr.Zero, out pidl, 0, out _) != 0 || pidl == IntPtr.Zero)
return false;
return SHOpenFolderAndSelectItems(pidl, 0, null, 0) == 0;
}
finally
{
if (pidl != IntPtr.Zero)
CoTaskMemFree(pidl);
}
}
private void RemovePathRow(PathEntry entry, FrameworkElement row)
{
_pathRowControls.Remove(entry);
_pathEntries.Remove(entry);
PathsPanel.Children.Remove(row);
UpdateEmptyPathsHint();
RefreshCommandPreview();
RecalculatePathStats();
}
private void SetRowDeletedVisual(PathEntry entry, bool isDeleted)
{
if (!_pathRowControls.TryGetValue(entry, out var controls))
return;
controls.PathTextBox.IsEnabled = !isDeleted;
controls.ContentsOnlyCheckBox.IsEnabled = !isDeleted;
controls.ChangeButton.IsEnabled = !isDeleted;
controls.IconButton.IsEnabled = !isDeleted;
controls.Row.Opacity = isDeleted ? 0.5 : 1.0;
}
private void MarkProcessedPathsAsDeleted()
{
foreach (var entry in _pathEntries.Where(p => !p.IsDeleted))
{
entry.IsDeleted = true;
SetRowDeletedVisual(entry, true);
}
RecalculatePathStats();
}
private void UpdateEmptyPathsHint()
{
EmptyPathsHint.Visibility = _pathEntries.Count == 0
? Visibility.Visible
: Visibility.Collapsed;
}
private enum PathStatsState { Counting, Done, Stopped }
private CancellationTokenSource? _statsCts;
private PathStats _lastStats;
private bool _statsInProgress;
private async void RecalculatePathStats()
{
_statsCts?.Cancel();
if (_pathEntries.Count == 0)
{
_statsInProgress = false;
PathsStatsText.Text = string.Empty;
return;
}
var cts = new CancellationTokenSource();
_statsCts = cts;
_statsInProgress = true;
_lastStats = default;
var snapshot = _pathEntries
.Where(p => !p.IsDeleted)
.Select(p => (p.Path, p.IsDirectory))
.ToList();
var progress = new Progress<PathStats>(stats =>
{
if (cts.IsCancellationRequested)
return;
_lastStats = stats;
ShowStats(stats, PathStatsState.Counting);
});
try
{
await Task.Delay(150, cts.Token);
var result = await Task.Run(
() => PathStatsCalculator.Calculate(snapshot, progress, cts.Token),
cts.Token);
if (cts.IsCancellationRequested)
return;
_lastStats = result;
ShowStats(result, PathStatsState.Done);
}
catch (OperationCanceledException)
{
}
finally
{
if (ReferenceEquals(_statsCts, cts))
_statsInProgress = false;
}
}
private void StopPathStatsCounting()
{
if (!_statsInProgress || _statsCts is null)
return;
_statsCts.Cancel();
_statsInProgress = false;
if (_lastStats == default)
PathsStatsText.Text = string.Empty;
else
ShowStats(_lastStats, PathStatsState.Stopped);
}
private void ShowStats(PathStats stats, PathStatsState state)
{
var key = state switch
{
PathStatsState.Counting => "PathsStatsCounting",
PathStatsState.Stopped => "PathsStatsStopped",
_ => "PathsStats"
};
PathsStatsText.Text = Loc.Format(
key,
stats.Files.ToString("N0"),
stats.Folders.ToString("N0"),
SizeFormatter.Format(stats.Bytes));
}
}