forked from adyanth/QuickLook.Plugin.FolderViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileListView.xaml.cs
More file actions
98 lines (87 loc) · 3.14 KB
/
Copy pathFileListView.xaml.cs
File metadata and controls
98 lines (87 loc) · 3.14 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
// Copyright © 2020 Paddy Xu, Frank Becker
// This file remains available under the GNU General Public License v3 or later.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
namespace QuickLook.Plugin.FolderViewer
{
public partial class FileListView : UserControl, IDisposable
{
private CancellationToken _cancellationToken;
private bool _disposed;
private Func<FileEntry, CancellationToken, Task<IReadOnlyList<FileEntry>>> _loadChildren;
public FileListView()
{
InitializeComponent();
}
public void Configure(
Func<FileEntry, CancellationToken, Task<IReadOnlyList<FileEntry>>> loadChildren,
CancellationToken cancellationToken)
{
_loadChildren = loadChildren ?? throw new ArgumentNullException(nameof(loadChildren));
_cancellationToken = cancellationToken;
}
public void SetItems(IReadOnlyList<FileEntry> entries)
{
treeGrid.DataContext = entries ?? Array.Empty<FileEntry>();
}
public void Dispose()
{
if (_disposed)
return;
_disposed = true;
_loadChildren = null;
treeGrid.DataContext = null;
GC.SuppressFinalize(this);
}
private async void OnItemExpanded(object sender, System.Windows.RoutedEventArgs args)
{
if (_disposed || _loadChildren == null || !(sender is TreeViewItem item) ||
!(item.DataContext is FileEntry entry) || !entry.TryBeginLoading())
{
return;
}
try
{
var children = await _loadChildren(entry, _cancellationToken);
if (!_disposed && !_cancellationToken.IsCancellationRequested)
entry.CompleteLoading(children);
}
catch (OperationCanceledException)
{
if (!_disposed)
entry.FailLoading("加载已取消。");
}
catch (Exception exception)
{
if (!_disposed)
entry.FailLoading(exception.Message);
}
}
private void OnItemMouseDoubleClick(object sender, MouseButtonEventArgs args)
{
if (_disposed || !(sender is TreeViewItem item) || !item.IsSelected ||
!(item.DataContext is FileEntry entry) ||
entry.IsPlaceholder || string.IsNullOrEmpty(entry.FullPath))
{
return;
}
try
{
Process.Start(new ProcessStartInfo(entry.FullPath) { UseShellExecute = true });
args.Handled = true;
}
catch (Exception exception) when (
exception is Win32Exception ||
exception is InvalidOperationException)
{
// The file may have disappeared after the preview was populated.
}
}
}
}