From ffa8fa7262ebc13672dd926656a6ca16e5cd5fd2 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Wed, 12 Aug 2020 20:31:06 +0800 Subject: [PATCH] implement ShowPassword Property in textpresenter and forward from textbox --- samples/ControlCatalog/Pages/TextBoxPage.xaml | 2 +- .../Presenters/TextPresenter.cs | 15 +++++++++++--- src/Avalonia.Controls/TextBox.cs | 20 ++++++------------- src/Avalonia.Themes.Fluent/TextBox.xaml | 10 +++++++++- 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/samples/ControlCatalog/Pages/TextBoxPage.xaml b/samples/ControlCatalog/Pages/TextBoxPage.xaml index cae3d20169..55231363d2 100644 --- a/samples/ControlCatalog/Pages/TextBoxPage.xaml +++ b/samples/ControlCatalog/Pages/TextBoxPage.xaml @@ -36,7 +36,7 @@ - + diff --git a/src/Avalonia.Controls/Presenters/TextPresenter.cs b/src/Avalonia.Controls/Presenters/TextPresenter.cs index 6e534bbb2a..ef33ac19f6 100644 --- a/src/Avalonia.Controls/Presenters/TextPresenter.cs +++ b/src/Avalonia.Controls/Presenters/TextPresenter.cs @@ -14,6 +14,9 @@ namespace Avalonia.Controls.Presenters o => o.CaretIndex, (o, v) => o.CaretIndex = v); + public static readonly StyledProperty ShowPasswordCharProperty = + AvaloniaProperty.Register(nameof(ShowPasswordChar), true); + public static readonly StyledProperty PasswordCharProperty = AvaloniaProperty.Register(nameof(PasswordChar)); @@ -75,7 +78,7 @@ namespace Avalonia.Controls.Presenters static TextPresenter() { AffectsRender(SelectionBrushProperty); - AffectsMeasure(TextProperty, PasswordCharProperty, + AffectsMeasure(TextProperty, PasswordCharProperty, ShowPasswordCharProperty, TextAlignmentProperty, TextWrappingProperty, TextBlock.FontSizeProperty, TextBlock.FontStyleProperty, TextBlock.FontWeightProperty, TextBlock.FontFamilyProperty); @@ -84,7 +87,7 @@ namespace Avalonia.Controls.Presenters TextBlock.FontSizeProperty.Changed, TextBlock.FontStyleProperty.Changed, TextBlock.FontWeightProperty.Changed, TextBlock.FontFamilyProperty.Changed, SelectionStartProperty.Changed, SelectionEndProperty.Changed, - SelectionForegroundBrushProperty.Changed, PasswordCharProperty.Changed + SelectionForegroundBrushProperty.Changed, PasswordCharProperty.Changed, ShowPasswordCharProperty.Changed ).AddClassHandler((x, _) => x.InvalidateFormattedText()); CaretIndexProperty.Changed.AddClassHandler((x, e) => x.CaretIndexChanged((int)e.NewValue)); @@ -210,6 +213,12 @@ namespace Avalonia.Controls.Presenters set => SetValue(PasswordCharProperty, value); } + public bool ShowPasswordChar + { + get => GetValue(ShowPasswordCharProperty); + set => SetValue(ShowPasswordCharProperty, value); + } + public IBrush SelectionBrush { get => GetValue(SelectionBrushProperty); @@ -426,7 +435,7 @@ namespace Avalonia.Controls.Presenters var text = Text; - if (PasswordChar != default(char)) + if (PasswordChar != default(char) && ShowPasswordChar) { result = CreateFormattedTextInternal(_constraint, new string(PasswordChar, text?.Length ?? 0)); } diff --git a/src/Avalonia.Controls/TextBox.cs b/src/Avalonia.Controls/TextBox.cs index 23728d7997..08e4e923f5 100644 --- a/src/Avalonia.Controls/TextBox.cs +++ b/src/Avalonia.Controls/TextBox.cs @@ -101,8 +101,8 @@ namespace Avalonia.Controls public static readonly StyledProperty InnerRightContentProperty = AvaloniaProperty.Register(nameof(InnerRightContent)); - public static readonly StyledProperty ShowPasswordProperty = - AvaloniaProperty.Register(nameof(ShowPassword)); + public static readonly StyledProperty ShowPasswordCharProperty = + AvaloniaProperty.Register(nameof(ShowPasswordChar)); struct UndoRedoState : IEquatable { @@ -132,10 +132,6 @@ namespace Avalonia.Controls static TextBox() { FocusableProperty.OverrideDefaultValue(typeof(TextBox), true); - ShowPasswordProperty.Changed.Subscribe((x)=> - { - - }); } public TextBox() @@ -351,14 +347,10 @@ namespace Avalonia.Controls set { SetValue(InnerRightContentProperty, value); } } - public bool ShowPassword + public bool ShowPasswordChar { - get { return GetValue(ShowPasswordProperty); } - set - { - - SetValue(ShowPasswordProperty, value); - } + get { return GetValue(ShowPasswordCharProperty); } + set { SetValue(ShowPasswordCharProperty, value); } } public TextWrapping TextWrapping @@ -1108,7 +1100,7 @@ namespace Avalonia.Controls SelectionEnd = CaretIndex; } - private bool IsPasswordBox => ShowPassword && PasswordChar != default(char); + private bool IsPasswordBox => PasswordChar != default(char); UndoRedoState UndoRedoHelper.IUndoRedoHost.UndoRedoState { diff --git a/src/Avalonia.Themes.Fluent/TextBox.xaml b/src/Avalonia.Themes.Fluent/TextBox.xaml index 5278e5d3d0..0735b83014 100644 --- a/src/Avalonia.Themes.Fluent/TextBox.xaml +++ b/src/Avalonia.Themes.Fluent/TextBox.xaml @@ -79,6 +79,7 @@ TextAlignment="{TemplateBinding TextAlignment}" TextWrapping="{TemplateBinding TextWrapping}" PasswordChar="{TemplateBinding PasswordChar}" + ShowPasswordChar="{TemplateBinding ShowPasswordChar}" SelectionBrush="{TemplateBinding SelectionBrush}" SelectionForegroundBrush="{TemplateBinding SelectionForegroundBrush}" CaretBrush="{TemplateBinding CaretBrush}" @@ -142,7 +143,6 @@ - @@ -151,4 +151,12 @@ + +