refactor: rewrite CommitMessageTextBox

This commit is contained in:
leo 2024-06-27 11:24:40 +08:00
parent 91ba181e02
commit 40cf5608d3
No known key found for this signature in database
5 changed files with 157 additions and 127 deletions

View file

@ -105,8 +105,8 @@
<x:String x:Key="Text.CommitDetail.Info.Parents" xml:space="preserve">PARENTS</x:String>
<x:String x:Key="Text.CommitDetail.Info.Refs" xml:space="preserve">REFS</x:String>
<x:String x:Key="Text.CommitDetail.Info.SHA" xml:space="preserve">SHA</x:String>
<x:String x:Key="Text.CommitMessageTextBox.Placeholder" xml:space="preserve">Enter commit subject &amp; message</x:String>
<x:String x:Key="Text.CommitMessageTextBox.Tip" xml:space="preserve">Git uses an empty line to separate the subject and extra message body.</x:String>
<x:String x:Key="Text.CommitMessageTextBox.SubjectPlaceholder" xml:space="preserve">Enter commit subject</x:String>
<x:String x:Key="Text.CommitMessageTextBox.MessagePlaceholder" xml:space="preserve">Description</x:String>
<x:String x:Key="Text.Configure" xml:space="preserve">Repository Configure</x:String>
<x:String x:Key="Text.Configure.Email" xml:space="preserve">Email Address</x:String>
<x:String x:Key="Text.Configure.Email.Placeholder" xml:space="preserve">Email address</x:String>

View file

@ -108,8 +108,8 @@
<x:String x:Key="Text.CommitDetail.Info.Parents" xml:space="preserve">父提交</x:String>
<x:String x:Key="Text.CommitDetail.Info.Refs" xml:space="preserve">相关引用</x:String>
<x:String x:Key="Text.CommitDetail.Info.SHA" xml:space="preserve">提交指纹</x:String>
<x:String x:Key="Text.CommitMessageTextBox.Placeholder" xml:space="preserve">填写提交信息</x:String>
<x:String x:Key="Text.CommitMessageTextBox.Tip" xml:space="preserve">Git使用空白行来划分提交信息中的主题与内容</x:String>
<x:String x:Key="Text.CommitMessageTextBox.SubjectPlaceholder" xml:space="preserve">填写提交信息主题</x:String>
<x:String x:Key="Text.CommitMessageTextBox.MessagePlaceholder" xml:space="preserve">详细描述</x:String>
<x:String x:Key="Text.Configure" xml:space="preserve">仓库配置</x:String>
<x:String x:Key="Text.Configure.Email" xml:space="preserve">电子邮箱</x:String>
<x:String x:Key="Text.Configure.Email.Placeholder" xml:space="preserve">邮箱地址</x:String>

View file

@ -108,8 +108,8 @@
<x:String x:Key="Text.CommitDetail.Info.Parents" xml:space="preserve">父提交</x:String>
<x:String x:Key="Text.CommitDetail.Info.Refs" xml:space="preserve">相關引用</x:String>
<x:String x:Key="Text.CommitDetail.Info.SHA" xml:space="preserve">提交指紋</x:String>
<x:String x:Key="Text.CommitMessageTextBox.Placeholder" xml:space="preserve">填寫提交資訊</x:String>
<x:String x:Key="Text.CommitMessageTextBox.Tip" xml:space="preserve">Git使用空白行來劃分提交資訊中的主題與內容</x:String>
<x:String x:Key="Text.CommitMessageTextBox.SubjectPlaceholder" xml:space="preserve">填寫提交信息主題</x:String>
<x:String x:Key="Text.CommitMessageTextBox.MessagePlaceholder" xml:space="preserve">詳細描述</x:String>
<x:String x:Key="Text.Configure" xml:space="preserve">倉庫配置</x:String>
<x:String x:Key="Text.Configure.Email" xml:space="preserve">電子郵箱</x:String>
<x:String x:Key="Text.Configure.Email.Placeholder" xml:space="preserve">郵箱地址</x:String>

View file

@ -2,9 +2,9 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ae="using:AvaloniaEdit"
xmlns:c="using:SourceGit.Converters"
xmlns:vm="using:SourceGit.ViewModels"
xmlns:v="using:SourceGit.Views"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="SourceGit.Views.CommitMessageTextBox"
x:Name="ThisControl">
@ -12,52 +12,67 @@
BorderThickness="1"
BorderBrush="{DynamicResource Brush.Border2}"
CornerRadius="4">
<Grid RowDefinitions="*,Auto">
<Grid Grid.Row="0">
<Rectangle x:Name="SubjectGuideLine"
Height="0.8"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"
<Grid RowDefinitions="Auto,1,*">
<Grid Grid.Row="0" ColumnDefinitions="*,1,Auto">
<v:EnhancedTextBox Grid.Column="0"
x:Name="SubjectEditor"
Classes="no_border"
Margin="0"
Padding="4"
CornerRadius="4,4,0,0"
BorderThickness="0"
Background="Transparent"
AcceptsReturn="False"
TextWrapping="Wrap"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
Text="{Binding #ThisControl.Subject, Mode=TwoWay}"
Watermark="{DynamicResource Text.CommitMessageTextBox.SubjectPlaceholder}"
PreviewKeyDown="OnSubjectTextBoxPreviewKeyDown"/>
<Rectangle Grid.Column="1"
Width="1"
HorizontalAlignment="Center"
VerticalAlignment="Stretch"
IsHitTestVisible="False"
Fill="{DynamicResource Brush.Border2}"/>
<ae:TextEditor x:Name="TextEditor"
Foreground="{DynamicResource Brush.FG1}"
Background="Transparent"
Padding="4"
BorderThickness="0"
WordWrap="True"
Document="{Binding #ThisControl.Document}"
TextChanged="OnTextEditorTextChanged"
LayoutUpdated="OnTextEditorLayoutUpdated"/>
<TextBlock Text="{DynamicResource Text.CommitMessageTextBox.Placeholder}"
Foreground="{DynamicResource Brush.FG2}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="3"
IsVisible="{Binding #ThisControl.Text, Converter={x:Static StringConverters.IsNullOrEmpty}}"
IsHitTestVisible="False"/>
<StackPanel Grid.Column="2"
Margin="8,0"
VerticalAlignment="Center"
Orientation="Horizontal">
<TextBlock Classes="monospace" Margin="2,0,0,0" FontSize="11" Text="{Binding #ThisControl.Subject.Length}" IsVisible="{Binding #ThisControl.Subject.Length, Converter={x:Static c:IntConverters.IsSubjectLengthGood}}"/>
<TextBlock Classes="monospace" Margin="2,0,0,0" FontSize="11" Foreground="DarkGoldenrod" Text="{Binding #ThisControl.Subject.Length}" IsVisible="{Binding #ThisControl.Subject.Length, Converter={x:Static c:IntConverters.IsSubjectLengthBad}}"/>
<TextBlock Classes="monospace" FontSize="11" Text="/"/>
<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.Subject.Length, Converter={x:Static c:IntConverters.IsSubjectLengthBad}}"/>
</StackPanel>
</Grid>
<Border Grid.Row="1" CornerRadius="0,0,4,4" BorderThickness="0,1,0,0" BorderBrush="{DynamicResource Brush.Border2}" Background="{DynamicResource Brush.Window}" ClipToBounds="True">
<Grid ColumnDefinitions="Auto,*,Auto">
<Border Grid.Column="0" Background="Transparent" Width="16" Height="16" ToolTip.Tip="{DynamicResource Text.CommitMessageTextBox.Tip}">
<Path Height="12" Width="12" Margin="4,0,0,0" Data="{DynamicResource Icons.Info}"/>
</Border>
<StackPanel Grid.Column="2" Orientation="Horizontal" Margin="4,2">
<TextBlock Text="Subject:" FontSize="10" Foreground="{DynamicResource Brush.FG2}"/>
<TextBlock Classes="monospace" Margin="2,0,0,0" FontSize="11" Text="{Binding #ThisControl.SubjectLength}" IsVisible="{Binding #ThisControl.SubjectLength, Converter={x:Static c:IntConverters.IsSubjectLengthGood}}"/>
<TextBlock Classes="monospace" Margin="2,0,0,0" FontSize="11" Foreground="DarkGoldenrod" Text="{Binding #ThisControl.SubjectLength}" IsVisible="{Binding #ThisControl.SubjectLength, Converter={x:Static c:IntConverters.IsSubjectLengthBad}}"/>
<TextBlock Classes="monospace" FontSize="11" Text="/"/>
<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.TotalLength}"/>
</StackPanel>
</Grid>
</Border>
<Rectangle Grid.Row="1"
Height="1"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
IsHitTestVisible="False"
Fill="{DynamicResource Brush.Border2}"/>
<v:EnhancedTextBox Grid.Row="2"
x:Name="DescriptionEditor"
Classes="no_border"
Margin="0"
Padding="4"
CornerRadius="0,0,4,4"
BorderThickness="0"
Background="Transparent"
VerticalContentAlignment="Top"
AcceptsReturn="True"
AcceptsTab="True"
TextWrapping="Wrap"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Text="{Binding #ThisControl.Description, Mode=TwoWay}"
Watermark="{DynamicResource Text.CommitMessageTextBox.MessagePlaceholder}"
PreviewKeyDown="OnDescriptionTextBoxPreviewKeyDown"/>
</Grid>
</Border>
</UserControl>

View file

@ -2,122 +2,137 @@ using System;
using Avalonia;
using Avalonia.Controls;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using Avalonia.Input;
using Avalonia.Interactivity;
namespace SourceGit.Views
{
public class EnhancedTextBox : TextBox
{
public static readonly RoutedEvent<KeyEventArgs> PreviewKeyDownEvent =
RoutedEvent.Register<ChangeCollectionView, KeyEventArgs>(nameof(KeyEventArgs), RoutingStrategies.Tunnel | RoutingStrategies.Bubble);
public event EventHandler<KeyEventArgs> PreviewKeyDown
{
add { AddHandler(PreviewKeyDownEvent, value); }
remove { RemoveHandler(PreviewKeyDownEvent, value); }
}
protected override Type StyleKeyOverride => typeof(TextBox);
protected override void OnKeyDown(KeyEventArgs e)
{
var dump = new KeyEventArgs()
{
RoutedEvent = PreviewKeyDownEvent,
Route = RoutingStrategies.Direct,
Source = e.Source,
Key = e.Key,
KeyModifiers = e.KeyModifiers,
PhysicalKey = e.PhysicalKey,
KeySymbol = e.KeySymbol,
};
RaiseEvent(dump);
if (dump.Handled)
e.Handled = true;
else
base.OnKeyDown(e);
}
}
public partial class CommitMessageTextBox : UserControl
{
public enum TextChangeWay
{
None,
FromSource,
FromEditor,
}
public static readonly StyledProperty<string> TextProperty =
AvaloniaProperty.Register<CommitMessageTextBox, string>(nameof(Text), string.Empty);
public static readonly DirectProperty<CommitMessageTextBox, int> SubjectLengthProperty =
AvaloniaProperty.RegisterDirect<CommitMessageTextBox, int>(nameof(SubjectLength), o => o.SubjectLength);
public static readonly StyledProperty<string> SubjectProperty =
AvaloniaProperty.Register<CommitMessageTextBox, string>(nameof(Subject), string.Empty);
public static readonly StyledProperty<string> DescriptionProperty =
AvaloniaProperty.Register<CommitMessageTextBox, string>(nameof(Description), string.Empty);
public static readonly DirectProperty<CommitMessageTextBox, int> TotalLengthProperty =
AvaloniaProperty.RegisterDirect<CommitMessageTextBox, int>(nameof(TotalLength), o => o.TotalLength);
public string Text
{
get => GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
public int SubjectLength
public string Subject
{
get => _subjectLength;
private set => SetAndRaise(SubjectLengthProperty, ref _subjectLength, value);
get => GetValue(SubjectProperty);
set => SetValue(SubjectProperty, value);
}
public int TotalLength
public string Description
{
get => _totalLength;
private set => SetAndRaise(TotalLengthProperty, ref _totalLength, value);
get => GetValue(DescriptionProperty);
set => SetValue(DescriptionProperty, value);
}
public TextDocument Document
{
get;
}
public CommitMessageTextBox()
{
Document = new TextDocument(Text);
InitializeComponent();
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == TextProperty && !_isDocumentTextChanging)
Document.Text = Text;
}
private void OnTextEditorLayoutUpdated(object sender, EventArgs e)
{
var view = TextEditor.TextArea?.TextView;
if (view is { VisualLinesValid: true })
if (change.Property == TextProperty && _changingWay == TextChangeWay.None)
{
if (_subjectEndLineNumber == 0)
_changingWay = TextChangeWay.FromSource;
var normalized = Text.ReplaceLineEndings("\n").Trim();
var subjectEnd = normalized.IndexOf("\n\n", StringComparison.Ordinal);
if (subjectEnd == -1)
{
SubjectGuideLine.Margin = new Thickness(0, view.DefaultLineHeight + 3, 0, 0);
SubjectGuideLine.IsVisible = true;
return;
}
foreach (var line in view.VisualLines)
{
var lineNumber = line.FirstDocumentLine.LineNumber;
if (lineNumber == _subjectEndLineNumber)
{
var y = line.GetTextLineVisualYPosition(line.TextLines[^1], VisualYPosition.LineBottom) - view.VerticalOffset + 3;
SubjectGuideLine.Margin = new Thickness(0, y, 0, 0);
SubjectGuideLine.IsVisible = true;
return;
}
}
}
SubjectGuideLine.IsVisible = false;
}
private void OnTextEditorTextChanged(object sender, EventArgs e)
{
var text = Document.Text;
_isDocumentTextChanging = true;
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.Length == 0)
{
if (foundData)
{
SubjectLength = text[..line.Offset].ReplaceLineEndings(" ").Trim().Length;
return;
}
SetCurrentValue(SubjectProperty, normalized.ReplaceLineEndings(" "));
SetCurrentValue(DescriptionProperty, string.Empty);
}
else
{
foundData = true;
SetCurrentValue(SubjectProperty, normalized.Substring(0, subjectEnd).ReplaceLineEndings(" "));
SetCurrentValue(DescriptionProperty, normalized.Substring(subjectEnd + 2));
}
_subjectEndLineNumber = line.LineNumber;
_changingWay = TextChangeWay.None;
}
else if ((change.Property == SubjectProperty || change.Property == DescriptionProperty) && _changingWay == TextChangeWay.None)
{
_changingWay = TextChangeWay.FromEditor;
SetCurrentValue(TextProperty, $"{Subject}\n\n{Description}");
_changingWay = TextChangeWay.None;
}
SubjectLength = text.ReplaceLineEndings(" ").Trim().Length;
}
private bool _isDocumentTextChanging = false;
private int _subjectEndLineNumber = 0;
private int _totalLength = 0;
private int _subjectLength = 0;
private void OnSubjectTextBoxPreviewKeyDown(object sender, KeyEventArgs e)
{
if ((e.Key == Key.Enter && e.KeyModifiers == KeyModifiers.None)
|| (e.Key == Key.Right && SubjectEditor.CaretIndex == Subject.Length))
{
DescriptionEditor.Focus();
DescriptionEditor.CaretIndex = 0;
e.Handled = true;
}
}
private void OnDescriptionTextBoxPreviewKeyDown(object sender, KeyEventArgs e)
{
if ((e.Key == Key.Back || e.Key == Key.Left) && DescriptionEditor.CaretIndex == 0)
{
SubjectEditor.Focus();
SubjectEditor.CaretIndex = Subject.Length;
e.Handled = true;
}
}
private TextChangeWay _changingWay = TextChangeWay.None;
}
}