enhance: add show mode prop for growl.

This commit is contained in:
NaBian committed 2022-09-04 15:30:31 +08:00
1 parent d9ac8a3061
commit ef084c7547
3 files changed
+124 -121

No files matched your search

@@ -23,30 +23,50 @@ namespace HandyControl.Controls;
[TemplatePart(Name = ElementButtonClose, Type = typeof(Button))] [TemplatePart(Name = ElementButtonClose, Type = typeof(Button))]
public class Growl : Control public class Growl : Control
{ {
#region Constants
private const string ElementPanelMore = "PART_PanelMore"; private const string ElementPanelMore = "PART_PanelMore";
private const string ElementGridMain = "PART_GridMain"; private const string ElementGridMain = "PART_GridMain";
private const string ElementButtonClose = "PART_ButtonClose"; private const string ElementButtonClose = "PART_ButtonClose";
private const int MinWaitTime = 2;
#endregion Constants public static readonly DependencyProperty GrowlParentProperty = DependencyProperty.RegisterAttached(
"GrowlParent", typeof(bool), typeof(Growl), new PropertyMetadata(ValueBoxes.FalseBox, (o, args) =>
#region Data {
if ((bool) args.NewValue && o is Panel panel)
{
SetGrowlPanel(panel);
}
}));
public static readonly DependencyProperty ShowModeProperty = DependencyProperty.RegisterAttached(
"ShowMode", typeof(GrowlShowMode), typeof(Growl),
new FrameworkPropertyMetadata(default(GrowlShowMode), FrameworkPropertyMetadataOptions.Inherits));
public static readonly DependencyProperty ShowDateTimeProperty = DependencyProperty.Register(
"ShowDateTime", typeof(bool), typeof(Growl), new PropertyMetadata(ValueBoxes.TrueBox));
public static readonly DependencyProperty MessageProperty = DependencyProperty.Register(
"Message", typeof(string), typeof(Growl), new PropertyMetadata(default(string)));
public static readonly DependencyProperty TimeProperty = DependencyProperty.Register(
"Time", typeof(DateTime), typeof(Growl), new PropertyMetadata(default(DateTime)));
public static readonly DependencyProperty IconProperty = DependencyProperty.Register(
"Icon", typeof(Geometry), typeof(Growl), new PropertyMetadata(default(Geometry)));
public static readonly DependencyProperty IconBrushProperty = DependencyProperty.Register(
"IconBrush", typeof(Brush), typeof(Growl), new PropertyMetadata(default(Brush)));
public static readonly DependencyProperty TypeProperty = DependencyProperty.Register(
"Type", typeof(InfoType), typeof(Growl), new PropertyMetadata(default(InfoType)));
public static readonly DependencyProperty TokenProperty = DependencyProperty.RegisterAttached(
"Token", typeof(string), typeof(Growl), new PropertyMetadata(default(string), OnTokenChanged));
internal static readonly DependencyProperty CancelStrProperty = DependencyProperty.Register(
"CancelStr", typeof(string), typeof(Growl), new PropertyMetadata(default(string)));
internal static readonly DependencyProperty ConfirmStrProperty = DependencyProperty.Register(
"ConfirmStr", typeof(string), typeof(Growl), new PropertyMetadata(default(string)));
private static readonly DependencyProperty IsCreatedAutomaticallyProperty = DependencyProperty.RegisterAttached(
"IsCreatedAutomatically", typeof(bool), typeof(Growl), new PropertyMetadata(ValueBoxes.FalseBox));
private static GrowlWindow GrowlWindow; private static GrowlWindow GrowlWindow;
private static readonly Dictionary<string, Panel> PanelDic = new();
private Panel _panelMore; private Panel _panelMore;
private Grid _gridMain; private Grid _gridMain;
private Button _buttonClose; private Button _buttonClose;
private bool _showCloseButton; private bool _showCloseButton;
private bool _staysOpen; private bool _staysOpen;
private int _waitTime = 6; private int _waitTime = 6;
/// <summary> /// <summary>
@@ -59,9 +79,60 @@ public class Growl : Control
/// </summary> /// </summary>
private DispatcherTimer _timerClose; private DispatcherTimer _timerClose;
private static readonly Dictionary<string, Panel> PanelDic = new(); /// <summary>
/// 消息容器
/// </summary>
public static Panel GrowlPanel { get; set; }
#endregion Data public InfoType Type
{
get => (InfoType) GetValue(TypeProperty);
set => SetValue(TypeProperty, value);
}
public bool ShowDateTime
{
get => (bool) GetValue(ShowDateTimeProperty);
set => SetValue(ShowDateTimeProperty, ValueBoxes.BooleanBox(value));
}
public string Message
{
get => (string) GetValue(MessageProperty);
set => SetValue(MessageProperty, value);
}
public DateTime Time
{
get => (DateTime) GetValue(TimeProperty);
set => SetValue(TimeProperty, value);
}
public Geometry Icon
{
get => (Geometry) GetValue(IconProperty);
set => SetValue(IconProperty, value);
}
public Brush IconBrush
{
get => (Brush) GetValue(IconBrushProperty);
set => SetValue(IconBrushProperty, value);
}
internal string CancelStr
{
get => (string) GetValue(CancelStrProperty);
set => SetValue(CancelStrProperty, value);
}
internal string ConfirmStr
{
get => (string) GetValue(ConfirmStrProperty);
set => SetValue(ConfirmStrProperty, value);
}
private Func<bool, bool> ActionBeforeClose { get; set; }
public Growl() public Growl()
{ {
@@ -148,40 +219,6 @@ public class Growl : Control
if (_panelMore == null || _gridMain == null || _buttonClose == null) throw new Exception(); if (_panelMore == null || _gridMain == null || _buttonClose == null) throw new Exception();
} }
private Func<bool, bool> ActionBeforeClose { get; set; }
/// <summary>
/// 消息容器
/// </summary>
public static Panel GrowlPanel { get; set; }
internal static readonly DependencyProperty CancelStrProperty = DependencyProperty.Register(
"CancelStr", typeof(string), typeof(Growl), new PropertyMetadata(default(string)));
internal static readonly DependencyProperty ConfirmStrProperty = DependencyProperty.Register(
"ConfirmStr", typeof(string), typeof(Growl), new PropertyMetadata(default(string)));
public static readonly DependencyProperty ShowDateTimeProperty = DependencyProperty.Register(
"ShowDateTime", typeof(bool), typeof(Growl), new PropertyMetadata(ValueBoxes.TrueBox));
public static readonly DependencyProperty MessageProperty = DependencyProperty.Register(
"Message", typeof(string), typeof(Growl), new PropertyMetadata(default(string)));
public static readonly DependencyProperty TimeProperty = DependencyProperty.Register(
"Time", typeof(DateTime), typeof(Growl), new PropertyMetadata(default(DateTime)));
public static readonly DependencyProperty IconProperty = DependencyProperty.Register(
"Icon", typeof(Geometry), typeof(Growl), new PropertyMetadata(default(Geometry)));
public static readonly DependencyProperty IconBrushProperty = DependencyProperty.Register(
"IconBrush", typeof(Brush), typeof(Growl), new PropertyMetadata(default(Brush)));
public static readonly DependencyProperty TypeProperty = DependencyProperty.Register(
"Type", typeof(InfoType), typeof(Growl), new PropertyMetadata(default(InfoType)));
public static readonly DependencyProperty TokenProperty = DependencyProperty.RegisterAttached(
"Token", typeof(string), typeof(Growl), new PropertyMetadata(default(string), OnTokenChanged));
private static void OnTokenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) private static void OnTokenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{ {
if (d is Panel panel) if (d is Panel panel)
@@ -197,81 +234,21 @@ public class Growl : Control
} }
} }
public static void SetToken(DependencyObject element, string value) public static void SetToken(DependencyObject element, string value) => element.SetValue(TokenProperty, value);
=> element.SetValue(TokenProperty, value);
public static string GetToken(DependencyObject element) public static string GetToken(DependencyObject element) => (string) element.GetValue(TokenProperty);
=> (string) element.GetValue(TokenProperty);
public static readonly DependencyProperty GrowlParentProperty = DependencyProperty.RegisterAttached( public static void SetShowMode(DependencyObject element, GrowlShowMode value) => element.SetValue(ShowModeProperty, value);
"GrowlParent", typeof(bool), typeof(Growl), new PropertyMetadata(ValueBoxes.FalseBox, (o, args) =>
{ public static GrowlShowMode GetShowMode(DependencyObject element) => (GrowlShowMode) element.GetValue(ShowModeProperty);
if ((bool) args.NewValue && o is Panel panel)
{
SetGrowlPanel(panel);
}
}));
public static void SetGrowlParent(DependencyObject element, bool value) => element.SetValue(GrowlParentProperty, ValueBoxes.BooleanBox(value)); public static void SetGrowlParent(DependencyObject element, bool value) => element.SetValue(GrowlParentProperty, ValueBoxes.BooleanBox(value));
public static bool GetGrowlParent(DependencyObject element) => (bool) element.GetValue(GrowlParentProperty); public static bool GetGrowlParent(DependencyObject element) => (bool) element.GetValue(GrowlParentProperty);
private static readonly DependencyProperty IsCreatedAutomaticallyProperty = DependencyProperty.RegisterAttached( private static void SetIsCreatedAutomatically(DependencyObject element, bool value) => element.SetValue(IsCreatedAutomaticallyProperty, ValueBoxes.BooleanBox(value));
"IsCreatedAutomatically", typeof(bool), typeof(Growl), new PropertyMetadata(ValueBoxes.FalseBox));
private static void SetIsCreatedAutomatically(DependencyObject element, bool value) private static bool GetIsCreatedAutomatically(DependencyObject element) => (bool) element.GetValue(IsCreatedAutomaticallyProperty);
=> element.SetValue(IsCreatedAutomaticallyProperty, ValueBoxes.BooleanBox(value));
private static bool GetIsCreatedAutomatically(DependencyObject element)
=> (bool) element.GetValue(IsCreatedAutomaticallyProperty);
public InfoType Type
{
get => (InfoType) GetValue(TypeProperty);
set => SetValue(TypeProperty, value);
}
internal string CancelStr
{
get => (string) GetValue(CancelStrProperty);
set => SetValue(CancelStrProperty, value);
}
internal string ConfirmStr
{
get => (string) GetValue(ConfirmStrProperty);
set => SetValue(ConfirmStrProperty, value);
}
public bool ShowDateTime
{
get => (bool) GetValue(ShowDateTimeProperty);
set => SetValue(ShowDateTimeProperty, ValueBoxes.BooleanBox(value));
}
public string Message
{
get => (string) GetValue(MessageProperty);
set => SetValue(MessageProperty, value);
}
public DateTime Time
{
get => (DateTime) GetValue(TimeProperty);
set => SetValue(TimeProperty, value);
}
public Geometry Icon
{
get => (Geometry) GetValue(IconProperty);
set => SetValue(IconProperty, value);
}
public Brush IconBrush
{
get => (Brush) GetValue(IconBrushProperty);
set => SetValue(IconBrushProperty, value);
}
/// <summary> /// <summary>
/// 开始计时器 /// 开始计时器
@@ -353,6 +330,23 @@ public class Growl : Control
if (!_staysOpen) StartTimer(); if (!_staysOpen) StartTimer();
} }
private static void ShowInternal(Panel panel, UIElement growl)
{
if (panel is null)
{
return;
}
if (GetShowMode(panel) == GrowlShowMode.Prepend)
{
panel.Children.Insert(0, growl);
}
else
{
panel.Children.Add(growl);
}
}
private static void ShowGlobal(GrowlInfo growlInfo) private static void ShowGlobal(GrowlInfo growlInfo)
{ {
Application.Current.Dispatcher?.Invoke( Application.Current.Dispatcher?.Invoke(
@@ -384,10 +378,11 @@ public class Growl : Control
ConfirmStr = growlInfo.ConfirmStr, ConfirmStr = growlInfo.ConfirmStr,
CancelStr = growlInfo.CancelStr, CancelStr = growlInfo.CancelStr,
Type = growlInfo.Type, Type = growlInfo.Type,
_waitTime = Math.Max(growlInfo.WaitTime, 2), _waitTime = Math.Max(growlInfo.WaitTime, MinWaitTime),
FlowDirection = growlInfo.FlowDirection FlowDirection = growlInfo.FlowDirection
}; };
GrowlWindow.GrowlPanel.Children.Insert(0, ctl);
ShowInternal(GrowlWindow.GrowlPanel, ctl);
} }
#if NET40 #if NET40
) )
@@ -420,21 +415,21 @@ public class Growl : Control
ConfirmStr = growlInfo.ConfirmStr, ConfirmStr = growlInfo.ConfirmStr,
CancelStr = growlInfo.CancelStr, CancelStr = growlInfo.CancelStr,
Type = growlInfo.Type, Type = growlInfo.Type,
_waitTime = Math.Max(growlInfo.WaitTime, 2) _waitTime = Math.Max(growlInfo.WaitTime, MinWaitTime)
}; };
if (!string.IsNullOrEmpty(growlInfo.Token)) if (!string.IsNullOrEmpty(growlInfo.Token))
{ {
if (PanelDic.TryGetValue(growlInfo.Token, out var panel)) if (PanelDic.TryGetValue(growlInfo.Token, out var panel))
{ {
panel?.Children.Insert(0, ctl); ShowInternal(panel, ctl);
} }
} }
else else
{ {
// GrowlPanel is null, we create it automatically // GrowlPanel is null, we create it automatically
GrowlPanel ??= CreateDefaultPanel(); GrowlPanel ??= CreateDefaultPanel();
GrowlPanel?.Children.Insert(0, ctl); ShowInternal(GrowlPanel, ctl);
} }
} }
#if NET40 #if NET40
@@ -0,0 +1,7 @@
namespace HandyControl.Data;
public enum GrowlShowMode
{
Prepend,
Append
}
@@ -93,6 +93,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Controls\Tag\TagContainer.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Controls\Tag\TagContainer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Text\HighlightTextBlock.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Controls\Text\HighlightTextBlock.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Text\SimpleText.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Controls\Text\SimpleText.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Data\Enum\GrowlShowMode.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Data\Enum\LinearLayout.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Data\Enum\LinearLayout.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\EventToCommand.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Interactivity\EventToCommand.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\IEventArgsConverter.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Interactivity\IEventArgsConverter.cs" />