Skip to content

Commit 91801cf

Browse files
committed
enhance: only trigger UpdateSelectedChunk if needed
1 parent 5b95344 commit 91801cf

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

src/ViewModels/TwoSideTextDiff.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,10 @@ public Vector SyncScrollOffset
2020
set => SetProperty(ref _syncScrollOffset, value);
2121
}
2222

23-
public Models.DiffOption Option
24-
{
25-
get;
26-
set;
27-
}
28-
2923
public TwoSideTextDiff(Models.TextDiff diff, TwoSideTextDiff previous = null)
3024
{
3125
File = diff.File;
3226
MaxLineNumber = diff.MaxLineNumber;
33-
Option = diff.Option;
3427

3528
foreach (var line in diff.Lines)
3629
{

src/Views/TextDiffView.axaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
UseSyntaxHighlighting="{Binding Source={x:Static vm:Preference.Instance}, Path=UseSyntaxHighlighting}"
2727
WordWrap="{Binding Source={x:Static vm:Preference.Instance}, Path=EnableDiffViewWordWrap}"
2828
ShowHiddenSymbols="{Binding Source={x:Static vm:Preference.Instance}, Path=ShowHiddenSymbolsInDiffView}"
29+
EnableChunkSelection="{Binding #ThisControl.EnableChunkSelection}"
2930
SelectedChunk="{Binding #ThisControl.SelectedChunk, Mode=TwoWay}"/>
3031
</DataTemplate>
3132

@@ -46,6 +47,7 @@
4647
UseSyntaxHighlighting="{Binding Source={x:Static vm:Preference.Instance}, Path=UseSyntaxHighlighting}"
4748
WordWrap="{Binding Source={x:Static vm:Preference.Instance}, Path=EnableDiffViewWordWrap}"
4849
ShowHiddenSymbols="{Binding Source={x:Static vm:Preference.Instance}, Path=ShowHiddenSymbolsInDiffView}"
50+
EnableChunkSelection="{Binding #ThisControl.EnableChunkSelection}"
4951
SelectedChunk="{Binding #ThisControl.SelectedChunk, Mode=TwoWay}"/>
5052

5153
<Rectangle Grid.Column="1" Fill="{DynamicResource Brush.Border2}" Width="1" HorizontalAlignment="Center" VerticalAlignment="Stretch"/>
@@ -65,6 +67,7 @@
6567
UseSyntaxHighlighting="{Binding Source={x:Static vm:Preference.Instance}, Path=UseSyntaxHighlighting}"
6668
WordWrap="{Binding Source={x:Static vm:Preference.Instance}, Path=EnableDiffViewWordWrap}"
6769
ShowHiddenSymbols="{Binding Source={x:Static vm:Preference.Instance}, Path=ShowHiddenSymbolsInDiffView}"
70+
EnableChunkSelection="{Binding #ThisControl.EnableChunkSelection}"
6871
SelectedChunk="{Binding #ThisControl.SelectedChunk, Mode=TwoWay}"/>
6972
</Grid>
7073
</DataTemplate>

src/Views/TextDiffView.axaml.cs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,15 @@ public bool ShowHiddenSymbols
336336
set => SetValue(ShowHiddenSymbolsProperty, value);
337337
}
338338

339+
public static readonly StyledProperty<bool> EnableChunkSelectionProperty =
340+
AvaloniaProperty.Register<ThemedTextDiffPresenter, bool>(nameof(EnableChunkSelection));
341+
342+
public bool EnableChunkSelection
343+
{
344+
get => GetValue(EnableChunkSelectionProperty);
345+
set => SetValue(EnableChunkSelectionProperty, value);
346+
}
347+
339348
public static readonly StyledProperty<TextDiffViewChunk> SelectedChunkProperty =
340349
AvaloniaProperty.Register<ThemedTextDiffPresenter, TextDiffViewChunk>(nameof(SelectedChunk));
341350

@@ -479,13 +488,13 @@ private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs
479488

480489
private void OnTextViewPointerMoved(object sender, PointerEventArgs e)
481490
{
482-
if (sender is TextView view)
491+
if (EnableChunkSelection && sender is TextView view)
483492
UpdateSelectedChunk(e.GetPosition(view).Y + view.VerticalOffset);
484493
}
485494

486495
private void OnTextViewPointerWheelChanged(object sender, PointerWheelEventArgs e)
487496
{
488-
if (sender is TextView view)
497+
if (EnableChunkSelection && sender is TextView view)
489498
{
490499
var y = e.GetPosition(view).Y + view.VerticalOffset;
491500
Dispatcher.UIThread.Post(() => UpdateSelectedChunk(y));
@@ -636,7 +645,7 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
636645
public override void UpdateSelectedChunk(double y)
637646
{
638647
var diff = DataContext as Models.TextDiff;
639-
if (diff == null || diff.Option.WorkingCopyChange == null)
648+
if (diff == null)
640649
return;
641650

642651
var view = TextArea.TextView;
@@ -796,7 +805,7 @@ public override int GetMaxLineNumber()
796805
public override void UpdateSelectedChunk(double y)
797806
{
798807
var diff = DataContext as ViewModels.TwoSideTextDiff;
799-
if (diff == null || diff.Option.WorkingCopyChange == null)
808+
if (diff == null)
800809
return;
801810

802811
var parent = this.FindAncestorOfType<TextDiffView>();
@@ -1012,6 +1021,15 @@ public bool IsUnstagedChange
10121021
set => SetValue(IsUnstagedChangeProperty, value);
10131022
}
10141023

1024+
public static readonly StyledProperty<bool> EnableChunkSelectionProperty =
1025+
AvaloniaProperty.Register<TextDiffView, bool>(nameof(EnableChunkSelection));
1026+
1027+
public bool EnableChunkSelection
1028+
{
1029+
get => GetValue(EnableChunkSelectionProperty);
1030+
set => SetValue(EnableChunkSelectionProperty, value);
1031+
}
1032+
10151033
static TextDiffView()
10161034
{
10171035
UseSideBySideDiffProperty.Changed.AddClassHandler<TextDiffView>((v, _) =>
@@ -1069,6 +1087,7 @@ protected override void OnDataContextChanged(EventArgs e)
10691087
Editor.Content = diff;
10701088

10711089
IsUnstagedChange = diff.Option.IsUnstaged;
1090+
EnableChunkSelection = diff.Option.WorkingCopyChange != null;
10721091
}
10731092

10741093
protected override void OnPointerExited(PointerEventArgs e)
@@ -1160,8 +1179,6 @@ private void OnUnstageChunk(object sender, RoutedEventArgs e)
11601179
if (!selection.HasChanges)
11611180
return;
11621181

1163-
// If all changes has been selected the use method provided by ViewModels.WorkingCopy.
1164-
// Otherwise, use `git apply`
11651182
if (!selection.HasLeftChanges)
11661183
{
11671184
var workcopyView = this.FindAncestorOfType<WorkingCopy>();
@@ -1218,8 +1235,6 @@ private void OnDiscardChunk(object sender, RoutedEventArgs e)
12181235
if (!selection.HasChanges)
12191236
return;
12201237

1221-
// If all changes has been selected the use method provided by ViewModels.WorkingCopy.
1222-
// Otherwise, use `git apply`
12231238
if (!selection.HasLeftChanges)
12241239
{
12251240
var workcopyView = this.FindAncestorOfType<WorkingCopy>();

0 commit comments

Comments
 (0)