Skip to content

Commit

Permalink
Make some controls respect Windows animation setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinnara committed Jan 2, 2020
1 parent fa56cb3 commit 0297e3e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
4 changes: 4 additions & 0 deletions ModernWpf.Controls/Common/SharedHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Interop;
using System.Windows.Media;

namespace ModernWpf.Controls
{
internal static class SharedHelpers
{
public static bool IsAnimationsEnabled => SystemParameters.ClientAreaAnimation &&
RenderCapability.Tier > 0;

public static bool DoRectsIntersect(
Rect rect1,
Rect rect2)
Expand Down
2 changes: 2 additions & 0 deletions ModernWpf.Controls/ToggleSwitch/ToggleSwitch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,8 @@ private void UpdateHeaderContentPresenterVisibility()

private void UpdateVisualStates(bool useTransitions)
{
useTransitions &= SharedHelpers.IsAnimationsEnabled;

string stateName;

if (!IsEnabled)
Expand Down
20 changes: 19 additions & 1 deletion ModernWpf.MahApps/HamburgerMenu/HamburgerMenuEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public HamburgerMenuEx()
{
DefaultStyleKey = typeof(HamburgerMenuEx);
SetResourceReference(DefaultItemFocusVisualStyleProperty, SystemParameters.FocusVisualStyleKey);
SetResourceReference(ClientAreaAnimationProperty, SystemParameters.ClientAreaAnimationKey);
}

#region IsBackButtonVisible
Expand Down Expand Up @@ -314,6 +315,23 @@ private void UpdateSelectedMenuItem()

#endregion

#region ClientAreaAnimation

private static readonly DependencyProperty ClientAreaAnimationProperty =
DependencyProperty.Register(
nameof(ClientAreaAnimation),
typeof(bool),
typeof(HamburgerMenuEx),
new PropertyMetadata(SystemParameters.ClientAreaAnimation));

private bool ClientAreaAnimation
{
get => (bool)GetValue(ClientAreaAnimationProperty);
set => SetValue(ClientAreaAnimationProperty, value);
}

#endregion

/// <summary>
/// Occurs when the back button receives an interaction such as a click or tap.
/// </summary>
Expand Down Expand Up @@ -481,7 +499,7 @@ private void AnimateSelectionChanged(object prevItem, object nextItem)
{
UIElement paneContentGrid = _paneGrid;

if ((prevItem != nextItem) && paneContentGrid != null && prevIndicator != null && nextIndicator != null && SystemParameters.MenuAnimation)
if ((prevItem != nextItem) && paneContentGrid != null && prevIndicator != null && nextIndicator != null && ClientAreaAnimation && RenderCapability.Tier > 0)
{
// Make sure both indicators are visible and in their original locations
ResetElementAnimationProperties(prevIndicator, 1.0);
Expand Down
10 changes: 7 additions & 3 deletions ModernWpf/Transitions/TransitionFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ public TransitionFrame()
: base()
{
InheritanceBehavior = InheritanceBehavior.Default;
SetCurrentValue(TransitionsEnabledProperty, SystemParameters.ClientAreaAnimation && RenderCapability.Tier > 0);

Navigating += OnNavigating;
NavigationStopped += OnNavigationStopped;
Expand Down Expand Up @@ -189,6 +188,11 @@ public bool TransitionsEnabled

#endregion

private bool Animates =>
SystemParameters.ClientAreaAnimation &&
RenderCapability.Tier > 0 &&
TransitionsEnabled;

/// <summary>
/// Flips the logical content presenters to prepare for the next visual
/// transition.
Expand Down Expand Up @@ -235,7 +239,7 @@ private void OnNavigating(object sender, NavigatingCancelEventArgs e)
NavigationOutTransition navigationOutTransition = null;
ITransition oldTransition = null;

if (TransitionsEnabled)
if (Animates)
{
if (Helper.HasDefaultValue(oldElement, TransitionService.NavigationOutTransitionProperty))
{
Expand Down Expand Up @@ -411,7 +415,7 @@ protected override void OnContentChanged(object oldContent, object newContent)
NavigationInTransition navigationInTransition = null;
ITransition newTransition = null;

if (oldElement != null && newElement != null && TransitionsEnabled)
if (oldElement != null && newElement != null && Animates)
{
if (Helper.HasDefaultValue(newElement, TransitionService.NavigationInTransitionProperty))
{
Expand Down
12 changes: 8 additions & 4 deletions samples/SamplesCommon/CrossFadeContentControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ static CrossFadeContentControl()
public CrossFadeContentControl()
: base()
{
SetCurrentValue(IsCrossFadeEnabledProperty, RenderCapability.Tier > 0);
}

#region IsCrossFadeEnabled
Expand All @@ -90,6 +89,11 @@ public bool IsCrossFadeEnabled

#endregion

private bool Animates =>
SystemParameters.ClientAreaAnimation &&
RenderCapability.Tier > 0 &&
IsCrossFadeEnabled;

/// <summary>
/// Flips the logical content presenters to prepare for the next visual
/// transition.
Expand Down Expand Up @@ -162,7 +166,7 @@ protected override void OnContentChanged(object oldContent, object newContent)
FlipPresenters();
}

bool useTransition = oldElement != null && IsCrossFadeEnabled;
bool useTransition = oldElement != null && Animates;

_newContentPresenter.Opacity = useTransition ? 0 : 1;
_newContentPresenter.Visibility = Visibility.Visible;
Expand All @@ -178,10 +182,10 @@ protected override void OnContentChanged(object oldContent, object newContent)
{
_newContentPresenter.Opacity = 1;

_fadeOutAnimation = new DoubleAnimation(1, 0, TimeSpan.FromMilliseconds(300));
_fadeOutAnimation = new DoubleAnimation(1, 0, TimeSpan.FromMilliseconds(300), FillBehavior.Stop);
_fadeOutAnimation.Completed += OnFadeOutAnimationCompleted;

var fadeInAnimation = new DoubleAnimation(0, 1, TimeSpan.FromMilliseconds(300));
var fadeInAnimation = new DoubleAnimation(0, 1, TimeSpan.FromMilliseconds(300), FillBehavior.Stop);

_oldContentPresenter.BeginAnimation(OpacityProperty, _fadeOutAnimation);
_newContentPresenter.BeginAnimation(OpacityProperty, fadeInAnimation);
Expand Down

0 comments on commit 0297e3e

Please sign in to comment.