// -----------------------------------------------------------------------
//
// Copyright 2015 MIT Licence. See licence.md for more information.
//
// -----------------------------------------------------------------------
namespace Perspex.Animation
{
using System;
using System.Collections.Generic;
using System.Reactive.Threading.Tasks;
using System.Threading.Tasks;
///
/// Defines a cross-fade animation between two s.
///
public class CrossFade : IPageTransition
{
///
/// Initializes a new instance of the class.
///
/// The duration of the animation.
public CrossFade(TimeSpan duration)
{
this.Duration = duration;
}
///
/// Gets the duration of the animation.
///
public TimeSpan Duration { get; }
///
/// Starts the animation.
///
///
/// The control that is being transitioned away from. May be null.
///
///
/// The control that is being transitioned to. May be null.
///
///
/// A that tracks the progress of the animation.
///
public async Task Start(IVisual from, IVisual to)
{
var tasks = new List();
if (to != null)
{
to.Opacity = 0;
}
if (from != null)
{
tasks.Add(Animate.Property(
(IObservablePropertyBag)from,
Visual.OpacityProperty,
from.Opacity,
0,
LinearEasing.For(),
this.Duration).ToTask());
}
if (to != null)
{
to.Opacity = 0;
to.IsVisible = true;
tasks.Add(Animate.Property(
(IObservablePropertyBag)to,
Visual.OpacityProperty,
0,
1,
LinearEasing.For(),
this.Duration).ToTask());
}
await Task.WhenAll(tasks.ToArray());
if (from != null)
{
from.IsVisible = false;
from.Opacity = 1;
}
to.Opacity = 1;
}
///
/// Starts the animation.
///
///
/// The control that is being transitioned away from. May be null.
///
///
/// The control that is being transitioned to. May be null.
///
///
/// Unused for cross-fades.
///
///
/// A that tracks the progress of the animation.
///
Task IPageTransition.Start(IVisual from, IVisual to, bool forward)
{
return this.Start(from, to);
}
}
}