Skip to content

Commit 03f49cc

Browse files
committed
refactor: text diff view block navigation
Signed-off-by: leo <[email protected]>
1 parent 39f4cd1 commit 03f49cc

File tree

3 files changed

+77
-86
lines changed

3 files changed

+77
-86
lines changed

src/Views/DiffView.axaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,9 @@
285285

286286
<!-- Text Diff -->
287287
<DataTemplate DataType="m:TextDiff">
288-
<v:TextDiffView
289-
UseSideBySideDiff="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseSideBySideDiff, Mode=OneWay}"
290-
UseBlockNavigation="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseBlockNavigationInDiffView, Mode=OneWay}"
291-
BlockNavigationIndicator="{Binding #BlockNavigationIndicator.Text, Mode=OneWayToSource}"/>
288+
<v:TextDiffView UseSideBySideDiff="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseSideBySideDiff, Mode=OneWay}"
289+
UseBlockNavigation="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseBlockNavigationInDiffView, Mode=OneWay}"
290+
BlockNavigationChanged="OnBlockNavigationChanged"/>
292291
</DataTemplate>
293292

294293
<!-- Empty or only EOL changes -->

src/Views/DiffView.axaml.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,32 @@ public DiffView()
1313

1414
private void OnGotoFirstChange(object _, RoutedEventArgs e)
1515
{
16-
var textDiff = this.FindDescendantOfType<TextDiffView>();
17-
textDiff?.GotoFirstChange();
16+
this.FindDescendantOfType<TextDiffView>()?.GotoFirstChange();
1817
e.Handled = true;
1918
}
2019

2120
private void OnGotoPrevChange(object _, RoutedEventArgs e)
2221
{
23-
var textDiff = this.FindDescendantOfType<TextDiffView>();
24-
textDiff?.GotoPrevChange();
22+
this.FindDescendantOfType<TextDiffView>()?.GotoPrevChange();
2523
e.Handled = true;
2624
}
2725

2826
private void OnGotoNextChange(object _, RoutedEventArgs e)
2927
{
30-
var textDiff = this.FindDescendantOfType<TextDiffView>();
31-
textDiff?.GotoNextChange();
28+
this.FindDescendantOfType<TextDiffView>()?.GotoNextChange();
3229
e.Handled = true;
3330
}
3431

3532
private void OnGotoLastChange(object _, RoutedEventArgs e)
3633
{
37-
var textDiff = this.FindDescendantOfType<TextDiffView>();
38-
textDiff?.GotoLastChange();
34+
this.FindDescendantOfType<TextDiffView>()?.GotoLastChange();
3935
e.Handled = true;
4036
}
37+
38+
private void OnBlockNavigationChanged(object sender, RoutedEventArgs e)
39+
{
40+
if (sender is TextDiffView { UseBlockNavigation: true } textDiff)
41+
BlockNavigationIndicator.Text = textDiff.BlockNavigation?.Indicator ?? string.Empty;
42+
}
4143
}
4244
}

src/Views/TextDiffView.axaml.cs

Lines changed: 64 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ public virtual void UpdateSelectedChunk(double y)
553553
{
554554
}
555555

556-
public void GotoFirstChange()
556+
public virtual void GotoFirstChange()
557557
{
558558
var blockNavigation = BlockNavigation;
559559
if (blockNavigation != null)
@@ -567,7 +567,7 @@ public void GotoFirstChange()
567567
}
568568
}
569569

570-
public void GotoPrevChange()
570+
public virtual void GotoPrevChange()
571571
{
572572
var blockNavigation = BlockNavigation;
573573
if (blockNavigation != null)
@@ -623,7 +623,7 @@ public void GotoPrevChange()
623623
}
624624
}
625625

626-
public void GotoNextChange()
626+
public virtual void GotoNextChange()
627627
{
628628
var blockNavigation = BlockNavigation;
629629
if (blockNavigation != null)
@@ -665,7 +665,7 @@ public void GotoNextChange()
665665
}
666666
}
667667

668-
public void GotoLastChange()
668+
public virtual void GotoLastChange()
669669
{
670670
var blockNavigation = BlockNavigation;
671671
if (blockNavigation != null)
@@ -766,15 +766,13 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
766766
{
767767
var oldValue = change.OldValue as ViewModels.BlockNavigation;
768768
if (oldValue != null)
769-
{
770769
oldValue.PropertyChanged -= OnBlockNavigationPropertyChanged;
771-
if (oldValue.Current != -1)
772-
TextArea?.TextView?.Redraw();
773-
}
774770

775771
var newValue = change.NewValue as ViewModels.BlockNavigation;
776772
if (newValue != null)
777773
newValue.PropertyChanged += OnBlockNavigationPropertyChanged;
774+
775+
TextArea?.TextView?.Redraw();
778776
}
779777
}
780778

@@ -793,9 +791,10 @@ private void OnTextAreaKeyDown(object sender, KeyEventArgs e)
793791
base.OnKeyDown(e);
794792
}
795793

796-
private void OnBlockNavigationPropertyChanged(object _1, PropertyChangedEventArgs _2)
794+
private void OnBlockNavigationPropertyChanged(object _1, PropertyChangedEventArgs e)
797795
{
798-
TextArea?.TextView?.Redraw();
796+
if (e.PropertyName == "Current")
797+
TextArea?.TextView?.Redraw();
799798
}
800799

801800
private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs e)
@@ -1223,19 +1222,18 @@ protected override void OnLoaded(RoutedEventArgs e)
12231222
{
12241223
base.OnLoaded(e);
12251224

1226-
var scroller = this.FindDescendantOfType<ScrollViewer>();
1227-
if (scroller != null)
1225+
_scrollViewer = this.FindDescendantOfType<ScrollViewer>();
1226+
if (_scrollViewer != null)
12281227
{
1229-
scroller.Bind(ScrollViewer.OffsetProperty, new Binding("ScrollOffset", BindingMode.TwoWay));
1230-
scroller.ScrollChanged += OnTextViewScrollChanged;
1228+
_scrollViewer.Bind(ScrollViewer.OffsetProperty, new Binding("ScrollOffset", BindingMode.TwoWay));
1229+
_scrollViewer.ScrollChanged += OnTextViewScrollChanged;
12311230
}
12321231
}
12331232

12341233
protected override void OnUnloaded(RoutedEventArgs e)
12351234
{
1236-
var scroller = this.FindDescendantOfType<ScrollViewer>();
1237-
if (scroller != null)
1238-
scroller.ScrollChanged -= OnTextViewScrollChanged;
1235+
if (_scrollViewer != null)
1236+
_scrollViewer.ScrollChanged -= OnTextViewScrollChanged;
12391237

12401238
base.OnUnloaded(e);
12411239
}
@@ -1274,9 +1272,11 @@ protected override void OnDataContextChanged(EventArgs e)
12741272

12751273
private void OnTextViewScrollChanged(object sender, ScrollChangedEventArgs e)
12761274
{
1277-
if (sender is ScrollViewer { IsExpanded: true, IsPointerOver: true } scroller)
1275+
if (_scrollViewer is { IsExpanded: true, IsPointerOver: true })
12781276
TrySetChunk(null);
12791277
}
1278+
1279+
private ScrollViewer _scrollViewer = null;
12801280
}
12811281

12821282
public class SingleSideTextDiffPresenter : ThemedTextDiffPresenter
@@ -1288,14 +1288,6 @@ public class SingleSideTextDiffPresenter : ThemedTextDiffPresenter
12881288
TextArea.LeftMargins.Add(new LineModifyTypeMargin());
12891289
}
12901290

1291-
public void ForceSyncScrollOffset()
1292-
{
1293-
if (_scrollViewer == null)
1294-
return;
1295-
if (DataContext is ViewModels.TwoSideTextDiff diff)
1296-
diff.SyncScrollOffset = _scrollViewer?.Offset ?? Vector.Zero;
1297-
}
1298-
12991291
public override List<Models.TextDiffLine> GetLines()
13001292
{
13011293
if (DataContext is ViewModels.TwoSideTextDiff diff)
@@ -1310,6 +1302,30 @@ public override int GetMaxLineNumber()
13101302
return 0;
13111303
}
13121304

1305+
public override void GotoFirstChange()
1306+
{
1307+
base.GotoFirstChange();
1308+
DirectSyncScrollOffset();
1309+
}
1310+
1311+
public override void GotoPrevChange()
1312+
{
1313+
base.GotoPrevChange();
1314+
DirectSyncScrollOffset();
1315+
}
1316+
1317+
public override void GotoNextChange()
1318+
{
1319+
base.GotoNextChange();
1320+
DirectSyncScrollOffset();
1321+
}
1322+
1323+
public override void GotoLastChange()
1324+
{
1325+
base.GotoLastChange();
1326+
DirectSyncScrollOffset();
1327+
}
1328+
13131329
public override void UpdateSelectedChunk(double y)
13141330
{
13151331
var diff = DataContext as ViewModels.TwoSideTextDiff;
@@ -1497,11 +1513,17 @@ private void OnTextViewScrollChanged(object sender, ScrollChangedEventArgs e)
14971513
{
14981514
diff.SyncScrollOffset = _scrollViewer?.Offset ?? Vector.Zero;
14991515

1500-
if (sender is ScrollViewer { IsExpanded: true, IsPointerOver: true } scroller )
1516+
if (_scrollViewer is { IsExpanded: true, IsPointerOver: true } )
15011517
TrySetChunk(null);
15021518
}
15031519
}
15041520

1521+
private void DirectSyncScrollOffset()
1522+
{
1523+
if (_scrollViewer is { } && DataContext is ViewModels.TwoSideTextDiff diff)
1524+
diff.SyncScrollOffset = _scrollViewer?.Offset ?? Vector.Zero;
1525+
}
1526+
15051527
private ScrollViewer _scrollViewer = null;
15061528
}
15071529

@@ -1679,13 +1701,13 @@ public ViewModels.BlockNavigation BlockNavigation
16791701
set => SetValue(BlockNavigationProperty, value);
16801702
}
16811703

1682-
public static readonly StyledProperty<string> BlockNavigationIndicatorProperty =
1683-
AvaloniaProperty.Register<TextDiffView, string>(nameof(BlockNavigationIndicator));
1704+
public static readonly RoutedEvent<RoutedEventArgs> BlockNavigationChangedEvent =
1705+
RoutedEvent.Register<TextDiffView, RoutedEventArgs>(nameof(BlockNavigationChanged), RoutingStrategies.Tunnel | RoutingStrategies.Bubble);
16841706

1685-
public string BlockNavigationIndicator
1707+
public event EventHandler<RoutedEventArgs> BlockNavigationChanged
16861708
{
1687-
get => GetValue(BlockNavigationIndicatorProperty);
1688-
set => SetValue(BlockNavigationIndicatorProperty, value);
1709+
add { AddHandler(BlockNavigationChangedEvent, value); }
1710+
remove { RemoveHandler(BlockNavigationChangedEvent, value); }
16891711
}
16901712

16911713
static TextDiffView()
@@ -1723,54 +1745,26 @@ public TextDiffView()
17231745

17241746
public void GotoFirstChange()
17251747
{
1726-
var presenter = this.FindDescendantOfType<ThemedTextDiffPresenter>();
1727-
if (presenter == null)
1728-
return;
1729-
1730-
presenter.GotoFirstChange();
1731-
if (presenter is SingleSideTextDiffPresenter singleSide)
1732-
singleSide.ForceSyncScrollOffset();
1733-
1734-
BlockNavigationIndicator = BlockNavigation?.Indicator ?? string.Empty;
1748+
this.FindDescendantOfType<ThemedTextDiffPresenter>()?.GotoFirstChange();
1749+
RaiseEvent(new RoutedEventArgs(BlockNavigationChangedEvent));
17351750
}
17361751

17371752
public void GotoPrevChange()
17381753
{
1739-
var presenter = this.FindDescendantOfType<ThemedTextDiffPresenter>();
1740-
if (presenter == null)
1741-
return;
1742-
1743-
presenter.GotoPrevChange();
1744-
if (presenter is SingleSideTextDiffPresenter singleSide)
1745-
singleSide.ForceSyncScrollOffset();
1746-
1747-
BlockNavigationIndicator = BlockNavigation?.Indicator ?? string.Empty;
1754+
this.FindDescendantOfType<ThemedTextDiffPresenter>()?.GotoPrevChange();
1755+
RaiseEvent(new RoutedEventArgs(BlockNavigationChangedEvent));
17481756
}
17491757

17501758
public void GotoNextChange()
17511759
{
1752-
var presenter = this.FindDescendantOfType<ThemedTextDiffPresenter>();
1753-
if (presenter == null)
1754-
return;
1755-
1756-
presenter.GotoNextChange();
1757-
if (presenter is SingleSideTextDiffPresenter singleSide)
1758-
singleSide.ForceSyncScrollOffset();
1759-
1760-
BlockNavigationIndicator = BlockNavigation?.Indicator ?? string.Empty;
1760+
this.FindDescendantOfType<ThemedTextDiffPresenter>()?.GotoNextChange();
1761+
RaiseEvent(new RoutedEventArgs(BlockNavigationChangedEvent));
17611762
}
17621763

17631764
public void GotoLastChange()
17641765
{
1765-
var presenter = this.FindDescendantOfType<ThemedTextDiffPresenter>();
1766-
if (presenter == null)
1767-
return;
1768-
1769-
presenter.GotoLastChange();
1770-
if (presenter is SingleSideTextDiffPresenter singleSide)
1771-
singleSide.ForceSyncScrollOffset();
1772-
1773-
BlockNavigationIndicator = BlockNavigation?.Indicator ?? string.Empty;
1766+
this.FindDescendantOfType<ThemedTextDiffPresenter>()?.GotoLastChange();
1767+
RaiseEvent(new RoutedEventArgs(BlockNavigationChangedEvent));
17741768
}
17751769

17761770
protected override void OnDataContextChanged(EventArgs e)
@@ -1820,15 +1814,11 @@ private void RefreshContent(Models.TextDiff diff, bool keepScrollOffset = true)
18201814
private void RefreshBlockNavigation()
18211815
{
18221816
if (UseBlockNavigation)
1823-
{
18241817
BlockNavigation = new ViewModels.BlockNavigation(Editor.Content);
1825-
BlockNavigationIndicator = BlockNavigation.Indicator;
1826-
}
18271818
else
1828-
{
18291819
BlockNavigation = null;
1830-
BlockNavigationIndicator = "-/-";
1831-
}
1820+
1821+
RaiseEvent(new RoutedEventArgs(BlockNavigationChangedEvent));
18321822
}
18331823

18341824
private void OnStageChunk(object _1, RoutedEventArgs _2)

0 commit comments

Comments
 (0)