fix: DataGrid does NOT scroll when navigation target is the same as current selected in Views.Histories. (#58)

This commit is contained in:
leo 2024-04-07 21:19:02 +08:00
parent d09e81b80a
commit 9a4f928ece
2 changed files with 31 additions and 0 deletions

View file

@ -61,6 +61,12 @@ namespace SourceGit.ViewModels
private set => SetProperty(ref _autoSelectedCommit, value); private set => SetProperty(ref _autoSelectedCommit, value);
} }
public long NavigationId
{
get => _navigationId;
private set => SetProperty(ref _navigationId, value);
}
public object DetailContext public object DetailContext
{ {
get => _detailContext; get => _detailContext;
@ -98,6 +104,7 @@ namespace SourceGit.ViewModels
if (commit != null) if (commit != null)
{ {
AutoSelectedCommit = commit; AutoSelectedCommit = commit;
NavigationId = _navigationId + 1;
if (_detailContext is CommitDetail detail) if (_detailContext is CommitDetail detail)
{ {
@ -597,6 +604,7 @@ namespace SourceGit.ViewModels
private List<Models.Commit> _commits = new List<Models.Commit>(); private List<Models.Commit> _commits = new List<Models.Commit>();
private Models.CommitGraph _graph = null; private Models.CommitGraph _graph = null;
private Models.Commit _autoSelectedCommit = null; private Models.Commit _autoSelectedCommit = null;
private long _navigationId = 0;
private object _detailContext = null; private object _detailContext = null;
} }
} }

View file

@ -3,6 +3,7 @@ using System;
using Avalonia; using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.Primitives; using Avalonia.Controls.Primitives;
using Avalonia.Data;
using Avalonia.Media; using Avalonia.Media;
using Avalonia.VisualTree; using Avalonia.VisualTree;
@ -258,9 +259,31 @@ namespace SourceGit.Views
public partial class Histories : UserControl public partial class Histories : UserControl
{ {
public static readonly StyledProperty<long> NavigationIdProperty =
AvaloniaProperty.Register<Histories, long>(nameof(NavigationId), 0);
public long NavigationId
{
get => GetValue(NavigationIdProperty);
set => SetValue(NavigationIdProperty, value);
}
static Histories()
{
NavigationIdProperty.Changed.AddClassHandler<Histories>((h, _) =>
{
// Force scroll selected item (current head) into view. see issue #58
var datagrid = h.commitDataGrid;
if (datagrid != null && datagrid.SelectedItems.Count == 1)
datagrid.ScrollIntoView(datagrid.SelectedItems[0], null);
});
}
public Histories() public Histories()
{ {
InitializeComponent(); InitializeComponent();
this.Bind(NavigationIdProperty, new Binding("NavigationId", BindingMode.OneWay));
} }
private void OnCommitDataGridLayoutUpdated(object sender, EventArgs e) private void OnCommitDataGridLayoutUpdated(object sender, EventArgs e)