Skip to content

Commit

Permalink
Merge pull request #444 from AvaloniaUI/fix-442
Browse files Browse the repository at this point in the history
Fix customMargin renders incorrectly when scrolling
  • Loading branch information
danipen authored Jul 29, 2024
2 parents 144483e + 251c1a8 commit f9d50b8
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/AvaloniaEdit.Demo/CustomMargin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Avalonia.Input;
using Avalonia.Media;
using Avalonia.Media.Immutable;
using AvaloniaEdit.Document;
using AvaloniaEdit.Editing;
using AvaloniaEdit.Rendering;

Expand Down Expand Up @@ -51,9 +52,16 @@ public CustomMargin()
protected override void OnTextViewChanged(TextView? oldTextView, TextView? newTextView)
{
if (oldTextView != null)
{
oldTextView.VisualLinesChanged -= OnVisualLinesChanged;
oldTextView.DocumentChanged -= OnDocumentChanged;
}

if (newTextView != null)
{
newTextView.VisualLinesChanged += OnVisualLinesChanged;
newTextView.DocumentChanged += OnDocumentChanged;
}

base.OnTextViewChanged(oldTextView, newTextView);
}
Expand All @@ -63,6 +71,12 @@ private void OnVisualLinesChanged(object? sender, EventArgs eventArgs)
InvalidateVisual();
}

private void OnDocumentChanged(object? sender, DocumentChangedEventArgs e)
{
_markedDocumentLines.Clear();
InvalidateVisual();
}

protected override Size MeasureOverride(Size availableSize)
{
return new Size(20, 0);
Expand All @@ -86,6 +100,7 @@ protected override void OnPointerMoved(PointerEventArgs e)
protected override void OnPointerExited(PointerEventArgs e)
{
_pointerOverLine = -1;
InvalidateVisual();

base.OnPointerExited(e);
}
Expand All @@ -112,12 +127,12 @@ public override void Render(DrawingContext context)
{
foreach (var visualLine in TextView.VisualLines)
{
double y = TextView.VerticalOffset + visualLine.VisualTop + visualLine.Height / 2;
double y = visualLine.VisualTop - TextView.VerticalOffset + visualLine.Height / 2;

if (_pointerOverLine == visualLine.FirstDocumentLine.LineNumber)
context.DrawEllipse(_pointerOverBrush, _pointerOverPen, new Point(10, y), 8, 8);
else if (_markedDocumentLines.Contains(visualLine.FirstDocumentLine.LineNumber))
if (_markedDocumentLines.Contains(visualLine.FirstDocumentLine.LineNumber))
context.DrawEllipse(_markerBrush, _markerPen, new Point(10, y), 8, 8);
else if (_pointerOverLine == visualLine.FirstDocumentLine.LineNumber)
context.DrawEllipse(_pointerOverBrush, _pointerOverPen, new Point(10, y), 8, 8);
}
}

Expand Down

0 comments on commit f9d50b8

Please sign in to comment.