From a37fc2acc1d025cf878c8e8df47f937a5464e3c5 Mon Sep 17 00:00:00 2001 From: trawzified <55751269+tr4wzified@users.noreply.github.com> Date: Sat, 25 May 2024 22:57:00 +0200 Subject: [PATCH] Load folder contents on expand instead of initially for much increased performance --- .../Compiler/CompilerFileManagerVM.cs | 22 ++++++++++++++----- .../Compiler/CompilerFileManagerView.xaml.cs | 2 ++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/Wabbajack.App.Wpf/ViewModels/Compiler/CompilerFileManagerVM.cs b/Wabbajack.App.Wpf/ViewModels/Compiler/CompilerFileManagerVM.cs index 917b16a4..ef772618 100644 --- a/Wabbajack.App.Wpf/ViewModels/Compiler/CompilerFileManagerVM.cs +++ b/Wabbajack.App.Wpf/ViewModels/Compiler/CompilerFileManagerVM.cs @@ -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 LoadFileTree(DirectoryInfo parent) + private IEnumerable LoadFiles(DirectoryInfo parent) { var parentTreeItem = new TreeViewItem() { Header = parent, IsExpanded = true, - ItemsSource = GetRecursiveFileTree(parent) + ItemsSource = LoadDirectoryContents(parent) }; return [parentTreeItem]; } - private IEnumerable GetRecursiveFileTree(DirectoryInfo parent) + private IEnumerable 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([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(); + 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 GetDirectoryContents(DirectoryInfo dir) { var directories = dir.EnumerateDirectories(); diff --git a/Wabbajack.App.Wpf/Views/Compiler/CompilerFileManagerView.xaml.cs b/Wabbajack.App.Wpf/Views/Compiler/CompilerFileManagerView.xaml.cs index 20714527..09706927 100644 --- a/Wabbajack.App.Wpf/Views/Compiler/CompilerFileManagerView.xaml.cs +++ b/Wabbajack.App.Wpf/Views/Compiler/CompilerFileManagerView.xaml.cs @@ -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); + }); }