Skip to content

Commit d9911b3

Browse files
committed
refactor: implementation of synchronous scrolling in side-by-side diff view
1 parent 32e6856 commit d9911b3

File tree

2 files changed

+14
-30
lines changed

2 files changed

+14
-30
lines changed

src/Models/DiffResult.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,11 +388,11 @@ public void GeneratePatchFromSelectionSingleSide(Change change, string fileTreeG
388388
}
389389

390390
[GeneratedRegex(@"^@@ \-(\d+),?\d* \+(\d+),?\d* @@")]
391-
private static partial Regex indicatorRegex();
391+
private static partial Regex REG_INDICATOR();
392392

393393
private bool ProcessIndicatorForPatch(StringBuilder builder, TextDiffLine indicator, int idx, int start, int end, int ignoreRemoves, int ignoreAdds, bool revert, bool tailed)
394394
{
395-
var match = indicatorRegex().Match(indicator.Content);
395+
var match = REG_INDICATOR().Match(indicator.Content);
396396
var oldStart = int.Parse(match.Groups[1].Value);
397397
var newStart = int.Parse(match.Groups[2].Value) + ignoreRemoves - ignoreAdds;
398398
var oldCount = 0;
@@ -461,7 +461,7 @@ private bool ProcessIndicatorForPatch(StringBuilder builder, TextDiffLine indica
461461

462462
private bool ProcessIndicatorForPatchSingleSide(StringBuilder builder, TextDiffLine indicator, int idx, int start, int end, int ignoreRemoves, int ignoreAdds, bool revert, bool isOldSide, bool tailed)
463463
{
464-
var match = indicatorRegex().Match(indicator.Content);
464+
var match = REG_INDICATOR().Match(indicator.Content);
465465
var oldStart = int.Parse(match.Groups[1].Value);
466466
var newStart = int.Parse(match.Groups[2].Value) + ignoreRemoves - ignoreAdds;
467467
var oldCount = 0;

src/Views/TextDiffView.axaml.cs

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Avalonia;
88
using Avalonia.Controls;
99
using Avalonia.Controls.Primitives;
10+
using Avalonia.Input;
1011
using Avalonia.Interactivity;
1112
using Avalonia.Media;
1213
using Avalonia.VisualTree;
@@ -657,6 +658,7 @@ protected override void OnLoaded(RoutedEventArgs e)
657658

658659
UpdateTextMate();
659660

661+
TextArea.PointerWheelChanged += OnTextAreaPointerWheelChanged;
660662
TextArea.TextView.ContextRequested += OnTextViewContextRequested;
661663
}
662664

@@ -676,21 +678,20 @@ protected override void OnUnloaded(RoutedEventArgs e)
676678
_textMate = null;
677679
}
678680

681+
TextArea.PointerWheelChanged -= OnTextAreaPointerWheelChanged;
679682
TextArea.TextView.ContextRequested -= OnTextViewContextRequested;
680683

681684
GC.Collect();
682685
}
683686

687+
private void OnTextAreaPointerWheelChanged(object sender, PointerWheelEventArgs e)
688+
{
689+
if (!TextArea.IsFocused) Focus();
690+
}
691+
684692
private void OnTextViewScrollChanged(object sender, ScrollChangedEventArgs e)
685693
{
686-
if (_syncScrollingByOthers)
687-
{
688-
_syncScrollingByOthers = false;
689-
}
690-
else
691-
{
692-
SetCurrentValue(SyncScrollOffsetProperty, _scrollViewer.Offset);
693-
}
694+
if (TextArea.IsFocused) SetCurrentValue(SyncScrollOffsetProperty, _scrollViewer.Offset);
694695
}
695696

696697
private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs e)
@@ -754,24 +755,8 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
754755
}
755756
else if (change.Property == SyncScrollOffsetProperty)
756757
{
757-
if (_scrollViewer == null)
758-
return;
759-
760-
var curOffset = _scrollViewer.Offset;
761-
if (!curOffset.Equals(SyncScrollOffset))
762-
{
763-
_syncScrollingByOthers = true;
764-
765-
if (curOffset.X != SyncScrollOffset.X)
766-
{
767-
var offset = new Vector(Math.Min(_scrollViewer.ScrollBarMaximum.X, SyncScrollOffset.X), SyncScrollOffset.Y);
768-
_scrollViewer.Offset = offset;
769-
}
770-
else
771-
{
772-
_scrollViewer.Offset = SyncScrollOffset;
773-
}
774-
}
758+
if (_scrollViewer != null)
759+
_scrollViewer.Offset = SyncScrollOffset;
775760
}
776761
else if (change.Property == UseSyntaxHighlightingProperty)
777762
{
@@ -813,7 +798,6 @@ private void UpdateTextMate()
813798
private TextMate.Installation _textMate;
814799
private readonly LineStyleTransformer _lineStyleTransformer = null;
815800
private ScrollViewer _scrollViewer = null;
816-
private bool _syncScrollingByOthers = false;
817801
}
818802

819803
public partial class TextDiffView : UserControl

0 commit comments

Comments
 (0)