Skip to content

Commit 4c4d8ae

Browse files
committed
feature: auto-select and scroll to current local branch when clicking Navigate To HEAD button in toolbar (#1022)
Signed-off-by: leo <[email protected]>
1 parent f63eefc commit 4c4d8ae

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/Views/BranchTree.axaml.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,47 @@ public BranchTree()
241241
InitializeComponent();
242242
}
243243

244+
public void Select(Models.Branch branch)
245+
{
246+
if (branch == null)
247+
return;
248+
249+
_disableSelectionChangingEvent = true;
250+
251+
var treePath = new List<ViewModels.BranchTreeNode>();
252+
FindTreePath(treePath, Nodes, branch.Name, 0);
253+
254+
if (treePath.Count == 0)
255+
{
256+
_disableSelectionChangingEvent = false;
257+
return;
258+
}
259+
260+
var oldRowCount = Rows.Count;
261+
var rows = Rows;
262+
for (var i = 0; i < treePath.Count - 1; i++)
263+
{
264+
var node = treePath[i];
265+
if (!node.IsExpanded)
266+
{
267+
node.IsExpanded = true;
268+
269+
var idx = rows.IndexOf(node);
270+
var subtree = new List<ViewModels.BranchTreeNode>();
271+
MakeRows(subtree, node.Children, node.Depth + 1);
272+
rows.InsertRange(idx + 1, subtree);
273+
}
274+
}
275+
276+
var target = treePath[treePath.Count - 1];
277+
BranchesPresenter.SelectedItem = target;
278+
BranchesPresenter.ScrollIntoView(target);
279+
_disableSelectionChangingEvent = false;
280+
281+
if (oldRowCount != rows.Count)
282+
RaiseEvent(new RoutedEventArgs(RowsChangedEvent));
283+
}
284+
244285
public void UnselectAll()
245286
{
246287
BranchesPresenter.SelectedItem = null;
@@ -534,6 +575,23 @@ private void CollectBranchesInNode(List<Models.Branch> outs, ViewModels.BranchTr
534575
CollectBranchesInNode(outs, sub);
535576
}
536577

578+
private void FindTreePath(List<ViewModels.BranchTreeNode> outPath, List<ViewModels.BranchTreeNode> collection, string path, int start)
579+
{
580+
if (start >= path.Length - 1)
581+
return;
582+
583+
var sepIdx = path.IndexOf('/', start);
584+
var name = sepIdx < 0 ? path.Substring(start) : path.Substring(start, sepIdx - start);
585+
foreach (var node in collection)
586+
{
587+
if (node.Name.Equals(name, StringComparison.Ordinal))
588+
{
589+
outPath.Add(node);
590+
FindTreePath(outPath, node.Children, path, sepIdx + 1);
591+
}
592+
}
593+
}
594+
537595
private bool _disableSelectionChangingEvent = false;
538596
}
539597
}

src/Views/RepositoryToolbar.axaml.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ private void NavigateToHead(object sender, RoutedEventArgs e)
157157
{
158158
if (DataContext is ViewModels.Repository { CurrentBranch: not null } repo)
159159
{
160+
var repoView = TopLevel.GetTopLevel(this)?.FindDescendantOfType<Repository>(false);
161+
repoView?.LocalBranchTree?.Select(repo.CurrentBranch);
162+
160163
repo.NavigateToCommit(repo.CurrentBranch.Head);
161164
e.Handled = true;
162165
}

0 commit comments

Comments
 (0)