enhance: keep repository tree sorted by name

This commit is contained in:
leo 2024-05-07 15:28:54 +08:00
parent 15456f0dee
commit 207e82b391
2 changed files with 45 additions and 2 deletions

View file

@ -1,5 +1,4 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
namespace SourceGit.ViewModels
@ -50,8 +49,13 @@ namespace SourceGit.ViewModels
public override Task<bool> Sure()
{
bool needSort = _node.Name != _name;
_node.Name = _name;
_node.Bookmark = _bookmark;
if (needSort)
Preference.SortByRenamedNode(_node);
return null;
}

View file

@ -362,6 +362,30 @@ namespace SourceGit.ViewModels
RemoveNodeRecursive(node, _instance._repositoryNodes);
}
public static void SortByRenamedNode(RepositoryNode node)
{
var container = FindNodeContainer(node, _instance._repositoryNodes);
if (container == null)
return;
var list = new List<RepositoryNode>();
list.AddRange(container);
list.Sort((l, r) =>
{
if (l.IsRepository != r.IsRepository)
{
return l.IsRepository ? 1 : -1;
}
else
{
return l.Name.CompareTo(r.Name);
}
});
container.Clear();
foreach (var one in list) container.Add(one);
}
public static Repository FindRepository(string path)
{
foreach (var repo in _instance.Repositories)
@ -417,6 +441,21 @@ namespace SourceGit.ViewModels
return null;
}
private static AvaloniaList<RepositoryNode> FindNodeContainer(RepositoryNode node, AvaloniaList<RepositoryNode> collection)
{
foreach (var sub in collection)
{
if (node == sub)
return collection;
var subCollection = FindNodeContainer(node, sub.SubNodes);
if (subCollection != null)
return subCollection;
}
return null;
}
private static bool RemoveNodeRecursive(RepositoryNode node, AvaloniaList<RepositoryNode> collection)
{
if (collection.Contains(node))