Skip to content

Commit 0ea4021

Browse files
authored
feature: update blame data when clicking/navigating commits (#1408)
* initial work on allowing navigation by clicking on commits in the blame window * cleanup
1 parent 11a46db commit 0ea4021

File tree

3 files changed

+108
-9
lines changed

3 files changed

+108
-9
lines changed

src/ViewModels/Blame.cs

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,24 @@ public class Blame : ObservableObject
1212
{
1313
public string Title
1414
{
15-
get;
16-
private set;
15+
get => _title;
16+
private set => SetProperty(ref _title, value);
1717
}
1818

1919
public bool IsBinary
2020
{
2121
get => _data != null && _data.IsBinary;
2222
}
2323

24+
public bool CanMoveBack
25+
{
26+
get => _shaHistoryIndex > 0 && _shaHistory.Count > 1;
27+
}
28+
public bool CanMoveForward
29+
{
30+
get => _shaHistoryIndex < _shaHistory.Count - 1;
31+
}
32+
2433
public Models.BlameData Data
2534
{
2635
get => _data;
@@ -30,20 +39,60 @@ public Models.BlameData Data
3039
public Blame(string repo, string file, string revision)
3140
{
3241
_repo = repo;
42+
_file = file;
43+
44+
SetBlameData($"{revision.AsSpan(0, 10)}", true);
45+
}
46+
47+
private void SetBlameData(string commitSHA, bool resetHistoryForward)
48+
{
49+
Title = $"{_file} @ {commitSHA}";
3350

34-
Title = $"{file} @ {revision.AsSpan(0, 10)}";
3551
Task.Run(() =>
3652
{
37-
var result = new Commands.Blame(repo, file, revision).Result();
53+
var result = new Commands.Blame(_repo, _file, commitSHA).Result();
3854
Dispatcher.UIThread.Invoke(() =>
3955
{
4056
Data = result;
4157
OnPropertyChanged(nameof(IsBinary));
4258
});
4359
});
60+
61+
if (resetHistoryForward)
62+
{
63+
if (_shaHistoryIndex < _shaHistory.Count - 1)
64+
_shaHistory.RemoveRange(_shaHistoryIndex + 1, _shaHistory.Count - _shaHistoryIndex - 1);
65+
66+
if (_shaHistory.Count == 0 || _shaHistory[_shaHistoryIndex] != commitSHA)
67+
{
68+
_shaHistory.Add(commitSHA);
69+
_shaHistoryIndex = _shaHistory.Count - 1;
70+
}
71+
}
72+
73+
OnPropertyChanged(nameof(CanMoveBack));
74+
OnPropertyChanged(nameof(CanMoveForward));
75+
}
76+
77+
public void Back()
78+
{
79+
--_shaHistoryIndex;
80+
if (_shaHistoryIndex < 0)
81+
_shaHistoryIndex = 0;
82+
83+
NavigateToCommit(_shaHistory[_shaHistoryIndex], false);
84+
}
85+
86+
public void Forward()
87+
{
88+
++_shaHistoryIndex;
89+
if (_shaHistoryIndex >= _shaHistory.Count)
90+
_shaHistoryIndex = _shaHistory.Count - 1;
91+
92+
NavigateToCommit(_shaHistory[_shaHistoryIndex], false);
4493
}
4594

46-
public void NavigateToCommit(string commitSHA)
95+
public void NavigateToCommit(string commitSHA, bool resetHistoryForward)
4796
{
4897
var launcher = App.GetLauncher();
4998
if (launcher == null)
@@ -54,6 +103,7 @@ public void NavigateToCommit(string commitSHA)
54103
if (page.Data is Repository repo && repo.FullPath.Equals(_repo))
55104
{
56105
repo.NavigateToCommit(commitSHA);
106+
SetBlameData(commitSHA, resetHistoryForward);
57107
break;
58108
}
59109
}
@@ -69,7 +119,11 @@ public string GetCommitMessage(string sha)
69119
return msg;
70120
}
71121

72-
private readonly string _repo;
122+
private string _repo;
123+
private string _file;
124+
private string _title;
125+
private int _shaHistoryIndex = 0;
126+
private List<string> _shaHistory = [];
73127
private Models.BlameData _data = null;
74128
private Dictionary<string, string> _commitMessages = new Dictionary<string, string>();
75129
}

src/Views/Blame.axaml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,24 @@
4242
</Grid>
4343

4444
<!-- File -->
45-
<Border Grid.Row="1" Padding="8,0">
46-
<TextBlock Text="{Binding Title}" VerticalAlignment="Center"/>
45+
<Border Grid.Row="1" Padding="8,0" >
46+
<Grid>
47+
<TextBlock Text="{Binding Title}" VerticalAlignment="Center"/>
48+
<StackPanel HorizontalAlignment="Right" VerticalAlignment="Center" Orientation="Horizontal">
49+
<Button Classes="icon_button"
50+
IsEnabled="{Binding CanMoveBack}"
51+
Width="28"
52+
Click="HistoryBack">
53+
<Path Width="12" Height="12" Stretch="Uniform" Data="{StaticResource Icons.TriangleLeft}"/>
54+
</Button>
55+
<Button Classes="icon_button"
56+
IsEnabled="{Binding CanMoveForward}"
57+
Width="28"
58+
Click="HistoryForward">
59+
<Path Width="12" Height="12" Stretch="Uniform" Data="{StaticResource Icons.TriangleRight}"/>
60+
</Button>
61+
</StackPanel>
62+
</Grid>
4763
</Border>
4864

4965
<!-- Body -->

src/Views/Blame.axaml.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ protected override void OnPointerPressed(PointerPressedEventArgs e)
225225
{
226226
if (DataContext is ViewModels.Blame blame)
227227
{
228-
blame.NavigateToCommit(info.CommitSHA);
228+
blame.NavigateToCommit(info.CommitSHA, true);
229229
}
230230

231231
e.Handled = true;
@@ -433,12 +433,41 @@ public partial class Blame : ChromelessWindow
433433
public Blame()
434434
{
435435
InitializeComponent();
436+
437+
AddHandler(PointerReleasedEvent, MouseUpHandler, handledEventsToo: true);
436438
}
437439

438440
protected override void OnClosed(EventArgs e)
439441
{
440442
base.OnClosed(e);
441443
GC.Collect();
442444
}
445+
446+
private void HistoryBack(object _, RoutedEventArgs e)
447+
{
448+
if (DataContext is ViewModels.Blame blame)
449+
{
450+
blame.Back();
451+
}
452+
}
453+
private void HistoryForward(object _, RoutedEventArgs e)
454+
{
455+
if (DataContext is ViewModels.Blame blame)
456+
{
457+
blame.Forward();
458+
}
459+
}
460+
461+
private void MouseUpHandler(object sender, PointerReleasedEventArgs e)
462+
{
463+
if (e.InitialPressMouseButton == MouseButton.XButton1)
464+
{
465+
HistoryBack(null, null);
466+
}
467+
else if (e.InitialPressMouseButton == MouseButton.XButton2)
468+
{
469+
HistoryForward(null, null);
470+
}
471+
}
443472
}
444473
}

0 commit comments

Comments
 (0)