refactor: sync scroll implement

This commit is contained in:
leo 2024-06-05 20:33:33 +08:00
parent ce35a0365d
commit 0c618998b2
No known key found for this signature in database
GPG key ID: B528468E49CD0E58
3 changed files with 33 additions and 13 deletions

View file

@ -5,8 +5,6 @@ using System.Text.RegularExpressions;
using Avalonia; using Avalonia;
using Avalonia.Media.Imaging; using Avalonia.Media.Imaging;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.Models namespace SourceGit.Models
{ {
public enum TextDiffLineType 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 string File { get; set; } = string.Empty;
public List<TextDiffLine> Lines { get; set; } = new List<TextDiffLine>(); public List<TextDiffLine> Lines { get; set; } = new List<TextDiffLine>();
public int MaxLineNumber = 0; public int MaxLineNumber = 0;
public Vector SyncScrollOffset = Vector.Zero;
private Vector _syncScrollOffset = Vector.Zero;
public Vector SyncScrollOffset {
get => _syncScrollOffset;
set => SetProperty(ref _syncScrollOffset, value);
}
public void GenerateNewPatchFromSelection(Change change, string fileBlobGuid, TextDiffSelection selection, bool revert, string output) public void GenerateNewPatchFromSelection(Change change, string fileBlobGuid, TextDiffSelection selection, bool revert, string output)
{ {

View file

@ -17,7 +17,7 @@
SecondaryFG="{DynamicResource Brush.FG2}" SecondaryFG="{DynamicResource Brush.FG2}"
FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont}" FontFamily="{Binding Source={x:Static vm:Preference.Instance}, Path=MonospaceFont}"
DiffData="{Binding}" 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}" UseSyntaxHighlighting="{Binding Source={x:Static vm:Preference.Instance}, Path=UseSyntaxHighlighting}"
WordWrap="{Binding Source={x:Static vm:Preference.Instance}, Path=EnableDiffViewWordWrap}"/> WordWrap="{Binding Source={x:Static vm:Preference.Instance}, Path=EnableDiffViewWordWrap}"/>
</DataTemplate> </DataTemplate>
@ -25,7 +25,7 @@
<DataTemplate DataType="vm:TwoSideTextDiff"> <DataTemplate DataType="vm:TwoSideTextDiff">
<Grid ColumnDefinitions="*,1,*"> <Grid ColumnDefinitions="*,1,*">
<v:SingleSideTextDiffPresenter Grid.Column="0" <v:SingleSideTextDiffPresenter Grid.Column="0"
SyncScrollOffset="{Binding #ThisControl.TextDiff.SyncScrollOffset, Mode=TwoWay}" SyncScrollOffset="{Binding #ThisControl.SyncScrollOffset, Mode=TwoWay}"
UseSyntaxHighlighting="{Binding Source={x:Static vm:Preference.Instance}, Path=UseSyntaxHighlighting}" UseSyntaxHighlighting="{Binding Source={x:Static vm:Preference.Instance}, Path=UseSyntaxHighlighting}"
WordWrap="{Binding Source={x:Static vm:Preference.Instance}, Path=EnableDiffViewWordWrap}" WordWrap="{Binding Source={x:Static vm:Preference.Instance}, Path=EnableDiffViewWordWrap}"
IsOld="True" IsOld="True"
@ -39,7 +39,7 @@
<Rectangle Grid.Column="1" Fill="{DynamicResource Brush.Border2}" Width="1" HorizontalAlignment="Center" VerticalAlignment="Stretch"/> <Rectangle Grid.Column="1" Fill="{DynamicResource Brush.Border2}" Width="1" HorizontalAlignment="Center" VerticalAlignment="Stretch"/>
<v:SingleSideTextDiffPresenter Grid.Column="2" <v:SingleSideTextDiffPresenter Grid.Column="2"
SyncScrollOffset="{Binding #ThisControl.TextDiff.SyncScrollOffset, Mode=TwoWay}" SyncScrollOffset="{Binding #ThisControl.SyncScrollOffset, Mode=TwoWay}"
UseSyntaxHighlighting="{Binding Source={x:Static vm:Preference.Instance}, Path=UseSyntaxHighlighting}" UseSyntaxHighlighting="{Binding Source={x:Static vm:Preference.Instance}, Path=UseSyntaxHighlighting}"
WordWrap="{Binding Source={x:Static vm:Preference.Instance}, Path=EnableDiffViewWordWrap}" WordWrap="{Binding Source={x:Static vm:Preference.Instance}, Path=EnableDiffViewWordWrap}"
IsOld="False" IsOld="False"

View file

@ -840,6 +840,15 @@ namespace SourceGit.Views
set => SetValue(UseSideBySideDiffProperty, value); set => SetValue(UseSideBySideDiffProperty, value);
} }
public static readonly StyledProperty<Vector> SyncScrollOffsetProperty =
AvaloniaProperty.Register<TextDiffView, Vector>(nameof(SyncScrollOffset));
public Vector SyncScrollOffset
{
get => GetValue(SyncScrollOffsetProperty);
set => SetValue(SyncScrollOffsetProperty, value);
}
public TextDiffView() public TextDiffView()
{ {
InitializeComponent(); InitializeComponent();
@ -1081,7 +1090,7 @@ namespace SourceGit.Views
{ {
base.OnPropertyChanged(change); base.OnPropertyChanged(change);
if (change.Property == TextDiffProperty || change.Property == UseSideBySideDiffProperty) if (change.Property == TextDiffProperty)
{ {
if (TextDiff == null) if (TextDiff == null)
{ {
@ -1090,12 +1099,30 @@ namespace SourceGit.Views
else if (UseSideBySideDiff) else if (UseSideBySideDiff)
{ {
Content = new ViewModels.TwoSideTextDiff(TextDiff); Content = new ViewModels.TwoSideTextDiff(TextDiff);
SyncScrollOffset = TextDiff.SyncScrollOffset;
} }
else else
{ {
Content = TextDiff; 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) private Models.TextDiffSelection GetUnifiedSelection(int startLine, int endLine, bool isOldSide)