|
| 1 | +using System; |
| 2 | + |
| 3 | +using Avalonia; |
| 4 | +using Avalonia.Controls; |
| 5 | + |
| 6 | +using AvaloniaEdit.Document; |
| 7 | +using AvaloniaEdit.Rendering; |
| 8 | + |
| 9 | +namespace SourceGit.Views |
| 10 | +{ |
| 11 | + public partial class CommitMessageTextBox : UserControl |
| 12 | + { |
| 13 | + public static readonly StyledProperty<string> TextProperty = |
| 14 | + AvaloniaProperty.Register<CommitMessageTextBox, string>(nameof(Text), string.Empty); |
| 15 | + |
| 16 | + public static readonly StyledProperty<int> SubjectLengthProperty = |
| 17 | + AvaloniaProperty.Register<CommitMessageTextBox, int>(nameof(SubjectLength)); |
| 18 | + |
| 19 | + public string Text |
| 20 | + { |
| 21 | + get => GetValue(TextProperty); |
| 22 | + set => SetValue(TextProperty, value); |
| 23 | + } |
| 24 | + |
| 25 | + public int SubjectLength |
| 26 | + { |
| 27 | + get => GetValue(SubjectLengthProperty); |
| 28 | + set => SetValue(SubjectLengthProperty, value); |
| 29 | + } |
| 30 | + |
| 31 | + public TextDocument Document |
| 32 | + { |
| 33 | + get; |
| 34 | + private set; |
| 35 | + } |
| 36 | + |
| 37 | + public CommitMessageTextBox() |
| 38 | + { |
| 39 | + Document = new TextDocument(Text); |
| 40 | + InitializeComponent(); |
| 41 | + } |
| 42 | + |
| 43 | + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) |
| 44 | + { |
| 45 | + base.OnPropertyChanged(change); |
| 46 | + |
| 47 | + if (change.Property == TextProperty) |
| 48 | + { |
| 49 | + if (!_isDocumentTextChanging) |
| 50 | + Document.Text = Text; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private void OnTextEditorLayoutUpdated(object sender, EventArgs e) |
| 55 | + { |
| 56 | + var view = TextEditor.TextArea?.TextView; |
| 57 | + if (view is { VisualLinesValid: true }) |
| 58 | + { |
| 59 | + if (_subjectEndLineNumber == 0) |
| 60 | + { |
| 61 | + SubjectGuideLine.Margin = new Thickness(1, view.DefaultLineHeight + 2, 1, 0); |
| 62 | + SubjectGuideLine.IsVisible = true; |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + foreach (var line in view.VisualLines) |
| 67 | + { |
| 68 | + var lineNumber = line.FirstDocumentLine.LineNumber; |
| 69 | + if (lineNumber == _subjectEndLineNumber) |
| 70 | + { |
| 71 | + var y = line.GetTextLineVisualYPosition(line.TextLines[^1], VisualYPosition.TextBottom) - view.VerticalOffset + 2; |
| 72 | + SubjectGuideLine.Margin = new Thickness(1, y, 1, 0); |
| 73 | + SubjectGuideLine.IsVisible = true; |
| 74 | + return; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + SubjectGuideLine.IsVisible = false; |
| 80 | + } |
| 81 | + |
| 82 | + private void OnTextEditorTextChanged(object sender, EventArgs e) |
| 83 | + { |
| 84 | + _isDocumentTextChanging = true; |
| 85 | + SetCurrentValue(TextProperty, Document.Text); |
| 86 | + _isDocumentTextChanging = false; |
| 87 | + |
| 88 | + var setSubject = false; |
| 89 | + for (int i = 0; i < Document.LineCount; i++) |
| 90 | + { |
| 91 | + var line = Document.Lines[i]; |
| 92 | + if (line.LineNumber > 1 && line.Length == 0) |
| 93 | + { |
| 94 | + var subject = Text.Substring(0, line.Offset).ReplaceLineEndings(" ").Trim(); |
| 95 | + SetCurrentValue(SubjectLengthProperty, subject.Length); |
| 96 | + setSubject = true; |
| 97 | + break; |
| 98 | + } |
| 99 | + |
| 100 | + _subjectEndLineNumber = line.LineNumber; |
| 101 | + } |
| 102 | + |
| 103 | + if (setSubject) |
| 104 | + return; |
| 105 | + |
| 106 | + SetCurrentValue(SubjectLengthProperty, Text.ReplaceLineEndings(" ").Trim().Length); |
| 107 | + } |
| 108 | + |
| 109 | + private bool _isDocumentTextChanging = false; |
| 110 | + private int _subjectEndLineNumber = 0; |
| 111 | + } |
| 112 | +} |
0 commit comments