@@ -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 {
0 commit comments