Skip to content

Commit 3b91079

Browse files
zeusongitCopilot
andauthored
DYN-8797: Optimize AddToRecent files (#16197)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 430d6a9 commit 3b91079

File tree

3 files changed

+68
-20
lines changed

3 files changed

+68
-20
lines changed

src/DynamoCoreWpf/Controls/StartPage.xaml.cs

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ internal StartPageViewModel(DynamoViewModel dynamoViewModel, bool isFirstRun)
211211
#endregion
212212

213213
var dvm = this.DynamoViewModel;
214-
RefreshRecentFileList(dvm.RecentFiles);
214+
RefreshRecentFileList(dvm.RecentFiles, true);
215215
RefreshBackupFileList(dvm.Model.PreferenceSettings.BackupFiles);
216216
dvm.RecentFiles.CollectionChanged += OnRecentFilesChanged;
217217
}
@@ -404,27 +404,57 @@ public string BackupTitle
404404

405405
private void OnRecentFilesChanged(object sender, NotifyCollectionChangedEventArgs e)
406406
{
407-
RefreshRecentFileList(sender as IEnumerable<string>);
407+
//when we are just moving existing files, no need to refresh the complete list
408+
if (e.Action == NotifyCollectionChangedAction.Move)
409+
{
410+
recentFiles.Move(e.OldStartingIndex, e.NewStartingIndex);
411+
}
412+
else if (e.Action == NotifyCollectionChangedAction.Add)
413+
{
414+
var newItems = e.NewItems?.Cast<string>();
415+
if (newItems != null)
416+
{
417+
RefreshRecentFileList(newItems);
418+
}
419+
}
420+
else if (e.Action == NotifyCollectionChangedAction.Remove)
421+
{
422+
recentFiles.RemoveRange(e.OldStartingIndex, recentFiles.Count - e.OldStartingIndex);
423+
}
424+
else
425+
{
426+
RefreshRecentFileList(sender as IEnumerable<string>, true);
427+
}
408428
}
409429

410430
#endregion
411431

412432
#region Private Class Helper Methods
413433

414-
private void RefreshRecentFileList(IEnumerable<string> filePaths)
434+
private void RefreshRecentFileList(IEnumerable<string> filePaths, bool fullRefresh = false)
415435
{
416-
RefreshFileList(recentFiles, filePaths);
436+
RefreshFileList(recentFiles, filePaths, fullRefresh);
417437
}
418438

419439
private void RefreshBackupFileList(IEnumerable<string> filePaths)
420440
{
421-
RefreshFileList(backupFiles, filePaths);
441+
RefreshFileList(backupFiles, filePaths, true);
422442
}
423443

444+
/// <summary>
445+
/// Refreshes the list of files in the specified collection. If fullRefresh is true,
446+
/// discards the current list and regenerates all file items based on the provided file paths.
447+
/// </summary>
448+
/// <param name="files">Files currently in the recent files list</param>
449+
/// <param name="filePaths">New files path which will be used to update the current list</param>
450+
/// <param name="fullRefresh">If true, will discard the current list and regenerate all file items based on the provided file paths</param>
424451
private void RefreshFileList(ObservableCollection<StartPageListItem> files,
425-
IEnumerable<string> filePaths)
452+
IEnumerable<string> filePaths, bool fullRefresh = false)
426453
{
427-
files.Clear();
454+
if (fullRefresh)
455+
{
456+
files.Clear();
457+
}
428458
foreach (var filePath in filePaths.Where(x => x != null))
429459
{
430460
try
@@ -441,7 +471,7 @@ private void RefreshFileList(ObservableCollection<StartPageListItem> files,
441471
// deserializes the file only once
442472
var properties = GetFileProperties(filePath);
443473

444-
files.Add(new StartPageListItem(caption)
474+
var startItem = new StartPageListItem(caption)
445475
{
446476
ContextData = filePath,
447477
ToolTip = filePath,
@@ -451,8 +481,16 @@ private void RefreshFileList(ObservableCollection<StartPageListItem> files,
451481
Author = properties.author,
452482
DateModified = properties.date,
453483
ClickAction = StartPageListItem.Action.FilePath,
484+
};
454485

455-
});
486+
if (fullRefresh)
487+
{
488+
files.Add(startItem);
489+
}
490+
else
491+
{
492+
files.Insert(0, startItem);
493+
}
456494
}
457495
catch (ArgumentException ex)
458496
{

src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,17 +1841,35 @@ private void OnRuleEvaluationResultsCollectionChanged(object sender, NotifyColle
18411841
RaisePropertyChanged(nameof(LinterIssuesCount));
18421842
}
18431843

1844+
/// <summary>
1845+
/// Adds the path to the list of recent files.
1846+
/// We don't do anything if the file is already added and is at the first place,
1847+
/// we move the file to the start of the list if it is already present,
1848+
/// or add it to the start of the list if it is not present.
1849+
/// Every other event, except Move will refresh all the recent files.
1850+
/// </summary>
1851+
/// <param name="path"></param>
18441852
internal void AddToRecentFiles(string path)
18451853
{
18461854
if (path == null) return;
18471855

1848-
if (RecentFiles.Contains(path))
1856+
var currIdx = RecentFiles.IndexOf(path);
1857+
if (currIdx == 0) return;
1858+
else if (currIdx > 0)
18491859
{
1850-
RecentFiles.Remove(path);
1860+
RecentFiles.Move(currIdx, 0);
1861+
return;
18511862
}
18521863

18531864
RecentFiles.Insert(0, path);
1865+
UpdateRecentFiles();
1866+
}
18541867

1868+
/// <summary>
1869+
/// Update recent files list and limits the number of recent files to the maximum number of recent files as set in the preferences.
1870+
/// </summary>
1871+
internal void UpdateRecentFiles()
1872+
{
18551873
int maxNumRecentFiles = Model.PreferenceSettings.MaxNumRecentFiles;
18561874
if (RecentFiles.Count > maxNumRecentFiles)
18571875
{

src/DynamoCoreWpf/ViewModels/Menu/PreferencesViewModel.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,7 +1820,7 @@ private void Model_PropertyChanged(object sender, PropertyChangedEventArgs e)
18201820
goto default;
18211821
case nameof(MaxNumRecentFiles):
18221822
description = Res.ResourceManager.GetString(nameof(Res.PreferencesSettingMaxRecentFiles), System.Globalization.CultureInfo.InvariantCulture);
1823-
UpdateRecentFiles();
1823+
dynamoViewModel.UpdateRecentFiles();
18241824
goto default;
18251825
case nameof(PythonTemplateFilePath):
18261826
description = Res.ResourceManager.GetString(nameof(Res.PreferencesSettingCustomPythomTemplate), System.Globalization.CultureInfo.InvariantCulture);
@@ -1964,14 +1964,6 @@ private void PythonEnginesChanged(object sender, NotifyCollectionChangedEventArg
19641964
AddPythonEnginesOptions();
19651965
}
19661966
}
1967-
1968-
private void UpdateRecentFiles()
1969-
{
1970-
if (dynamoViewModel.RecentFiles.Count > MaxNumRecentFiles)
1971-
{
1972-
dynamoViewModel.RecentFiles.RemoveRange(MaxNumRecentFiles, dynamoViewModel.RecentFiles.Count - MaxNumRecentFiles);
1973-
}
1974-
}
19751967
}
19761968

19771969
public class PythonTemplatePathEventArgs : EventArgs

0 commit comments

Comments
 (0)