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 RowDefinitions="*,Auto">
<Grid Grid.Row="0"> <Grid Grid.Row="0">
<Rectangle x:Name="SubjectGuideLine" <Rectangle x:Name="SubjectGuideLine"
Height="1" Height="0.8"
HorizontalAlignment="Stretch" HorizontalAlignment="Stretch"
VerticalAlignment="Top" VerticalAlignment="Top"
IsHitTestVisible="False" IsHitTestVisible="False"
@ -24,7 +24,7 @@
<ae:TextEditor x:Name="TextEditor" <ae:TextEditor x:Name="TextEditor"
Foreground="{DynamicResource Brush.FG1}" Foreground="{DynamicResource Brush.FG1}"
Background="Transparent" Background="Transparent"
Padding="2,1" Padding="4"
BorderThickness="0" BorderThickness="0"
WordWrap="True" WordWrap="True"
Document="{Binding #ThisControl.Document}" Document="{Binding #ThisControl.Document}"
@ -35,7 +35,7 @@
Foreground="{DynamicResource Brush.FG2}" Foreground="{DynamicResource Brush.FG2}"
HorizontalAlignment="Left" HorizontalAlignment="Left"
VerticalAlignment="Top" VerticalAlignment="Top"
Margin="2,1" Margin="3"
IsVisible="{Binding #ThisControl.Text, Converter={x:Static StringConverters.IsNullOrEmpty}}" IsVisible="{Binding #ThisControl.Text, Converter={x:Static StringConverters.IsNullOrEmpty}}"
IsHitTestVisible="False"/> IsHitTestVisible="False"/>
</Grid> </Grid>
@ -54,7 +54,7 @@
<TextBlock Classes="monospace" FontSize="11" Text="{Binding Source={x:Static vm:Preference.Instance}, Path=SubjectGuideLength}"/> <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}}"/> <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 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> </StackPanel>
</Grid> </Grid>
</Border> </Border>

View file

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