feature: update blame data when clicking/navigating commits (#1408)

* initial work on allowing navigation by clicking on commits in the blame window
* cleanup
This commit is contained in:
johanw1232 2025-06-10 03:30:12 +02:00 committed by GitHub
parent 11a46dbc93
commit 0ea4021a64
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 108 additions and 9 deletions

View file

@ -12,8 +12,8 @@ namespace SourceGit.ViewModels
{ {
public string Title public string Title
{ {
get; get => _title;
private set; private set => SetProperty(ref _title, value);
} }
public bool IsBinary public bool IsBinary
@ -21,6 +21,15 @@ namespace SourceGit.ViewModels
get => _data != null && _data.IsBinary; get => _data != null && _data.IsBinary;
} }
public bool CanMoveBack
{
get => _shaHistoryIndex > 0 && _shaHistory.Count > 1;
}
public bool CanMoveForward
{
get => _shaHistoryIndex < _shaHistory.Count - 1;
}
public Models.BlameData Data public Models.BlameData Data
{ {
get => _data; get => _data;
@ -30,20 +39,60 @@ namespace SourceGit.ViewModels
public Blame(string repo, string file, string revision) public Blame(string repo, string file, string revision)
{ {
_repo = repo; _repo = repo;
_file = file;
SetBlameData($"{revision.AsSpan(0, 10)}", true);
}
private void SetBlameData(string commitSHA, bool resetHistoryForward)
{
Title = $"{_file} @ {commitSHA}";
Title = $"{file} @ {revision.AsSpan(0, 10)}";
Task.Run(() => Task.Run(() =>
{ {
var result = new Commands.Blame(repo, file, revision).Result(); var result = new Commands.Blame(_repo, _file, commitSHA).Result();
Dispatcher.UIThread.Invoke(() => Dispatcher.UIThread.Invoke(() =>
{ {
Data = result; Data = result;
OnPropertyChanged(nameof(IsBinary)); OnPropertyChanged(nameof(IsBinary));
}); });
}); });
if (resetHistoryForward)
{
if (_shaHistoryIndex < _shaHistory.Count - 1)
_shaHistory.RemoveRange(_shaHistoryIndex + 1, _shaHistory.Count - _shaHistoryIndex - 1);
if (_shaHistory.Count == 0 || _shaHistory[_shaHistoryIndex] != commitSHA)
{
_shaHistory.Add(commitSHA);
_shaHistoryIndex = _shaHistory.Count - 1;
}
}
OnPropertyChanged(nameof(CanMoveBack));
OnPropertyChanged(nameof(CanMoveForward));
} }
public void NavigateToCommit(string commitSHA) public void Back()
{
--_shaHistoryIndex;
if (_shaHistoryIndex < 0)
_shaHistoryIndex = 0;
NavigateToCommit(_shaHistory[_shaHistoryIndex], false);
}
public void Forward()
{
++_shaHistoryIndex;
if (_shaHistoryIndex >= _shaHistory.Count)
_shaHistoryIndex = _shaHistory.Count - 1;
NavigateToCommit(_shaHistory[_shaHistoryIndex], false);
}
public void NavigateToCommit(string commitSHA, bool resetHistoryForward)
{ {
var launcher = App.GetLauncher(); var launcher = App.GetLauncher();
if (launcher == null) if (launcher == null)
@ -54,6 +103,7 @@ namespace SourceGit.ViewModels
if (page.Data is Repository repo && repo.FullPath.Equals(_repo)) if (page.Data is Repository repo && repo.FullPath.Equals(_repo))
{ {
repo.NavigateToCommit(commitSHA); repo.NavigateToCommit(commitSHA);
SetBlameData(commitSHA, resetHistoryForward);
break; break;
} }
} }
@ -69,7 +119,11 @@ namespace SourceGit.ViewModels
return msg; return msg;
} }
private readonly string _repo; private string _repo;
private string _file;
private string _title;
private int _shaHistoryIndex = 0;
private List<string> _shaHistory = [];
private Models.BlameData _data = null; private Models.BlameData _data = null;
private Dictionary<string, string> _commitMessages = new Dictionary<string, string>(); private Dictionary<string, string> _commitMessages = new Dictionary<string, string>();
} }

View file

@ -42,8 +42,24 @@
</Grid> </Grid>
<!-- File --> <!-- File -->
<Border Grid.Row="1" Padding="8,0"> <Border Grid.Row="1" Padding="8,0" >
<TextBlock Text="{Binding Title}" VerticalAlignment="Center"/> <Grid>
<TextBlock Text="{Binding Title}" VerticalAlignment="Center"/>
<StackPanel HorizontalAlignment="Right" VerticalAlignment="Center" Orientation="Horizontal">
<Button Classes="icon_button"
IsEnabled="{Binding CanMoveBack}"
Width="28"
Click="HistoryBack">
<Path Width="12" Height="12" Stretch="Uniform" Data="{StaticResource Icons.TriangleLeft}"/>
</Button>
<Button Classes="icon_button"
IsEnabled="{Binding CanMoveForward}"
Width="28"
Click="HistoryForward">
<Path Width="12" Height="12" Stretch="Uniform" Data="{StaticResource Icons.TriangleRight}"/>
</Button>
</StackPanel>
</Grid>
</Border> </Border>
<!-- Body --> <!-- Body -->

View file

@ -225,7 +225,7 @@ namespace SourceGit.Views
{ {
if (DataContext is ViewModels.Blame blame) if (DataContext is ViewModels.Blame blame)
{ {
blame.NavigateToCommit(info.CommitSHA); blame.NavigateToCommit(info.CommitSHA, true);
} }
e.Handled = true; e.Handled = true;
@ -433,6 +433,8 @@ namespace SourceGit.Views
public Blame() public Blame()
{ {
InitializeComponent(); InitializeComponent();
AddHandler(PointerReleasedEvent, MouseUpHandler, handledEventsToo: true);
} }
protected override void OnClosed(EventArgs e) protected override void OnClosed(EventArgs e)
@ -440,5 +442,32 @@ namespace SourceGit.Views
base.OnClosed(e); base.OnClosed(e);
GC.Collect(); GC.Collect();
} }
private void HistoryBack(object _, RoutedEventArgs e)
{
if (DataContext is ViewModels.Blame blame)
{
blame.Back();
}
}
private void HistoryForward(object _, RoutedEventArgs e)
{
if (DataContext is ViewModels.Blame blame)
{
blame.Forward();
}
}
private void MouseUpHandler(object sender, PointerReleasedEventArgs e)
{
if (e.InitialPressMouseButton == MouseButton.XButton1)
{
HistoryBack(null, null);
}
else if (e.InitialPressMouseButton == MouseButton.XButton2)
{
HistoryForward(null, null);
}
}
} }
} }