2024-03-17 18:37:06 -07:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
using Avalonia;
|
2024-03-17 18:37:06 -07:00
|
|
|
using Avalonia.Controls;
|
|
|
|
using Avalonia.Controls.ApplicationLifetimes;
|
2024-02-05 23:08:37 -08:00
|
|
|
using Avalonia.Input;
|
|
|
|
using Avalonia.Interactivity;
|
|
|
|
using Avalonia.Media;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
using AvaloniaEdit;
|
|
|
|
using AvaloniaEdit.Document;
|
|
|
|
using AvaloniaEdit.Editing;
|
|
|
|
using AvaloniaEdit.Rendering;
|
|
|
|
using AvaloniaEdit.TextMate;
|
|
|
|
using AvaloniaEdit.Utils;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
|
|
|
namespace SourceGit.Views
|
|
|
|
{
|
|
|
|
public class BlameTextEditor : TextEditor
|
|
|
|
{
|
|
|
|
public class CommitInfoMargin : AbstractMargin
|
|
|
|
{
|
|
|
|
public CommitInfoMargin(BlameTextEditor editor)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
_editor = editor;
|
|
|
|
ClipToBounds = true;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public override void Render(DrawingContext context)
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
if (_editor.BlameData == null)
|
|
|
|
return;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var view = TextView;
|
2024-03-17 18:37:06 -07:00
|
|
|
if (view != null && view.VisualLinesValid)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var typeface = view.CreateTypeface();
|
|
|
|
var underlinePen = new Pen(Brushes.DarkOrange, 1);
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
foreach (var line in view.VisualLines)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var lineNumber = line.FirstDocumentLine.LineNumber;
|
2024-03-31 01:54:29 -07:00
|
|
|
if (lineNumber > _editor.BlameData.LineInfos.Count)
|
|
|
|
break;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var info = _editor.BlameData.LineInfos[lineNumber - 1];
|
|
|
|
var x = 0.0;
|
|
|
|
var y = line.GetTextLineVisualYPosition(line.TextLines[0], VisualYPosition.TextTop) - view.VerticalOffset;
|
2024-03-31 01:54:29 -07:00
|
|
|
if (!info.IsFirstInGroup && y > view.DefaultLineHeight * 0.6)
|
|
|
|
continue;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var shaLink = new FormattedText(
|
|
|
|
info.CommitSHA,
|
|
|
|
CultureInfo.CurrentCulture,
|
|
|
|
FlowDirection.LeftToRight,
|
|
|
|
typeface,
|
|
|
|
_editor.FontSize,
|
|
|
|
Brushes.DarkOrange);
|
|
|
|
context.DrawText(shaLink, new Point(x, y));
|
|
|
|
context.DrawLine(underlinePen, new Point(x, y + shaLink.Baseline + 2), new Point(x + shaLink.Width, y + shaLink.Baseline + 2));
|
|
|
|
x += shaLink.Width + 8;
|
|
|
|
|
|
|
|
var time = new FormattedText(
|
|
|
|
info.Time,
|
|
|
|
CultureInfo.CurrentCulture,
|
|
|
|
FlowDirection.LeftToRight,
|
|
|
|
typeface,
|
|
|
|
_editor.FontSize,
|
|
|
|
_editor.Foreground);
|
|
|
|
context.DrawText(time, new Point(x, y));
|
|
|
|
x += time.Width + 8;
|
|
|
|
|
|
|
|
var author = new FormattedText(
|
|
|
|
info.Author,
|
|
|
|
CultureInfo.CurrentCulture,
|
|
|
|
FlowDirection.LeftToRight,
|
|
|
|
typeface,
|
|
|
|
_editor.FontSize,
|
|
|
|
_editor.Foreground);
|
|
|
|
context.DrawText(author, new Point(x, y));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
protected override Size MeasureOverride(Size availableSize)
|
|
|
|
{
|
2024-02-29 23:09:17 -08:00
|
|
|
var view = TextView;
|
|
|
|
var maxWidth = 0.0;
|
2024-03-17 18:37:06 -07:00
|
|
|
if (view != null && view.VisualLinesValid && _editor.BlameData != null)
|
|
|
|
{
|
2024-02-29 23:09:17 -08:00
|
|
|
var typeface = view.CreateTypeface();
|
|
|
|
var calculated = new HashSet<string>();
|
2024-03-17 18:37:06 -07:00
|
|
|
foreach (var line in view.VisualLines)
|
|
|
|
{
|
2024-02-29 23:09:17 -08:00
|
|
|
var lineNumber = line.FirstDocumentLine.LineNumber;
|
2024-03-31 01:54:29 -07:00
|
|
|
if (lineNumber > _editor.BlameData.LineInfos.Count)
|
|
|
|
break;
|
2024-02-29 23:09:17 -08:00
|
|
|
|
|
|
|
var info = _editor.BlameData.LineInfos[lineNumber - 1];
|
|
|
|
|
2024-03-31 01:54:29 -07:00
|
|
|
if (calculated.Contains(info.CommitSHA))
|
|
|
|
continue;
|
2024-02-29 23:09:17 -08:00
|
|
|
calculated.Add(info.CommitSHA);
|
|
|
|
|
|
|
|
var x = 0.0;
|
|
|
|
var shaLink = new FormattedText(
|
|
|
|
info.CommitSHA,
|
|
|
|
CultureInfo.CurrentCulture,
|
|
|
|
FlowDirection.LeftToRight,
|
|
|
|
typeface,
|
|
|
|
_editor.FontSize,
|
|
|
|
Brushes.DarkOrange);
|
|
|
|
x += shaLink.Width + 8;
|
|
|
|
|
|
|
|
var time = new FormattedText(
|
|
|
|
info.Time,
|
|
|
|
CultureInfo.CurrentCulture,
|
|
|
|
FlowDirection.LeftToRight,
|
|
|
|
typeface,
|
|
|
|
_editor.FontSize,
|
|
|
|
_editor.Foreground);
|
|
|
|
x += time.Width + 8;
|
|
|
|
|
|
|
|
var author = new FormattedText(
|
|
|
|
info.Author,
|
|
|
|
CultureInfo.CurrentCulture,
|
|
|
|
FlowDirection.LeftToRight,
|
|
|
|
typeface,
|
|
|
|
_editor.FontSize,
|
|
|
|
_editor.Foreground);
|
|
|
|
x += author.Width;
|
|
|
|
|
2024-03-31 01:54:29 -07:00
|
|
|
if (maxWidth < x)
|
|
|
|
maxWidth = x;
|
2024-02-29 23:09:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Size(maxWidth, 0);
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
protected override void OnPointerPressed(PointerPressedEventArgs e)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
base.OnPointerPressed(e);
|
|
|
|
|
|
|
|
var view = TextView;
|
2024-03-17 18:37:06 -07:00
|
|
|
if (!e.Handled && e.GetCurrentPoint(this).Properties.IsLeftButtonPressed && view != null && view.VisualLinesValid)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var pos = e.GetPosition(this);
|
|
|
|
var typeface = view.CreateTypeface();
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
foreach (var line in view.VisualLines)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var lineNumber = line.FirstDocumentLine.LineNumber;
|
2024-03-31 01:54:29 -07:00
|
|
|
if (lineNumber >= _editor.BlameData.LineInfos.Count)
|
|
|
|
break;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
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);
|
2024-03-17 18:37:06 -07:00
|
|
|
if (rect.Contains(pos))
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
_editor.OnCommitSHAClicked(info.CommitSHA);
|
|
|
|
e.Handled = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private readonly BlameTextEditor _editor = null;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public class VerticalSeperatorMargin : AbstractMargin
|
|
|
|
{
|
|
|
|
public VerticalSeperatorMargin(BlameTextEditor editor)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
_editor = editor;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public override void Render(DrawingContext context)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var pen = new Pen(_editor.BorderBrush, 1);
|
|
|
|
context.DrawLine(pen, new Point(0, 0), new Point(0, Bounds.Height));
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
protected override Size MeasureOverride(Size availableSize)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
return new Size(1, 0);
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private readonly BlameTextEditor _editor = null;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public static readonly StyledProperty<Models.BlameData> BlameDataProperty =
|
|
|
|
AvaloniaProperty.Register<BlameTextEditor, Models.BlameData>(nameof(BlameData));
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public Models.BlameData BlameData
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
get => GetValue(BlameDataProperty);
|
|
|
|
set => SetValue(BlameDataProperty, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override Type StyleKeyOverride => typeof(TextEditor);
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public BlameTextEditor() : base(new TextArea(), new TextDocument())
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
IsReadOnly = true;
|
|
|
|
ShowLineNumbers = false;
|
|
|
|
WordWrap = false;
|
|
|
|
|
2024-03-20 03:27:48 -07:00
|
|
|
_textMate = Models.TextMateHelper.CreateForEditor(this);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
TextArea.LeftMargins.Add(new LineNumberMargin() { Margin = new Thickness(8, 0) });
|
|
|
|
TextArea.LeftMargins.Add(new VerticalSeperatorMargin(this));
|
|
|
|
TextArea.LeftMargins.Add(new CommitInfoMargin(this) { Margin = new Thickness(8, 0) });
|
|
|
|
TextArea.LeftMargins.Add(new VerticalSeperatorMargin(this));
|
|
|
|
TextArea.TextView.ContextRequested += OnTextViewContextRequested;
|
2024-02-29 23:09:17 -08:00
|
|
|
TextArea.TextView.VisualLinesChanged += OnTextViewVisualLinesChanged;
|
2024-02-05 23:08:37 -08:00
|
|
|
TextArea.TextView.Margin = new Thickness(4, 0);
|
2024-04-09 20:02:33 -07:00
|
|
|
TextArea.TextView.Options.EnableHyperlinks = false;
|
|
|
|
TextArea.TextView.Options.EnableEmailHyperlinks = false;
|
2024-03-20 03:27:48 -07:00
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-03-20 03:27:48 -07:00
|
|
|
public void OnCommitSHAClicked(string sha)
|
|
|
|
{
|
|
|
|
if (DataContext is ViewModels.Blame blame)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-03-20 03:27:48 -07:00
|
|
|
blame.NavigateToCommit(sha);
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
protected override void OnUnloaded(RoutedEventArgs e)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
base.OnUnloaded(e);
|
|
|
|
|
|
|
|
TextArea.LeftMargins.Clear();
|
|
|
|
TextArea.TextView.ContextRequested -= OnTextViewContextRequested;
|
2024-02-29 23:09:17 -08:00
|
|
|
TextArea.TextView.VisualLinesChanged -= OnTextViewVisualLinesChanged;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-03-20 03:27:48 -07:00
|
|
|
if (_textMate != null)
|
|
|
|
{
|
|
|
|
_textMate.Dispose();
|
|
|
|
_textMate = null;
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
base.OnPropertyChanged(change);
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
if (change.Property == BlameDataProperty)
|
|
|
|
{
|
|
|
|
if (BlameData != null)
|
|
|
|
{
|
2024-03-20 03:27:48 -07:00
|
|
|
Models.TextMateHelper.SetGrammarByFileName(_textMate, BlameData.File);
|
2024-02-05 23:08:37 -08:00
|
|
|
Text = BlameData.Content;
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
Text = string.Empty;
|
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
2024-03-20 03:27:48 -07:00
|
|
|
else if (change.Property.Name == "ActualThemeVariant" && change.NewValue != null)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-03-20 03:27:48 -07:00
|
|
|
Models.TextMateHelper.SetThemeByApp(_textMate);
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs e)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var selected = SelectedText;
|
2024-03-31 01:54:29 -07:00
|
|
|
if (string.IsNullOrEmpty(selected))
|
|
|
|
return;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var icon = new Avalonia.Controls.Shapes.Path();
|
|
|
|
icon.Width = 10;
|
|
|
|
icon.Height = 10;
|
|
|
|
icon.Stretch = Stretch.Uniform;
|
|
|
|
icon.Data = App.Current?.FindResource("Icons.Copy") as StreamGeometry;
|
|
|
|
|
|
|
|
var copy = new MenuItem();
|
|
|
|
copy.Header = App.Text("Copy");
|
|
|
|
copy.Icon = icon;
|
2024-03-17 18:37:06 -07:00
|
|
|
copy.Click += (o, ev) =>
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
App.CopyText(selected);
|
|
|
|
ev.Handled = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
var menu = new ContextMenu();
|
|
|
|
menu.Items.Add(copy);
|
|
|
|
menu.Open(TextArea.TextView);
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private void OnTextViewVisualLinesChanged(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
foreach (var margin in TextArea.LeftMargins)
|
|
|
|
{
|
|
|
|
if (margin is CommitInfoMargin commitInfo)
|
|
|
|
{
|
2024-02-29 23:09:17 -08:00
|
|
|
commitInfo.InvalidateMeasure();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
private TextMate.Installation _textMate = null;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public partial class Blame : Window
|
|
|
|
{
|
|
|
|
public Blame()
|
|
|
|
{
|
|
|
|
if (App.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
Owner = desktop.MainWindow;
|
|
|
|
}
|
|
|
|
|
|
|
|
InitializeComponent();
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private void MaximizeOrRestoreWindow(object sender, TappedEventArgs e)
|
|
|
|
{
|
|
|
|
if (WindowState == WindowState.Maximized)
|
|
|
|
{
|
2024-03-14 03:23:36 -07:00
|
|
|
WindowState = WindowState.Normal;
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-14 03:23:36 -07:00
|
|
|
WindowState = WindowState.Maximized;
|
|
|
|
}
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private void CustomResizeWindow(object sender, PointerPressedEventArgs e)
|
|
|
|
{
|
|
|
|
if (sender is Border border)
|
|
|
|
{
|
|
|
|
if (border.Tag is WindowEdge edge)
|
|
|
|
{
|
2024-03-14 03:23:36 -07:00
|
|
|
BeginResizeDrag(edge, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private void BeginMoveWindow(object sender, PointerPressedEventArgs e)
|
|
|
|
{
|
2024-03-14 03:23:36 -07:00
|
|
|
BeginMoveDrag(e);
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
protected override void OnClosed(EventArgs e)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
base.OnClosed(e);
|
|
|
|
GC.Collect();
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private void OnCommitSHAPointerPressed(object sender, PointerPressedEventArgs e)
|
|
|
|
{
|
|
|
|
if (DataContext is ViewModels.Blame blame)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var txt = sender as TextBlock;
|
|
|
|
blame.NavigateToCommit(txt.Text);
|
|
|
|
}
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
}
|