Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions MainDemo.Wpf/Buttons.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -1091,5 +1091,39 @@
</materialDesign:PopupBox>
</smtx:XamlDisplay>
</StackPanel>

<Rectangle
Margin="0 24 0 0"
Height="1"
Fill="{DynamicResource MaterialDesignDivider}"
Grid.Row="11" />

<TextBlock
Style="{StaticResource MaterialDesignHeadline5TextBlock}"
Grid.Row="12"
Margin="0 24"
Text="Buttons - With Custom Animation Duration"/>

<StackPanel
Grid.Row="13"
Orientation="Horizontal">
<smtx:XamlDisplay UniqueKey="button_duration_1" Margin="0,0,20,0">
<Button Style="{StaticResource MaterialDesignRaisedAccentButton}"
materialDesign:ShadowAssist.ShadowAnimationDuration="0:0:0">
Instant Duration
</Button>
</smtx:XamlDisplay>
<smtx:XamlDisplay UniqueKey="button_duration_2" Margin="0,0,20,0">
<Button>
Default Duration
</Button>
</smtx:XamlDisplay>
<smtx:XamlDisplay UniqueKey="button_duration_3" Margin="0,0,20,0">
<Button Style="{StaticResource MaterialDesignRaisedDarkButton}"
materialDesign:ShadowAssist.ShadowAnimationDuration="0:0:0.5">
Long Duration
</Button>
</smtx:XamlDisplay>
</StackPanel>
</Grid>
</UserControl>
55 changes: 50 additions & 5 deletions MaterialDesignThemes.Wpf/ShadowAssist.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;

namespace MaterialDesignThemes.Wpf
{

public enum ShadowDepth
{
Depth0,
Expand Down Expand Up @@ -37,8 +40,10 @@ public ShadowLocalInfo(double standardOpacity)
public double StandardOpacity { get; }
}

public static class ShadowAssist
public class ShadowAssist
{

#region AttachedProperty : ShadowDepthProperty
public static readonly DependencyProperty ShadowDepthProperty = DependencyProperty.RegisterAttached(
"ShadowDepth", typeof(ShadowDepth), typeof(ShadowAssist), new FrameworkPropertyMetadata(default(ShadowDepth), FrameworkPropertyMetadataOptions.AffectsRender));

Expand All @@ -51,7 +56,9 @@ public static ShadowDepth GetShadowDepth(DependencyObject element)
{
return (ShadowDepth)element.GetValue(ShadowDepthProperty);
}
#endregion

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

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

private static ShadowLocalInfo? GetLocalInfo(DependencyObject element)
=> (ShadowLocalInfo?)element.GetValue(LocalInfoPropertyKey.DependencyProperty);
#endregion

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

private static void DarkenPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{

var uiElement = dependencyObject as UIElement;
var dropShadowEffect = uiElement?.Effect as DropShadowEffect;


if (dropShadowEffect == null) return;

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

var doubleAnimation = new DoubleAnimation(1, new Duration(TimeSpan.FromMilliseconds(350)))
TimeSpan time = GetShadowAnimationDuration(dependencyObject);

var doubleAnimation = new DoubleAnimation()
{
FillBehavior = FillBehavior.HoldEnd
To = 1,
Duration = new Duration(time),
FillBehavior = FillBehavior.HoldEnd,
EasingFunction = new CubicEase(),
AccelerationRatio = 0.4,
DecelerationRatio = 0.2
};
dropShadowEffect.BeginAnimation(DropShadowEffect.OpacityProperty, doubleAnimation);
}
Expand All @@ -86,9 +104,16 @@ private static void DarkenPropertyChangedCallback(DependencyObject dependencyObj
var shadowLocalInfo = GetLocalInfo(dependencyObject);
if (shadowLocalInfo == null) return;

var doubleAnimation = new DoubleAnimation(shadowLocalInfo.StandardOpacity, new Duration(TimeSpan.FromMilliseconds(350)))
TimeSpan time = GetShadowAnimationDuration(dependencyObject);

var doubleAnimation = new DoubleAnimation()
{
FillBehavior = FillBehavior.HoldEnd
To = shadowLocalInfo.StandardOpacity,
Duration = new Duration(time),
FillBehavior = FillBehavior.HoldEnd,
EasingFunction = new CubicEase(),
AccelerationRatio = 0.4,
DecelerationRatio = 0.2
};
dropShadowEffect.BeginAnimation(DropShadowEffect.OpacityProperty, doubleAnimation);
}
Expand All @@ -103,7 +128,9 @@ public static bool GetDarken(DependencyObject element)
{
return (bool)element.GetValue(DarkenProperty);
}
#endregion

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

Expand All @@ -116,7 +143,9 @@ public static CacheMode GetCacheMode(DependencyObject element)
{
return (CacheMode)element.GetValue(CacheModeProperty);
}
#endregion

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

Expand All @@ -129,5 +158,21 @@ public static ShadowEdges GetShadowEdges(DependencyObject element)
{
return (ShadowEdges)element.GetValue(ShadowEdgesProperty);
}
#endregion

#region AttachedProperty : ShadowAnimationDurationProperty
public static readonly DependencyProperty ShadowAnimationDurationProperty =
DependencyProperty.RegisterAttached(
name: "ShadowAnimationDuration",
propertyType: typeof(TimeSpan),
ownerType: typeof(ShadowAssist),
defaultMetadata: new FrameworkPropertyMetadata(
defaultValue: new TimeSpan(0, 0, 0, 0, 180),
flags: FrameworkPropertyMetadataOptions.Inherits)
);

public static TimeSpan GetShadowAnimationDuration(DependencyObject element) => (TimeSpan)element.GetValue(ShadowAnimationDurationProperty);
public static void SetShadowAnimationDuration(DependencyObject element, TimeSpan value) => element.SetValue(ShadowAnimationDurationProperty, value);
#endregion
}
}