|
2 | 2 |
|
3 | 3 | using Avalonia; |
4 | 4 | using Avalonia.Controls; |
5 | | - |
6 | | -using AvaloniaEdit.Document; |
7 | | -using AvaloniaEdit.Rendering; |
| 5 | +using Avalonia.Input; |
| 6 | +using Avalonia.Interactivity; |
8 | 7 |
|
9 | 8 | namespace SourceGit.Views |
10 | 9 | { |
| 10 | + public class EnhancedTextBox : TextBox |
| 11 | + { |
| 12 | + public static readonly RoutedEvent<KeyEventArgs> PreviewKeyDownEvent = |
| 13 | + RoutedEvent.Register<ChangeCollectionView, KeyEventArgs>(nameof(KeyEventArgs), RoutingStrategies.Tunnel | RoutingStrategies.Bubble); |
| 14 | + |
| 15 | + public event EventHandler<KeyEventArgs> PreviewKeyDown |
| 16 | + { |
| 17 | + add { AddHandler(PreviewKeyDownEvent, value); } |
| 18 | + remove { RemoveHandler(PreviewKeyDownEvent, value); } |
| 19 | + } |
| 20 | + |
| 21 | + protected override Type StyleKeyOverride => typeof(TextBox); |
| 22 | + |
| 23 | + protected override void OnKeyDown(KeyEventArgs e) |
| 24 | + { |
| 25 | + var dump = new KeyEventArgs() |
| 26 | + { |
| 27 | + RoutedEvent = PreviewKeyDownEvent, |
| 28 | + Route = RoutingStrategies.Direct, |
| 29 | + Source = e.Source, |
| 30 | + Key = e.Key, |
| 31 | + KeyModifiers = e.KeyModifiers, |
| 32 | + PhysicalKey = e.PhysicalKey, |
| 33 | + KeySymbol = e.KeySymbol, |
| 34 | + }; |
| 35 | + |
| 36 | + RaiseEvent(dump); |
| 37 | + |
| 38 | + if (dump.Handled) |
| 39 | + e.Handled = true; |
| 40 | + else |
| 41 | + base.OnKeyDown(e); |
| 42 | + } |
| 43 | + } |
| 44 | + |
11 | 45 | public partial class CommitMessageTextBox : UserControl |
12 | 46 | { |
| 47 | + public enum TextChangeWay |
| 48 | + { |
| 49 | + None, |
| 50 | + FromSource, |
| 51 | + FromEditor, |
| 52 | + } |
| 53 | + |
13 | 54 | public static readonly StyledProperty<string> TextProperty = |
14 | 55 | AvaloniaProperty.Register<CommitMessageTextBox, string>(nameof(Text), string.Empty); |
15 | 56 |
|
16 | | - public static readonly DirectProperty<CommitMessageTextBox, int> SubjectLengthProperty = |
17 | | - AvaloniaProperty.RegisterDirect<CommitMessageTextBox, int>(nameof(SubjectLength), o => o.SubjectLength); |
| 57 | + public static readonly StyledProperty<string> SubjectProperty = |
| 58 | + AvaloniaProperty.Register<CommitMessageTextBox, string>(nameof(Subject), string.Empty); |
| 59 | + |
| 60 | + public static readonly StyledProperty<string> DescriptionProperty = |
| 61 | + AvaloniaProperty.Register<CommitMessageTextBox, string>(nameof(Description), string.Empty); |
18 | 62 |
|
19 | | - public static readonly DirectProperty<CommitMessageTextBox, int> TotalLengthProperty = |
20 | | - AvaloniaProperty.RegisterDirect<CommitMessageTextBox, int>(nameof(TotalLength), o => o.TotalLength); |
21 | | - |
22 | 63 | public string Text |
23 | 64 | { |
24 | 65 | get => GetValue(TextProperty); |
25 | 66 | set => SetValue(TextProperty, value); |
26 | 67 | } |
27 | 68 |
|
28 | | - public int SubjectLength |
| 69 | + public string Subject |
29 | 70 | { |
30 | | - get => _subjectLength; |
31 | | - private set => SetAndRaise(SubjectLengthProperty, ref _subjectLength, value); |
| 71 | + get => GetValue(SubjectProperty); |
| 72 | + set => SetValue(SubjectProperty, value); |
32 | 73 | } |
33 | 74 |
|
34 | | - public int TotalLength |
| 75 | + public string Description |
35 | 76 | { |
36 | | - get => _totalLength; |
37 | | - private set => SetAndRaise(TotalLengthProperty, ref _totalLength, value); |
| 77 | + get => GetValue(DescriptionProperty); |
| 78 | + set => SetValue(DescriptionProperty, value); |
38 | 79 | } |
39 | 80 |
|
40 | | - public TextDocument Document |
41 | | - { |
42 | | - get; |
43 | | - } |
44 | | - |
45 | 81 | public CommitMessageTextBox() |
46 | 82 | { |
47 | | - Document = new TextDocument(Text); |
48 | 83 | InitializeComponent(); |
49 | 84 | } |
50 | | - |
| 85 | + |
51 | 86 | protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) |
52 | 87 | { |
53 | 88 | base.OnPropertyChanged(change); |
54 | 89 |
|
55 | | - if (change.Property == TextProperty && !_isDocumentTextChanging) |
56 | | - Document.Text = Text; |
57 | | - } |
58 | | - |
59 | | - private void OnTextEditorLayoutUpdated(object sender, EventArgs e) |
60 | | - { |
61 | | - var view = TextEditor.TextArea?.TextView; |
62 | | - if (view is { VisualLinesValid: true }) |
| 90 | + if (change.Property == TextProperty && _changingWay == TextChangeWay.None) |
63 | 91 | { |
64 | | - if (_subjectEndLineNumber == 0) |
| 92 | + _changingWay = TextChangeWay.FromSource; |
| 93 | + var normalized = Text.ReplaceLineEndings("\n").Trim(); |
| 94 | + var subjectEnd = normalized.IndexOf("\n\n", StringComparison.Ordinal); |
| 95 | + if (subjectEnd == -1) |
65 | 96 | { |
66 | | - SubjectGuideLine.Margin = new Thickness(0, view.DefaultLineHeight + 3, 0, 0); |
67 | | - SubjectGuideLine.IsVisible = true; |
68 | | - return; |
| 97 | + SetCurrentValue(SubjectProperty, normalized.ReplaceLineEndings(" ")); |
| 98 | + SetCurrentValue(DescriptionProperty, string.Empty); |
69 | 99 | } |
70 | | - |
71 | | - foreach (var line in view.VisualLines) |
| 100 | + else |
72 | 101 | { |
73 | | - var lineNumber = line.FirstDocumentLine.LineNumber; |
74 | | - if (lineNumber == _subjectEndLineNumber) |
75 | | - { |
76 | | - var y = line.GetTextLineVisualYPosition(line.TextLines[^1], VisualYPosition.LineBottom) - view.VerticalOffset + 3; |
77 | | - SubjectGuideLine.Margin = new Thickness(0, y, 0, 0); |
78 | | - SubjectGuideLine.IsVisible = true; |
79 | | - return; |
80 | | - } |
| 102 | + SetCurrentValue(SubjectProperty, normalized.Substring(0, subjectEnd).ReplaceLineEndings(" ")); |
| 103 | + SetCurrentValue(DescriptionProperty, normalized.Substring(subjectEnd + 2)); |
81 | 104 | } |
| 105 | + _changingWay = TextChangeWay.None; |
| 106 | + } |
| 107 | + else if ((change.Property == SubjectProperty || change.Property == DescriptionProperty) && _changingWay == TextChangeWay.None) |
| 108 | + { |
| 109 | + _changingWay = TextChangeWay.FromEditor; |
| 110 | + SetCurrentValue(TextProperty, $"{Subject}\n\n{Description}"); |
| 111 | + _changingWay = TextChangeWay.None; |
82 | 112 | } |
83 | | - |
84 | | - SubjectGuideLine.IsVisible = false; |
85 | 113 | } |
86 | 114 |
|
87 | | - private void OnTextEditorTextChanged(object sender, EventArgs e) |
| 115 | + private void OnSubjectTextBoxPreviewKeyDown(object sender, KeyEventArgs e) |
88 | 116 | { |
89 | | - var text = Document.Text; |
90 | | - _isDocumentTextChanging = true; |
91 | | - SetCurrentValue(TextProperty, text); |
92 | | - TotalLength = text.Trim().Length; |
93 | | - _isDocumentTextChanging = false; |
94 | | - |
95 | | - var foundData = false; |
96 | | - for (var i = 0; i < Document.LineCount; i++) |
| 117 | + if ((e.Key == Key.Enter && e.KeyModifiers == KeyModifiers.None) |
| 118 | + || (e.Key == Key.Right && SubjectEditor.CaretIndex == Subject.Length)) |
97 | 119 | { |
98 | | - var line = Document.Lines[i]; |
99 | | - if (line.Length == 0) |
100 | | - { |
101 | | - if (foundData) |
102 | | - { |
103 | | - SubjectLength = text[..line.Offset].ReplaceLineEndings(" ").Trim().Length; |
104 | | - return; |
105 | | - } |
106 | | - } |
107 | | - else |
108 | | - { |
109 | | - foundData = true; |
110 | | - } |
111 | | - |
112 | | - _subjectEndLineNumber = line.LineNumber; |
| 120 | + DescriptionEditor.Focus(); |
| 121 | + DescriptionEditor.CaretIndex = 0; |
| 122 | + e.Handled = true; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private void OnDescriptionTextBoxPreviewKeyDown(object sender, KeyEventArgs e) |
| 127 | + { |
| 128 | + if ((e.Key == Key.Back || e.Key == Key.Left) && DescriptionEditor.CaretIndex == 0) |
| 129 | + { |
| 130 | + SubjectEditor.Focus(); |
| 131 | + SubjectEditor.CaretIndex = Subject.Length; |
| 132 | + e.Handled = true; |
113 | 133 | } |
114 | | - |
115 | | - SubjectLength = text.ReplaceLineEndings(" ").Trim().Length; |
116 | 134 | } |
117 | 135 |
|
118 | | - private bool _isDocumentTextChanging = false; |
119 | | - private int _subjectEndLineNumber = 0; |
120 | | - private int _totalLength = 0; |
121 | | - private int _subjectLength = 0; |
| 136 | + private TextChangeWay _changingWay = TextChangeWay.None; |
122 | 137 | } |
123 | 138 | } |
0 commit comments