Skip to content

Commit

Permalink
improvement (#39)
Browse files Browse the repository at this point in the history
New Properties:
  PasswordBoxHelper.PlainButtonStyle
  PasswordBoxHelper.PlainButtonVisibility
  
Fixup: 
	NoticeBox might throw an exception when calling from another thread.
	TextBlockHelper will not update highlights when properties are changed.
  • Loading branch information
Mochengvia authored Nov 27, 2021
1 parent e007de4 commit 19427e3
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 53 deletions.
13 changes: 0 additions & 13 deletions DotNet/WPF/Src/Samples/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,6 @@
</pu:MessageBoxXSettings.ButtonStyle>
</pu:MessageBoxXSettings>

<pu:NoticeBoxSettings x:Key="noticeSetting">
<pu:NoticeBoxSettings.NoticeBoxItemStyle>
<Style TargetType="pu:NoticeBoxItem"
BasedOn="{StaticResource {ComponentResourceKey ResourceId=NoticeBoxItemStyle, TypeInTargetAssembly={x:Type pu:NoticeBox}}}">
<Setter Property="Background"
Value="#1E1E1E" />
<Setter Property="BorderBrush"
Value="#4E4E4E" />
<Setter Property="Foreground"
Value="White" />
</Style>
</pu:NoticeBoxSettings.NoticeBoxItemStyle>
</pu:NoticeBoxSettings>

</ResourceDictionary>
<ResourceDictionary Source="pack://application:,,,/Panuon.UI.Silver;component/Control.xaml" />
Expand Down
2 changes: 1 addition & 1 deletion DotNet/WPF/Src/Samples/Views/Examples/SignInView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
<Trigger Property="pu:ButtonHelper.IsPending"
Value="True">
<Setter Property="pu:ButtonHelper.ClickEffect"
Value="Sink" />
Value="Shake" />
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,37 @@
<Setter Property="Foreground"
Value="{Binding Foreground, RelativeSource={RelativeSource AncestorType=PasswordBox}, Mode=OneWay}" />
</Style>


<Style x:Key="{ComponentResourceKey ResourceId=PlainButtonStyle, TypeInTargetAssembly={x:Type local:PasswordBoxHelper}}"
TargetType="Button"
BasedOn="{StaticResource {x:Static rs:StyleKeys.ButtonStyle}}">
<Setter Property="local:ButtonHelper.HoverForeground"
Value="{Binding Foreground, Converter={StaticResource {x:Static irs:ConverterKeys.LightenSolidColorBrushConverter}}, ConverterParameter=0.6, RelativeSource={RelativeSource Self}, Mode=OneWay}" />
<Setter Property="Background"
Value="Transparent" />
<Setter Property="local:ButtonHelper.HoverBackground"
Value="{x:Null}" />
<Setter Property="local:ButtonHelper.ClickBackground"
Value="{x:Null}" />
<Setter Property="FontFamily"
Value="/Panuon.UI.Silver;component/Resources/Fonts/#PanuonIcon" />
<Setter Property="FontSize"
Value="{Binding FontSize, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}" />
<Setter Property="Margin"
Value="0,0,5,0" />
<Setter Property="Content"
Value="&#xe94b;" />
<Setter Property="Foreground"
Value="{Binding Foreground, RelativeSource={RelativeSource AncestorType=PasswordBox}, Mode=OneWay}" />
<Style.Triggers>
<Trigger Property="IsPressed"
Value="True">
<Setter Property="Content"
Value="&#xe94c;" />
</Trigger>
</Style.Triggers>
</Style>

<Style x:Key="{x:Static rs:StyleKeys.PasswordBoxStyle}"
TargetType="PasswordBox">
<Setter Property="i:VisualStateHelper.Regist"
Expand All @@ -59,6 +89,8 @@
Value="{Binding Path=(i:VisualStateHelper.Foreground), Converter={StaticResource {x:Static irs:ConverterKeys.BrushOpacityConverter}}, ConverterParameter=0.8, RelativeSource={RelativeSource Self}, Mode=OneWay}" />
<Setter Property="local:PasswordBoxHelper.ClearButtonStyle"
Value="{StaticResource {ComponentResourceKey ResourceId=ClearButtonStyle, TypeInTargetAssembly={x:Type local:PasswordBoxHelper}}}" />
<Setter Property="local:PasswordBoxHelper.PlainButtonStyle"
Value="{StaticResource {ComponentResourceKey ResourceId=PlainButtonStyle, TypeInTargetAssembly={x:Type local:PasswordBoxHelper}}}" />
<Setter Property="VerticalContentAlignment"
Value="Center" />
<Setter Property="HorizontalContentAlignment"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,27 @@
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock IsHitTestVisible="False"
Focusable="False"
Margin="{TemplateBinding Padding}"
Padding="2,0,0,0"
Visibility="{Binding Path=(local:PasswordBoxHelper.Password), Converter={StaticResource {x:Static rs:ConverterKeys.StringNonnullAndNotEmptyToCollapseConverter}},RelativeSource={RelativeSource TemplatedParent},Mode=OneWay}"
Text="{Binding Path=(local:PasswordBoxHelper.Watermark),RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
Foreground="{Binding Path=(local:PasswordBoxHelper.WatermarkBrush),RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
<TextBlock IsHitTestVisible="False"
Focusable="False"
Margin="{TemplateBinding Padding}"
Padding="2,0,0,0"
Visibility="{Binding IsPressed, Converter={StaticResource {x:Static rs:ConverterKeys.FalseToHiddenConverter}}, ElementName=BtnPlain, Mode=OneWay}"
Text="{Binding Path=(local:PasswordBoxHelper.Password),RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
<ScrollViewer x:Name="PART_ContentHost"
Visibility="{Binding IsPressed, Converter={StaticResource {x:Static rs:ConverterKeys.TrueToHiddenConverter}}, ElementName=BtnPlain, Mode=OneWay}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" />
<Button x:Name="BtnClear"
Expand All @@ -42,6 +53,10 @@
Style="{Binding Path=(local:PasswordBoxHelper.ClearButtonStyle), RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
Command="{Binding Path=(local:PasswordBoxHelper.ClearCommand), RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
CommandParameter="{Binding RelativeSource={RelativeSource TemplatedParent}}" />
<Button x:Name="BtnPlain"
Grid.Column="2"
Visibility="Collapsed"
Style="{Binding Path=(local:PasswordBoxHelper.PlainButtonStyle), RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}" />
</Grid>
</i:ContentControlX>
<ControlTemplate.Triggers>
Expand Down Expand Up @@ -110,6 +125,71 @@
Property="Visibility"
Value="Visible" />
</MultiDataTrigger>
<Trigger Property="local:PasswordBoxHelper.PlainButtonVisibility"
Value="Visible">
<Setter TargetName="BtnPlain"
Property="Visibility"
Value="Visible" />
</Trigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=(local:PasswordBoxHelper.PlainButtonVisibility), RelativeSource={RelativeSource Self}, Mode=OneWay}"
Value="VisibleOnNonnull" />
<Condition Binding="{Binding Path=(local:PasswordBoxHelper.Password), Converter={StaticResource {x:Static rs:ConverterKeys.IsStringNonnullAndNotEmptyConverter}}, RelativeSource={RelativeSource Self}, Mode=OneWay}"
Value="True" />
</MultiDataTrigger.Conditions>
<Setter TargetName="BtnPlain"
Property="Visibility"
Value="Visible" />
</MultiDataTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="local:PasswordBoxHelper.PlainButtonVisibility"
Value="VisibleOnHover" />
<Condition Property="IsMouseOver"
Value="True" />
</MultiTrigger.Conditions>
<Setter TargetName="BtnPlain"
Property="Visibility"
Value="Visible" />
</MultiTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=(local:PasswordBoxHelper.PlainButtonVisibility), RelativeSource={RelativeSource Self}, Mode=OneWay}"
Value="VisibleOnHoverAndNonnull" />
<Condition Binding="{Binding Path=(local:PasswordBoxHelper.Password), Converter={StaticResource {x:Static rs:ConverterKeys.IsStringNonnullAndNotEmptyConverter}}, RelativeSource={RelativeSource Self}, Mode=OneWay}"
Value="True" />
<Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}, Mode=OneWay}"
Value="True" />
</MultiDataTrigger.Conditions>
<Setter TargetName="BtnPlain"
Property="Visibility"
Value="Visible" />
</MultiDataTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="local:PasswordBoxHelper.PlainButtonVisibility"
Value="VisibleOnFocused" />
<Condition Property="IsKeyboardFocusWithin"
Value="True" />
</MultiTrigger.Conditions>
<Setter TargetName="BtnPlain"
Property="Visibility"
Value="Visible" />
</MultiTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=(local:PasswordBoxHelper.PlainButtonVisibility), RelativeSource={RelativeSource Self}, Mode=OneWay}"
Value="VisibleOnFocusedAndNonnull" />
<Condition Binding="{Binding Path=(local:PasswordBoxHelper.Password), Converter={StaticResource {x:Static rs:ConverterKeys.IsStringNonnullAndNotEmptyConverter}}, RelativeSource={RelativeSource Self}, Mode=OneWay}"
Value="True" />
<Condition Binding="{Binding IsKeyboardFocusWithin, RelativeSource={RelativeSource Self}, Mode=OneWay}"
Value="True" />
</MultiDataTrigger.Conditions>
<Setter TargetName="BtnPlain"
Property="Visibility"
Value="Visible" />
</MultiDataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;

Expand Down Expand Up @@ -86,48 +87,45 @@ public static INoticeHandler Show(string message, string caption, bool canClose,
#region Functions
private static INoticeHandler Show(string message, string caption, bool canClose, MessageBoxIcon icon, ImageSource imageIcon, int? duration)
{
var setting = NoticeBoxSettings.Setting;
AnimationEase animationEase = default;
TimeSpan animationDuration = default;
string noticeBoxItemStyle = default;
bool createOnNewThread = false;
setting.Dispatcher.Invoke(new Action(() =>
return Application.Current.Dispatcher.Invoke(new Func<INoticeHandler>(() =>
{
animationEase = setting.AnimationEase;
animationDuration = setting.AnimationDuration;
noticeBoxItemStyle = XamlUtil.ToXaml(setting.NoticeBoxItemStyle);
createOnNewThread = setting.CreateOnNewThread;
}));
var setting = NoticeBoxSettings.Setting;
var animationEase = setting.AnimationEase;
var animationDuration = setting.AnimationDuration;
var noticeBoxItemStyle = XamlUtil.ToXaml(setting.NoticeBoxItemStyle);
var createOnNewThread = setting.CreateOnNewThread;

if (_noticeWindow == null)
{
if (createOnNewThread)
if (_noticeWindow == null)
{
var autoReset = new AutoResetEvent(false);
_thread = new Thread(() =>
if (createOnNewThread)
{
_noticeWindow = new NoticeBoxWindow(animationEase, animationDuration);
_noticeWindow.Closed += delegate
var autoReset = new AutoResetEvent(false);
_thread = new Thread(() =>
{
_noticeWindow.Dispatcher.InvokeShutdown();
};
_noticeWindow = new NoticeBoxWindow(animationEase, animationDuration);
_noticeWindow.Closed += delegate
{
_noticeWindow.Dispatcher.InvokeShutdown();
};
_noticeWindow.Show();
autoReset.Set();
Dispatcher.Run();
});
_thread.SetApartmentState(ApartmentState.STA);
_thread.IsBackground = true;
_thread.Start();
autoReset.WaitOne();
}
else
{
_noticeWindow = new NoticeBoxWindow(animationEase, animationDuration);
_noticeWindow.Show();
autoReset.Set();
Dispatcher.Run();
});
_thread.SetApartmentState(ApartmentState.STA);
_thread.IsBackground = true;
_thread.Start();
autoReset.WaitOne();
}
}
else
{
_noticeWindow = new NoticeBoxWindow(animationEase, animationDuration);
_noticeWindow.Show();
}
}
var handler = _noticeWindow.AddItem(message, caption, icon, imageIcon, duration, canClose, animationDuration, noticeBoxItemStyle);
return handler;
var handler = _noticeWindow.AddItem(message, caption, icon, imageIcon, duration, canClose, animationDuration, noticeBoxItemStyle);
return handler;

}));
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,36 @@ public static void SetClearButtonStyle(PasswordBox passwordBox, Style value)
DependencyProperty.RegisterAttached("ClearButtonStyle", typeof(Style), typeof(PasswordBoxHelper));
#endregion

#region PlainButtonVisibility
public static AuxiliaryButtonVisibility GetPlainButtonVisibility(PasswordBox passwordBox)
{
return (AuxiliaryButtonVisibility)passwordBox.GetValue(PlainButtonVisibilityProperty);
}

public static void SetPlainButtonVisibility(PasswordBox passwordBox, AuxiliaryButtonVisibility value)
{
passwordBox.SetValue(PlainButtonVisibilityProperty, value);
}

public static readonly DependencyProperty PlainButtonVisibilityProperty =
DependencyProperty.RegisterAttached("PlainButtonVisibility", typeof(AuxiliaryButtonVisibility), typeof(PasswordBoxHelper));
#endregion

#region PlainButtonStyle
public static Style GetPlainButtonStyle(PasswordBox passwordBox)
{
return (Style)passwordBox.GetValue(PlainButtonStyleProperty);
}

public static void SetPlainButtonStyle(PasswordBox passwordBox, Style value)
{
passwordBox.SetValue(PlainButtonStyleProperty, value);
}

public static readonly DependencyProperty PlainButtonStyleProperty =
DependencyProperty.RegisterAttached("PlainButtonStyle", typeof(Style), typeof(PasswordBoxHelper));
#endregion

#endregion

#region Commands
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static void SetHighlightRule(TextBlock textBlock, HighlightRule value)
}

public static readonly DependencyProperty HighlightRuleProperty =
DependencyProperty.RegisterAttached("HighlightRule", typeof(HighlightRule), typeof(TextBlockHelper), new PropertyMetadata(HighlightRule.All));
DependencyProperty.RegisterAttached("HighlightRule", typeof(HighlightRule), typeof(TextBlockHelper), new PropertyMetadata(HighlightRule.All, OnHighlightTextChanged));
#endregion

#region HighlightForeground
Expand All @@ -52,7 +52,7 @@ public static void SetHighlightForeground(TextBlock textBlock, Brush value)
}

public static readonly DependencyProperty HighlightForegroundProperty =
DependencyProperty.RegisterAttached("HighlightForeground", typeof(Brush), typeof(TextBlockHelper), new PropertyMetadata(Brushes.Red));
DependencyProperty.RegisterAttached("HighlightForeground", typeof(Brush), typeof(TextBlockHelper), new PropertyMetadata(Brushes.Red, OnHighlightTextChanged));
#endregion

#region HighlightBackground
Expand All @@ -67,7 +67,7 @@ public static void SetHighlightBackground(TextBlock textBlock, Brush value)
}

public static readonly DependencyProperty HighlightBackgroundProperty =
DependencyProperty.RegisterAttached("HighlightBackground", typeof(Brush), typeof(TextBlockHelper), new PropertyMetadata(null));
DependencyProperty.RegisterAttached("HighlightBackground", typeof(Brush), typeof(TextBlockHelper), new PropertyMetadata(null, OnHighlightTextChanged));

#endregion

Expand Down

0 comments on commit 19427e3

Please sign in to comment.