Skip to content

Commit d4302b4

Browse files
committed
refactor: render +/- marks after line number
Signed-off-by: leo <[email protected]>
1 parent 5707d0b commit d4302b4

File tree

1 file changed

+83
-22
lines changed

1 file changed

+83
-22
lines changed

src/Views/TextDiffView.axaml.cs

Lines changed: 83 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ public LineNumberMargin(bool usePresenter, bool isOld)
7272
{
7373
_usePresenter = usePresenter;
7474
_isOld = isOld;
75+
76+
Margin = new Thickness(8, 0);
7577
ClipToBounds = true;
7678
}
7779

@@ -105,11 +107,80 @@ public override void Render(DrawingContext context)
105107
continue;
106108

107109
var y = line.GetTextLineVisualYPosition(line.TextLines[0], VisualYPosition.LineMiddle) - view.VerticalOffset;
110+
var txt = new FormattedText(
111+
lineNumber,
112+
CultureInfo.CurrentCulture,
113+
FlowDirection.LeftToRight,
114+
typeface,
115+
presenter.FontSize,
116+
presenter.Foreground);
117+
context.DrawText(txt, new Point(Bounds.Width - txt.Width, y - txt.Height * 0.5));
118+
}
119+
}
120+
}
121+
122+
protected override Size MeasureOverride(Size availableSize)
123+
{
124+
var presenter = this.FindAncestorOfType<ThemedTextDiffPresenter>();
125+
if (presenter == null)
126+
return new Size(32, 0);
127+
128+
var maxLineNumber = presenter.GetMaxLineNumber();
129+
var typeface = TextView.CreateTypeface();
130+
var test = new FormattedText(
131+
$"{maxLineNumber}",
132+
CultureInfo.CurrentCulture,
133+
FlowDirection.LeftToRight,
134+
typeface,
135+
presenter.FontSize,
136+
Brushes.White);
137+
return new Size(test.Width, 0);
138+
}
139+
140+
protected override void OnDataContextChanged(EventArgs e)
141+
{
142+
base.OnDataContextChanged(e);
143+
InvalidateMeasure();
144+
}
108145

109-
var prefix = null as FormattedText;
146+
private bool _usePresenter = false;
147+
private bool _isOld = false;
148+
}
149+
150+
public class LineModifyTypeMargin : AbstractMargin
151+
{
152+
public LineModifyTypeMargin()
153+
{
154+
Margin = new Thickness(1, 0);
155+
ClipToBounds = true;
156+
}
157+
158+
public override void Render(DrawingContext context)
159+
{
160+
var presenter = this.FindAncestorOfType<ThemedTextDiffPresenter>();
161+
if (presenter == null)
162+
return;
163+
164+
var lines = presenter.GetLines();
165+
var view = TextView;
166+
if (view != null && view.VisualLinesValid)
167+
{
168+
var typeface = view.CreateTypeface();
169+
foreach (var line in view.VisualLines)
170+
{
171+
if (line.IsDisposed || line.FirstDocumentLine == null || line.FirstDocumentLine.IsDeleted)
172+
continue;
173+
174+
var index = line.FirstDocumentLine.LineNumber;
175+
if (index > lines.Count)
176+
break;
177+
178+
var info = lines[index - 1];
179+
var y = line.GetTextLineVisualYPosition(line.TextLines[0], VisualYPosition.LineMiddle) - view.VerticalOffset;
180+
var indicator = null as FormattedText;
110181
if (info.Type == Models.TextDiffLineType.Added)
111182
{
112-
prefix = new FormattedText(
183+
indicator = new FormattedText(
113184
"+",
114185
CultureInfo.CurrentCulture,
115186
FlowDirection.LeftToRight,
@@ -119,7 +190,7 @@ public override void Render(DrawingContext context)
119190
}
120191
else if (info.Type == Models.TextDiffLineType.Deleted)
121192
{
122-
prefix = new FormattedText(
193+
indicator = new FormattedText(
123194
"-",
124195
CultureInfo.CurrentCulture,
125196
FlowDirection.LeftToRight,
@@ -128,17 +199,8 @@ public override void Render(DrawingContext context)
128199
Brushes.Red);
129200
}
130201

131-
if (prefix != null)
132-
context.DrawText(prefix, new Point(0, y - prefix.Height * 0.5));
133-
134-
var txt = new FormattedText(
135-
lineNumber,
136-
CultureInfo.CurrentCulture,
137-
FlowDirection.LeftToRight,
138-
typeface,
139-
presenter.FontSize,
140-
presenter.Foreground);
141-
context.DrawText(txt, new Point(Bounds.Width - txt.Width, y - txt.Height * 0.5));
202+
if (indicator != null)
203+
context.DrawText(indicator, new Point(0, y - indicator.Height * 0.5));
142204
}
143205
}
144206
}
@@ -147,12 +209,12 @@ protected override Size MeasureOverride(Size availableSize)
147209
{
148210
var presenter = this.FindAncestorOfType<ThemedTextDiffPresenter>();
149211
if (presenter == null)
150-
return new Size(32, 0);
212+
return new Size(0, 0);
151213

152214
var maxLineNumber = presenter.GetMaxLineNumber();
153215
var typeface = TextView.CreateTypeface();
154216
var test = new FormattedText(
155-
$"- {maxLineNumber}",
217+
$"-",
156218
CultureInfo.CurrentCulture,
157219
FlowDirection.LeftToRight,
158220
typeface,
@@ -166,9 +228,6 @@ protected override void OnDataContextChanged(EventArgs e)
166228
base.OnDataContextChanged(e);
167229
InvalidateMeasure();
168230
}
169-
170-
private bool _usePresenter = false;
171-
private bool _isOld = false;
172231
}
173232

174233
public class LineBackgroundRenderer : IBackgroundRenderer
@@ -700,10 +759,11 @@ public class CombinedTextDiffPresenter : ThemedTextDiffPresenter
700759
{
701760
public CombinedTextDiffPresenter() : base(new TextArea(), new TextDocument())
702761
{
703-
TextArea.LeftMargins.Add(new LineNumberMargin(false, true) { Margin = new Thickness(8, 0) });
762+
TextArea.LeftMargins.Add(new LineNumberMargin(false, true));
704763
TextArea.LeftMargins.Add(new VerticalSeperatorMargin());
705-
TextArea.LeftMargins.Add(new LineNumberMargin(false, false) { Margin = new Thickness(8, 0) });
764+
TextArea.LeftMargins.Add(new LineNumberMargin(false, false));
706765
TextArea.LeftMargins.Add(new VerticalSeperatorMargin());
766+
TextArea.LeftMargins.Add(new LineModifyTypeMargin());
707767
}
708768

709769
public override List<Models.TextDiffLine> GetLines()
@@ -904,8 +964,9 @@ public class SingleSideTextDiffPresenter : ThemedTextDiffPresenter
904964
{
905965
public SingleSideTextDiffPresenter() : base(new TextArea(), new TextDocument())
906966
{
907-
TextArea.LeftMargins.Add(new LineNumberMargin(true, false) { Margin = new Thickness(8, 0) });
967+
TextArea.LeftMargins.Add(new LineNumberMargin(true, false));
908968
TextArea.LeftMargins.Add(new VerticalSeperatorMargin());
969+
TextArea.LeftMargins.Add(new LineModifyTypeMargin());
909970
}
910971

911972
public override List<Models.TextDiffLine> GetLines()

0 commit comments

Comments
 (0)