-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathBlame.axaml.cs
More file actions
492 lines (412 loc) · 18.4 KB
/
Blame.axaml.cs
File metadata and controls
492 lines (412 loc) · 18.4 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
using System;
using System.Collections.Generic;
using System.Globalization;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Media;
using AvaloniaEdit;
using AvaloniaEdit.Document;
using AvaloniaEdit.Editing;
using AvaloniaEdit.Rendering;
using AvaloniaEdit.TextMate;
using AvaloniaEdit.Utils;
namespace SourceGit.Views
{
public class BlameTextEditor : TextEditor
{
public class CommitInfoMargin : AbstractMargin
{
public CommitInfoMargin(BlameTextEditor editor)
{
_editor = editor;
ClipToBounds = true;
}
public override void Render(DrawingContext context)
{
if (_editor.BlameData == null)
return;
var view = TextView;
if (view is { VisualLinesValid: true })
{
var typeface = view.CreateTypeface();
var underlinePen = new Pen(Brushes.DarkOrange);
var width = Bounds.Width;
var lineHeight = view.DefaultLineHeight;
var pixelHeight = PixelSnapHelpers.GetPixelSize(view).Height;
foreach (var line in view.VisualLines)
{
if (line.IsDisposed || line.FirstDocumentLine == null || line.FirstDocumentLine.IsDeleted)
continue;
var lineNumber = line.FirstDocumentLine.LineNumber;
if (lineNumber > _editor.BlameData.LineInfos.Count)
break;
var info = _editor.BlameData.LineInfos[lineNumber - 1];
var x = 0.0;
var y = line.GetTextLineVisualYPosition(line.TextLines[0], VisualYPosition.LineMiddle) - view.VerticalOffset;
if (!info.IsFirstInGroup && y > lineHeight)
continue;
var shaLink = new FormattedText(
info.CommitSHA,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
_editor.FontSize,
Brushes.DarkOrange);
var shaLinkTop = y - shaLink.Height * 0.5;
var underlineY = PixelSnapHelpers.PixelAlign(y + shaLink.Height * 0.5 + 0.5, pixelHeight);
context.DrawText(shaLink, new Point(x, shaLinkTop));
context.DrawLine(underlinePen, new Point(x, underlineY), new Point(x + shaLink.Width, underlineY));
x += shaLink.Width + 8;
var author = new FormattedText(
info.Author,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
_editor.FontSize,
_editor.Foreground);
var authorTop = y - author.Height * 0.5;
context.DrawText(author, new Point(x, authorTop));
var timeStr = Models.DateTimeFormat.Format(info.Timestamp, true);
var time = new FormattedText(
timeStr,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
_editor.FontSize,
_editor.Foreground);
var timeTop = y - time.Height * 0.5;
context.DrawText(time, new Point(width - time.Width, timeTop));
}
}
}
protected override Size MeasureOverride(Size availableSize)
{
var view = TextView;
var maxWidth = 0.0;
if (view is { VisualLinesValid: true } && _editor.BlameData != null)
{
var typeface = view.CreateTypeface();
var calculated = new HashSet<string>();
foreach (var line in view.VisualLines)
{
var lineNumber = line.FirstDocumentLine.LineNumber;
if (lineNumber > _editor.BlameData.LineInfos.Count)
break;
var info = _editor.BlameData.LineInfos[lineNumber - 1];
if (!calculated.Add(info.CommitSHA))
continue;
var x = 0.0;
var shaLink = new FormattedText(
info.CommitSHA,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
_editor.FontSize,
Brushes.DarkOrange);
x += shaLink.Width + 8;
var author = new FormattedText(
info.Author,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
_editor.FontSize,
_editor.Foreground);
x += author.Width + 8;
var timeStr = Models.DateTimeFormat.Format(info.Timestamp, true);
var time = new FormattedText(
timeStr,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
_editor.FontSize,
_editor.Foreground);
x += time.Width;
if (maxWidth < x)
maxWidth = x;
}
}
return new Size(maxWidth, 0);
}
protected override void OnPointerMoved(PointerEventArgs e)
{
base.OnPointerMoved(e);
var view = TextView;
if (!e.Handled && view is { VisualLinesValid: true })
{
var pos = e.GetPosition(this);
var typeface = view.CreateTypeface();
var lineHeight = view.DefaultLineHeight;
foreach (var line in view.VisualLines)
{
if (line.IsDisposed || line.FirstDocumentLine == null || line.FirstDocumentLine.IsDeleted)
continue;
var lineNumber = line.FirstDocumentLine.LineNumber;
if (lineNumber > _editor.BlameData.LineInfos.Count)
break;
var info = _editor.BlameData.LineInfos[lineNumber - 1];
var y = line.GetTextLineVisualYPosition(line.TextLines[0], VisualYPosition.LineTop) - view.VerticalOffset;
var shaLink = new FormattedText(
info.CommitSHA,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
_editor.FontSize,
Brushes.DarkOrange);
var rect = new Rect(0, y, shaLink.Width, lineHeight);
if (rect.Contains(pos))
{
Cursor = Cursor.Parse("Hand");
if (DataContext is ViewModels.Blame blame)
{
var msg = blame.GetCommitMessage(info.CommitSHA);
ToolTip.SetTip(this, msg);
}
return;
}
}
Cursor = Cursor.Default;
ToolTip.SetTip(this, null);
}
}
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
base.OnPointerPressed(e);
var view = TextView;
if (!e.Handled && e.GetCurrentPoint(this).Properties.IsLeftButtonPressed && view is { VisualLinesValid: true })
{
var pos = e.GetPosition(this);
var typeface = view.CreateTypeface();
foreach (var line in view.VisualLines)
{
if (line.IsDisposed || line.FirstDocumentLine == null || line.FirstDocumentLine.IsDeleted)
continue;
var lineNumber = line.FirstDocumentLine.LineNumber;
if (lineNumber > _editor.BlameData.LineInfos.Count)
break;
var info = _editor.BlameData.LineInfos[lineNumber - 1];
var y = line.GetTextLineVisualYPosition(line.TextLines[0], VisualYPosition.TextTop) - view.VerticalOffset;
var shaLink = new FormattedText(
info.CommitSHA,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
_editor.FontSize,
Brushes.DarkOrange);
var rect = new Rect(0, y, shaLink.Width, shaLink.Height);
if (rect.Contains(pos))
{
if (DataContext is ViewModels.Blame blame)
blame.NavigateToCommit(info.File, info.CommitSHA);
e.Handled = true;
break;
}
}
}
}
private readonly BlameTextEditor _editor = null;
}
public class VerticalSeparatorMargin : AbstractMargin
{
public VerticalSeparatorMargin(BlameTextEditor editor)
{
_editor = editor;
}
public override void Render(DrawingContext context)
{
var pen = new Pen(_editor.BorderBrush);
context.DrawLine(pen, new Point(0.5, 0), new Point(0.5, Bounds.Height));
}
protected override Size MeasureOverride(Size availableSize)
{
return new Size(1, 0);
}
private readonly BlameTextEditor _editor = null;
}
public class LineBackgroundRenderer : IBackgroundRenderer
{
public KnownLayer Layer => KnownLayer.Background;
public LineBackgroundRenderer(BlameTextEditor owner)
{
_owner = owner;
}
public void Draw(TextView textView, DrawingContext drawingContext)
{
if (!textView.VisualLinesValid)
return;
var w = textView.Bounds.Width;
if (double.IsNaN(w) || double.IsInfinity(w) || w <= 0)
return;
var highlight = _owner._highlight;
if (string.IsNullOrEmpty(highlight))
return;
var color = (Color)_owner.FindResource("SystemAccentColor")!;
var brush = new SolidColorBrush(color, 0.2);
var lines = _owner.BlameData.LineInfos;
foreach (var line in textView.VisualLines)
{
if (line.IsDisposed || line.FirstDocumentLine == null || line.FirstDocumentLine.IsDeleted)
continue;
var lineNumber = line.FirstDocumentLine.LineNumber;
if (lineNumber > lines.Count)
break;
var info = lines[lineNumber - 1];
if (!info.CommitSHA.Equals(highlight, StringComparison.Ordinal))
continue;
var startY = line.GetTextLineVisualYPosition(line.TextLines[0], VisualYPosition.LineTop) - textView.VerticalOffset;
var endY = line.GetTextLineVisualYPosition(line.TextLines[^1], VisualYPosition.LineBottom) - textView.VerticalOffset;
drawingContext.FillRectangle(brush, new Rect(0, startY, w, endY - startY));
}
}
private readonly BlameTextEditor _owner;
}
public static readonly StyledProperty<string> FileProperty =
AvaloniaProperty.Register<BlameTextEditor, string>(nameof(File));
public string File
{
get => GetValue(FileProperty);
set => SetValue(FileProperty, value);
}
public static readonly StyledProperty<Models.BlameData> BlameDataProperty =
AvaloniaProperty.Register<BlameTextEditor, Models.BlameData>(nameof(BlameData));
public Models.BlameData BlameData
{
get => GetValue(BlameDataProperty);
set => SetValue(BlameDataProperty, value);
}
public static readonly StyledProperty<int> TabWidthProperty =
AvaloniaProperty.Register<BlameTextEditor, int>(nameof(TabWidth), 4);
public int TabWidth
{
get => GetValue(TabWidthProperty);
set => SetValue(TabWidthProperty, value);
}
protected override Type StyleKeyOverride => typeof(TextEditor);
public BlameTextEditor() : base(new TextArea(), new TextDocument())
{
IsReadOnly = true;
ShowLineNumbers = false;
WordWrap = false;
Options.IndentationSize = TabWidth;
Options.EnableHyperlinks = false;
Options.EnableEmailHyperlinks = false;
_textMate = Models.TextMateHelper.CreateForEditor(this);
TextArea.LeftMargins.Add(new CommitInfoMargin(this) { Margin = new Thickness(8, 0) });
TextArea.LeftMargins.Add(new VerticalSeparatorMargin(this));
TextArea.LeftMargins.Add(new LineNumberMargin() { Margin = new Thickness(8, 0) });
TextArea.LeftMargins.Add(new VerticalSeparatorMargin(this));
TextArea.Caret.PositionChanged += OnTextAreaCaretPositionChanged;
TextArea.TextView.BackgroundRenderers.Add(new LineBackgroundRenderer(this));
TextArea.TextView.ContextRequested += OnTextViewContextRequested;
TextArea.TextView.VisualLinesChanged += OnTextViewVisualLinesChanged;
TextArea.TextView.Margin = new Thickness(4, 0);
}
protected override void OnUnloaded(RoutedEventArgs e)
{
base.OnUnloaded(e);
TextArea.LeftMargins.Clear();
TextArea.Caret.PositionChanged -= OnTextAreaCaretPositionChanged;
TextArea.TextView.ContextRequested -= OnTextViewContextRequested;
TextArea.TextView.VisualLinesChanged -= OnTextViewVisualLinesChanged;
if (_textMate != null)
{
_textMate.Dispose();
_textMate = null;
}
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == FileProperty)
{
if (File is { Length: > 0 })
Models.TextMateHelper.SetGrammarByFileName(_textMate, File);
}
if (change.Property == BlameDataProperty)
{
if (BlameData is { IsBinary: false } blame)
Text = blame.Content;
else
Text = string.Empty;
}
else if (change.Property == TabWidthProperty)
{
Options.IndentationSize = TabWidth;
}
else if (change.Property.Name == nameof(ActualThemeVariant) && change.NewValue != null)
{
Models.TextMateHelper.SetThemeByApp(_textMate);
}
}
private void OnTextAreaCaretPositionChanged(object sender, EventArgs e)
{
if (!TextArea.IsFocused)
return;
var caret = TextArea.Caret;
if (caret == null || caret.Line > BlameData.LineInfos.Count)
return;
_highlight = BlameData.LineInfos[caret.Line - 1].CommitSHA;
}
private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs e)
{
var selected = SelectedText;
if (string.IsNullOrEmpty(selected))
return;
var copy = new MenuItem();
copy.Header = App.Text("Copy");
copy.Icon = this.CreateMenuIcon("Icons.Copy");
copy.Click += async (_, ev) =>
{
await this.CopyTextAsync(selected);
ev.Handled = true;
};
var menu = new ContextMenu();
menu.Items.Add(copy);
menu.Open(TextArea.TextView);
e.Handled = true;
}
private void OnTextViewVisualLinesChanged(object sender, EventArgs e)
{
foreach (var margin in TextArea.LeftMargins)
{
if (margin is CommitInfoMargin commitInfo)
{
commitInfo.InvalidateMeasure();
break;
}
}
}
private TextMate.Installation _textMate = null;
private string _highlight = string.Empty;
}
public partial class Blame : ChromelessWindow
{
public Blame()
{
InitializeComponent();
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
GC.Collect();
}
protected override void OnPointerReleased(PointerReleasedEventArgs e)
{
base.OnPointerReleased(e);
if (!e.Handled && DataContext is ViewModels.Blame blame)
{
if (e.InitialPressMouseButton == MouseButton.XButton1)
{
blame.Back();
e.Handled = true;
}
else if (e.InitialPressMouseButton == MouseButton.XButton2)
{
blame.Forward();
e.Handled = true;
}
}
}
}
}