Skip to content

[Feature] Keep tree state and selection in 'Files' tab when changing commit in history #1396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/Views/RevisionFileTreeView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="SourceGit.Views.RevisionFileTreeView"
x:Name="ThisControl">
<v:RevisionFileRowsListBox ItemsSource="{Binding #ThisControl.Rows}"
<v:RevisionFileRowsListBox x:Name="RevisionFileRowsListBox" ItemsSource="{Binding #ThisControl.Rows}"
Background="Transparent"
SelectionMode="Single"
SelectionChanged="OnRowsSelectionChanged"
Expand Down
56 changes: 55 additions & 1 deletion src/Views/RevisionFileTreeView.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;

using System.Linq;
using Avalonia;
using Avalonia.Collections;
using Avalonia.Controls;
Expand All @@ -9,6 +9,7 @@
using Avalonia.Layout;
using Avalonia.Media;
using Avalonia.VisualTree;
using SourceGit.ViewModels;

namespace SourceGit.Views
{
Expand Down Expand Up @@ -249,6 +250,17 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang

if (change.Property == RevisionProperty)
{
var selectedNode = _revisionFileRowsListBox?.SelectedItem as RevisionFileTreeNode;

var expandedObjects = new List<Models.Object>();
foreach (var node in _rows)
{
if (node.IsExpanded)
{
expandedObjects.Add(node.Backend);
}
}

_tree.Clear();
_rows.Clear();
_searchResult.Clear();
Expand Down Expand Up @@ -280,10 +292,51 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
var topTree = new List<ViewModels.RevisionFileTreeNode>();
MakeRows(topTree, _tree, 0);
_rows.AddRange(topTree);

_revisionFileRowsListBox ??= this.Find<RevisionFileRowsListBox>("RevisionFileRowsListBox");

if (_revisionFileRowsListBox is { IsArrangeValid: true })
{
RestoreTreeState(expandedObjects, selectedNode);
}

GC.Collect();
}
}

private void RestoreTreeState(List<Models.Object> expandedObjects, RevisionFileTreeNode selectedNode)
{
for (int i = 0; i < _rows.Count; i++)
{
var revisionFileTreeNode = _rows[i];

if (!revisionFileTreeNode.IsFolder)
continue;

if (expandedObjects.FirstOrDefault(o => o.SHA == revisionFileTreeNode.Backend.SHA || o.Path == revisionFileTreeNode.Backend.Path) != null)
{
ToggleNodeIsExpanded(revisionFileTreeNode);
}
}

if (selectedNode != null)
{
foreach (var node in _rows)
{
if (node.Backend.SHA != selectedNode.Backend.SHA && node.Backend.Path != selectedNode.Backend.Path)
continue;

selectedNode = node;
break;
}
}

if (_revisionFileRowsListBox != null)
{
_revisionFileRowsListBox.SelectedItem = selectedNode;
}
}

private void OnTreeNodeContextRequested(object sender, ContextRequestedEventArgs e)
{
if (DataContext is ViewModels.CommitDetail vm &&
Expand Down Expand Up @@ -372,5 +425,6 @@ private void MakeRows(List<ViewModels.RevisionFileTreeNode> rows, List<ViewModel
private AvaloniaList<ViewModels.RevisionFileTreeNode> _rows = [];
private bool _disableSelectionChangingEvent = false;
private List<ViewModels.RevisionFileTreeNode> _searchResult = [];
private RevisionFileRowsListBox _revisionFileRowsListBox;
}
}