@@ -256,6 +256,54 @@ protected override Size MeasureOverride(Size availableSize)
256256 private readonly BlameTextEditor _editor = null ;
257257 }
258258
259+ public class LineBackgroundRenderer : IBackgroundRenderer
260+ {
261+ public KnownLayer Layer => KnownLayer . Background ;
262+
263+ public LineBackgroundRenderer ( BlameTextEditor owner )
264+ {
265+ _owner = owner ;
266+ }
267+
268+ public void Draw ( TextView textView , DrawingContext drawingContext )
269+ {
270+ if ( ! textView . VisualLinesValid )
271+ return ;
272+
273+ var w = textView . Bounds . Width ;
274+ if ( double . IsNaN ( w ) || double . IsInfinity ( w ) || w <= 0 )
275+ return ;
276+
277+ var highlight = _owner . _highlight ;
278+ if ( string . IsNullOrEmpty ( highlight ) )
279+ return ;
280+
281+ var color = ( Color ) _owner . FindResource ( "SystemAccentColor" ) ! ;
282+ var brush = new SolidColorBrush ( color , 0.2 ) ;
283+ var lines = _owner . BlameData . LineInfos ;
284+
285+ foreach ( var line in textView . VisualLines )
286+ {
287+ if ( line . IsDisposed || line . FirstDocumentLine == null || line . FirstDocumentLine . IsDeleted )
288+ continue ;
289+
290+ var lineNumber = line . FirstDocumentLine . LineNumber ;
291+ if ( lineNumber > lines . Count )
292+ break ;
293+
294+ var info = lines [ lineNumber - 1 ] ;
295+ if ( ! info . CommitSHA . Equals ( highlight , StringComparison . Ordinal ) )
296+ continue ;
297+
298+ var startY = line . GetTextLineVisualYPosition ( line . TextLines [ 0 ] , VisualYPosition . LineTop ) - textView . VerticalOffset ;
299+ var endY = line . GetTextLineVisualYPosition ( line . TextLines [ ^ 1 ] , VisualYPosition . LineBottom ) - textView . VerticalOffset ;
300+ drawingContext . FillRectangle ( brush , new Rect ( 0 , startY , w , endY - startY ) ) ;
301+ }
302+ }
303+
304+ private readonly BlameTextEditor _owner ;
305+ }
306+
259307 public static readonly StyledProperty < string > FileProperty =
260308 AvaloniaProperty . Register < BlameTextEditor , string > ( nameof ( File ) ) ;
261309
@@ -302,43 +350,12 @@ public int TabWidth
302350 TextArea . LeftMargins . Add ( new LineNumberMargin ( ) { Margin = new Thickness ( 8 , 0 ) } ) ;
303351 TextArea . LeftMargins . Add ( new VerticalSeparatorMargin ( this ) ) ;
304352 TextArea . Caret . PositionChanged += OnTextAreaCaretPositionChanged ;
353+ TextArea . TextView . BackgroundRenderers . Add ( new LineBackgroundRenderer ( this ) ) ;
305354 TextArea . TextView . ContextRequested += OnTextViewContextRequested ;
306355 TextArea . TextView . VisualLinesChanged += OnTextViewVisualLinesChanged ;
307356 TextArea . TextView . Margin = new Thickness ( 4 , 0 ) ;
308357 }
309358
310- public override void Render ( DrawingContext context )
311- {
312- base . Render ( context ) ;
313-
314- if ( string . IsNullOrEmpty ( _highlight ) )
315- return ;
316-
317- var view = TextArea . TextView ;
318- if ( view is not { VisualLinesValid : true } )
319- return ;
320-
321- var color = ( Color ) this . FindResource ( "SystemAccentColor" ) ! ;
322- var brush = new SolidColorBrush ( color , 0.4 ) ;
323- foreach ( var line in view . VisualLines )
324- {
325- if ( line . IsDisposed || line . FirstDocumentLine == null || line . FirstDocumentLine . IsDeleted )
326- continue ;
327-
328- var lineNumber = line . FirstDocumentLine . LineNumber ;
329- if ( lineNumber > BlameData . LineInfos . Count )
330- break ;
331-
332- var info = BlameData . LineInfos [ lineNumber - 1 ] ;
333- if ( info . CommitSHA != _highlight )
334- continue ;
335-
336- var startY = line . GetTextLineVisualYPosition ( line . TextLines [ 0 ] , VisualYPosition . LineTop ) - view . VerticalOffset ;
337- var endY = line . GetTextLineVisualYPosition ( line . TextLines [ ^ 1 ] , VisualYPosition . LineBottom ) - view . VerticalOffset ;
338- context . FillRectangle ( brush , new Rect ( 0 , startY , Bounds . Width , endY - startY ) ) ;
339- }
340- }
341-
342359 protected override void OnUnloaded ( RoutedEventArgs e )
343360 {
344361 base . OnUnloaded ( e ) ;
@@ -391,7 +408,6 @@ private void OnTextAreaCaretPositionChanged(object sender, EventArgs e)
391408 return ;
392409
393410 _highlight = BlameData . LineInfos [ caret . Line - 1 ] . CommitSHA ;
394- InvalidateVisual ( ) ;
395411 }
396412
397413 private void OnTextViewContextRequested ( object sender , ContextRequestedEventArgs e )
@@ -426,8 +442,6 @@ private void OnTextViewVisualLinesChanged(object sender, EventArgs e)
426442 break ;
427443 }
428444 }
429-
430- InvalidateVisual ( ) ;
431445 }
432446
433447 private TextMate . Installation _textMate = null ;
0 commit comments