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 2 commits 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
57 changes: 57 additions & 0 deletions src/Views/RevisionFileTreeView.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

using Avalonia;
using Avalonia.Collections;
using Avalonia.Controls;
Expand All @@ -12,6 +14,8 @@
using Avalonia.Platform.Storage;
using Avalonia.VisualTree;

using SourceGit.ViewModels;

namespace SourceGit.Views
{
public class RevisionFileTreeNodeToggleButton : ToggleButton
Expand Down Expand Up @@ -295,6 +299,17 @@ protected override async void OnPropertyChanged(AvaloniaPropertyChangedEventArgs

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();
_searchResult.Clear();

Expand Down Expand Up @@ -324,10 +339,51 @@ protected override async void OnPropertyChanged(AvaloniaPropertyChangedEventArgs

Rows.Clear();
Rows.AddRange(topTree);

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

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

GC.Collect();
}
}

private async 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)
{
await ToggleNodeIsExpandedAsync(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 { Repository: ViewModels.Repository repo, Commit: Models.Commit commit } vm &&
Expand Down Expand Up @@ -671,5 +727,6 @@ public ContextMenu CreateRevisionFileContextMenu(ViewModels.Repository repo, Vie
private List<ViewModels.RevisionFileTreeNode> _tree = [];
private bool _disableSelectionChangingEvent = false;
private List<ViewModels.RevisionFileTreeNode> _searchResult = [];
private RevisionFileRowsListBox _revisionFileRowsListBox;
}
}
Loading