From 0ea4021a64a47e961294c5d6f27ecec33206d5fc Mon Sep 17 00:00:00 2001 From: johanw1232 <73111635+johanw1232@users.noreply.github.com> Date: Tue, 10 Jun 2025 03:30:12 +0200 Subject: [PATCH] feature: update blame data when clicking/navigating commits (#1408) * initial work on allowing navigation by clicking on commits in the blame window * cleanup --- src/ViewModels/Blame.cs | 66 ++++++++++++++++++++++++++++++++++++---- src/Views/Blame.axaml | 20 ++++++++++-- src/Views/Blame.axaml.cs | 31 ++++++++++++++++++- 3 files changed, 108 insertions(+), 9 deletions(-) diff --git a/src/ViewModels/Blame.cs b/src/ViewModels/Blame.cs index d3558670..4b57575b 100644 --- a/src/ViewModels/Blame.cs +++ b/src/ViewModels/Blame.cs @@ -12,8 +12,8 @@ namespace SourceGit.ViewModels { public string Title { - get; - private set; + get => _title; + private set => SetProperty(ref _title, value); } public bool IsBinary @@ -21,6 +21,15 @@ namespace SourceGit.ViewModels 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 { get => _data; @@ -30,20 +39,60 @@ namespace SourceGit.ViewModels public Blame(string repo, string file, string revision) { _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(() => { - var result = new Commands.Blame(repo, file, revision).Result(); + var result = new Commands.Blame(_repo, _file, commitSHA).Result(); Dispatcher.UIThread.Invoke(() => { Data = result; 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(); if (launcher == null) @@ -54,6 +103,7 @@ namespace SourceGit.ViewModels if (page.Data is Repository repo && repo.FullPath.Equals(_repo)) { repo.NavigateToCommit(commitSHA); + SetBlameData(commitSHA, resetHistoryForward); break; } } @@ -69,7 +119,11 @@ namespace SourceGit.ViewModels return msg; } - private readonly string _repo; + private string _repo; + private string _file; + private string _title; + private int _shaHistoryIndex = 0; + private List _shaHistory = []; private Models.BlameData _data = null; private Dictionary _commitMessages = new Dictionary(); } diff --git a/src/Views/Blame.axaml b/src/Views/Blame.axaml index 7a3d7ddf..e89bb2ec 100644 --- a/src/Views/Blame.axaml +++ b/src/Views/Blame.axaml @@ -42,8 +42,24 @@ - - + + + + + + + + diff --git a/src/Views/Blame.axaml.cs b/src/Views/Blame.axaml.cs index 00342b0b..47f3809d 100644 --- a/src/Views/Blame.axaml.cs +++ b/src/Views/Blame.axaml.cs @@ -225,7 +225,7 @@ namespace SourceGit.Views { if (DataContext is ViewModels.Blame blame) { - blame.NavigateToCommit(info.CommitSHA); + blame.NavigateToCommit(info.CommitSHA, true); } e.Handled = true; @@ -433,6 +433,8 @@ namespace SourceGit.Views public Blame() { InitializeComponent(); + + AddHandler(PointerReleasedEvent, MouseUpHandler, handledEventsToo: true); } protected override void OnClosed(EventArgs e) @@ -440,5 +442,32 @@ namespace SourceGit.Views base.OnClosed(e); 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); + } + } } }