From 0c618998b210ca8b489fe463133f7d5a0ffc4180 Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 5 Jun 2024 20:33:33 +0800 Subject: [PATCH] refactor: sync scroll implement --- src/Models/DiffResult.cs | 11 ++--------- src/Views/TextDiffView.axaml | 6 +++--- src/Views/TextDiffView.axaml.cs | 29 ++++++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/Models/DiffResult.cs b/src/Models/DiffResult.cs index 71099390..d99c7a0e 100644 --- a/src/Models/DiffResult.cs +++ b/src/Models/DiffResult.cs @@ -5,8 +5,6 @@ using System.Text.RegularExpressions; using Avalonia; using Avalonia.Media.Imaging; -using CommunityToolkit.Mvvm.ComponentModel; - namespace SourceGit.Models { public enum TextDiffLineType @@ -61,17 +59,12 @@ namespace SourceGit.Models } } - public partial class TextDiff : ObservableObject + public partial class TextDiff { public string File { get; set; } = string.Empty; public List Lines { get; set; } = new List(); public int MaxLineNumber = 0; - - private Vector _syncScrollOffset = Vector.Zero; - public Vector SyncScrollOffset { - get => _syncScrollOffset; - set => SetProperty(ref _syncScrollOffset, value); - } + public Vector SyncScrollOffset = Vector.Zero; public void GenerateNewPatchFromSelection(Change change, string fileBlobGuid, TextDiffSelection selection, bool revert, string output) { diff --git a/src/Views/TextDiffView.axaml b/src/Views/TextDiffView.axaml index bef3c878..b4758f06 100644 --- a/src/Views/TextDiffView.axaml +++ b/src/Views/TextDiffView.axaml @@ -17,7 +17,7 @@ SecondaryFG="{DynamicResource Brush.FG2}" FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont}" DiffData="{Binding}" - SyncScrollOffset="{Binding #ThisControl.TextDiff.SyncScrollOffset, Mode=TwoWay}" + SyncScrollOffset="{Binding #ThisControl.SyncScrollOffset, Mode=TwoWay}" UseSyntaxHighlighting="{Binding Source={x:Static vm:Preference.Instance}, Path=UseSyntaxHighlighting}" WordWrap="{Binding Source={x:Static vm:Preference.Instance}, Path=EnableDiffViewWordWrap}"/> @@ -25,7 +25,7 @@ SetValue(UseSideBySideDiffProperty, value); } + public static readonly StyledProperty SyncScrollOffsetProperty = + AvaloniaProperty.Register(nameof(SyncScrollOffset)); + + public Vector SyncScrollOffset + { + get => GetValue(SyncScrollOffsetProperty); + set => SetValue(SyncScrollOffsetProperty, value); + } + public TextDiffView() { InitializeComponent(); @@ -1081,7 +1090,7 @@ namespace SourceGit.Views { base.OnPropertyChanged(change); - if (change.Property == TextDiffProperty || change.Property == UseSideBySideDiffProperty) + if (change.Property == TextDiffProperty) { if (TextDiff == null) { @@ -1090,12 +1099,30 @@ namespace SourceGit.Views else if (UseSideBySideDiff) { Content = new ViewModels.TwoSideTextDiff(TextDiff); + SyncScrollOffset = TextDiff.SyncScrollOffset; } else { Content = TextDiff; + SyncScrollOffset = TextDiff.SyncScrollOffset; } } + else if (change.Property == UseSideBySideDiffProperty) + { + SyncScrollOffset = Vector.Zero; + + if (TextDiff == null) + Content = null; + else if (UseSideBySideDiff) + Content = new ViewModels.TwoSideTextDiff(TextDiff); + else + Content = TextDiff; + } + else if (change.Property == SyncScrollOffsetProperty) + { + if (TextDiff != null) + TextDiff.SyncScrollOffset = SyncScrollOffset; + } } private Models.TextDiffSelection GetUnifiedSelection(int startLine, int endLine, bool isOldSide)