Skip to content

Commit

Permalink
enhance: better sync scroll logic with two-side diff - if the width i…
Browse files Browse the repository at this point in the history
…n each side is different
  • Loading branch information
love-linger committed Mar 12, 2024
1 parent b8a230c commit 318a9c6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/ViewModels/TwoSideTextDiff.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Avalonia;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.Generic;

namespace SourceGit.ViewModels {
Expand Down
38 changes: 31 additions & 7 deletions src/Views/TextDiffView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -508,12 +508,16 @@ public Vector SyncScrollOffset {
protected override void OnLoaded(RoutedEventArgs e) {
base.OnLoaded(e);

_scrollViewer = this.FindDescendantOfType<ScrollViewer>();
if (_scrollViewer != null) {
_scrollViewer.ScrollChanged += OnTextViewScrollChanged;
}

TextArea.LeftMargins.Add(new LineNumberMargin(this) { Margin = new Thickness(8, 0) });
TextArea.LeftMargins.Add(new VerticalSeperatorMargin(this));
TextArea.TextView.Margin = new Thickness(4, 0);
TextArea.TextView.BackgroundRenderers.Add(new LineBackgroundRenderer(this));
TextArea.TextView.ContextRequested += OnTextViewContextRequested;
TextArea.TextView.ScrollOffsetChanged += OnTextViewScrollOffsetChanged;

if (App.Current?.ActualThemeVariant == ThemeVariant.Dark) {
_registryOptions = new RegistryOptions(ThemeName.DarkPlus);
Expand All @@ -531,19 +535,27 @@ protected override void OnLoaded(RoutedEventArgs e) {
protected override void OnUnloaded(RoutedEventArgs e) {
base.OnUnloaded(e);

if (_scrollViewer != null) {
_scrollViewer.ScrollChanged -= OnTextViewScrollChanged;
_scrollViewer = null;
}

TextArea.LeftMargins.Clear();
TextArea.TextView.BackgroundRenderers.Clear();
TextArea.TextView.LineTransformers.Clear();
TextArea.TextView.ContextRequested -= OnTextViewContextRequested;
TextArea.TextView.ScrollOffsetChanged -= OnTextViewScrollOffsetChanged;
_registryOptions = null;
_textMate.Dispose();
_textMate = null;
GC.Collect();
}

private void OnTextViewScrollOffsetChanged(object sender, EventArgs e) {
SyncScrollOffset = TextArea.TextView.ScrollOffset;
private void OnTextViewScrollChanged(object sender, ScrollChangedEventArgs e) {
if (_syncScrollingByOthers) {
_syncScrollingByOthers = false;
} else {
SyncScrollOffset = _scrollViewer.Offset;
}
}

private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs e) {
Expand Down Expand Up @@ -591,9 +603,18 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
Text = string.Empty;
}
} else if (change.Property == SyncScrollOffsetProperty) {
if (TextArea.TextView.ScrollOffset != SyncScrollOffset) {
IScrollable scrollable = TextArea.TextView;
scrollable.Offset = SyncScrollOffset;
if (_scrollViewer == null) return;

var curOffset = _scrollViewer.Offset;
if (!curOffset.Equals(SyncScrollOffset)) {
_syncScrollingByOthers = true;

if (curOffset.X != SyncScrollOffset.X) {
var offset = new Vector(Math.Min(_scrollViewer.ScrollBarMaximum.X, SyncScrollOffset.X), SyncScrollOffset.Y);
_scrollViewer.Offset = offset;
} else {
_scrollViewer.Offset = SyncScrollOffset;
}
}
} else if (change.Property.Name == "ActualThemeVariant" && change.NewValue != null && _textMate != null) {
if (App.Current?.ActualThemeVariant == ThemeVariant.Dark) {
Expand All @@ -617,6 +638,9 @@ private void UpdateGrammar() {

private RegistryOptions _registryOptions;
private TextMate.Installation _textMate;

private ScrollViewer _scrollViewer = null;
private bool _syncScrollingByOthers = false;
}

public partial class TextDiffView : UserControl {
Expand Down

0 comments on commit 318a9c6

Please sign in to comment.