Skip to content

Commit 9c6e2ea

Browse files
committed
fix: auto-navigating to first change sometimes does not work (#1952)
Signed-off-by: leo <longshuang@msn.cn>
1 parent e2feac3 commit 9c6e2ea

File tree

4 files changed

+38
-115
lines changed

4 files changed

+38
-115
lines changed

src/ViewModels/BlockNavigation.cs

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33

44
namespace SourceGit.ViewModels
55
{
6+
public enum BlockNavigationDirection
7+
{
8+
First = 0,
9+
Prev,
10+
Next,
11+
Last
12+
}
13+
614
public class BlockNavigation : ObservableObject
715
{
816
public record Block(int Start, int End)
@@ -67,7 +75,7 @@ public BlockNavigation(List<Models.TextDiffLine> lines, bool gotoFirst)
6775
_blocks.AddRange(blocks);
6876

6977
if (gotoFirst)
70-
GotoFirst();
78+
Goto(BlockNavigationDirection.First);
7179
}
7280

7381
public Block GetCurrentBlock()
@@ -78,48 +86,20 @@ public Block GetCurrentBlock()
7886
return null;
7987
}
8088

81-
public Block GotoFirst()
89+
public Block Goto(BlockNavigationDirection direction)
8290
{
8391
if (_blocks.Count == 0)
8492
return null;
8593

86-
_current = 0;
87-
OnPropertyChanged(nameof(Indicator));
88-
return _blocks[_current];
89-
}
90-
91-
public Block GotoPrev()
92-
{
93-
if (_blocks.Count == 0)
94-
return null;
95-
96-
if (_current == -1)
97-
_current = 0;
98-
else if (_current > 0)
99-
_current--;
100-
101-
OnPropertyChanged(nameof(Indicator));
102-
return _blocks[_current];
103-
}
104-
105-
public Block GotoNext()
106-
{
107-
if (_blocks.Count == 0)
108-
return null;
109-
110-
if (_current < _blocks.Count - 1)
111-
_current++;
112-
113-
OnPropertyChanged(nameof(Indicator));
114-
return _blocks[_current];
115-
}
116-
117-
public Block GotoLast()
118-
{
119-
if (_blocks.Count == 0)
120-
return null;
94+
_current = direction switch
95+
{
96+
BlockNavigationDirection.First => 0,
97+
BlockNavigationDirection.Prev => _current <= 0 ? 0 : _current - 1,
98+
BlockNavigationDirection.Next => _current >= _blocks.Count - 1 ? _blocks.Count - 1 : _current + 1,
99+
BlockNavigationDirection.Last => _blocks.Count - 1,
100+
_ => _current
101+
};
121102

122-
_current = _blocks.Count - 1;
123103
OnPropertyChanged(nameof(Indicator));
124104
return _blocks[_current];
125105
}

src/ViewModels/DiffContext.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,7 @@ public void CheckSettings()
135135
}
136136

137137
if (ctx.IsSideBySide() != UseSideBySide)
138-
{
139-
ctx = ctx.SwitchMode();
140-
Content = ctx;
141-
}
138+
Content = ctx.SwitchMode();
142139
}
143140
}
144141

src/Views/DiffView.axaml.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,25 @@ protected override void OnLoaded(RoutedEventArgs e)
2121

2222
private void OnGotoFirstChange(object _, RoutedEventArgs e)
2323
{
24-
this.FindDescendantOfType<ThemedTextDiffPresenter>()?.GotoFirstChange();
24+
this.FindDescendantOfType<ThemedTextDiffPresenter>()?.GotoChange(ViewModels.BlockNavigationDirection.First);
2525
e.Handled = true;
2626
}
2727

2828
private void OnGotoPrevChange(object _, RoutedEventArgs e)
2929
{
30-
this.FindDescendantOfType<ThemedTextDiffPresenter>()?.GotoPrevChange();
30+
this.FindDescendantOfType<ThemedTextDiffPresenter>()?.GotoChange(ViewModels.BlockNavigationDirection.Prev);
3131
e.Handled = true;
3232
}
3333

3434
private void OnGotoNextChange(object _, RoutedEventArgs e)
3535
{
36-
this.FindDescendantOfType<ThemedTextDiffPresenter>()?.GotoNextChange();
36+
this.FindDescendantOfType<ThemedTextDiffPresenter>()?.GotoChange(ViewModels.BlockNavigationDirection.Next);
3737
e.Handled = true;
3838
}
3939

4040
private void OnGotoLastChange(object _, RoutedEventArgs e)
4141
{
42-
this.FindDescendantOfType<ThemedTextDiffPresenter>()?.GotoLastChange();
42+
this.FindDescendantOfType<ThemedTextDiffPresenter>()?.GotoChange(ViewModels.BlockNavigationDirection.Last);
4343
e.Handled = true;
4444
}
4545
}

src/Views/TextDiffView.axaml.cs

Lines changed: 15 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -487,43 +487,21 @@ public virtual void UpdateSelectedChunk(double y)
487487
{
488488
}
489489

490-
public virtual void GotoFirstChange()
490+
public void GotoChange(ViewModels.BlockNavigationDirection direction)
491491
{
492-
var first = BlockNavigation.GotoFirst();
493-
if (first != null)
494-
{
495-
TextArea.Caret.Line = first.Start;
496-
ScrollToLine(first.Start);
497-
}
498-
}
492+
if (DataContext is not ViewModels.TextDiffContext ctx)
493+
return;
499494

500-
public virtual void GotoPrevChange()
501-
{
502-
var prev = BlockNavigation.GotoPrev();
503-
if (prev != null)
495+
var block = BlockNavigation.Goto(direction);
496+
if (block != null)
504497
{
505-
TextArea.Caret.Line = prev.Start;
506-
ScrollToLine(prev.Start);
507-
}
508-
}
498+
TextArea.Caret.Line = block.Start;
499+
ScrollToLine(block.Start);
509500

510-
public virtual void GotoNextChange()
511-
{
512-
var next = BlockNavigation.GotoNext();
513-
if (next != null)
514-
{
515-
TextArea.Caret.Line = next.Start;
516-
ScrollToLine(next.Start);
517-
}
518-
}
501+
if (ctx.IsSideBySide())
502+
{
519503

520-
public virtual void GotoLastChange()
521-
{
522-
var next = BlockNavigation.GotoLast();
523-
if (next != null)
524-
{
525-
TextArea.Caret.Line = next.Start;
526-
ScrollToLine(next.Start);
504+
}
527505
}
528506
}
529507

@@ -618,9 +596,9 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
618596
}
619597
}
620598

621-
protected override void OnDataContextBeginUpdate()
599+
protected override void OnDataContextChanged(EventArgs e)
622600
{
623-
base.OnDataContextBeginUpdate();
601+
base.OnDataContextChanged(e);
624602
AutoScrollToFirstChange();
625603
}
626604

@@ -805,9 +783,6 @@ private void AutoScrollToFirstChange()
805783
if (DataContext is not ViewModels.TextDiffContext ctx)
806784
return;
807785

808-
if (ctx.IsSideBySide() && !IsOld)
809-
return;
810-
811786
var curBlock = ctx.BlockNavigation.GetCurrentBlock();
812787
if (curBlock == null)
813788
return;
@@ -819,8 +794,9 @@ private void AutoScrollToFirstChange()
819794
var scroller = this.FindDescendantOfType<ScrollViewer>();
820795
if (scroller != null)
821796
{
822-
ctx.ScrollOffset = new Vector(0, vOffset);
823-
scroller.Offset = ctx.ScrollOffset;
797+
var scrollOffset = new Vector(0, vOffset);
798+
scroller.Offset = scrollOffset;
799+
ctx.ScrollOffset = scrollOffset;
824800
}
825801
}
826802
}
@@ -1098,30 +1074,6 @@ public class SingleSideTextDiffPresenter : ThemedTextDiffPresenter
10981074
return [];
10991075
}
11001076

1101-
public override void GotoFirstChange()
1102-
{
1103-
base.GotoFirstChange();
1104-
SyncScrollOffset();
1105-
}
1106-
1107-
public override void GotoPrevChange()
1108-
{
1109-
base.GotoPrevChange();
1110-
SyncScrollOffset();
1111-
}
1112-
1113-
public override void GotoNextChange()
1114-
{
1115-
base.GotoNextChange();
1116-
SyncScrollOffset();
1117-
}
1118-
1119-
public override void GotoLastChange()
1120-
{
1121-
base.GotoLastChange();
1122-
SyncScrollOffset();
1123-
}
1124-
11251077
public override void UpdateSelectedChunk(double y)
11261078
{
11271079
if (DataContext is not ViewModels.TwoSideTextDiff diff)
@@ -1318,12 +1270,6 @@ private void OnBlockNavigationPropertyChanged(object sender, PropertyChangedEven
13181270
TextArea?.TextView?.Redraw();
13191271
}
13201272

1321-
private void SyncScrollOffset()
1322-
{
1323-
if (_scrollViewer is not null && DataContext is ViewModels.TwoSideTextDiff diff)
1324-
diff.ScrollOffset = _scrollViewer.Offset;
1325-
}
1326-
13271273
private ScrollViewer _scrollViewer = null;
13281274
}
13291275

0 commit comments

Comments
 (0)