Skip to content

Commit ed7465d

Browse files
committed
enhance: render highlight background manually instead of using TextRunProperties.SetBackgroundBrush(IBrush) (#364)
1 parent 9f39a9b commit ed7465d

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

src/Models/DiffResult.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public enum TextDiffLineType
1919
public class TextInlineRange
2020
{
2121
public int Start { get; set; }
22-
public int Count { get; set; }
23-
public TextInlineRange(int p, int n) { Start = p; Count = n; }
22+
public int End { get; set; }
23+
public TextInlineRange(int p, int n) { Start = p; End = p + n - 1; }
2424
}
2525

2626
public class TextDiffLine

src/Views/TextDiffView.axaml.cs

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,50 @@ public void Draw(TextView textView, DrawingContext drawingContext)
175175
var startY = line.GetTextLineVisualYPosition(line.TextLines[0], VisualYPosition.LineTop) - textView.VerticalOffset;
176176
var endY = line.GetTextLineVisualYPosition(line.TextLines[^1], VisualYPosition.LineBottom) - textView.VerticalOffset;
177177
drawingContext.DrawRectangle(bg, null, new Rect(0, startY, width, endY - startY));
178+
179+
if (info.Highlights.Count > 0)
180+
{
181+
var highlightBG = info.Type == Models.TextDiffLineType.Added ? _presenter.AddedHighlightBrush : _presenter.DeletedHighlightBrush;
182+
var processingIdxStart = 0;
183+
var processingIdxEnd = 0;
184+
var nextHightlight = 0;
185+
186+
var cloned = new List<Models.TextInlineRange>();
187+
cloned.AddRange(info.Highlights);
188+
189+
foreach (var tl in line.TextLines)
190+
{
191+
processingIdxEnd += tl.Length;
192+
193+
var y = line.GetTextLineVisualYPosition(tl, VisualYPosition.LineTop) - textView.VerticalOffset;
194+
var height = line.GetTextLineVisualYPosition(tl, VisualYPosition.LineBottom) - textView.VerticalOffset - y;
195+
196+
while (nextHightlight < cloned.Count)
197+
{
198+
var highlight = cloned[nextHightlight];
199+
if (highlight.Start >= processingIdxEnd)
200+
{
201+
processingIdxStart = processingIdxEnd;
202+
break;
203+
}
204+
205+
var start = highlight.Start < processingIdxStart ? processingIdxStart : highlight.Start;
206+
var end = highlight.End >= processingIdxEnd ? processingIdxEnd : highlight.End + 1;
207+
208+
var x = line.GetTextLineVisualXPosition(tl, start) - textView.HorizontalOffset;
209+
var w = line.GetTextLineVisualXPosition(tl, end) - textView.HorizontalOffset - x;
210+
var rect = new Rect(x, y, w, height);
211+
drawingContext.DrawRectangle(highlightBG, null, rect);
212+
213+
if (highlight.End >= processingIdxEnd)
214+
break;
215+
216+
nextHightlight++;
217+
}
218+
219+
processingIdxStart = processingIdxEnd;
220+
}
221+
}
178222
}
179223
}
180224

@@ -221,18 +265,6 @@ protected override void ColorizeLine(DocumentLine line)
221265

222266
return;
223267
}
224-
225-
if (info.Highlights.Count > 0)
226-
{
227-
var bg = info.Type == Models.TextDiffLineType.Added ? _presenter.AddedHighlightBrush : _presenter.DeletedHighlightBrush;
228-
foreach (var highlight in info.Highlights)
229-
{
230-
ChangeLinePart(line.Offset + highlight.Start, line.Offset + highlight.Start + highlight.Count, v =>
231-
{
232-
v.TextRunProperties.SetBackgroundBrush(bg);
233-
});
234-
}
235-
}
236268
}
237269

238270
private readonly ThemedTextDiffPresenter _presenter;

0 commit comments

Comments
 (0)