mirror of
https://github.com/sourcegit-scm/sourcegit
synced 2025-05-22 04:34:59 +00:00
refactor: rewrite the welcome page since the original TreeView
has many limitations (#391)
This commit is contained in:
parent
af6d2cc725
commit
38e2e0f3f4
12 changed files with 227 additions and 243 deletions
|
@ -12,10 +12,11 @@ namespace SourceGit.ViewModels
|
|||
{
|
||||
public static Welcome Instance => _instance;
|
||||
|
||||
public AvaloniaList<RepositoryNode> RepositoryNodes
|
||||
public AvaloniaList<RepositoryNode> Rows
|
||||
{
|
||||
get => Preference.Instance.RepositoryNodes;
|
||||
}
|
||||
get;
|
||||
private set;
|
||||
} = [];
|
||||
|
||||
public string SearchFilter
|
||||
{
|
||||
|
@ -27,6 +28,60 @@ namespace SourceGit.ViewModels
|
|||
}
|
||||
}
|
||||
|
||||
public Welcome()
|
||||
{
|
||||
Refresh();
|
||||
}
|
||||
|
||||
public void Refresh()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_searchFilter))
|
||||
{
|
||||
foreach (var node in Preference.Instance.RepositoryNodes)
|
||||
ResetVisibility(node);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var node in Preference.Instance.RepositoryNodes)
|
||||
SetVisibilityBySearch(node);
|
||||
}
|
||||
|
||||
var rows = new List<RepositoryNode>();
|
||||
MakeTreeRows(rows, Preference.Instance.RepositoryNodes);
|
||||
Rows.Clear();
|
||||
Rows.AddRange(rows);
|
||||
}
|
||||
|
||||
public void ToggleNodeIsExpanded(RepositoryNode node)
|
||||
{
|
||||
node.IsExpanded = !node.IsExpanded;
|
||||
|
||||
var depth = node.Depth;
|
||||
var idx = Rows.IndexOf(node);
|
||||
if (idx == -1)
|
||||
return;
|
||||
|
||||
if (node.IsExpanded)
|
||||
{
|
||||
var subrows = new List<RepositoryNode>();
|
||||
MakeTreeRows(subrows, node.SubNodes, depth + 1);
|
||||
Rows.InsertRange(idx + 1, subrows);
|
||||
}
|
||||
else
|
||||
{
|
||||
var removeCount = 0;
|
||||
for (int i = idx + 1; i < Rows.Count; i++)
|
||||
{
|
||||
var row = Rows[i];
|
||||
if (row.Depth <= depth)
|
||||
break;
|
||||
|
||||
removeCount++;
|
||||
}
|
||||
Rows.RemoveRange(idx + 1, removeCount);
|
||||
}
|
||||
}
|
||||
|
||||
public void InitRepository(string path, RepositoryNode parent)
|
||||
{
|
||||
if (!Preference.Instance.IsGitConfigured())
|
||||
|
@ -36,9 +91,7 @@ namespace SourceGit.ViewModels
|
|||
}
|
||||
|
||||
if (PopupHost.CanCreatePopup())
|
||||
{
|
||||
PopupHost.ShowPopup(new Init(path, parent));
|
||||
}
|
||||
}
|
||||
|
||||
public void Clone()
|
||||
|
@ -75,30 +128,7 @@ namespace SourceGit.ViewModels
|
|||
public void MoveNode(RepositoryNode from, RepositoryNode to)
|
||||
{
|
||||
Preference.Instance.MoveNode(from, to);
|
||||
}
|
||||
|
||||
public RepositoryNode GetPrevVisible(RepositoryNode node)
|
||||
{
|
||||
var visibleRows = new List<RepositoryNode>();
|
||||
CollectVisibleRows(visibleRows, RepositoryNodes);
|
||||
|
||||
var idx = visibleRows.IndexOf(node);
|
||||
if (idx <= 1)
|
||||
return null;
|
||||
|
||||
return visibleRows[idx - 1];
|
||||
}
|
||||
|
||||
public RepositoryNode GetNextVisible(RepositoryNode node)
|
||||
{
|
||||
var visibleRows = new List<RepositoryNode>();
|
||||
CollectVisibleRows(visibleRows, RepositoryNodes);
|
||||
|
||||
var idx = visibleRows.IndexOf(node);
|
||||
if (idx < 0 || idx >= visibleRows.Count - 1)
|
||||
return null;
|
||||
|
||||
return visibleRows[idx + 1];
|
||||
Refresh();
|
||||
}
|
||||
|
||||
public ContextMenu CreateContextMenu(RepositoryNode node)
|
||||
|
@ -178,20 +208,6 @@ namespace SourceGit.ViewModels
|
|||
return menu;
|
||||
}
|
||||
|
||||
private void Refresh()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_searchFilter))
|
||||
{
|
||||
foreach (var node in RepositoryNodes)
|
||||
ResetVisibility(node);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var node in RepositoryNodes)
|
||||
SetVisibilityBySearch(node);
|
||||
}
|
||||
}
|
||||
|
||||
private void ResetVisibility(RepositoryNode node)
|
||||
{
|
||||
node.IsVisible = true;
|
||||
|
@ -226,6 +242,23 @@ namespace SourceGit.ViewModels
|
|||
}
|
||||
}
|
||||
|
||||
private void MakeTreeRows(List<RepositoryNode> rows, AvaloniaList<RepositoryNode> nodes, int depth = 0)
|
||||
{
|
||||
foreach (var node in nodes)
|
||||
{
|
||||
if (!node.IsVisible)
|
||||
continue;
|
||||
|
||||
node.Depth = depth;
|
||||
rows.Add(node);
|
||||
|
||||
if (node.IsRepository || !node.IsExpanded)
|
||||
continue;
|
||||
|
||||
MakeTreeRows(rows, node.SubNodes, depth+1);
|
||||
}
|
||||
}
|
||||
|
||||
private void OpenAllInNode(Launcher launcher, RepositoryNode node)
|
||||
{
|
||||
foreach (var subNode in node.SubNodes)
|
||||
|
@ -237,20 +270,6 @@ namespace SourceGit.ViewModels
|
|||
}
|
||||
}
|
||||
|
||||
private void CollectVisibleRows(List<RepositoryNode> visible, AvaloniaList<RepositoryNode> collection)
|
||||
{
|
||||
foreach (var node in collection)
|
||||
{
|
||||
if (node.IsVisible)
|
||||
{
|
||||
visible.Add(node);
|
||||
|
||||
if (!node.IsRepository)
|
||||
CollectVisibleRows(visible, node.SubNodes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Welcome _instance = new Welcome();
|
||||
private string _searchFilter = string.Empty;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue