Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 47 additions & 9 deletions src/DynamoCoreWpf/Controls/StartPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ internal StartPageViewModel(DynamoViewModel dynamoViewModel, bool isFirstRun)
#endregion

var dvm = this.DynamoViewModel;
RefreshRecentFileList(dvm.RecentFiles);
RefreshRecentFileList(dvm.RecentFiles, true);
RefreshBackupFileList(dvm.Model.PreferenceSettings.BackupFiles);
dvm.RecentFiles.CollectionChanged += OnRecentFilesChanged;
}
Expand Down Expand Up @@ -404,27 +404,57 @@ public string BackupTitle

private void OnRecentFilesChanged(object sender, NotifyCollectionChangedEventArgs e)
{
RefreshRecentFileList(sender as IEnumerable<string>);
//when we are just moving existing files, no need to refresh the complete list
if (e.Action == NotifyCollectionChangedAction.Move)
{
recentFiles.Move(e.OldStartingIndex, e.NewStartingIndex);
}
else if (e.Action == NotifyCollectionChangedAction.Add)
{
var newItems = e.NewItems?.Cast<string>();
if (newItems != null)
{
RefreshRecentFileList(newItems);
}
}
else if (e.Action == NotifyCollectionChangedAction.Remove)
{
recentFiles.RemoveRange(e.OldStartingIndex, recentFiles.Count - e.OldStartingIndex);
}
else
{
RefreshRecentFileList(sender as IEnumerable<string>, true);
}
}

#endregion

#region Private Class Helper Methods

private void RefreshRecentFileList(IEnumerable<string> filePaths)
private void RefreshRecentFileList(IEnumerable<string> filePaths, bool fullRefresh = false)
{
RefreshFileList(recentFiles, filePaths);
RefreshFileList(recentFiles, filePaths, fullRefresh);
}

private void RefreshBackupFileList(IEnumerable<string> filePaths)
{
RefreshFileList(backupFiles, filePaths);
RefreshFileList(backupFiles, filePaths, true);
}

/// <summary>
/// Refreshes the list of files in the specified collection. If fullRefresh is true,
/// discards the current list and regenerates all file items based on the provided file paths.
/// </summary>
/// <param name="files">Files currently in the recent files list</param>
/// <param name="filePaths">New files path which will be used to update the current list</param>
/// <param name="fullRefresh">If true, will discard the current list and regenerate all file items based on the provided file paths</param>
private void RefreshFileList(ObservableCollection<StartPageListItem> files,
IEnumerable<string> filePaths)
IEnumerable<string> filePaths, bool fullRefresh = false)
{
files.Clear();
if (fullRefresh)
{
files.Clear();
}
foreach (var filePath in filePaths.Where(x => x != null))
{
try
Expand All @@ -441,7 +471,7 @@ private void RefreshFileList(ObservableCollection<StartPageListItem> files,
// deserializes the file only once
var properties = GetFileProperties(filePath);

files.Add(new StartPageListItem(caption)
var startItem = new StartPageListItem(caption)
{
ContextData = filePath,
ToolTip = filePath,
Expand All @@ -451,8 +481,16 @@ private void RefreshFileList(ObservableCollection<StartPageListItem> files,
Author = properties.author,
DateModified = properties.date,
ClickAction = StartPageListItem.Action.FilePath,
};

});
if (fullRefresh)
{
files.Add(startItem);
}
else
{
files.Insert(0, startItem);
}
}
catch (ArgumentException ex)
{
Expand Down
22 changes: 20 additions & 2 deletions src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1830,17 +1830,35 @@ private void OnRuleEvaluationResultsCollectionChanged(object sender, NotifyColle
RaisePropertyChanged(nameof(LinterIssuesCount));
}

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

if (RecentFiles.Contains(path))
var currIdx = RecentFiles.IndexOf(path);
if (currIdx == 0) return;
else if (currIdx > 0)
{
RecentFiles.Remove(path);
RecentFiles.Move(currIdx, 0);
return;
}

RecentFiles.Insert(0, path);
UpdateRecentFiles();
}

/// <summary>
/// Update recent files list and limits the number of recent files to the maximum number of recent files as set in the preferences.
/// </summary>
internal void UpdateRecentFiles()
{
int maxNumRecentFiles = Model.PreferenceSettings.MaxNumRecentFiles;
if (RecentFiles.Count > maxNumRecentFiles)
{
Expand Down
10 changes: 1 addition & 9 deletions src/DynamoCoreWpf/ViewModels/Menu/PreferencesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1820,7 +1820,7 @@ private void Model_PropertyChanged(object sender, PropertyChangedEventArgs e)
goto default;
case nameof(MaxNumRecentFiles):
description = Res.ResourceManager.GetString(nameof(Res.PreferencesSettingMaxRecentFiles), System.Globalization.CultureInfo.InvariantCulture);
UpdateRecentFiles();
dynamoViewModel.UpdateRecentFiles();
goto default;
case nameof(PythonTemplateFilePath):
description = Res.ResourceManager.GetString(nameof(Res.PreferencesSettingCustomPythomTemplate), System.Globalization.CultureInfo.InvariantCulture);
Expand Down Expand Up @@ -1964,14 +1964,6 @@ private void PythonEnginesChanged(object sender, NotifyCollectionChangedEventArg
AddPythonEnginesOptions();
}
}

private void UpdateRecentFiles()
{
if (dynamoViewModel.RecentFiles.Count > MaxNumRecentFiles)
{
dynamoViewModel.RecentFiles.RemoveRange(MaxNumRecentFiles, dynamoViewModel.RecentFiles.Count - MaxNumRecentFiles);
}
}
}

public class PythonTemplatePathEventArgs : EventArgs
Expand Down
Loading