diff --git a/src/Models/Watcher.cs b/src/Models/Watcher.cs index c26d9673..d20e52d3 100644 --- a/src/Models/Watcher.cs +++ b/src/Models/Watcher.cs @@ -145,7 +145,7 @@ namespace SourceGit.Models { name.StartsWith("worktrees/")) { _updateBranch = DateTime.Now.AddSeconds(.5).ToFileTime(); } else if (name.StartsWith("objects/", StringComparison.Ordinal) || name.Equals("index", StringComparison.Ordinal)) { - _updateWC = DateTime.Now.AddSeconds(.5).ToFileTime(); + _updateWC = DateTime.Now.AddSeconds(1).ToFileTime(); } } @@ -154,7 +154,7 @@ namespace SourceGit.Models { var name = e.Name.Replace("\\", "/"); if (name == ".git" || name.StartsWith(".git/", StringComparison.Ordinal)) return; - if (_updateWC == 0) _updateWC = DateTime.Now.AddSeconds(1).ToFileTime(); + _updateWC = DateTime.Now.AddSeconds(1).ToFileTime(); } private IRepository _repo = null; diff --git a/src/ViewModels/TwoSideTextDiff.cs b/src/ViewModels/TwoSideTextDiff.cs index f8bc46d4..a916f54f 100644 --- a/src/ViewModels/TwoSideTextDiff.cs +++ b/src/ViewModels/TwoSideTextDiff.cs @@ -1,5 +1,4 @@ -using Avalonia; -using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.ComponentModel; using System.Collections.Generic; namespace SourceGit.ViewModels { diff --git a/src/Views/TextDiffView.axaml.cs b/src/Views/TextDiffView.axaml.cs index 15e9c1a9..3fc5fb05 100644 --- a/src/Views/TextDiffView.axaml.cs +++ b/src/Views/TextDiffView.axaml.cs @@ -490,7 +490,7 @@ namespace SourceGit.Views { } public static readonly StyledProperty SyncScrollOffsetProperty = - AvaloniaProperty.Register(nameof(SyncScrollOffset)); + AvaloniaProperty.Register(nameof(SyncScrollOffset), Vector.Zero); public Vector SyncScrollOffset { get => GetValue(SyncScrollOffsetProperty); @@ -508,12 +508,17 @@ namespace SourceGit.Views { protected override void OnLoaded(RoutedEventArgs e) { base.OnLoaded(e); + _scrollViewer = this.FindDescendantOfType(); + if (_scrollViewer != null) { + _scrollViewer.Offset = SyncScrollOffset; + _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); @@ -531,19 +536,27 @@ namespace SourceGit.Views { 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) { @@ -591,9 +604,18 @@ namespace SourceGit.Views { 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) { @@ -617,6 +639,9 @@ namespace SourceGit.Views { private RegistryOptions _registryOptions; private TextMate.Installation _textMate; + + private ScrollViewer _scrollViewer = null; + private bool _syncScrollingByOthers = false; } public partial class TextDiffView : UserControl {