diff --git a/src/ViewModels/Blame.cs b/src/ViewModels/Blame.cs index c04842e5..7cd3841f 100644 --- a/src/ViewModels/Blame.cs +++ b/src/ViewModels/Blame.cs @@ -12,8 +12,8 @@ public class Blame : ObservableObject { public string Title { - get; - private set; + get => _title; + private set => SetProperty(ref _title, value); } public bool IsBinary @@ -21,6 +21,15 @@ public bool 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 { get => _data; @@ -30,20 +39,60 @@ public Models.BlameData Data 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 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) + public void NavigateToCommit(string commitSHA, bool resetHistoryForward) { var launcher = App.GetLauncer(); if (launcher == null) @@ -54,6 +103,7 @@ public void NavigateToCommit(string commitSHA) if (page.Data is Repository repo && repo.FullPath.Equals(_repo)) { repo.NavigateToCommit(commitSHA); + SetBlameData(commitSHA, resetHistoryForward); break; } } @@ -69,7 +119,11 @@ public string GetCommitMessage(string sha) 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 @@ protected override void OnPointerPressed(PointerPressedEventArgs e) { if (DataContext is ViewModels.Blame blame) { - blame.NavigateToCommit(info.CommitSHA); + blame.NavigateToCommit(info.CommitSHA, true); } e.Handled = true; @@ -433,6 +433,8 @@ public partial class Blame : ChromelessWindow public Blame() { InitializeComponent(); + + AddHandler(PointerReleasedEvent, MouseUpHandler, handledEventsToo: true); } protected override void OnClosed(EventArgs e) @@ -440,5 +442,32 @@ protected override void OnClosed(EventArgs e) 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); + } + } } }