/
FilesPageViewModel.cs
250 lines (219 loc) · 8.93 KB
/
FilesPageViewModel.cs
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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reactive.Linq;
using System.Runtime.InteropServices;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Models.TreeDataGrid;
using Avalonia.Controls.Selection;
using Avalonia.Data.Converters;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using ReactiveUI;
using TreeDataGridDemo.Models;
namespace TreeDataGridDemo.ViewModels
{
public class FilesPageViewModel : ReactiveObject
{
private static IconConverter? s_iconConverter;
private bool _cellSelection;
private FileTreeNodeModel? _root;
private string _selectedDrive;
private string? _selectedPath;
public FilesPageViewModel()
{
Drives = DriveInfo.GetDrives().Select(x => x.Name).ToList();
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
_selectedDrive = "C:\\";
}
else
{
_selectedDrive = Drives.FirstOrDefault() ?? "/";
}
Source = new HierarchicalTreeDataGridSource<FileTreeNodeModel>(Array.Empty<FileTreeNodeModel>())
{
Columns =
{
new CheckBoxColumn<FileTreeNodeModel>(
null,
x => x.IsChecked,
(o, v) => o.IsChecked = v,
options: new()
{
CanUserResizeColumn = false,
}),
new HierarchicalExpanderColumn<FileTreeNodeModel>(
new TemplateColumn<FileTreeNodeModel>(
"Name",
"FileNameCell",
"FileNameEditCell",
new GridLength(1, GridUnitType.Star),
new()
{
CompareAscending = FileTreeNodeModel.SortAscending(x => x.Name),
CompareDescending = FileTreeNodeModel.SortDescending(x => x.Name),
IsTextSearchEnabled = true,
TextSearchValueSelector = x => x.Name
}),
x => x.Children,
x => x.HasChildren,
x => x.IsExpanded),
new TextColumn<FileTreeNodeModel, long?>(
"Size",
x => x.Size,
options: new()
{
CompareAscending = FileTreeNodeModel.SortAscending(x => x.Size),
CompareDescending = FileTreeNodeModel.SortDescending(x => x.Size),
}),
new TextColumn<FileTreeNodeModel, DateTimeOffset?>(
"Modified",
x => x.Modified,
options: new()
{
CompareAscending = FileTreeNodeModel.SortAscending(x => x.Modified),
CompareDescending = FileTreeNodeModel.SortDescending(x => x.Modified),
}),
}
};
Source.RowSelection!.SingleSelect = false;
Source.RowSelection.SelectionChanged += SelectionChanged;
this.WhenAnyValue(x => x.SelectedDrive)
.Subscribe(x =>
{
_root = new FileTreeNodeModel(_selectedDrive, isDirectory: true, isRoot: true);
Source.Items = new[] { _root };
});
}
public bool CellSelection
{
get => _cellSelection;
set
{
if (_cellSelection != value)
{
_cellSelection = value;
if (_cellSelection)
Source.Selection = new TreeDataGridCellSelectionModel<FileTreeNodeModel>(Source) { SingleSelect = false };
else
Source.Selection = new TreeDataGridRowSelectionModel<FileTreeNodeModel>(Source) { SingleSelect = false };
this.RaisePropertyChanged();
}
}
}
public IList<string> Drives { get; }
public string SelectedDrive
{
get => _selectedDrive;
set => this.RaiseAndSetIfChanged(ref _selectedDrive, value);
}
public string? SelectedPath
{
get => _selectedPath;
set => SetSelectedPath(value);
}
public HierarchicalTreeDataGridSource<FileTreeNodeModel> Source { get; }
public static IMultiValueConverter FileIconConverter
{
get
{
if (s_iconConverter is null)
{
using (var fileStream = AssetLoader.Open(new Uri("avares://TreeDataGridDemo/Assets/file.png")))
using (var folderStream = AssetLoader.Open(new Uri("avares://TreeDataGridDemo/Assets/folder.png")))
using (var folderOpenStream = AssetLoader.Open(new Uri("avares://TreeDataGridDemo/Assets/folder-open.png")))
{
var fileIcon = new Bitmap(fileStream);
var folderIcon = new Bitmap(folderStream);
var folderOpenIcon = new Bitmap(folderOpenStream);
s_iconConverter = new IconConverter(fileIcon, folderOpenIcon, folderIcon);
}
}
return s_iconConverter;
}
}
private void SetSelectedPath(string? value)
{
if (string.IsNullOrEmpty(value))
{
Source.RowSelection!.Clear();
return;
}
var path = value;
var components = new Stack<string>();
DirectoryInfo? d = null;
if (File.Exists(path))
{
var f = new FileInfo(path);
components.Push(f.Name);
d = f.Directory;
}
else if (Directory.Exists(path))
{
d = new DirectoryInfo(path);
}
while (d is not null)
{
components.Push(d.Name);
d = d.Parent;
}
var index = IndexPath.Unselected;
if (components.Count > 0)
{
var drive = components.Pop();
var driveIndex = Drives.FindIndex(x => string.Equals(x, drive, StringComparison.OrdinalIgnoreCase));
if (driveIndex >= 0)
SelectedDrive = Drives[driveIndex];
FileTreeNodeModel? node = _root;
index = new IndexPath(0);
while (node is not null && components.Count > 0)
{
node.IsExpanded = true;
var component = components.Pop();
var i = node.Children.FindIndex(x => string.Equals(x.Name, component, StringComparison.OrdinalIgnoreCase));
node = i >= 0 ? node.Children[i] : null;
index = i >= 0 ? index.Append(i) : default;
}
}
Source.RowSelection!.SelectedIndex = index;
}
private void SelectionChanged(object? sender, TreeSelectionModelSelectionChangedEventArgs<FileTreeNodeModel> e)
{
var selectedPath = Source.RowSelection?.SelectedItem?.Path;
this.RaiseAndSetIfChanged(ref _selectedPath, selectedPath, nameof(SelectedPath));
foreach (var i in e.DeselectedItems)
System.Diagnostics.Trace.WriteLine($"Deselected '{i?.Path}'");
foreach (var i in e.SelectedItems)
System.Diagnostics.Trace.WriteLine($"Selected '{i?.Path}'");
}
private class IconConverter : IMultiValueConverter
{
private readonly Bitmap _file;
private readonly Bitmap _folderExpanded;
private readonly Bitmap _folderCollapsed;
public IconConverter(Bitmap file, Bitmap folderExpanded, Bitmap folderCollapsed)
{
_file = file;
_folderExpanded = folderExpanded;
_folderCollapsed = folderCollapsed;
}
public object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
{
if (values.Count == 2 &&
values[0] is bool isDirectory &&
values[1] is bool isExpanded)
{
if (!isDirectory)
return _file;
else
return isExpanded ? _folderExpanded : _folderCollapsed;
}
return null;
}
}
}
}