@@ -221,7 +221,7 @@ public void Draw(TextView textView, DrawingContext drawingContext)
221221 if ( _presenter . Document == null || ! textView . VisualLinesValid )
222222 return ;
223223
224- var changeBlock = _presenter . BlockNavigation ? . GetCurrentBlock ( ) ;
224+ var changeBlock = _presenter . BlockNavigation . GetCurrentBlock ( ) ;
225225 var changeBlockBG = new SolidColorBrush ( Colors . Gray , 0.25 ) ;
226226 var changeBlockFG = new Pen ( Brushes . Gray ) ;
227227
@@ -285,7 +285,7 @@ public void Draw(TextView textView, DrawingContext drawingContext)
285285 }
286286 }
287287
288- if ( changeBlock != null && changeBlock . IsInRange ( index ) )
288+ if ( changeBlock != null && changeBlock . Contains ( index ) )
289289 {
290290 drawingContext . DrawRectangle ( changeBlockBG , null , new Rect ( 0 , startY , width , endY - startY ) ) ;
291291 if ( index == changeBlock . Start )
@@ -498,7 +498,7 @@ public virtual void UpdateSelectedChunk(double y)
498498
499499 public virtual void GotoFirstChange ( )
500500 {
501- var first = BlockNavigation ? . GotoFirst ( ) ;
501+ var first = BlockNavigation . GotoFirst ( ) ;
502502 if ( first != null )
503503 {
504504 TextArea . Caret . Line = first . Start ;
@@ -508,7 +508,7 @@ public virtual void GotoFirstChange()
508508
509509 public virtual void GotoPrevChange ( )
510510 {
511- var prev = BlockNavigation ? . GotoPrev ( ) ;
511+ var prev = BlockNavigation . GotoPrev ( ) ;
512512 if ( prev != null )
513513 {
514514 TextArea . Caret . Line = prev . Start ;
@@ -518,7 +518,7 @@ public virtual void GotoPrevChange()
518518
519519 public virtual void GotoNextChange ( )
520520 {
521- var next = BlockNavigation ? . GotoNext ( ) ;
521+ var next = BlockNavigation . GotoNext ( ) ;
522522 if ( next != null )
523523 {
524524 TextArea . Caret . Line = next . Start ;
@@ -528,7 +528,7 @@ public virtual void GotoNextChange()
528528
529529 public virtual void GotoLastChange ( )
530530 {
531- var next = BlockNavigation ? . GotoLast ( ) ;
531+ var next = BlockNavigation . GotoLast ( ) ;
532532 if ( next != null )
533533 {
534534 TextArea . Caret . Line = next . Start ;
@@ -627,6 +627,23 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
627627 }
628628 }
629629
630+ protected override void OnDataContextBeginUpdate ( )
631+ {
632+ base . OnDataContextBeginUpdate ( ) ;
633+ AutoScrollToFirstChange ( ) ;
634+ }
635+
636+ protected override void OnSizeChanged ( SizeChangedEventArgs e )
637+ {
638+ base . OnSizeChanged ( e ) ;
639+
640+ if ( ! _execSizeChanged )
641+ {
642+ _execSizeChanged = true ;
643+ AutoScrollToFirstChange ( ) ;
644+ }
645+ }
646+
630647 private async void OnTextAreaKeyDown ( object sender , KeyEventArgs e )
631648 {
632649 if ( e . KeyModifiers . Equals ( OperatingSystem . IsMacOS ( ) ? KeyModifiers . Meta : KeyModifiers . Control ) )
@@ -644,7 +661,7 @@ private async void OnTextAreaKeyDown(object sender, KeyEventArgs e)
644661
645662 private void OnTextAreaCaretPositionChanged ( object sender , EventArgs e )
646663 {
647- BlockNavigation ? . UpdateByCaretPosition ( TextArea ? . Caret ? . Line ?? 0 ) ;
664+ BlockNavigation . UpdateByCaretPosition ( TextArea ? . Caret ? . Line ?? 0 ) ;
648665 }
649666
650667 private void OnTextViewContextRequested ( object sender , ContextRequestedEventArgs e )
@@ -783,6 +800,34 @@ private void UpdateTextMate()
783800 }
784801 }
785802
803+ private void AutoScrollToFirstChange ( )
804+ {
805+ if ( Bounds . Height < 0.1 )
806+ return ;
807+
808+ if ( DataContext is not ViewModels . TextDiffContext ctx )
809+ return ;
810+
811+ if ( ctx . IsSideBySide ( ) && ! IsOld )
812+ return ;
813+
814+ var curBlock = ctx . BlockNavigation . GetCurrentBlock ( ) ;
815+ if ( curBlock == null )
816+ return ;
817+
818+ var lineHeight = TextArea . TextView . DefaultLineHeight ;
819+ var vOffset = lineHeight * ( curBlock . Start - 1 ) - Bounds . Height * 0.5 ;
820+ if ( vOffset >= 0 )
821+ {
822+ var scroller = this . FindDescendantOfType < ScrollViewer > ( ) ;
823+ if ( scroller != null )
824+ {
825+ ctx . ScrollOffset = new Vector ( 0 , vOffset ) ;
826+ scroller . Offset = ctx . ScrollOffset ;
827+ }
828+ }
829+ }
830+
786831 private async Task CopyWithoutIndicatorsAsync ( )
787832 {
788833 var selection = TextArea . Selection ;
@@ -852,6 +897,7 @@ private async Task CopyWithoutIndicatorsAsync()
852897 await App . CopyTextAsync ( builder . ToString ( ) ) ;
853898 }
854899
900+ private bool _execSizeChanged = false ;
855901 private TextMate . Installation _textMate = null ;
856902 private TextLocation _lastSelectStart = TextLocation . Empty ;
857903 private TextLocation _lastSelectEnd = TextLocation . Empty ;
0 commit comments