upgraded growl

now if user do not set hc:Growl.GrowlParent="True", growl will create a default panel.
This commit is contained in:
NaBian committed 2021-05-29 17:56:56 +08:00
1 parent 30578843f6
commit 74768577ef
4 files changed
+101 -20

No files matched your search

@@ -1,5 +1,4 @@
<Border xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:hc="https://handyorg.github.io/handycontrol"
BorderThickness="0,1,0,0"
BorderBrush="{DynamicResource BorderBrush}"
Background="{StaticResource CloudDrawingBrush}"
@@ -14,8 +13,5 @@
<userControl:LeftMainContent/>
<userControl:MainContent Grid.Column="1"/>
<GridSplitter Margin="0,26,0,26" Grid.Column="0" HorizontalAlignment="Right" Width="4" Background="Transparent"/>
<hc:ScrollViewer Grid.Column="1" VerticalScrollBarVisibility="Hidden" HorizontalAlignment="Right" IsInertiaEnabled="True" IsPenetrating="True">
<StackPanel hc:Growl.GrowlParent="True" VerticalAlignment="Top" Margin="0,10,10,10"/>
</hc:ScrollViewer>
</Grid>
</Border>
</Border>
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
@@ -25,7 +26,9 @@ namespace HandyControl.Controls
#region Constants
private const string ElementPanelMore = "PART_PanelMore";
private const string ElementGridMain = "PART_GridMain";
private const string ElementButtonClose = "PART_ButtonClose";
#endregion Constants
@@ -176,15 +179,6 @@ namespace HandyControl.Controls
public static readonly DependencyProperty TypeProperty = DependencyProperty.Register(
"Type", typeof(InfoType), typeof(Growl), new PropertyMetadata(default(InfoType)));
public static readonly DependencyProperty GrowlParentProperty = DependencyProperty.RegisterAttached(
"GrowlParent", typeof(bool), typeof(Growl), new PropertyMetadata(ValueBoxes.FalseBox, (o, args) =>
{
if ((bool) args.NewValue && o is Panel panel)
{
SetGrowlPanel(panel);
}
}));
public static readonly DependencyProperty TokenProperty = DependencyProperty.RegisterAttached(
"Token", typeof(string), typeof(Growl), new PropertyMetadata(default(string), OnTokenChanged));
@@ -209,10 +203,28 @@ namespace HandyControl.Controls
public static string GetToken(DependencyObject element)
=> (string) element.GetValue(TokenProperty);
public static readonly DependencyProperty GrowlParentProperty = DependencyProperty.RegisterAttached(
"GrowlParent", typeof(bool), typeof(Growl), new PropertyMetadata(ValueBoxes.FalseBox, (o, args) =>
{
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 bool GetGrowlParent(DependencyObject element) => (bool) element.GetValue(GrowlParentProperty);
private static readonly DependencyProperty IsCreatedAutomaticallyProperty = DependencyProperty.RegisterAttached(
"IsCreatedAutomatically", typeof(bool), typeof(Growl), new PropertyMetadata(ValueBoxes.FalseBox));
private static void SetIsCreatedAutomatically(DependencyObject element, bool value)
=> element.SetValue(IsCreatedAutomaticallyProperty, ValueBoxes.BooleanBox(value));
private static bool GetIsCreatedAutomatically(DependencyObject element)
=> (bool) element.GetValue(IsCreatedAutomaticallyProperty);
public InfoType Type
{
get => (InfoType) GetValue(TypeProperty);
@@ -406,6 +418,7 @@ namespace HandyControl.Controls
Type = growlInfo.Type,
_waitTime = Math.Max(growlInfo.WaitTime, 2)
};
if (!string.IsNullOrEmpty(growlInfo.Token))
{
if (PanelDic.TryGetValue(growlInfo.Token, out var panel))
@@ -415,6 +428,8 @@ namespace HandyControl.Controls
}
else
{
// GrowlPanel is null, we create it automatically
GrowlPanel ??= CreateDefaultPanel();
GrowlPanel?.Children.Insert(0, ctl);
}
}
@@ -424,6 +439,64 @@ namespace HandyControl.Controls
);
}
private static Panel CreateDefaultPanel()
{
FrameworkElement element = WindowHelper.GetActiveWindow();
var decorator = VisualHelper.GetChild<AdornerDecorator>(element);
if (decorator != null)
{
var layer = decorator.AdornerLayer;
if (layer != null)
{
var panel = new StackPanel
{
VerticalAlignment = VerticalAlignment.Top
};
InitGrowlPanel(panel);
SetIsCreatedAutomatically(panel, true);
var scrollViewer = new ScrollViewer
{
HorizontalAlignment = HorizontalAlignment.Right,
VerticalScrollBarVisibility = ScrollBarVisibility.Hidden,
IsInertiaEnabled = true,
IsPenetrating = true,
Content = panel
};
var container = new AdornerContainer(layer)
{
Child = scrollViewer
};
layer.Add(container);
return panel;
}
}
return null;
}
private static void RemoveDefaultPanel(Panel panel)
{
FrameworkElement element = WindowHelper.GetActiveWindow();
var decorator = VisualHelper.GetChild<AdornerDecorator>(element);
if (decorator != null)
{
var layer = decorator.AdornerLayer;
var adorner = VisualHelper.GetParent<Adorner>(panel);
if (adorner != null)
{
layer?.Remove(adorner);
}
}
}
private static void InitGrowlInfo(ref GrowlInfo growlInfo, InfoType infoType)
{
if (growlInfo == null) throw new ArgumentNullException(nameof(growlInfo));
@@ -791,10 +864,22 @@ namespace HandyControl.Controls
{
panel.Children.Remove(this);
if (GrowlWindow?.GrowlPanel != null && GrowlWindow.GrowlPanel.Children.Count == 0)
if (GrowlWindow != null)
{
GrowlWindow.Close();
GrowlWindow = null;
if (GrowlWindow.GrowlPanel != null && GrowlWindow.GrowlPanel.Children.Count == 0)
{
GrowlWindow.Close();
GrowlWindow = null;
}
}
else
{
if (GrowlPanel != null && GrowlPanel.Children.Count == 0 && GetIsCreatedAutomatically(GrowlPanel))
{
// If the count of children is zero, we need to remove the panel, provided that the panel was created automatically
RemoveDefaultPanel(GrowlPanel);
GrowlPanel = null;
}
}
}
};
@@ -26,7 +26,7 @@
<Setter Property="Focusable" Value="False"/>
<Setter Property="MinHeight" Value="60"/>
<Setter Property="Width" Value="320"/>
<Setter Property="Margin" Value="10,0,0,10"/>
<Setter Property="Margin" Value="10,10,10,0"/>
<Setter Property="MaxWidth" Value="320"/>
<Setter Property="hc:BorderElement.CornerRadius" Value="8"/>
<Setter Property="Background" Value="{DynamicResource ThirdlyRegionBrush}"/>
@@ -71,4 +71,4 @@
</Setter>
</Style>
</ResourceDictionary>
</ResourceDictionary>
@@ -11893,7 +11893,7 @@
<Setter Property="Focusable" Value="False" />
<Setter Property="MinHeight" Value="60" />
<Setter Property="Width" Value="320" />
<Setter Property="Margin" Value="10,0,0,10" />
<Setter Property="Margin" Value="10,10,10,0" />
<Setter Property="MaxWidth" Value="320" />
<Setter Property="hc:BorderElement.CornerRadius" Value="8" />
<Setter Property="Background" Value="{DynamicResource ThirdlyRegionBrush}" />