refactor<TextEdit>: rewrite implementation of placeholder for TextEdit

This commit is contained in:
leo 2023-07-18 19:21:42 +08:00
parent 535e05a667
commit e56a267fc8
5 changed files with 41 additions and 71 deletions

View file

@ -1,8 +1,6 @@
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace SourceGit.Views.Controls {
@ -10,8 +8,6 @@ namespace SourceGit.Views.Controls {
/// 扩展默认TextBox
/// </summary>
public class TextEdit : TextBox {
private bool isPlaceholderShow = false;
public static readonly DependencyProperty PlaceholderProperty = DependencyProperty.Register(
"Placeholder",
typeof(string),
@ -23,15 +19,15 @@ namespace SourceGit.Views.Controls {
set { SetValue(PlaceholderProperty, value); }
}
public static readonly DependencyProperty PlaceholderBaselineProperty = DependencyProperty.Register(
"PlaceholderBaseline",
typeof(AlignmentY),
public static readonly DependencyProperty PlaceholderVisibilityProperty = DependencyProperty.Register(
"PlaceholderVisibility",
typeof(Visibility),
typeof(TextEdit),
new PropertyMetadata(AlignmentY.Center));
new PropertyMetadata(Visibility.Visible));
public AlignmentY PlaceholderBaseline {
get { return (AlignmentY)GetValue(PlaceholderBaselineProperty); }
set { SetValue(PlaceholderBaselineProperty, value); }
public Visibility PlaceholderVisibility {
get { return (Visibility)GetValue(PlaceholderVisibilityProperty); }
set { SetValue(PlaceholderVisibilityProperty, value); }
}
public TextEdit() {
@ -39,41 +35,8 @@ namespace SourceGit.Views.Controls {
SelectionChanged += OnSelectionChanged;
}
protected override void OnRender(DrawingContext dc) {
base.OnRender(dc);
if (string.IsNullOrEmpty(Text) && !string.IsNullOrEmpty(Placeholder)) {
isPlaceholderShow = true;
var text = new FormattedText(
Placeholder,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface(new FontFamily(Models.Preference.Instance.General.FontFamilyWindow), FontStyle, FontWeight, FontStretch),
FontSize,
FindResource("Brush.FG2") as Brush,
new NumberSubstitution(),
TextFormattingMode.Display,
VisualTreeHelper.GetDpi(this).PixelsPerDip);
switch (PlaceholderBaseline) {
case AlignmentY.Top:
dc.DrawText(text, new Point(4, 4));
break;
case AlignmentY.Center:
dc.DrawText(text, new Point(4, ActualHeight * .5 - text.Height * .5));
break;
default:
dc.DrawText(text, new Point(4, ActualHeight - text.Height - 4));
break;
}
} else {
isPlaceholderShow = false;
}
}
private void OnTextChanged(object sender, TextChangedEventArgs e) {
if (string.IsNullOrEmpty(Text) || isPlaceholderShow) InvalidateVisual();
PlaceholderVisibility = string.IsNullOrEmpty(Text) ? Visibility.Visible : Visibility.Collapsed;
}
private void OnSelectionChanged(object sender, RoutedEventArgs e) {

View file

@ -66,7 +66,6 @@
Text="{Binding ElementName=me, Path=Message, Mode=TwoWay}"
Height="56" Padding="0,2"
AcceptsReturn="True"
Placeholder="{DynamicResource Text.CreateTag.Message.Placeholder}"
PlaceholderBaseline="Top"/>
Placeholder="{DynamicResource Text.CreateTag.Message.Placeholder}"/>
</Grid>
</controls:PopupWidget>

View file

@ -191,8 +191,9 @@
KeyDown="CommitMessageKeyDown"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Placeholder="{DynamicResource Text.WorkingCopy.CommitMessageTip}"
PlaceholderBaseline="Top">
Background="{DynamicResource Brush.Contents}"
BorderBrush="{DynamicResource Brush.Border2}"
Placeholder="{DynamicResource Text.WorkingCopy.CommitMessageTip}">
<TextBox.Text>
<Binding ElementName="me" Path="CommitMessage" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>