Skip to content

Commit dab0519

Browse files
committed
refactor: use dynamic resource binding property rather than hard code for commit message toolbox placeholder
Signed-off-by: leo <longshuang@msn.cn>
1 parent bc4f06e commit dab0519

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

src/Views/CommitMessageToolBox.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<v:CommitMessageTextEditor Grid.Row="0"
1717
x:Name="Editor"
1818
CommitMessage="{Binding #ThisControl.CommitMessage, Mode=TwoWay}"
19+
Placeholder="{DynamicResource Text.CommitMessageTextBox.Placeholder}"
1920
SubjectLineBrush="{DynamicResource Brush.Border2}"
2021
Foreground="{DynamicResource Brush.FG1}"
2122
FontFamily="{DynamicResource Fonts.Default}"

src/Views/CommitMessageToolBox.axaml.cs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ public string CommitMessage
6868
set => SetValue(CommitMessageProperty, value);
6969
}
7070

71+
public static readonly StyledProperty<string> PlaceholderProperty =
72+
AvaloniaProperty.Register<CommitMessageTextEditor, string>(nameof(Placeholder), string.Empty);
73+
74+
public string Placeholder
75+
{
76+
get => GetValue(PlaceholderProperty);
77+
set => SetValue(PlaceholderProperty, value);
78+
}
79+
7180
public static readonly StyledProperty<int> SubjectLengthProperty =
7281
AvaloniaProperty.Register<CommitMessageTextEditor, int>(nameof(SubjectLength), 0);
7382

@@ -109,20 +118,25 @@ public override void Render(DrawingContext context)
109118
var w = Bounds.Width;
110119
var pen = new Pen(SubjectLineBrush) { DashStyle = DashStyle.Dash };
111120

112-
if (SubjectLength == 0 || CommitMessage.Trim().Length == 0)
121+
if (SubjectLength == 0)
113122
{
114-
var placeholder = new FormattedText(
115-
App.Text("CommitMessageTextBox.Placeholder"),
116-
CultureInfo.CurrentCulture,
117-
FlowDirection.LeftToRight,
118-
new Typeface(FontFamily),
119-
FontSize,
120-
Brushes.Gray);
121-
122-
context.DrawText(placeholder, new Point(4, 2));
123-
124-
var y = 6 + placeholder.Height;
125-
context.DrawLine(pen, new Point(0, y), new Point(w, y));
123+
var placeholder = Placeholder;
124+
if (!string.IsNullOrEmpty(placeholder))
125+
{
126+
var formatted = new FormattedText(
127+
Placeholder,
128+
CultureInfo.CurrentCulture,
129+
FlowDirection.LeftToRight,
130+
new Typeface(FontFamily),
131+
FontSize,
132+
Brushes.Gray);
133+
134+
context.DrawText(formatted, new Point(4, 2));
135+
136+
var y = 6 + formatted.Height;
137+
context.DrawLine(pen, new Point(0, y), new Point(w, y));
138+
}
139+
126140
return;
127141
}
128142

@@ -209,6 +223,11 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
209223

210224
SetCurrentValue(SubjectLengthProperty, subjectLen);
211225
}
226+
else if (change.Property == PlaceholderProperty && IsLoaded)
227+
{
228+
if (string.IsNullOrWhiteSpace(CommitMessage))
229+
InvalidateVisual();
230+
}
212231
}
213232

214233
protected override void OnTextChanged(EventArgs e)

0 commit comments

Comments
 (0)