refactor: use dynamic resource binding property rather than hard code for commit message toolbox placeholder

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo
2025-11-06 10:45:08 +08:00
parent bc4f06eda9
commit dab051951e
2 changed files with 31 additions and 11 deletions

View File

@@ -16,6 +16,7 @@
<v:CommitMessageTextEditor Grid.Row="0"
x:Name="Editor"
CommitMessage="{Binding #ThisControl.CommitMessage, Mode=TwoWay}"
Placeholder="{DynamicResource Text.CommitMessageTextBox.Placeholder}"
SubjectLineBrush="{DynamicResource Brush.Border2}"
Foreground="{DynamicResource Brush.FG1}"
FontFamily="{DynamicResource Fonts.Default}"

View File

@@ -68,6 +68,15 @@ namespace SourceGit.Views
set => SetValue(CommitMessageProperty, value);
}
public static readonly StyledProperty<string> PlaceholderProperty =
AvaloniaProperty.Register<CommitMessageTextEditor, string>(nameof(Placeholder), string.Empty);
public string Placeholder
{
get => GetValue(PlaceholderProperty);
set => SetValue(PlaceholderProperty, value);
}
public static readonly StyledProperty<int> SubjectLengthProperty =
AvaloniaProperty.Register<CommitMessageTextEditor, int>(nameof(SubjectLength), 0);
@@ -109,20 +118,25 @@ namespace SourceGit.Views
var w = Bounds.Width;
var pen = new Pen(SubjectLineBrush) { DashStyle = DashStyle.Dash };
if (SubjectLength == 0 || CommitMessage.Trim().Length == 0)
if (SubjectLength == 0)
{
var placeholder = new FormattedText(
App.Text("CommitMessageTextBox.Placeholder"),
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface(FontFamily),
FontSize,
Brushes.Gray);
var placeholder = Placeholder;
if (!string.IsNullOrEmpty(placeholder))
{
var formatted = new FormattedText(
Placeholder,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface(FontFamily),
FontSize,
Brushes.Gray);
context.DrawText(placeholder, new Point(4, 2));
context.DrawText(formatted, new Point(4, 2));
var y = 6 + formatted.Height;
context.DrawLine(pen, new Point(0, y), new Point(w, y));
}
var y = 6 + placeholder.Height;
context.DrawLine(pen, new Point(0, y), new Point(w, y));
return;
}
@@ -209,6 +223,11 @@ namespace SourceGit.Views
SetCurrentValue(SubjectLengthProperty, subjectLen);
}
else if (change.Property == PlaceholderProperty && IsLoaded)
{
if (string.IsNullOrWhiteSpace(CommitMessage))
InvalidateVisual();
}
}
protected override void OnTextChanged(EventArgs e)