Skip to content

[Feature] Update blame window when clicking/navigating commits #1408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 60 additions & 6 deletions src/ViewModels/Blame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,24 @@ public class Blame : ObservableObject
{
public string Title
{
get;
private set;
get => _title;
private set => SetProperty(ref _title, value);
}

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;
Expand All @@ -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)
Expand All @@ -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;
}
}
Expand All @@ -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<string> _shaHistory = [];
private Models.BlameData _data = null;
private Dictionary<string, string> _commitMessages = new Dictionary<string, string>();
}
Expand Down
20 changes: 18 additions & 2 deletions src/Views/Blame.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,24 @@
</Grid>

<!-- File -->
<Border Grid.Row="1" Padding="8,0">
<TextBlock Text="{Binding Title}" VerticalAlignment="Center"/>
<Border Grid.Row="1" Padding="8,0" >
<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>

<!-- Body -->
Expand Down
31 changes: 30 additions & 1 deletion src/Views/Blame.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -433,12 +433,41 @@ public partial class Blame : ChromelessWindow
public Blame()
{
InitializeComponent();

AddHandler(PointerReleasedEvent, MouseUpHandler, handledEventsToo: true);
}

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);
}
}
}
}
Loading