mirror of
https://github.com/sourcegit-scm/sourcegit.git
synced 2024-12-23 20:47:25 -08:00
refactor<TextEdit>: rewrite implementation of placeholder for TextEdit
This commit is contained in:
parent
535e05a667
commit
e56a267fc8
5 changed files with 41 additions and 71 deletions
|
@ -19,10 +19,6 @@
|
|||
</ControlTemplate.Triggers>
|
||||
</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}">
|
||||
<Setter Property="SnapsToDevicePixels" Value="true" />
|
||||
<Setter Property="OverridesDefaultStyle" Value="true" />
|
||||
|
@ -38,7 +34,6 @@
|
|||
<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}}"/>
|
||||
<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">
|
||||
<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}"/>
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
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="OverridesDefaultStyle" Value="True"/>
|
||||
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
|
||||
|
@ -58,16 +58,25 @@
|
|||
</Setter>
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="{x:Type TextBox}">
|
||||
<ControlTemplate TargetType="{x:Type controls:TextEdit}">
|
||||
<Border
|
||||
x:Name="Border"
|
||||
Background="{TemplateBinding Background}"
|
||||
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
|
||||
BorderThickness="{TemplateBinding BorderThickness}"
|
||||
BorderBrush="{TemplateBinding BorderBrush}">
|
||||
BorderBrush="{TemplateBinding BorderBrush}"
|
||||
Padding="{TemplateBinding Padding}">
|
||||
<Grid>
|
||||
<TextBlock
|
||||
x:Name="PART_Placeholder"
|
||||
Margin="4,0"
|
||||
Text="{TemplateBinding Placeholder}"
|
||||
Foreground="{DynamicResource Brush.FG2}"
|
||||
Visibility="{TemplateBinding PlaceholderVisibility}"
|
||||
VerticalAlignment="Center"/>
|
||||
|
||||
<ScrollViewer
|
||||
x:Name="PART_ContentHost"
|
||||
Margin="{TemplateBinding Padding}"
|
||||
VerticalAlignment="Center"
|
||||
Background="Transparent"
|
||||
BorderThickness="0"
|
||||
|
@ -75,6 +84,7 @@
|
|||
RenderOptions.ClearTypeHint="Enabled"
|
||||
CanContentScroll="True"
|
||||
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
|
||||
</Grid>
|
||||
</Border>
|
||||
|
||||
<ControlTemplate.Triggers>
|
||||
|
@ -82,6 +92,11 @@
|
|||
<Setter TargetName="Border" Property="BorderBrush" Value="{DynamicResource Brush.Accent1}"/>
|
||||
</Trigger>
|
||||
<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"/>
|
||||
</Trigger>
|
||||
</ControlTemplate.Triggers>
|
||||
|
@ -89,7 +104,4 @@
|
|||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
|
||||
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource Style.TextBox}"/>
|
||||
<Style TargetType="{x:Type controls:TextEdit}" BasedOn="{StaticResource Style.TextBox}"/>
|
||||
</ResourceDictionary>
|
|
@ -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) {
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
Loading…
Reference in a new issue