feature: allow to scroll text diff view to clicked position in minimap bar (#1679)

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo
2025-08-27 17:08:36 +08:00
parent b47986d6d3
commit bb52b0d06b
2 changed files with 45 additions and 1 deletions

View File

@@ -12,7 +12,7 @@
<ContentControl Content="{Binding}">
<ContentControl.DataTemplates>
<DataTemplate DataType="vm:CombinedTextDiff">
<Grid ColumnDefinitions="*,1,8">
<Grid ColumnDefinitions="*,1,12">
<v:CombinedTextDiffPresenter Grid.Column="0"
FileName="{Binding File}"
Foreground="{DynamicResource Brush.FG1}"

View File

@@ -1334,6 +1334,8 @@ namespace SourceGit.Views
public override void Render(DrawingContext context)
{
context.DrawRectangle(Brushes.Transparent, null, new Rect(0, 0, Bounds.Width, Bounds.Height));
var total = 0;
if (DataContext is ViewModels.TwoSideTextDiff twoSideDiff)
{
@@ -1371,6 +1373,42 @@ namespace SourceGit.Views
InvalidateVisual();
}
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
base.OnPointerPressed(e);
var range = DisplayRange;
if (range == null || range.End == 0)
return;
var total = 0;
if (DataContext is ViewModels.TwoSideTextDiff twoSideDiff)
{
var halfWidth = Bounds.Width * 0.5;
total = Math.Max(twoSideDiff.Old.Count, twoSideDiff.New.Count);
}
else if (DataContext is ViewModels.CombinedTextDiff combined)
{
var data = combined.Data;
total = data.Lines.Count;
}
else
{
return;
}
var height = Bounds.Height;
var startY = range.Start / (total * 1.0) * height;
var endY = range.End / (total * 1.0) * height;
var pressedY = e.GetPosition(this).Y;
if (pressedY >= startY && pressedY <= endY)
return;
var line = Math.Max(1, Math.Min(total, (int)Math.Ceiling(pressedY * total / height)));
this.FindAncestorOfType<TextDiffView>()?.ScrollToLine(line);
e.Handled = true;
}
private void RenderSingleSide(DrawingContext context, List<Models.TextDiffLine> lines, double x, double width)
{
var total = lines.Count;
@@ -1414,11 +1452,17 @@ namespace SourceGit.Views
get => GetValue(SelectedChunkProperty);
set => SetValue(SelectedChunkProperty, value);
}
public TextDiffView()
{
InitializeComponent();
}
public void ScrollToLine(int line)
{
this.FindDescendantOfType<ThemedTextDiffPresenter>()?.ScrollToLine(line);
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);