feature: auto-select and scroll to current local branch when clicking Navigate To HEAD button in toolbar (#1022)

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2025-06-24 18:08:35 +08:00
parent f63eefc81b
commit 4c4d8ae031
No known key found for this signature in database
2 changed files with 61 additions and 0 deletions

View file

@ -241,6 +241,47 @@ namespace SourceGit.Views
InitializeComponent();
}
public void Select(Models.Branch branch)
{
if (branch == null)
return;
_disableSelectionChangingEvent = true;
var treePath = new List<ViewModels.BranchTreeNode>();
FindTreePath(treePath, Nodes, branch.Name, 0);
if (treePath.Count == 0)
{
_disableSelectionChangingEvent = false;
return;
}
var oldRowCount = Rows.Count;
var rows = Rows;
for (var i = 0; i < treePath.Count - 1; i++)
{
var node = treePath[i];
if (!node.IsExpanded)
{
node.IsExpanded = true;
var idx = rows.IndexOf(node);
var subtree = new List<ViewModels.BranchTreeNode>();
MakeRows(subtree, node.Children, node.Depth + 1);
rows.InsertRange(idx + 1, subtree);
}
}
var target = treePath[treePath.Count - 1];
BranchesPresenter.SelectedItem = target;
BranchesPresenter.ScrollIntoView(target);
_disableSelectionChangingEvent = false;
if (oldRowCount != rows.Count)
RaiseEvent(new RoutedEventArgs(RowsChangedEvent));
}
public void UnselectAll()
{
BranchesPresenter.SelectedItem = null;
@ -534,6 +575,23 @@ namespace SourceGit.Views
CollectBranchesInNode(outs, sub);
}
private void FindTreePath(List<ViewModels.BranchTreeNode> outPath, List<ViewModels.BranchTreeNode> collection, string path, int start)
{
if (start >= path.Length - 1)
return;
var sepIdx = path.IndexOf('/', start);
var name = sepIdx < 0 ? path.Substring(start) : path.Substring(start, sepIdx - start);
foreach (var node in collection)
{
if (node.Name.Equals(name, StringComparison.Ordinal))
{
outPath.Add(node);
FindTreePath(outPath, node.Children, path, sepIdx + 1);
}
}
}
private bool _disableSelectionChangingEvent = false;
}
}

View file

@ -157,6 +157,9 @@ namespace SourceGit.Views
{
if (DataContext is ViewModels.Repository { CurrentBranch: not null } repo)
{
var repoView = TopLevel.GetTopLevel(this)?.FindDescendantOfType<Repository>(false);
repoView?.LocalBranchTree?.Select(repo.CurrentBranch);
repo.NavigateToCommit(repo.CurrentBranch.Head);
e.Handled = true;
}