2024-06-23 00:45:54 -07:00
|
|
|
using System;
|
|
|
|
|
|
|
|
using Avalonia;
|
|
|
|
using Avalonia.Controls;
|
|
|
|
|
|
|
|
using AvaloniaEdit.Document;
|
|
|
|
using AvaloniaEdit.Rendering;
|
|
|
|
|
|
|
|
namespace SourceGit.Views
|
|
|
|
{
|
|
|
|
public partial class CommitMessageTextBox : UserControl
|
|
|
|
{
|
|
|
|
public static readonly StyledProperty<string> TextProperty =
|
|
|
|
AvaloniaProperty.Register<CommitMessageTextBox, string>(nameof(Text), string.Empty);
|
|
|
|
|
2024-06-24 05:40:47 -07:00
|
|
|
public static readonly DirectProperty<CommitMessageTextBox, int> SubjectLengthProperty =
|
|
|
|
AvaloniaProperty.RegisterDirect<CommitMessageTextBox, int>(nameof(SubjectLength), o => o.SubjectLength);
|
|
|
|
|
|
|
|
public static readonly DirectProperty<CommitMessageTextBox, int> TotalLengthProperty =
|
|
|
|
AvaloniaProperty.RegisterDirect<CommitMessageTextBox, int>(nameof(TotalLength), o => o.TotalLength);
|
2024-06-23 00:45:54 -07:00
|
|
|
|
|
|
|
public string Text
|
|
|
|
{
|
|
|
|
get => GetValue(TextProperty);
|
|
|
|
set => SetValue(TextProperty, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int SubjectLength
|
|
|
|
{
|
2024-06-24 05:40:47 -07:00
|
|
|
get => _subjectLength;
|
|
|
|
private set => SetAndRaise(SubjectLengthProperty, ref _subjectLength, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int TotalLength
|
|
|
|
{
|
|
|
|
get => _totalLength;
|
|
|
|
private set => SetAndRaise(TotalLengthProperty, ref _totalLength, value);
|
2024-06-23 00:45:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public TextDocument Document
|
|
|
|
{
|
|
|
|
get;
|
|
|
|
}
|
|
|
|
|
|
|
|
public CommitMessageTextBox()
|
|
|
|
{
|
|
|
|
Document = new TextDocument(Text);
|
|
|
|
InitializeComponent();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
|
|
|
|
{
|
|
|
|
base.OnPropertyChanged(change);
|
|
|
|
|
2024-06-23 06:35:28 -07:00
|
|
|
if (change.Property == TextProperty && !_isDocumentTextChanging)
|
2024-06-23 00:45:54 -07:00
|
|
|
Document.Text = Text;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnTextEditorLayoutUpdated(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
var view = TextEditor.TextArea?.TextView;
|
|
|
|
if (view is { VisualLinesValid: true })
|
|
|
|
{
|
|
|
|
if (_subjectEndLineNumber == 0)
|
|
|
|
{
|
2024-06-24 05:40:47 -07:00
|
|
|
SubjectGuideLine.Margin = new Thickness(0, view.DefaultLineHeight + 3, 0, 0);
|
2024-06-23 00:45:54 -07:00
|
|
|
SubjectGuideLine.IsVisible = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var line in view.VisualLines)
|
|
|
|
{
|
|
|
|
var lineNumber = line.FirstDocumentLine.LineNumber;
|
|
|
|
if (lineNumber == _subjectEndLineNumber)
|
|
|
|
{
|
2024-06-24 05:40:47 -07:00
|
|
|
var y = line.GetTextLineVisualYPosition(line.TextLines[^1], VisualYPosition.LineBottom) - view.VerticalOffset + 3;
|
|
|
|
SubjectGuideLine.Margin = new Thickness(0, y, 0, 0);
|
2024-06-23 00:45:54 -07:00
|
|
|
SubjectGuideLine.IsVisible = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SubjectGuideLine.IsVisible = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnTextEditorTextChanged(object sender, EventArgs e)
|
|
|
|
{
|
2024-06-24 05:40:47 -07:00
|
|
|
var text = Document.Text;
|
2024-06-23 00:45:54 -07:00
|
|
|
_isDocumentTextChanging = true;
|
2024-06-24 05:40:47 -07:00
|
|
|
SetCurrentValue(TextProperty, text);
|
|
|
|
TotalLength = text.Trim().Length;
|
2024-06-23 00:45:54 -07:00
|
|
|
_isDocumentTextChanging = false;
|
2024-06-24 05:40:47 -07:00
|
|
|
|
|
|
|
var foundData = false;
|
2024-06-23 06:35:28 -07:00
|
|
|
for (var i = 0; i < Document.LineCount; i++)
|
2024-06-23 00:45:54 -07:00
|
|
|
{
|
|
|
|
var line = Document.Lines[i];
|
2024-06-24 05:40:47 -07:00
|
|
|
if (line.Length == 0)
|
2024-06-23 00:45:54 -07:00
|
|
|
{
|
2024-06-24 05:40:47 -07:00
|
|
|
if (foundData)
|
|
|
|
{
|
|
|
|
SubjectLength = text[..line.Offset].ReplaceLineEndings(" ").Trim().Length;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
foundData = true;
|
2024-06-23 00:45:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
_subjectEndLineNumber = line.LineNumber;
|
|
|
|
}
|
|
|
|
|
2024-06-24 05:40:47 -07:00
|
|
|
SubjectLength = text.ReplaceLineEndings(" ").Trim().Length;
|
2024-06-23 00:45:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private bool _isDocumentTextChanging = false;
|
|
|
|
private int _subjectEndLineNumber = 0;
|
2024-06-24 05:40:47 -07:00
|
|
|
private int _totalLength = 0;
|
|
|
|
private int _subjectLength = 0;
|
2024-06-23 00:45:54 -07:00
|
|
|
}
|
|
|
|
}
|