enhance: Growl demo.

This commit is contained in:
NaBian committed 2025-09-08 23:20:47 +08:00
1 parent 8405e7f2b1
commit 83fc434e7d
5 files changed
+166 -69

No files matched your search

@@ -30,6 +30,7 @@
<hc:EnumDataProvider x:Key="DemoTypes" Type="data:DemoType"/> <hc:EnumDataProvider x:Key="DemoTypes" Type="data:DemoType"/>
<hc:EnumDataProvider x:Key="ShowAnimations" Type="hc:ShowAnimation"/> <hc:EnumDataProvider x:Key="ShowAnimations" Type="hc:ShowAnimation"/>
<hc:EnumDataProvider x:Key="HatchStyles" Type="hc:HatchStyle"/> <hc:EnumDataProvider x:Key="HatchStyles" Type="hc:HatchStyle"/>
<hc:EnumDataProvider x:Key="TransitionModes" Type="hc:TransitionMode"/>
</ResourceDictionary> </ResourceDictionary>
</Application.Resources> </Application.Resources>
</Application> </Application>
@@ -7,30 +7,82 @@
DataContext="{Binding GrowlDemo,Source={StaticResource Locator}}" DataContext="{Binding GrowlDemo,Source={StaticResource Locator}}"
Background="{DynamicResource RegionBrush}"> Background="{DynamicResource RegionBrush}">
<hc:TransitioningContentControl> <hc:TransitioningContentControl>
<StackPanel Margin="32"> <hc:UniformSpacingPanel Margin="32"
<GroupBox Header="Window" Padding="10,5"> Orientation="Vertical"
Spacing="32">
<ComboBox SelectedValue="{Binding TransitionMode}"
ItemsSource="{Binding Source={StaticResource TransitionModes}}" />
<GroupBox Header="Window"
Padding="10,5">
<WrapPanel VerticalAlignment="Center"> <WrapPanel VerticalAlignment="Center">
<Button Command="{Binding InfoCmd}" Content="{ex:Lang Key={x:Static langs:LangKeys.Info}}" Margin="0,5,5,5" Style="{StaticResource ButtonInfo}"/> <Button Command="{Binding InfoCmd}"
<Button Command="{Binding SuccessCmd}" Content="{ex:Lang Key={x:Static langs:LangKeys.Success}}" Margin="5" Style="{StaticResource ButtonSuccess}"/> Content="{ex:Lang Key={x:Static langs:LangKeys.Info}}"
<Button Command="{Binding WarningCmd}" Content="{ex:Lang Key={x:Static langs:LangKeys.Warning}}" Margin="5" Style="{StaticResource ButtonWarning}"/> Margin="0,5,5,5"
<Button Command="{Binding ErrorCmd}" Content="{ex:Lang Key={x:Static langs:LangKeys.Error}}" Margin="5" Style="{StaticResource ButtonDanger}"/> Style="{StaticResource ButtonInfo}" />
<Button Command="{Binding AskCmd}" Content="{ex:Lang Key={x:Static langs:LangKeys.Ask}}" Margin="5" Background="BlueViolet" Foreground="White" BorderThickness="0"/> <Button Command="{Binding SuccessCmd}"
<Button Command="{Binding FatalCmd}" Content="{ex:Lang Key={x:Static langs:LangKeys.Fatal}}" Margin="5,0"/> Content="{ex:Lang Key={x:Static langs:LangKeys.Success}}"
<Button Command="{Binding ClearCmd}" Content="{ex:Lang Key={x:Static langs:LangKeys.Clear}}" Margin="5,0,0,0" Style="{StaticResource ButtonPrimary}"/> Margin="5"
Style="{StaticResource ButtonSuccess}" />
<Button Command="{Binding WarningCmd}"
Content="{ex:Lang Key={x:Static langs:LangKeys.Warning}}"
Margin="5"
Style="{StaticResource ButtonWarning}" />
<Button Command="{Binding ErrorCmd}"
Content="{ex:Lang Key={x:Static langs:LangKeys.Error}}"
Margin="5"
Style="{StaticResource ButtonDanger}" />
<Button Command="{Binding AskCmd}"
Content="{ex:Lang Key={x:Static langs:LangKeys.Ask}}"
Margin="5"
Background="BlueViolet"
Foreground="White"
BorderThickness="0" />
<Button Command="{Binding FatalCmd}"
Content="{ex:Lang Key={x:Static langs:LangKeys.Fatal}}"
Margin="5,0" />
<Button Command="{Binding ClearCmd}"
Content="{ex:Lang Key={x:Static langs:LangKeys.Clear}}"
Margin="5,0,0,0"
Style="{StaticResource ButtonPrimary}" />
</WrapPanel> </WrapPanel>
</GroupBox> </GroupBox>
<Button Command="{Binding NewWindowCmd}" Content="{ex:Lang Key={x:Static langs:LangKeys.NewWindow}}" Margin="0,32" HorizontalAlignment="Left"/> <Button Command="{Binding NewWindowCmd}"
<GroupBox Header="Desktop" Padding="10,5"> Content="{ex:Lang Key={x:Static langs:LangKeys.NewWindow}}"
HorizontalAlignment="Left" />
<GroupBox Header="Desktop"
Padding="10,5">
<WrapPanel VerticalAlignment="Center"> <WrapPanel VerticalAlignment="Center">
<Button Command="{Binding InfoGlobalCmd}" Content="{ex:Lang Key={x:Static langs:LangKeys.Info}}" Margin="0,5,5,5" Style="{StaticResource ButtonInfo}"/> <Button Command="{Binding InfoGlobalCmd}"
<Button Command="{Binding SuccessGlobalCmd}" Content="{ex:Lang Key={x:Static langs:LangKeys.Success}}" Margin="5" Style="{StaticResource ButtonSuccess}"/> Content="{ex:Lang Key={x:Static langs:LangKeys.Info}}"
<Button Command="{Binding WarningGlobalCmd}" Content="{ex:Lang Key={x:Static langs:LangKeys.Warning}}" Margin="5" Style="{StaticResource ButtonWarning}"/> Margin="0,5,5,5"
<Button Command="{Binding ErrorGlobalCmd}" Content="{ex:Lang Key={x:Static langs:LangKeys.Error}}" Margin="5" Style="{StaticResource ButtonDanger}"/> Style="{StaticResource ButtonInfo}" />
<Button Command="{Binding AskGlobalCmd}" Content="{ex:Lang Key={x:Static langs:LangKeys.Ask}}" Margin="5" Background="BlueViolet" Foreground="White" BorderThickness="0"/> <Button Command="{Binding SuccessGlobalCmd}"
<Button Command="{Binding FatalGlobalCmd}" Content="{ex:Lang Key={x:Static langs:LangKeys.Fatal}}" Margin="5,0"/> Content="{ex:Lang Key={x:Static langs:LangKeys.Success}}"
<Button Command="{Binding ClearGlobalCmd}" Content="{ex:Lang Key={x:Static langs:LangKeys.Clear}}" Margin="5,0,0,0" Style="{StaticResource ButtonPrimary}"/> Margin="5"
Style="{StaticResource ButtonSuccess}" />
<Button Command="{Binding WarningGlobalCmd}"
Content="{ex:Lang Key={x:Static langs:LangKeys.Warning}}"
Margin="5"
Style="{StaticResource ButtonWarning}" />
<Button Command="{Binding ErrorGlobalCmd}"
Content="{ex:Lang Key={x:Static langs:LangKeys.Error}}"
Margin="5"
Style="{StaticResource ButtonDanger}" />
<Button Command="{Binding AskGlobalCmd}"
Content="{ex:Lang Key={x:Static langs:LangKeys.Ask}}"
Margin="5"
Background="BlueViolet"
Foreground="White"
BorderThickness="0" />
<Button Command="{Binding FatalGlobalCmd}"
Content="{ex:Lang Key={x:Static langs:LangKeys.Fatal}}"
Margin="5,0" />
<Button Command="{Binding ClearGlobalCmd}"
Content="{ex:Lang Key={x:Static langs:LangKeys.Clear}}"
Margin="5,0,0,0"
Style="{StaticResource ButtonPrimary}" />
</WrapPanel> </WrapPanel>
</GroupBox> </GroupBox>
</StackPanel> </hc:UniformSpacingPanel>
</hc:TransitioningContentControl> </hc:TransitioningContentControl>
</UserControl> </UserControl>
@@ -1,4 +1,5 @@
using System.Windows; using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command; using GalaSoft.MvvmLight.Command;
using HandyControl.Controls; using HandyControl.Controls;
using HandyControl.Data; using HandyControl.Data;
@@ -6,10 +7,12 @@ using HandyControlDemo.Window;
namespace HandyControlDemo.ViewModel; namespace HandyControlDemo.ViewModel;
public class GrowlDemoViewModel public class GrowlDemoViewModel : ViewModelBase
{ {
private readonly string _token; private readonly string _token;
private TransitionMode _transitionMode;
public GrowlDemoViewModel() public GrowlDemoViewModel()
{ {
@@ -20,6 +23,24 @@ public class GrowlDemoViewModel
_token = token; _token = token;
} }
public TransitionMode TransitionMode
{
get => _transitionMode;
#if NET40
set
{
Set(nameof(TransitionMode), ref _transitionMode, value);
Growl.SetTransitionMode(Application.Current.MainWindow, value);
}
#else
set
{
Set(ref _transitionMode, value);
Growl.SetTransitionMode(Application.Current.MainWindow, value);
}
#endif
}
#region Window #region Window
public RelayCommand InfoCmd => new(() => Growl.Info(Properties.Langs.Lang.GrowlInfo, _token)); public RelayCommand InfoCmd => new(() => Growl.Info(Properties.Langs.Lang.GrowlInfo, _token));
@@ -47,11 +47,13 @@ public class Growl : Control
"ShowMode", typeof(GrowlShowMode), typeof(Growl), "ShowMode", typeof(GrowlShowMode), typeof(Growl),
new FrameworkPropertyMetadata(default(GrowlShowMode), FrameworkPropertyMetadataOptions.Inherits)); new FrameworkPropertyMetadata(default(GrowlShowMode), FrameworkPropertyMetadataOptions.Inherits));
public static readonly DependencyProperty TransitionModeProperty = DependencyProperty.Register( public static readonly DependencyProperty TransitionModeProperty = DependencyProperty.RegisterAttached(
nameof(TransitionMode), typeof(TransitionMode), typeof(Growl), new PropertyMetadata(default(TransitionMode))); "TransitionMode", typeof(TransitionMode), typeof(Growl),
new FrameworkPropertyMetadata(default(TransitionMode), FrameworkPropertyMetadataOptions.Inherits));
public static readonly DependencyProperty TransitionStoryboardProperty = DependencyProperty.Register( public static readonly DependencyProperty TransitionStoryboardProperty = DependencyProperty.RegisterAttached(
nameof(TransitionStoryboard), typeof(Storyboard), typeof(Growl), new PropertyMetadata(default(Storyboard))); "TransitionStoryboard", typeof(Storyboard), typeof(Growl),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));
public static readonly DependencyProperty ShowDateTimeProperty = DependencyProperty.Register( public static readonly DependencyProperty ShowDateTimeProperty = DependencyProperty.Register(
nameof(ShowDateTime), typeof(bool), typeof(Growl), new PropertyMetadata(ValueBoxes.TrueBox)); nameof(ShowDateTime), typeof(bool), typeof(Growl), new PropertyMetadata(ValueBoxes.TrueBox));
@@ -105,18 +107,6 @@ public class Growl : Control
/// </summary> /// </summary>
public static Panel GrowlPanel { get; set; } public static Panel GrowlPanel { get; set; }
public TransitionMode TransitionMode
{
get => (TransitionMode) GetValue(TransitionModeProperty);
set => SetValue(TransitionModeProperty, value);
}
public Storyboard TransitionStoryboard
{
get => (Storyboard) GetValue(TransitionStoryboardProperty);
set => SetValue(TransitionStoryboardProperty, value);
}
public InfoType Type public InfoType Type
{ {
get => (InfoType) GetValue(TypeProperty); get => (InfoType) GetValue(TypeProperty);
@@ -231,6 +221,18 @@ public class Growl : Control
public static GrowlShowMode GetShowMode(DependencyObject element) => public static GrowlShowMode GetShowMode(DependencyObject element) =>
(GrowlShowMode) element.GetValue(ShowModeProperty); (GrowlShowMode) element.GetValue(ShowModeProperty);
public static void SetTransitionMode(DependencyObject element, TransitionMode value)
=> element.SetValue(TransitionModeProperty, value);
public static TransitionMode GetTransitionMode(DependencyObject element)
=> (TransitionMode) element.GetValue(TransitionModeProperty);
public static void SetTransitionStoryboard(DependencyObject element, Storyboard value)
=> element.SetValue(TransitionStoryboardProperty, value);
public static Storyboard GetTransitionStoryboard(DependencyObject element)
=> (Storyboard) element.GetValue(TransitionStoryboardProperty);
public static void SetGrowlParent(DependencyObject element, bool value) => public static void SetGrowlParent(DependencyObject element, bool value) =>
element.SetValue(GrowlParentProperty, ValueBoxes.BooleanBox(value)); element.SetValue(GrowlParentProperty, ValueBoxes.BooleanBox(value));
@@ -371,7 +373,6 @@ public class Growl : Control
CancelStr = growlInfo.CancelStr, CancelStr = growlInfo.CancelStr,
Type = growlInfo.Type, Type = growlInfo.Type,
_waitTime = Math.Max(growlInfo.WaitTime, MinWaitTime), _waitTime = Math.Max(growlInfo.WaitTime, MinWaitTime),
FlowDirection = growlInfo.FlowDirection
}; };
ShowInternal(GrowlWindow.GrowlPanel, ctl); ShowInternal(GrowlWindow.GrowlPanel, ctl);
@@ -422,6 +423,10 @@ public class Growl : Control
// GrowlPanel is null, we create it automatically // GrowlPanel is null, we create it automatically
GrowlPanel ??= CreateDefaultPanel(); GrowlPanel ??= CreateDefaultPanel();
ShowInternal(GrowlPanel, ctl); ShowInternal(GrowlPanel, ctl);
var transitionMode = GetTransitionMode(ctl);
GrowlPanel.VerticalAlignment = GetPanelVerticalAlignment(transitionMode);
GrowlPanel.HorizontalAlignment = GetPanelHorizontalAlignment(transitionMode);
} }
} }
#if NET40 #if NET40
@@ -435,40 +440,33 @@ public class Growl : Control
FrameworkElement element = WindowHelper.GetActiveWindow(); FrameworkElement element = WindowHelper.GetActiveWindow();
var decorator = VisualHelper.GetChild<AdornerDecorator>(element); var decorator = VisualHelper.GetChild<AdornerDecorator>(element);
if (decorator != null) var layer = decorator?.AdornerLayer;
if (layer == null)
{ {
var layer = decorator.AdornerLayer; return null;
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; var panel = new SimpleStackPanel();
InitGrowlPanel(panel);
SetIsCreatedAutomatically(panel, true);
var scrollViewer = new ScrollViewer
{
VerticalScrollBarVisibility = ScrollBarVisibility.Hidden,
IsInertiaEnabled = true,
IsPenetrating = true,
Content = panel
};
var container = new AdornerContainer(layer)
{
Child = scrollViewer
};
layer.Add(container);
return panel;
} }
private static void RemoveDefaultPanel(Panel panel) private static void RemoveDefaultPanel(Panel panel)
@@ -918,7 +916,7 @@ public class Growl : Control
private void StartTransition(bool isClose, Action completed = null) private void StartTransition(bool isClose, Action completed = null)
{ {
var actualStoryboard = TransitionStoryboard ?? CreateStoryboard(isClose, TransitionMode); var actualStoryboard = GetTransitionStoryboard(this) ?? CreateStoryboard(isClose, GetTransitionMode(this));
if (actualStoryboard is null) if (actualStoryboard is null)
{ {
return; return;
@@ -1064,4 +1062,28 @@ public class Growl : Control
_ => null _ => null
}; };
} }
private static VerticalAlignment GetPanelVerticalAlignment(TransitionMode transitionMode)
{
return transitionMode switch
{
TransitionMode.Right2Left or TransitionMode.Right2LeftWithFade or TransitionMode.Left2Right
or TransitionMode.Left2RightWithFade => VerticalAlignment.Top,
TransitionMode.Bottom2Top or TransitionMode.Bottom2TopWithFade => VerticalAlignment.Bottom,
TransitionMode.Top2Bottom or TransitionMode.Top2BottomWithFade => VerticalAlignment.Top,
_ => VerticalAlignment.Stretch
};
}
private static HorizontalAlignment GetPanelHorizontalAlignment(TransitionMode transitionMode)
{
return transitionMode switch
{
TransitionMode.Right2Left or TransitionMode.Right2LeftWithFade => HorizontalAlignment.Right,
TransitionMode.Left2Right or TransitionMode.Left2RightWithFade => HorizontalAlignment.Left,
TransitionMode.Bottom2Top or TransitionMode.Bottom2TopWithFade or TransitionMode.Top2Bottom
or TransitionMode.Top2BottomWithFade => HorizontalAlignment.Center,
_ => HorizontalAlignment.Stretch
};
}
} }
@@ -37,6 +37,7 @@ public class GrowlInfo
public string Token { get; set; } public string Token { get; set; }
[Obsolete("use TransitionMode instead")]
public FlowDirection FlowDirection { get; set; } public FlowDirection FlowDirection { get; set; }
public Dispatcher Dispatcher { get; set; } public Dispatcher Dispatcher { get; set; }