forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommitSubjectPresenter.cs
More file actions
374 lines (315 loc) · 13.8 KB
/
CommitSubjectPresenter.cs
File metadata and controls
374 lines (315 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
using Avalonia;
using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Media;
namespace SourceGit.Views
{
public partial class CommitSubjectPresenter : Control
{
public static readonly StyledProperty<FontFamily> FontFamilyProperty =
AvaloniaProperty.Register<CommitSubjectPresenter, FontFamily>(nameof(FontFamily));
public FontFamily FontFamily
{
get => GetValue(FontFamilyProperty);
set => SetValue(FontFamilyProperty, value);
}
public static readonly StyledProperty<FontFamily> CodeFontFamilyProperty =
AvaloniaProperty.Register<CommitSubjectPresenter, FontFamily>(nameof(CodeFontFamily));
public FontFamily CodeFontFamily
{
get => GetValue(CodeFontFamilyProperty);
set => SetValue(CodeFontFamilyProperty, value);
}
public static readonly StyledProperty<double> FontSizeProperty =
TextBlock.FontSizeProperty.AddOwner<CommitSubjectPresenter>();
public double FontSize
{
get => GetValue(FontSizeProperty);
set => SetValue(FontSizeProperty, value);
}
public static readonly StyledProperty<FontWeight> FontWeightProperty =
TextBlock.FontWeightProperty.AddOwner<CommitSubjectPresenter>();
public FontWeight FontWeight
{
get => GetValue(FontWeightProperty);
set => SetValue(FontWeightProperty, value);
}
public static readonly StyledProperty<IBrush> InlineCodeBackgroundProperty =
AvaloniaProperty.Register<CommitSubjectPresenter, IBrush>(nameof(InlineCodeBackground), Brushes.Transparent);
public IBrush InlineCodeBackground
{
get => GetValue(InlineCodeBackgroundProperty);
set => SetValue(InlineCodeBackgroundProperty, value);
}
public static readonly StyledProperty<IBrush> ForegroundProperty =
AvaloniaProperty.Register<CommitSubjectPresenter, IBrush>(nameof(Foreground), Brushes.White);
public IBrush Foreground
{
get => GetValue(ForegroundProperty);
set => SetValue(ForegroundProperty, value);
}
public static readonly StyledProperty<IBrush> LinkForegroundProperty =
AvaloniaProperty.Register<CommitSubjectPresenter, IBrush>(nameof(LinkForeground), Brushes.White);
public IBrush LinkForeground
{
get => GetValue(LinkForegroundProperty);
set => SetValue(LinkForegroundProperty, value);
}
public static readonly StyledProperty<string> SubjectProperty =
AvaloniaProperty.Register<CommitSubjectPresenter, string>(nameof(Subject));
public string Subject
{
get => GetValue(SubjectProperty);
set => SetValue(SubjectProperty, value);
}
public static readonly StyledProperty<AvaloniaList<Models.IssueTrackerRule>> IssueTrackerRulesProperty =
AvaloniaProperty.Register<CommitSubjectPresenter, AvaloniaList<Models.IssueTrackerRule>>(nameof(IssueTrackerRules));
public AvaloniaList<Models.IssueTrackerRule> IssueTrackerRules
{
get => GetValue(IssueTrackerRulesProperty);
set => SetValue(IssueTrackerRulesProperty, value);
}
public static readonly StyledProperty<AvaloniaList<Models.IssueTrackerRule>> SharedIssueTrackerRulesProperty =
AvaloniaProperty.Register<CommitSubjectPresenter, AvaloniaList<Models.IssueTrackerRule>>(nameof(SharedIssueTrackerRules));
public AvaloniaList<Models.IssueTrackerRule> SharedIssueTrackerRules
{
get => GetValue(SharedIssueTrackerRulesProperty);
set => SetValue(SharedIssueTrackerRulesProperty, value);
}
public override void Render(DrawingContext context)
{
if (_needRebuildInlines)
{
_needRebuildInlines = false;
GenerateFormattedTextElements();
}
if (_inlines.Count == 0)
return;
var ro = new RenderOptions()
{
TextRenderingMode = TextRenderingMode.SubpixelAntialias,
EdgeMode = EdgeMode.Antialias
};
using (context.PushRenderOptions(ro))
{
var height = Bounds.Height;
var width = Bounds.Width;
foreach (var inline in _inlines)
{
if (inline.X > width)
return;
if (inline.Element is { Type: Models.InlineElementType.Code })
{
var rect = new Rect(inline.X, (height - inline.Text.Height - 2) * 0.5, inline.Text.WidthIncludingTrailingWhitespace + 8, inline.Text.Height + 2);
var roundedRect = new RoundedRect(rect, new CornerRadius(4));
context.DrawRectangle(InlineCodeBackground, null, roundedRect);
context.DrawText(inline.Text, new Point(inline.X + 4, (height - inline.Text.Height) * 0.5));
}
else
{
context.DrawText(inline.Text, new Point(inline.X, (height - inline.Text.Height) * 0.5));
}
}
}
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == SubjectProperty ||
change.Property == IssueTrackerRulesProperty ||
change.Property == SharedIssueTrackerRulesProperty)
{
_elements.Clear();
ClearHoveredIssueLink();
var subject = Subject;
if (string.IsNullOrEmpty(subject))
{
_needRebuildInlines = true;
InvalidateVisual();
return;
}
var rules = IssueTrackerRules ?? [];
foreach (var rule in rules)
rule.Matches(_elements, subject);
var sharedRules = SharedIssueTrackerRules ?? [];
foreach (var rule in sharedRules)
rule.Matches(_elements, subject);
var keywordMatch = REG_KEYWORD_FORMAT1().Match(subject);
if (!keywordMatch.Success)
keywordMatch = REG_KEYWORD_FORMAT2().Match(subject);
if (keywordMatch.Success && _elements.Intersect(0, keywordMatch.Length) == null)
_elements.Add(new Models.InlineElement(Models.InlineElementType.Keyword, 0, keywordMatch.Length, string.Empty));
var codeMatches = REG_INLINECODE_FORMAT().Matches(subject);
foreach (Match match in codeMatches)
{
var start = match.Index;
var len = match.Length;
if (_elements.Intersect(start, len) != null)
continue;
_elements.Add(new Models.InlineElement(Models.InlineElementType.Code, start, len, string.Empty));
}
_elements.Sort();
_needRebuildInlines = true;
InvalidateVisual();
}
else if (change.Property == FontFamilyProperty ||
change.Property == CodeFontFamilyProperty ||
change.Property == FontSizeProperty ||
change.Property == FontWeightProperty ||
change.Property == ForegroundProperty ||
change.Property == LinkForegroundProperty)
{
_needRebuildInlines = true;
InvalidateVisual();
}
else if (change.Property == InlineCodeBackgroundProperty)
{
InvalidateVisual();
}
}
protected override void OnPointerMoved(PointerEventArgs e)
{
base.OnPointerMoved(e);
var point = e.GetPosition(this);
foreach (var inline in _inlines)
{
if (inline.Element is not { Type: Models.InlineElementType.Link } link)
continue;
if (inline.X > point.X || inline.X + inline.Text.WidthIncludingTrailingWhitespace < point.X)
continue;
_lastHover = link;
SetCurrentValue(CursorProperty, Cursor.Parse("Hand"));
ToolTip.SetTip(this, link.Link);
e.Handled = true;
return;
}
ClearHoveredIssueLink();
}
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
base.OnPointerPressed(e);
if (_lastHover != null)
Native.OS.OpenBrowser(_lastHover.Link);
}
protected override void OnPointerExited(PointerEventArgs e)
{
base.OnPointerExited(e);
ClearHoveredIssueLink();
}
private void GenerateFormattedTextElements()
{
_inlines.Clear();
var subject = Subject;
if (string.IsNullOrEmpty(subject))
return;
var fontFamily = FontFamily;
var codeFontFamily = CodeFontFamily;
var fontSize = FontSize;
var foreground = Foreground;
var linkForeground = LinkForeground;
var typeface = new Typeface(fontFamily, FontStyle.Normal, FontWeight);
var codeTypeface = new Typeface(codeFontFamily, FontStyle.Normal, FontWeight);
var pos = 0;
var x = 0.0;
for (var i = 0; i < _elements.Count; i++)
{
var elem = _elements[i];
if (elem.Start > pos)
{
var normal = new FormattedText(
subject.Substring(pos, elem.Start - pos),
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
fontSize,
foreground);
_inlines.Add(new Inline(x, normal, null));
x += normal.WidthIncludingTrailingWhitespace;
}
if (elem.Type == Models.InlineElementType.Keyword)
{
var keyword = new FormattedText(
subject.Substring(elem.Start, elem.Length),
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface(fontFamily, FontStyle.Normal, FontWeight.Bold),
fontSize,
foreground);
_inlines.Add(new Inline(x, keyword, elem));
x += keyword.WidthIncludingTrailingWhitespace;
}
else if (elem.Type == Models.InlineElementType.Link)
{
var link = new FormattedText(
subject.Substring(elem.Start, elem.Length),
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
fontSize,
linkForeground);
_inlines.Add(new Inline(x, link, elem));
x += link.WidthIncludingTrailingWhitespace;
}
else if (elem.Type == Models.InlineElementType.Code)
{
var link = new FormattedText(
subject.Substring(elem.Start + 1, elem.Length - 2),
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
codeTypeface,
fontSize - 0.5,
foreground);
_inlines.Add(new Inline(x, link, elem));
x += link.WidthIncludingTrailingWhitespace + 8;
}
pos = elem.Start + elem.Length;
}
if (pos < subject.Length)
{
var normal = new FormattedText(
subject.Substring(pos),
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
fontSize,
foreground);
_inlines.Add(new Inline(x, normal, null));
}
}
private void ClearHoveredIssueLink()
{
if (_lastHover != null)
{
ToolTip.SetTip(this, null);
SetCurrentValue(CursorProperty, Cursor.Parse("Arrow"));
_lastHover = null;
}
}
[GeneratedRegex(@"`.*?`")]
private static partial Regex REG_INLINECODE_FORMAT();
[GeneratedRegex(@"^\[[\w\s]+\]")]
private static partial Regex REG_KEYWORD_FORMAT1();
[GeneratedRegex(@"^\S+([\<\(][\w\s_\-\*,]+[\>\)])?\!?\s?:\s")]
private static partial Regex REG_KEYWORD_FORMAT2();
private class Inline
{
public double X { get; set; } = 0;
public FormattedText Text { get; set; } = null;
public Models.InlineElement Element { get; set; } = null;
public Inline(double x, FormattedText text, Models.InlineElement elem)
{
X = x;
Text = text;
Element = elem;
}
}
private Models.InlineElementCollector _elements = new();
private List<Inline> _inlines = [];
private Models.InlineElement _lastHover = null;
private bool _needRebuildInlines = false;
}
}