Skip to content

Commit

Permalink
Try to prevent flicker when playing NavView indicator animations
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinnara committed May 4, 2020
1 parent 6387566 commit 19e8659
Showing 1 changed file with 34 additions and 34 deletions.
68 changes: 34 additions & 34 deletions ModernWpf.Controls/NavigationView/NavigationView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1667,7 +1667,7 @@ void AnimateSelectionChanged(object nextItem)
{
if (prevIndicator != null && prevIndicator != m_prevIndicator)
{
ResetElementAnimationProperties(prevIndicator, 0.0f);
ResetElementAnimationProperties(prevIndicator, 0.0);
}
haveValidAnimation = true;
}
Expand All @@ -1685,8 +1685,8 @@ void AnimateSelectionChanged(object nextItem)
if ((prevIndicator != nextIndicator) && paneContentGrid != null && prevIndicator != null && nextIndicator != null && SharedHelpers.IsAnimationsEnabled)
{
// Make sure both indicators are visible and in their original locations
ResetElementAnimationProperties(prevIndicator, 1.0f);
ResetElementAnimationProperties(nextIndicator, 1.0f);
ResetElementAnimationProperties(prevIndicator, 1.0);
ResetElementAnimationProperties(nextIndicator, 1.0);

// get the item positions in the pane
Point point = new Point(0, 0);
Expand Down Expand Up @@ -1740,8 +1740,8 @@ void AnimateSelectionChanged(object nextItem)
else
{

float outgoingEndPosition = (float)(nextPos - prevPos);
float incomingStartPosition = (float)(prevPos - nextPos);
double outgoingEndPosition = nextPos - prevPos;
double incomingStartPosition = prevPos - nextPos;

// Play the animation on both the previous and next indicators
PlayIndicatorAnimations(prevIndicator,
Expand Down Expand Up @@ -1775,8 +1775,8 @@ void AnimateSelectionChanged(object nextItem)
else
{
// if all else fails, or if animations are turned off, attempt to correctly set the positions and opacities of the indicators.
ResetElementAnimationProperties(prevIndicator, 0.0f);
ResetElementAnimationProperties(nextIndicator, 1.0f);
ResetElementAnimationProperties(prevIndicator, 0.0);
ResetElementAnimationProperties(nextIndicator, 1.0);
}

m_activeIndicator = nextIndicator;
Expand All @@ -1788,8 +1788,8 @@ void PlayIndicatorNonSameLevelAnimations(UIElement indicator, bool isOutgoing, b
var animations = new TimelineCollection();

// Determine scaling of indicator (whether it is appearing or dissapearing)
float beginScale = isOutgoing ? 1.0f : 0.0f;
float endScale = isOutgoing ? 0.0f : 1.0f;
double beginScale = isOutgoing ? 1.0 : 0.0;
double endScale = isOutgoing ? 0.0 : 1.0;
var scaleAnim = new DoubleAnimationUsingKeyFrames
{
KeyFrames =
Expand All @@ -1804,7 +1804,7 @@ void PlayIndicatorNonSameLevelAnimations(UIElement indicator, bool isOutgoing, b
// Determine where the indicator is animating from/to
Size size = indicator.RenderSize;
double dimension = IsTopNavigationView() ? size.Width : size.Height;
double newCenter = fromTop ? 0.0f : dimension;
double newCenter = fromTop ? 0.0 : dimension;
var indicatorCenterPoint = new Point();
indicatorCenterPoint.Y = newCenter;

Expand All @@ -1817,8 +1817,8 @@ void PlayIndicatorNonSameLevelTopPrimaryAnimation(UIElement indicator, bool isOu
var animations = new TimelineCollection();

// Determine scaling of indicator (whether it is appearing or dissapearing)
float beginScale = isOutgoing ? 1.0f : 0.0f;
float endScale = isOutgoing ? 0.0f : 1.0f;
double beginScale = isOutgoing ? 1.0 : 0.0;
double endScale = isOutgoing ? 0.0 : 1.0;
var scaleAnim = new DoubleAnimationUsingKeyFrames
{
KeyFrames =
Expand All @@ -1840,7 +1840,7 @@ void PlayIndicatorNonSameLevelTopPrimaryAnimation(UIElement indicator, bool isOu
PlayIndicatorAnimationsHelper(indicator, animations, indicatorCenterPoint);
}

void PlayIndicatorAnimations(UIElement indicator, float from, float to, Size beginSize, Size endSize, bool isOutgoing)
void PlayIndicatorAnimations(UIElement indicator, double from, double to, Size beginSize, Size endSize, bool isOutgoing)
{
Size size = indicator.RenderSize;
double dimension = IsTopNavigationView() ? size.Width : size.Height;
Expand All @@ -1855,23 +1855,6 @@ void PlayIndicatorAnimations(UIElement indicator, float from, float to, Size beg

var animations = new TimelineCollection();

if (isOutgoing)
{
// fade the outgoing indicator so it looks nice when animating over the scroll area
var opacityAnim = new DoubleAnimationUsingKeyFrames
{
KeyFrames =
{
new DiscreteDoubleKeyFrame(1.0, KeyTime.FromPercent(0.0)),
new DiscreteDoubleKeyFrame(1.0, KeyTime.FromPercent(0.333)),
new SplineDoubleKeyFrame(0.0, KeyTime.FromPercent(1.0), new KeySpline(c_frame2point1, c_frame2point2)),
},
Duration = TimeSpan.FromMilliseconds(600)
};
Storyboard.SetTargetProperty(opacityAnim, s_opacityPath);
animations.Add(opacityAnim);
}

var posAnim = new DoubleAnimationUsingKeyFrames
{
KeyFrames =
Expand Down Expand Up @@ -1909,6 +1892,23 @@ void PlayIndicatorAnimations(UIElement indicator, float from, float to, Size beg
};
animations.Add(centerAnim);

if (isOutgoing)
{
// fade the outgoing indicator so it looks nice when animating over the scroll area
var opacityAnim = new DoubleAnimationUsingKeyFrames
{
KeyFrames =
{
new DiscreteDoubleKeyFrame(1.0, KeyTime.FromPercent(0.0)),
new DiscreteDoubleKeyFrame(1.0, KeyTime.FromPercent(0.333)),
new SplineDoubleKeyFrame(0.0, KeyTime.FromPercent(1.0), new KeySpline(c_frame2point1, c_frame2point2)),
},
Duration = TimeSpan.FromMilliseconds(600)
};
Storyboard.SetTargetProperty(opacityAnim, s_opacityPath);
animations.Add(opacityAnim);
}

if (IsTopNavigationView())
{
Storyboard.SetTargetProperty(posAnim, s_translateXPath);
Expand Down Expand Up @@ -1968,15 +1968,15 @@ void PlayIndicatorAnimationsHelper(UIElement indicator, TimelineCollection anima
void OnAnimationComplete(object sender, EventArgs args)
{
var indicator = m_prevIndicator;
ResetElementAnimationProperties(indicator, 0.0f);
ResetElementAnimationProperties(indicator, 0.0);
m_prevIndicator = null;

indicator = m_nextIndicator;
ResetElementAnimationProperties(indicator, 1.0f);
ResetElementAnimationProperties(indicator, 1.0);
m_nextIndicator = null;
}

void ResetElementAnimationProperties(UIElement element, float desiredOpacity)
void ResetElementAnimationProperties(UIElement element, double desiredOpacity)
{
if (element != null)
{
Expand Down Expand Up @@ -4330,7 +4330,7 @@ void UpdateTitleBarPadding()
GeneralTransform gt = this.SafeTransformToVisual(root);
Point pos = gt.Transform(new Point());

if (pos.Y == 0.0f)
if (pos.Y == 0.0)
{
topPadding = coreTitleBar.Height;
}
Expand Down

0 comments on commit 19e8659

Please sign in to comment.