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

@ -19,10 +19,6 @@
</ControlTemplate.Triggers> </ControlTemplate.Triggers>
</ControlTemplate> </ControlTemplate>
<ControlTemplate x:Key="Template.ComboBox.TextBox" TargetType="{x:Type TextBox}">
<Border x:Name="PART_ContentHost" Focusable="False" Background="{TemplateBinding Background}" />
</ControlTemplate>
<Style TargetType="{x:Type ComboBox}"> <Style TargetType="{x:Type ComboBox}">
<Setter Property="SnapsToDevicePixels" Value="true" /> <Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="OverridesDefaultStyle" Value="true" /> <Setter Property="OverridesDefaultStyle" Value="true" />
@ -38,7 +34,6 @@
<Grid> <Grid>
<ToggleButton x:Name="ToggleButton" BorderThickness="{TemplateBinding BorderThickness}" Template="{StaticResource Template.ComboBox.ToggleButton}" Grid.Column="2" Focusable="false" ClickMode="Press" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"/> <ToggleButton x:Name="ToggleButton" BorderThickness="{TemplateBinding BorderThickness}" Template="{StaticResource Template.ComboBox.ToggleButton}" Grid.Column="2" Focusable="false" ClickMode="Press" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"/>
<ContentPresenter x:Name="ContentSite" IsHitTestVisible="False" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Margin="3,3,23,3" VerticalAlignment="Center" HorizontalAlignment="Left" TextElement.Foreground="{DynamicResource Brush.FG1}"/> <ContentPresenter x:Name="ContentSite" IsHitTestVisible="False" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Margin="3,3,23,3" VerticalAlignment="Center" HorizontalAlignment="Left" TextElement.Foreground="{DynamicResource Brush.FG1}"/>
<TextBox x:Name="PART_EditableTextBox" Style="{x:Null}" Template="{StaticResource Template.ComboBox.TextBox}" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="3,3,23,3" Focusable="True" Background="Transparent" Visibility="Hidden" IsReadOnly="{TemplateBinding IsReadOnly}" />
<Popup x:Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide"> <Popup x:Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide">
<Grid x:Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}"> <Grid x:Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Border x:Name="DropDownBorder" BorderThickness="1" BorderBrush="{DynamicResource Brush.Accent1}" Background="{DynamicResource Brush.Contents}"/> <Border x:Name="DropDownBorder" BorderThickness="1" BorderBrush="{DynamicResource Brush.Accent1}" Background="{DynamicResource Brush.Contents}"/>

View file

@ -1,7 +1,7 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:SourceGit.Views.Controls"> xmlns:controls="clr-namespace:SourceGit.Views.Controls">
<Style x:Key="Style.TextBox" TargetType="{x:Type TextBox}"> <Style TargetType="{x:Type controls:TextEdit}">
<Setter Property="SnapsToDevicePixels" Value="True"/> <Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="True"/> <Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/> <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
@ -58,30 +58,45 @@
</Setter> </Setter>
<Setter Property="Template"> <Setter Property="Template">
<Setter.Value> <Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}"> <ControlTemplate TargetType="{x:Type controls:TextEdit}">
<Border <Border
x:Name="Border" x:Name="Border"
Background="{TemplateBinding Background}" Background="{TemplateBinding Background}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
BorderThickness="{TemplateBinding BorderThickness}" BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"> BorderBrush="{TemplateBinding BorderBrush}"
<ScrollViewer Padding="{TemplateBinding Padding}">
x:Name="PART_ContentHost" <Grid>
Margin="{TemplateBinding Padding}" <TextBlock
VerticalAlignment="Center" x:Name="PART_Placeholder"
Background="Transparent" Margin="4,0"
BorderThickness="0" Text="{TemplateBinding Placeholder}"
IsTabStop="False" Foreground="{DynamicResource Brush.FG2}"
RenderOptions.ClearTypeHint="Enabled" Visibility="{TemplateBinding PlaceholderVisibility}"
CanContentScroll="True" VerticalAlignment="Center"/>
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
<ScrollViewer
x:Name="PART_ContentHost"
VerticalAlignment="Center"
Background="Transparent"
BorderThickness="0"
IsTabStop="False"
RenderOptions.ClearTypeHint="Enabled"
CanContentScroll="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Grid>
</Border> </Border>
<ControlTemplate.Triggers> <ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true"> <Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Border" Property="BorderBrush" Value="{DynamicResource Brush.Accent1}"/> <Setter TargetName="Border" Property="BorderBrush" Value="{DynamicResource Brush.Accent1}"/>
</Trigger> </Trigger>
<Trigger Property="AcceptsReturn" Value="True"> <Trigger Property="AcceptsReturn" Value="True">
<Setter TargetName="PART_Placeholder" Property="VerticalAlignment" Value="Top"/>
<Setter TargetName="PART_ContentHost" Property="VerticalAlignment" Value="Top"/>
</Trigger>
<Trigger Property="TextWrapping" Value="Wrap">
<Setter TargetName="PART_Placeholder" Property="VerticalAlignment" Value="Top"/>
<Setter TargetName="PART_ContentHost" Property="VerticalAlignment" Value="Top"/> <Setter TargetName="PART_ContentHost" Property="VerticalAlignment" Value="Top"/>
</Trigger> </Trigger>
</ControlTemplate.Triggers> </ControlTemplate.Triggers>
@ -89,7 +104,4 @@
</Setter.Value> </Setter.Value>
</Setter> </Setter>
</Style> </Style>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource Style.TextBox}"/>
<Style TargetType="{x:Type controls:TextEdit}" BasedOn="{StaticResource Style.TextBox}"/>
</ResourceDictionary> </ResourceDictionary>

View file

@ -1,8 +1,6 @@
using System.Globalization;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media;
namespace SourceGit.Views.Controls { namespace SourceGit.Views.Controls {
@ -10,8 +8,6 @@ namespace SourceGit.Views.Controls {
/// 扩展默认TextBox /// 扩展默认TextBox
/// </summary> /// </summary>
public class TextEdit : TextBox { public class TextEdit : TextBox {
private bool isPlaceholderShow = false;
public static readonly DependencyProperty PlaceholderProperty = DependencyProperty.Register( public static readonly DependencyProperty PlaceholderProperty = DependencyProperty.Register(
"Placeholder", "Placeholder",
typeof(string), typeof(string),
@ -23,15 +19,15 @@ namespace SourceGit.Views.Controls {
set { SetValue(PlaceholderProperty, value); } set { SetValue(PlaceholderProperty, value); }
} }
public static readonly DependencyProperty PlaceholderBaselineProperty = DependencyProperty.Register( public static readonly DependencyProperty PlaceholderVisibilityProperty = DependencyProperty.Register(
"PlaceholderBaseline", "PlaceholderVisibility",
typeof(AlignmentY), typeof(Visibility),
typeof(TextEdit), typeof(TextEdit),
new PropertyMetadata(AlignmentY.Center)); new PropertyMetadata(Visibility.Visible));
public AlignmentY PlaceholderBaseline { public Visibility PlaceholderVisibility {
get { return (AlignmentY)GetValue(PlaceholderBaselineProperty); } get { return (Visibility)GetValue(PlaceholderVisibilityProperty); }
set { SetValue(PlaceholderBaselineProperty, value); } set { SetValue(PlaceholderVisibilityProperty, value); }
} }
public TextEdit() { public TextEdit() {
@ -39,41 +35,8 @@ namespace SourceGit.Views.Controls {
SelectionChanged += OnSelectionChanged; 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) { 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) { private void OnSelectionChanged(object sender, RoutedEventArgs e) {

View file

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

View file

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