Load folder contents on expand instead of initially for much increased performance

This commit is contained in:
trawzified 2024-05-25 22:57:00 +02:00
parent a4df16628b
commit a37fc2acc1
2 changed files with 19 additions and 5 deletions

View File

@ -69,33 +69,45 @@ namespace Wabbajack
this.WhenActivated(disposables =>
{
var fileTree = GetDirectoryContents(new DirectoryInfo(Settings.Source.ToString()));
Files = LoadFileTree(new DirectoryInfo(Settings.Source.ToString()));
Files = LoadFiles(new DirectoryInfo(Settings.Source.ToString()));
Disposable.Create(() => { }).DisposeWith(disposables);
});
}
private IEnumerable<TreeViewItem> LoadFileTree(DirectoryInfo parent)
private IEnumerable<TreeViewItem> LoadFiles(DirectoryInfo parent)
{
var parentTreeItem = new TreeViewItem()
{
Header = parent,
IsExpanded = true,
ItemsSource = GetRecursiveFileTree(parent)
ItemsSource = LoadDirectoryContents(parent)
};
return [parentTreeItem];
}
private IEnumerable<TreeViewItem> GetRecursiveFileTree(DirectoryInfo parent)
private IEnumerable<TreeViewItem> LoadDirectoryContents(DirectoryInfo parent)
{
return parent.EnumerateDirectories()
.OrderBy(dir => dir.Name)
.Select(dir => new TreeViewItem() { Header = dir, ItemsSource = GetRecursiveFileTree(dir) })
.Select(dir => new TreeViewItem() { Header = dir, ItemsSource = (dir.EnumerateDirectories().Any() || dir.EnumerateFiles().Any()) ? new ObservableCollection<TreeViewItem>([new TreeViewItem() { Header = "Loading..." }]) : null}).Select(item => {
item.Expanded += LoadingItem_Expanded;
return item;
})
.Concat(parent.EnumerateFiles()
.OrderBy(file => file.Name)
.Select(file => new TreeViewItem() { Header = file }));
}
private void LoadingItem_Expanded(object sender, System.Windows.RoutedEventArgs e)
{
var parent = (TreeViewItem)e.OriginalSource;
var children = parent.ItemsSource.OfType<TreeViewItem>();
var firstChild = children.Any() ? children.First().Header : null;
if (firstChild != null && firstChild is string firstString && firstString == "Loading...")
parent.ItemsSource = LoadDirectoryContents((DirectoryInfo)parent.Header);
}
private IEnumerable<FileSystemInfo> GetDirectoryContents(DirectoryInfo dir)
{
var directories = dir.EnumerateDirectories();

View File

@ -15,6 +15,7 @@ using Wabbajack.Paths;
using Wabbajack.Paths.IO;
using Wabbajack.ViewModels.Controls;
using Wabbajack.Services.OSIntegrated;
using ReactiveMarbles.ObservableEvents;
namespace Wabbajack
{
@ -33,6 +34,7 @@ namespace Wabbajack
this.WhenAny(x => x.ViewModel.Files)
.BindToStrict(this, v => v.FileTreeView.ItemsSource)
.DisposeWith(disposables);
});
}