Skip to content

Commit 9ef05e0

Browse files
Fix #3240 - Fixed Visibility bug on FadeWipe class (#3621)
1 parent fe81676 commit 9ef05e0

File tree

1 file changed

+54
-27
lines changed

1 file changed

+54
-27
lines changed

src/MaterialDesignThemes.Wpf/Transitions/FadeWipe.cs

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,77 @@ namespace MaterialDesignThemes.Wpf.Transitions;
44

55
public class FadeWipe : ITransitionWipe
66
{
7-
private readonly SineEase _sineEase = new SineEase();
8-
private readonly KeyTime zeroKeyTime = KeyTime.FromTimeSpan(TimeSpan.Zero);
7+
private readonly SineEase sineEase = new();
8+
private KeyTime startKeyTime, endKeyTime;
9+
10+
private TimeSpan _duration = TimeSpan.FromMilliseconds(500);
11+
12+
public FadeWipe()
13+
{
14+
startKeyTime = KeyTime.FromTimeSpan(TimeSpan.Zero);
15+
CalculateEndKeyTime();
16+
}
917

1018
/// <summary>
1119
/// Duration of the animation
1220
/// </summary>
13-
public TimeSpan Duration { get; set; } = TimeSpan.FromSeconds(0.5);
21+
public TimeSpan Duration
22+
{
23+
get => _duration;
24+
set
25+
{
26+
_duration = value;
27+
CalculateEndKeyTime();
28+
}
29+
}
1430

1531
public void Wipe(TransitionerSlide fromSlide, TransitionerSlide toSlide, Point origin, IZIndexController zIndexController)
1632
{
17-
if (fromSlide == null) throw new ArgumentNullException(nameof(fromSlide));
18-
if (toSlide == null) throw new ArgumentNullException(nameof(toSlide));
19-
if (zIndexController == null) throw new ArgumentNullException(nameof(zIndexController));
20-
21-
// Set up time points
22-
var endKeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(Duration.TotalSeconds / 2));
33+
if (fromSlide is null)
34+
{
35+
throw new ArgumentNullException(nameof(fromSlide));
36+
}
37+
if (toSlide is null)
38+
{
39+
throw new ArgumentNullException(nameof(toSlide));
40+
}
41+
if (zIndexController is null)
42+
{
43+
throw new ArgumentNullException(nameof(zIndexController));
44+
}
2345

24-
// From
25-
var fromAnimation = new DoubleAnimationUsingKeyFrames();
26-
fromAnimation.KeyFrames.Add(new LinearDoubleKeyFrame(1, zeroKeyTime));
27-
fromAnimation.KeyFrames.Add(new EasingDoubleKeyFrame(0, endKeyTime, _sineEase));
46+
// Remove current animations and reset to base value
47+
double currentFromOpacity = fromSlide.Opacity;
48+
fromSlide.BeginAnimation(UIElement.OpacityProperty, null);
49+
fromSlide.Opacity = currentFromOpacity;
2850

29-
// To
30-
var toAnimation = new DoubleAnimationUsingKeyFrames();
31-
toAnimation.KeyFrames.Add(new LinearDoubleKeyFrame(0, zeroKeyTime));
32-
toAnimation.KeyFrames.Add(new EasingDoubleKeyFrame(1, endKeyTime, _sineEase));
51+
double currentToOpacity = toSlide.Opacity;
52+
toSlide.BeginAnimation(UIElement.OpacityProperty, null);
53+
toSlide.Opacity = currentToOpacity != 1 ? currentToOpacity : 0;
3354

34-
// Preset
35-
fromSlide.Opacity = 1;
36-
toSlide.Opacity = 0;
55+
zIndexController.Stack(toSlide, fromSlide);
56+
DoubleAnimationUsingKeyFrames fromAnimation = SetupOpacityAnimation(currentFromOpacity, 0);
57+
DoubleAnimationUsingKeyFrames toAnimation = SetupOpacityAnimation(currentToOpacity, 1);
3758

38-
// Set up events
39-
toAnimation.Completed += (sender, args) =>
40-
{
41-
toSlide.BeginAnimation(UIElement.OpacityProperty, null);
42-
};
4359
fromAnimation.Completed += (sender, args) =>
4460
{
4561
fromSlide.BeginAnimation(UIElement.OpacityProperty, null);
4662
toSlide.BeginAnimation(UIElement.OpacityProperty, toAnimation);
4763
};
4864

49-
// Animate
5065
fromSlide.BeginAnimation(UIElement.OpacityProperty, fromAnimation);
51-
zIndexController.Stack(toSlide, fromSlide);
66+
}
67+
68+
private DoubleAnimationUsingKeyFrames SetupOpacityAnimation(double from, double to)
69+
{
70+
DoubleAnimationUsingKeyFrames animation = new();
71+
animation.KeyFrames.Add(new LinearDoubleKeyFrame(from, startKeyTime));
72+
animation.KeyFrames.Add(new EasingDoubleKeyFrame(to, endKeyTime, sineEase));
73+
return animation;
74+
}
75+
76+
private void CalculateEndKeyTime()
77+
{
78+
endKeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(Duration.TotalSeconds / 2));
5279
}
5380
}

0 commit comments

Comments
 (0)