Skip to content

Commit 32e909d

Browse files
authored
Changes for Shadow assist animation (#2651)
* merge + add property + add demo * change default value + change demo * change default duration + add easing
1 parent 8142e75 commit 32e909d

File tree

2 files changed

+84
-5
lines changed

2 files changed

+84
-5
lines changed

MainDemo.Wpf/Buttons.xaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,5 +1091,39 @@
10911091
</materialDesign:PopupBox>
10921092
</smtx:XamlDisplay>
10931093
</StackPanel>
1094+
1095+
<Rectangle
1096+
Margin="0 24 0 0"
1097+
Height="1"
1098+
Fill="{DynamicResource MaterialDesignDivider}"
1099+
Grid.Row="11" />
1100+
1101+
<TextBlock
1102+
Style="{StaticResource MaterialDesignHeadline5TextBlock}"
1103+
Grid.Row="12"
1104+
Margin="0 24"
1105+
Text="Buttons - With Custom Animation Duration"/>
1106+
1107+
<StackPanel
1108+
Grid.Row="13"
1109+
Orientation="Horizontal">
1110+
<smtx:XamlDisplay UniqueKey="button_duration_1" Margin="0,0,20,0">
1111+
<Button Style="{StaticResource MaterialDesignRaisedAccentButton}"
1112+
materialDesign:ShadowAssist.ShadowAnimationDuration="0:0:0">
1113+
Instant Duration
1114+
</Button>
1115+
</smtx:XamlDisplay>
1116+
<smtx:XamlDisplay UniqueKey="button_duration_2" Margin="0,0,20,0">
1117+
<Button>
1118+
Default Duration
1119+
</Button>
1120+
</smtx:XamlDisplay>
1121+
<smtx:XamlDisplay UniqueKey="button_duration_3" Margin="0,0,20,0">
1122+
<Button Style="{StaticResource MaterialDesignRaisedDarkButton}"
1123+
materialDesign:ShadowAssist.ShadowAnimationDuration="0:0:0.5">
1124+
Long Duration
1125+
</Button>
1126+
</smtx:XamlDisplay>
1127+
</StackPanel>
10941128
</Grid>
10951129
</UserControl>

MaterialDesignThemes.Wpf/ShadowAssist.cs

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
using System;
22
using System.Windows;
3+
using System.Windows.Controls;
4+
using System.Windows.Controls.Primitives;
35
using System.Windows.Media;
46
using System.Windows.Media.Animation;
57
using System.Windows.Media.Effects;
68

79
namespace MaterialDesignThemes.Wpf
810
{
11+
912
public enum ShadowDepth
1013
{
1114
Depth0,
@@ -37,8 +40,10 @@ public ShadowLocalInfo(double standardOpacity)
3740
public double StandardOpacity { get; }
3841
}
3942

40-
public static class ShadowAssist
43+
public class ShadowAssist
4144
{
45+
46+
#region AttachedProperty : ShadowDepthProperty
4247
public static readonly DependencyProperty ShadowDepthProperty = DependencyProperty.RegisterAttached(
4348
"ShadowDepth", typeof(ShadowDepth), typeof(ShadowAssist), new FrameworkPropertyMetadata(default(ShadowDepth), FrameworkPropertyMetadataOptions.AffectsRender));
4449

@@ -51,7 +56,9 @@ public static ShadowDepth GetShadowDepth(DependencyObject element)
5156
{
5257
return (ShadowDepth)element.GetValue(ShadowDepthProperty);
5358
}
59+
#endregion
5460

61+
#region AttachedProperty : LocalInfoPropertyKey
5562
private static readonly DependencyPropertyKey LocalInfoPropertyKey = DependencyProperty.RegisterAttachedReadOnly(
5663
"LocalInfo", typeof(ShadowLocalInfo), typeof(ShadowAssist), new PropertyMetadata(default(ShadowLocalInfo)));
5764

@@ -60,24 +67,35 @@ private static void SetLocalInfo(DependencyObject element, ShadowLocalInfo? valu
6067

6168
private static ShadowLocalInfo? GetLocalInfo(DependencyObject element)
6269
=> (ShadowLocalInfo?)element.GetValue(LocalInfoPropertyKey.DependencyProperty);
70+
#endregion
6371

72+
#region AttachedProperty : DarkenProperty
6473
public static readonly DependencyProperty DarkenProperty = DependencyProperty.RegisterAttached(
6574
"Darken", typeof(bool), typeof(ShadowAssist), new FrameworkPropertyMetadata(default(bool), FrameworkPropertyMetadataOptions.AffectsRender, DarkenPropertyChangedCallback));
6675

6776
private static void DarkenPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
6877
{
78+
6979
var uiElement = dependencyObject as UIElement;
7080
var dropShadowEffect = uiElement?.Effect as DropShadowEffect;
7181

82+
7283
if (dropShadowEffect == null) return;
7384

7485
if ((bool)dependencyPropertyChangedEventArgs.NewValue)
7586
{
7687
SetLocalInfo(dependencyObject, new ShadowLocalInfo(dropShadowEffect.Opacity));
7788

78-
var doubleAnimation = new DoubleAnimation(1, new Duration(TimeSpan.FromMilliseconds(350)))
89+
TimeSpan time = GetShadowAnimationDuration(dependencyObject);
90+
91+
var doubleAnimation = new DoubleAnimation()
7992
{
80-
FillBehavior = FillBehavior.HoldEnd
93+
To = 1,
94+
Duration = new Duration(time),
95+
FillBehavior = FillBehavior.HoldEnd,
96+
EasingFunction = new CubicEase(),
97+
AccelerationRatio = 0.4,
98+
DecelerationRatio = 0.2
8199
};
82100
dropShadowEffect.BeginAnimation(DropShadowEffect.OpacityProperty, doubleAnimation);
83101
}
@@ -86,9 +104,16 @@ private static void DarkenPropertyChangedCallback(DependencyObject dependencyObj
86104
var shadowLocalInfo = GetLocalInfo(dependencyObject);
87105
if (shadowLocalInfo == null) return;
88106

89-
var doubleAnimation = new DoubleAnimation(shadowLocalInfo.StandardOpacity, new Duration(TimeSpan.FromMilliseconds(350)))
107+
TimeSpan time = GetShadowAnimationDuration(dependencyObject);
108+
109+
var doubleAnimation = new DoubleAnimation()
90110
{
91-
FillBehavior = FillBehavior.HoldEnd
111+
To = shadowLocalInfo.StandardOpacity,
112+
Duration = new Duration(time),
113+
FillBehavior = FillBehavior.HoldEnd,
114+
EasingFunction = new CubicEase(),
115+
AccelerationRatio = 0.4,
116+
DecelerationRatio = 0.2
92117
};
93118
dropShadowEffect.BeginAnimation(DropShadowEffect.OpacityProperty, doubleAnimation);
94119
}
@@ -103,7 +128,9 @@ public static bool GetDarken(DependencyObject element)
103128
{
104129
return (bool)element.GetValue(DarkenProperty);
105130
}
131+
#endregion
106132

133+
#region AttachedProperty : CacheModeProperty
107134
public static readonly DependencyProperty CacheModeProperty = DependencyProperty.RegisterAttached(
108135
"CacheMode", typeof(CacheMode), typeof(ShadowAssist), new FrameworkPropertyMetadata(new BitmapCache { EnableClearType = true, SnapsToDevicePixels = true }, FrameworkPropertyMetadataOptions.Inherits));
109136

@@ -116,7 +143,9 @@ public static CacheMode GetCacheMode(DependencyObject element)
116143
{
117144
return (CacheMode)element.GetValue(CacheModeProperty);
118145
}
146+
#endregion
119147

148+
#region AttachedProperty : ShadowEdgesProperty
120149
public static readonly DependencyProperty ShadowEdgesProperty = DependencyProperty.RegisterAttached(
121150
"ShadowEdges", typeof(ShadowEdges), typeof(ShadowAssist), new PropertyMetadata(ShadowEdges.All));
122151

@@ -129,5 +158,21 @@ public static ShadowEdges GetShadowEdges(DependencyObject element)
129158
{
130159
return (ShadowEdges)element.GetValue(ShadowEdgesProperty);
131160
}
161+
#endregion
162+
163+
#region AttachedProperty : ShadowAnimationDurationProperty
164+
public static readonly DependencyProperty ShadowAnimationDurationProperty =
165+
DependencyProperty.RegisterAttached(
166+
name: "ShadowAnimationDuration",
167+
propertyType: typeof(TimeSpan),
168+
ownerType: typeof(ShadowAssist),
169+
defaultMetadata: new FrameworkPropertyMetadata(
170+
defaultValue: new TimeSpan(0, 0, 0, 0, 180),
171+
flags: FrameworkPropertyMetadataOptions.Inherits)
172+
);
173+
174+
public static TimeSpan GetShadowAnimationDuration(DependencyObject element) => (TimeSpan)element.GetValue(ShadowAnimationDurationProperty);
175+
public static void SetShadowAnimationDuration(DependencyObject element, TimeSpan value) => element.SetValue(ShadowAnimationDurationProperty, value);
176+
#endregion
132177
}
133178
}

0 commit comments

Comments
 (0)