Browse Source

implement ShowPassword Property in textpresenter and forward from textbox

Jumar Macato 5 years ago
parent
commit
ffa8fa7262

+ 1 - 1
samples/ControlCatalog/Pages/TextBoxPage.xaml

@@ -36,7 +36,7 @@
         <TextBox Width="200" Watermark="Watermark" />
         <TextBox Width="200" Watermark="Floating Watermark" UseFloatingWatermark="True" Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit."/>
 
-        <TextBox Width="200" Classes="Password" Watermark="Password Box" UseFloatingWatermark="True" PasswordChar="*" Text="Password" />
+        <TextBox Width="200" Classes="revealPasswordButton" Watermark="Password Box" UseFloatingWatermark="True" PasswordChar="*" Text="Password" />
         
         <TextBox Width="200" Text="Left aligned text" TextAlignment="Left" />
         <TextBox Width="200" Text="Center aligned text" TextAlignment="Center" />

+ 12 - 3
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<bool> ShowPasswordCharProperty =
+            AvaloniaProperty.Register<TextPresenter, bool>(nameof(ShowPasswordChar), true);
+
         public static readonly StyledProperty<char> PasswordCharProperty =
             AvaloniaProperty.Register<TextPresenter, char>(nameof(PasswordChar));
 
@@ -75,7 +78,7 @@ namespace Avalonia.Controls.Presenters
         static TextPresenter()
         {
             AffectsRender<TextPresenter>(SelectionBrushProperty);
-            AffectsMeasure<TextPresenter>(TextProperty, PasswordCharProperty, 
+            AffectsMeasure<TextPresenter>(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<TextPresenter>((x, _) => x.InvalidateFormattedText());
 
             CaretIndexProperty.Changed.AddClassHandler<TextPresenter>((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));
             }

+ 6 - 14
src/Avalonia.Controls/TextBox.cs

@@ -101,8 +101,8 @@ namespace Avalonia.Controls
         public static readonly StyledProperty<object> InnerRightContentProperty =
             AvaloniaProperty.Register<TextBox, object>(nameof(InnerRightContent));
 
-        public static readonly StyledProperty<bool> ShowPasswordProperty =
-            AvaloniaProperty.Register<TextBox, bool>(nameof(ShowPassword));
+        public static readonly StyledProperty<bool> ShowPasswordCharProperty =
+            AvaloniaProperty.Register<TextBox, bool>(nameof(ShowPasswordChar));
 
         struct UndoRedoState : IEquatable<UndoRedoState>
         {
@@ -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<UndoRedoState>.IUndoRedoHost.UndoRedoState
         {

+ 9 - 1
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 @@
     <Setter Property="BorderThickness" Value="{DynamicResource TextControlBorderThemeThicknessFocused}" />
   </Style>
 
-
   <Style Selector="TextBox:error /template/ Border#border">
     <Setter Property="BorderBrush" Value="{DynamicResource ErrorBrush}"/>
   </Style>
@@ -151,4 +151,12 @@
     <Setter Property="Cursor" Value="IBeam" />
   </Style>
 
+  <Style Selector="TextBox.revealPasswordButton">
+    <Setter Property="InnerRightContent">
+      <Template>
+        <ToggleButton Content="ToggleButton" IsChecked="{Binding $parent[TextBox].ShowPasswordChar, Mode=TwoWay}"/>
+      </Template>
+    </Setter>
+  </Style> 
+
 </Styles>