From 0cdb6d7543bacc925144322d366192e455c5d2f9 Mon Sep 17 00:00:00 2001
From: NaBian <836904362@qq.com>
Date: Sun, 24 Mar 2019 16:54:42 +0800
Subject: [PATCH] Growl multi-window support
---
HandyControl/Controls/Attach/PanelElement.cs | 4 +-
HandyControl/Controls/Other/Growl.cs | 48 +++++++++++--------
HandyControl/Data/ResourceToken.cs | 1 +
.../Interactivity/BehaviorCollection.cs | 4 +-
HandyControlDemo/HandyControlDemo.csproj | 7 +++
HandyControlDemo/MainWindow.xaml | 2 +-
HandyControlDemo/MainWindow.xaml.cs | 19 +++++++-
.../Properties/Langs/Lang.Designer.cs | 18 +++++++
.../Properties/Langs/Lang.en.resx | 6 +++
HandyControlDemo/Properties/Langs/Lang.resx | 6 +++
.../UserControl/Controls/GrowlDemoCtl.xaml | 21 ++++----
.../ViewModel/Controls/GrowlDemoViewModel.cs | 4 ++
HandyControlDemo/Window/GrowlDemoWindow.xaml | 29 +++++++++++
.../Window/GrowlDemoWindow.xaml.cs | 27 +++++++++++
14 files changed, 160 insertions(+), 36 deletions(-)
create mode 100644 HandyControlDemo/Window/GrowlDemoWindow.xaml
create mode 100644 HandyControlDemo/Window/GrowlDemoWindow.xaml.cs
diff --git a/HandyControl/Controls/Attach/PanelElement.cs b/HandyControl/Controls/Attach/PanelElement.cs
index f1788378..05578452 100644
--- a/HandyControl/Controls/Attach/PanelElement.cs
+++ b/HandyControl/Controls/Attach/PanelElement.cs
@@ -15,12 +15,12 @@ namespace HandyControl.Controls
{
var collection = Interaction.GetBehaviors(panel);
var oldBehavior = GetTempFluidMoveBehavior(panel);
- collection.Remove(oldBehavior);
+ collection.ItemRemoved(oldBehavior);
panel.SetCurrentValue(TempFluidMoveBehaviorProperty, DependencyProperty.UnsetValue);
if (e.NewValue is FluidMoveBehavior behavior)
{
- collection.Add(behavior);
+ collection.ItemAdded(behavior);
SetTempFluidMoveBehavior(panel, behavior);
}
}
diff --git a/HandyControl/Controls/Other/Growl.cs b/HandyControl/Controls/Other/Growl.cs
index f6ec5b4c..ed274460 100644
--- a/HandyControl/Controls/Other/Growl.cs
+++ b/HandyControl/Controls/Other/Growl.cs
@@ -4,7 +4,6 @@ using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
-using System.Windows.Media.Animation;
using System.Windows.Threading;
using HandyControl.Data;
using HandyControl.Interactivity;
@@ -43,11 +42,6 @@ namespace HandyControl.Controls
private int _waitTime = 6;
- ///
- /// 消息容器
- ///
- public static Panel GrowlPanel { get; set; }
-
///
/// 计数
///
@@ -65,14 +59,21 @@ namespace HandyControl.Controls
CommandBindings.Add(new CommandBinding(ControlCommands.Close, ButtonClose_OnClick));
CommandBindings.Add(new CommandBinding(ControlCommands.Cancel, ButtonCancel_OnClick));
CommandBindings.Add(new CommandBinding(ControlCommands.Confirm, ButtonOk_OnClick));
-
- MouseEnter += Growl_MouseEnter;
- MouseLeave += Growl_MouseLeave;
}
- private void Growl_MouseLeave(object sender, MouseEventArgs e) => _buttonClose.Collapse();
+ protected override void OnMouseEnter(MouseEventArgs e)
+ {
+ base.OnMouseEnter(e);
- private void Growl_MouseEnter(object sender, MouseEventArgs e) => _buttonClose.Show(_showCloseButton);
+ _buttonClose.Show(_showCloseButton);
+ }
+
+ protected override void OnMouseLeave(MouseEventArgs e)
+ {
+ base.OnMouseLeave(e);
+
+ _buttonClose.Collapse();
+ }
public override void OnApplyTemplate()
{
@@ -93,6 +94,11 @@ namespace HandyControl.Controls
private Func ActionBeforeClose { get; set; }
+ ///
+ /// 消息容器
+ ///
+ public static Panel GrowlPanel { get; set; }
+
internal static readonly DependencyProperty CancelStrProperty = DependencyProperty.Register(
"CancelStr", typeof(string), typeof(Growl), new PropertyMetadata(default(string)));
@@ -120,7 +126,7 @@ namespace HandyControl.Controls
public static readonly DependencyProperty GrowlParentProperty = DependencyProperty.RegisterAttached(
"GrowlParent", typeof(bool), typeof(Growl), new PropertyMetadata(ValueBoxes.FalseBox, (o, args) =>
{
- if (o is Panel panel)
+ if ((bool)args.NewValue && o is Panel panel)
{
SetGrowlPanel(panel);
}
@@ -226,14 +232,8 @@ namespace HandyControl.Controls
menuItem
}
};
- var behavior = new FluidMoveBehavior
- {
- AppliesTo = FluidMoveScope.Children,
- Duration = new Duration(TimeSpan.FromMilliseconds(400)),
- EaseY = new PowerEase()
- };
- var collection = Interaction.GetBehaviors(GrowlPanel);
- collection.Add(behavior);
+
+ PanelElement.SetFluidMoveBehavior(GrowlPanel, ResourceHelper.GetResource(ResourceToken.BehaviorXY400));
}
private void Update()
@@ -451,7 +451,13 @@ namespace HandyControl.Controls
var transform = new TranslateTransform();
_gridMain.RenderTransform = transform;
var animation = AnimationHelper.CreateAnimation(MaxWidth);
- animation.Completed += delegate { GrowlPanel.Children.Remove(this); };
+ animation.Completed += (s, e) =>
+ {
+ if (Parent is Panel panel)
+ {
+ panel.Children.Remove(this);
+ }
+ };
transform.BeginAnimation(TranslateTransform.XProperty, animation);
}
diff --git a/HandyControl/Data/ResourceToken.cs b/HandyControl/Data/ResourceToken.cs
index bba58672..a8ebcb90 100644
--- a/HandyControl/Data/ResourceToken.cs
+++ b/HandyControl/Data/ResourceToken.cs
@@ -64,5 +64,6 @@
internal const string TabItemCapsuleVerticalLast = nameof(TabItemCapsuleVerticalLast);
+ internal const string BehaviorXY400 = nameof(BehaviorXY400);
}
}
\ No newline at end of file
diff --git a/HandyControl/Interactivity/BehaviorCollection.cs b/HandyControl/Interactivity/BehaviorCollection.cs
index 125384f9..3c95df71 100644
--- a/HandyControl/Interactivity/BehaviorCollection.cs
+++ b/HandyControl/Interactivity/BehaviorCollection.cs
@@ -22,14 +22,14 @@ namespace HandyControl.Interactivity
internal override void ItemAdded(Behavior item)
{
- if (AssociatedObject == null)
+ if (item == null || AssociatedObject == null)
return;
item.Attach(AssociatedObject);
}
internal override void ItemRemoved(Behavior item)
{
- if (((IAttachedObject) item).AssociatedObject == null)
+ if (((IAttachedObject) item)?.AssociatedObject == null)
return;
item.Detach();
}
diff --git a/HandyControlDemo/HandyControlDemo.csproj b/HandyControlDemo/HandyControlDemo.csproj
index 5a62c661..dee20316 100644
--- a/HandyControlDemo/HandyControlDemo.csproj
+++ b/HandyControlDemo/HandyControlDemo.csproj
@@ -381,6 +381,9 @@
CustomNonClientAreaWindow.xaml
+
+ GrowlDemoWindow.xaml
+
NoNonClientAreaDragableWindow.xaml
@@ -764,6 +767,10 @@
Designer
MSBuild:Compile
+
+ Designer
+ MSBuild:Compile
+
Designer
MSBuild:Compile
diff --git a/HandyControlDemo/MainWindow.xaml b/HandyControlDemo/MainWindow.xaml
index 99344426..85db7f43 100644
--- a/HandyControlDemo/MainWindow.xaml
+++ b/HandyControlDemo/MainWindow.xaml
@@ -33,7 +33,7 @@
-
+
\ No newline at end of file
diff --git a/HandyControlDemo/MainWindow.xaml.cs b/HandyControlDemo/MainWindow.xaml.cs
index 1287253a..bbd4e185 100644
--- a/HandyControlDemo/MainWindow.xaml.cs
+++ b/HandyControlDemo/MainWindow.xaml.cs
@@ -1,4 +1,7 @@
-namespace HandyControlDemo
+using System;
+using HandyControl.Controls;
+
+namespace HandyControlDemo
{
public partial class MainWindow
{
@@ -6,5 +9,19 @@
{
InitializeComponent();
}
+
+ protected override void OnActivated(EventArgs e)
+ {
+ base.OnActivated(e);
+
+ Growl.SetGrowlParent(GrowlPanel, true);
+ }
+
+ protected override void OnDeactivated(EventArgs e)
+ {
+ base.OnDeactivated(e);
+
+ Growl.SetGrowlParent(GrowlPanel, false);
+ }
}
}
\ No newline at end of file
diff --git a/HandyControlDemo/Properties/Langs/Lang.Designer.cs b/HandyControlDemo/Properties/Langs/Lang.Designer.cs
index 0ed9c7fa..8808342d 100644
--- a/HandyControlDemo/Properties/Langs/Lang.Designer.cs
+++ b/HandyControlDemo/Properties/Langs/Lang.Designer.cs
@@ -519,6 +519,15 @@ namespace HandyControlDemo.Properties.Langs {
}
}
+ ///
+ /// 查找类似 消息通知示例 的本地化字符串。
+ ///
+ public static string GrowlDemo {
+ get {
+ return ResourceManager.GetString("GrowlDemo", resourceCulture);
+ }
+ }
+
///
/// 查找类似 连接失败,请检查网络! 的本地化字符串。
///
@@ -672,6 +681,15 @@ namespace HandyControlDemo.Properties.Langs {
}
}
+ ///
+ /// 查找类似 新建窗口 的本地化字符串。
+ ///
+ public static string NewWindow {
+ get {
+ return ResourceManager.GetString("NewWindow", resourceCulture);
+ }
+ }
+
///
/// 查找类似 下一步 的本地化字符串。
///
diff --git a/HandyControlDemo/Properties/Langs/Lang.en.resx b/HandyControlDemo/Properties/Langs/Lang.en.resx
index 5a368f22..da18fdc5 100644
--- a/HandyControlDemo/Properties/Langs/Lang.en.resx
+++ b/HandyControlDemo/Properties/Langs/Lang.en.resx
@@ -522,4 +522,10 @@
Please wait...
+
+ Growl demo
+
+
+ New window
+
\ No newline at end of file
diff --git a/HandyControlDemo/Properties/Langs/Lang.resx b/HandyControlDemo/Properties/Langs/Lang.resx
index 05c82167..1718b806 100644
--- a/HandyControlDemo/Properties/Langs/Lang.resx
+++ b/HandyControlDemo/Properties/Langs/Lang.resx
@@ -522,4 +522,10 @@
请稍后...
+
+ 消息通知示例
+
+
+ 新建窗口
+
\ No newline at end of file
diff --git a/HandyControlDemo/UserControl/Controls/GrowlDemoCtl.xaml b/HandyControlDemo/UserControl/Controls/GrowlDemoCtl.xaml
index 7f5b6932..7739acc0 100644
--- a/HandyControlDemo/UserControl/Controls/GrowlDemoCtl.xaml
+++ b/HandyControlDemo/UserControl/Controls/GrowlDemoCtl.xaml
@@ -6,14 +6,17 @@
DataContext="{Binding GrowlDemo,Source={StaticResource Locator}}"
Background="{DynamicResource RegionBrush}">
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/HandyControlDemo/ViewModel/Controls/GrowlDemoViewModel.cs b/HandyControlDemo/ViewModel/Controls/GrowlDemoViewModel.cs
index e752c50c..6c0ce412 100644
--- a/HandyControlDemo/ViewModel/Controls/GrowlDemoViewModel.cs
+++ b/HandyControlDemo/ViewModel/Controls/GrowlDemoViewModel.cs
@@ -2,6 +2,7 @@
using GalaSoft.MvvmLight.Command;
using HandyControl.Controls;
using HandyControl.Data;
+using HandyControlDemo.Window;
namespace HandyControlDemo.ViewModel
{
@@ -44,5 +45,8 @@ namespace HandyControlDemo.ViewModel
public RelayCommand ClearCmd => new Lazy(() =>
new RelayCommand(Growl.Clear)).Value;
+
+ public RelayCommand NewWindowCmd => new Lazy(() =>
+ new RelayCommand(() => new GrowlDemoWindow().Show())).Value;
}
}
\ No newline at end of file
diff --git a/HandyControlDemo/Window/GrowlDemoWindow.xaml b/HandyControlDemo/Window/GrowlDemoWindow.xaml
new file mode 100644
index 00000000..c77f86a7
--- /dev/null
+++ b/HandyControlDemo/Window/GrowlDemoWindow.xaml
@@ -0,0 +1,29 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/HandyControlDemo/Window/GrowlDemoWindow.xaml.cs b/HandyControlDemo/Window/GrowlDemoWindow.xaml.cs
new file mode 100644
index 00000000..225a77ed
--- /dev/null
+++ b/HandyControlDemo/Window/GrowlDemoWindow.xaml.cs
@@ -0,0 +1,27 @@
+using System;
+using HandyControl.Controls;
+
+namespace HandyControlDemo.Window
+{
+ public partial class GrowlDemoWindow
+ {
+ public GrowlDemoWindow()
+ {
+ InitializeComponent();
+ }
+
+ protected override void OnActivated(EventArgs e)
+ {
+ base.OnActivated(e);
+
+ Growl.SetGrowlParent(GrowlPanel, true);
+ }
+
+ protected override void OnDeactivated(EventArgs e)
+ {
+ base.OnDeactivated(e);
+
+ Growl.SetGrowlParent(GrowlPanel, false);
+ }
+ }
+}