fix: ignore all whitespace before real content when calculate subject/total message length

This commit is contained in:
leo 2024-06-24 20:40:47 +08:00
parent 885dccb52b
commit 3b7545e4fb
No known key found for this signature in database
2 changed files with 38 additions and 18 deletions

View file

@ -15,7 +15,7 @@
<Grid RowDefinitions="*,Auto">
<Grid Grid.Row="0">
<Rectangle x:Name="SubjectGuideLine"
Height="1"
Height="0.8"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"
IsHitTestVisible="False"
@ -24,7 +24,7 @@
<ae:TextEditor x:Name="TextEditor"
Foreground="{DynamicResource Brush.FG1}"
Background="Transparent"
Padding="2,1"
Padding="4"
BorderThickness="0"
WordWrap="True"
Document="{Binding #ThisControl.Document}"
@ -35,7 +35,7 @@
Foreground="{DynamicResource Brush.FG2}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="2,1"
Margin="3"
IsVisible="{Binding #ThisControl.Text, Converter={x:Static StringConverters.IsNullOrEmpty}}"
IsHitTestVisible="False"/>
</Grid>
@ -54,7 +54,7 @@
<TextBlock Classes="monospace" FontSize="11" Text="{Binding Source={x:Static vm:Preference.Instance}, Path=SubjectGuideLength}"/>
<Path Width="10" Height="10" Margin="4,0,0,0" Data="{StaticResource Icons.Error}" Fill="DarkGoldenrod" IsVisible="{Binding #ThisControl.SubjectLength, Converter={x:Static c:IntConverters.IsSubjectLengthBad}}"/>
<TextBlock Margin="8,0,0,0" Text="Total:" FontSize="11" Foreground="{DynamicResource Brush.FG2}"/>
<TextBlock Classes="monospace" Margin="2,0,0,0" FontSize="11" Text="{Binding #ThisControl.Text.Length}"/>
<TextBlock Classes="monospace" Margin="2,0,0,0" FontSize="11" Text="{Binding #ThisControl.TotalLength}"/>
</StackPanel>
</Grid>
</Border>

View file

@ -13,8 +13,11 @@ namespace SourceGit.Views
public static readonly StyledProperty<string> TextProperty =
AvaloniaProperty.Register<CommitMessageTextBox, string>(nameof(Text), string.Empty);
public static readonly StyledProperty<int> SubjectLengthProperty =
AvaloniaProperty.Register<CommitMessageTextBox, int>(nameof(SubjectLength));
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);
public string Text
{
@ -24,8 +27,14 @@ namespace SourceGit.Views
public int SubjectLength
{
get => GetValue(SubjectLengthProperty);
set => SetValue(SubjectLengthProperty, value);
get => _subjectLength;
private set => SetAndRaise(SubjectLengthProperty, ref _subjectLength, value);
}
public int TotalLength
{
get => _totalLength;
private set => SetAndRaise(TotalLengthProperty, ref _totalLength, value);
}
public TextDocument Document
@ -54,7 +63,7 @@ namespace SourceGit.Views
{
if (_subjectEndLineNumber == 0)
{
SubjectGuideLine.Margin = new Thickness(1, view.DefaultLineHeight + 2, 1, 0);
SubjectGuideLine.Margin = new Thickness(0, view.DefaultLineHeight + 3, 0, 0);
SubjectGuideLine.IsVisible = true;
return;
}
@ -64,8 +73,8 @@ namespace SourceGit.Views
var lineNumber = line.FirstDocumentLine.LineNumber;
if (lineNumber == _subjectEndLineNumber)
{
var y = line.GetTextLineVisualYPosition(line.TextLines[^1], VisualYPosition.TextBottom) - view.VerticalOffset + 2;
SubjectGuideLine.Margin = new Thickness(1, y, 1, 0);
var y = line.GetTextLineVisualYPosition(line.TextLines[^1], VisualYPosition.LineBottom) - view.VerticalOffset + 3;
SubjectGuideLine.Margin = new Thickness(0, y, 0, 0);
SubjectGuideLine.IsVisible = true;
return;
}
@ -77,27 +86,38 @@ namespace SourceGit.Views
private void OnTextEditorTextChanged(object sender, EventArgs e)
{
var text = Document.Text;
_isDocumentTextChanging = true;
SetCurrentValue(TextProperty, Document.Text);
SetCurrentValue(TextProperty, text);
TotalLength = text.Trim().Length;
_isDocumentTextChanging = false;
var foundData = false;
for (var i = 0; i < Document.LineCount; i++)
{
var line = Document.Lines[i];
if (line.LineNumber > 1 && line.Length == 0)
if (line.Length == 0)
{
var subject = Text[..line.Offset].ReplaceLineEndings(" ").Trim();
SetCurrentValue(SubjectLengthProperty, subject.Length);
return;
if (foundData)
{
SubjectLength = text[..line.Offset].ReplaceLineEndings(" ").Trim().Length;
return;
}
}
else
{
foundData = true;
}
_subjectEndLineNumber = line.LineNumber;
}
SetCurrentValue(SubjectLengthProperty, Text.ReplaceLineEndings(" ").Trim().Length);
SubjectLength = text.ReplaceLineEndings(" ").Trim().Length;
}
private bool _isDocumentTextChanging = false;
private int _subjectEndLineNumber = 0;
private int _totalLength = 0;
private int _subjectLength = 0;
}
}