From ed7465def5bd867cbeef3f73fcdd6f1fc79213db Mon Sep 17 00:00:00 2001 From: leo Date: Fri, 16 Aug 2024 18:59:48 +0800 Subject: [PATCH] enhance: render highlight background manually instead of using `TextRunProperties.SetBackgroundBrush(IBrush)` (#364) --- src/Models/DiffResult.cs | 4 +-- src/Views/TextDiffView.axaml.cs | 56 ++++++++++++++++++++++++++------- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/src/Models/DiffResult.cs b/src/Models/DiffResult.cs index 500ff549..e7cecaa3 100644 --- a/src/Models/DiffResult.cs +++ b/src/Models/DiffResult.cs @@ -19,8 +19,8 @@ namespace SourceGit.Models public class TextInlineRange { public int Start { get; set; } - public int Count { get; set; } - public TextInlineRange(int p, int n) { Start = p; Count = n; } + public int End { get; set; } + public TextInlineRange(int p, int n) { Start = p; End = p + n - 1; } } public class TextDiffLine diff --git a/src/Views/TextDiffView.axaml.cs b/src/Views/TextDiffView.axaml.cs index 8c7cfbac..110a712b 100644 --- a/src/Views/TextDiffView.axaml.cs +++ b/src/Views/TextDiffView.axaml.cs @@ -175,6 +175,50 @@ namespace SourceGit.Views var startY = line.GetTextLineVisualYPosition(line.TextLines[0], VisualYPosition.LineTop) - textView.VerticalOffset; var endY = line.GetTextLineVisualYPosition(line.TextLines[^1], VisualYPosition.LineBottom) - textView.VerticalOffset; drawingContext.DrawRectangle(bg, null, new Rect(0, startY, width, endY - startY)); + + if (info.Highlights.Count > 0) + { + var highlightBG = info.Type == Models.TextDiffLineType.Added ? _presenter.AddedHighlightBrush : _presenter.DeletedHighlightBrush; + var processingIdxStart = 0; + var processingIdxEnd = 0; + var nextHightlight = 0; + + var cloned = new List(); + cloned.AddRange(info.Highlights); + + foreach (var tl in line.TextLines) + { + processingIdxEnd += tl.Length; + + var y = line.GetTextLineVisualYPosition(tl, VisualYPosition.LineTop) - textView.VerticalOffset; + var height = line.GetTextLineVisualYPosition(tl, VisualYPosition.LineBottom) - textView.VerticalOffset - y; + + while (nextHightlight < cloned.Count) + { + var highlight = cloned[nextHightlight]; + if (highlight.Start >= processingIdxEnd) + { + processingIdxStart = processingIdxEnd; + break; + } + + var start = highlight.Start < processingIdxStart ? processingIdxStart : highlight.Start; + var end = highlight.End >= processingIdxEnd ? processingIdxEnd : highlight.End + 1; + + var x = line.GetTextLineVisualXPosition(tl, start) - textView.HorizontalOffset; + var w = line.GetTextLineVisualXPosition(tl, end) - textView.HorizontalOffset - x; + var rect = new Rect(x, y, w, height); + drawingContext.DrawRectangle(highlightBG, null, rect); + + if (highlight.End >= processingIdxEnd) + break; + + nextHightlight++; + } + + processingIdxStart = processingIdxEnd; + } + } } } @@ -221,18 +265,6 @@ namespace SourceGit.Views return; } - - if (info.Highlights.Count > 0) - { - var bg = info.Type == Models.TextDiffLineType.Added ? _presenter.AddedHighlightBrush : _presenter.DeletedHighlightBrush; - foreach (var highlight in info.Highlights) - { - ChangeLinePart(line.Offset + highlight.Start, line.Offset + highlight.Start + highlight.Count, v => - { - v.TextRunProperties.SetBackgroundBrush(bg); - }); - } - } } private readonly ThemedTextDiffPresenter _presenter;