Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes for Shadow assist animation #2651

Merged
merged 3 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
merge + add property + add demo
  • Loading branch information
ElieTaillard committed Apr 21, 2022
commit 9aad0ee3381438d3a84f4a6e42d18f348e787a7b
19 changes: 19 additions & 0 deletions MainDemo.Wpf/Buttons.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock
Style="{StaticResource MaterialDesignHeadline5TextBlock}"
Expand Down Expand Up @@ -1091,5 +1095,20 @@
</materialDesign:PopupBox>
</smtx:XamlDisplay>
</StackPanel>

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

<StackPanel
Grid.Row="12"
Orientation="Horizontal">
<Button materialDesign:ShadowAssist.ShadowAnimationDuration="0:0:0" Margin="0,0,20,0">Instant</Button>
<Button Margin="0,0,20,0">Default</Button>
<Button materialDesign:ShadowAssist.ShadowAnimationDuration="0:0:2">Long</Button>
</StackPanel>

</Grid>
</UserControl>
47 changes: 43 additions & 4 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,7 +104,8 @@ 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(shadowLocalInfo.StandardOpacity, new Duration(time))
{
FillBehavior = FillBehavior.HoldEnd
};
Expand All @@ -103,7 +122,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 +137,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 +152,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
}
}