Browse Source

Merge pull request #1795 from jkoritzinsky/pagetransitions

Reimplement Page Transitions
pull/1796/head
Jumar Macato 8 years ago
committed by GitHub
parent
commit
2f803cb504
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      src/Avalonia.Controls/Expander.cs
  2. 60
      src/Avalonia.Visuals/Animation/CrossFade.cs
  3. 2
      src/Avalonia.Visuals/Animation/IPageTransition.cs
  4. 64
      src/Avalonia.Visuals/Animation/PageSlide.cs

6
src/Avalonia.Controls/Expander.cs

@ -66,9 +66,7 @@ namespace Avalonia.Controls
protected virtual void OnIsExpandedChanged(AvaloniaPropertyChangedEventArgs e)
{
IVisual visualContent = Presenter;
if (Content != null && ContentTransition != null && visualContent != null)
if (Content != null && ContentTransition != null && Presenter is Visual visualContent)
{
bool forward = ExpandDirection == ExpandDirection.Left ||
ExpandDirection == ExpandDirection.Up;
@ -87,4 +85,4 @@ namespace Avalonia.Controls
private ExpandDirection _expandDirection;
private bool _isExpanded;
}
}
}

60
src/Avalonia.Visuals/Animation/CrossFade.cs

@ -5,6 +5,7 @@ using System;
using System.Collections.Generic;
using System.Reactive.Threading.Tasks;
using System.Threading.Tasks;
using Avalonia.Styling;
using Avalonia.VisualTree;
namespace Avalonia.Animation
@ -14,10 +15,14 @@ namespace Avalonia.Animation
/// </summary>
public class CrossFade : IPageTransition
{
private Animation _fadeOutAnimation;
private Animation _fadeInAnimation;
/// <summary>
/// Initializes a new instance of the <see cref="CrossFade"/> class.
/// </summary>
public CrossFade()
:this(TimeSpan.Zero)
{
}
@ -27,13 +32,51 @@ namespace Avalonia.Animation
/// <param name="duration">The duration of the animation.</param>
public CrossFade(TimeSpan duration)
{
Duration = duration;
_fadeOutAnimation = new Animation
{
new KeyFrame
(
new Setter
{
Property = Visual.OpacityProperty,
Value = 0.0
}
)
{
Cue = new Cue(1.0)
}
};
_fadeInAnimation = new Animation
{
new KeyFrame
(
new Setter
{
Property = Visual.OpacityProperty,
Value = 0.0
}
)
{
Cue = new Cue(0.0)
}
};
_fadeOutAnimation.Duration = _fadeInAnimation.Duration = duration;
}
/// <summary>
/// Gets the duration of the animation.
/// </summary>
public TimeSpan Duration { get; set; }
public TimeSpan Duration
{
get
{
return _fadeOutAnimation.Duration;
}
set
{
_fadeOutAnimation.Duration = _fadeInAnimation.Duration = value;
}
}
/// <summary>
/// Starts the animation.
@ -47,12 +90,10 @@ namespace Avalonia.Animation
/// <returns>
/// A <see cref="Task"/> that tracks the progress of the animation.
/// </returns>
public async Task Start(IVisual from, IVisual to)
public async Task Start(Visual from, Visual to)
{
var tasks = new List<Task>();
// TODO: Implement relevant transition logic here (or discard this class)
// in favor of XAML based transition for pages
if (to != null)
{
to.Opacity = 0;
@ -60,22 +101,21 @@ namespace Avalonia.Animation
if (from != null)
{
tasks.Add(_fadeOutAnimation.RunAsync(from));
}
if (to != null)
{
to.Opacity = 0;
to.IsVisible = true;
tasks.Add(_fadeInAnimation.RunAsync(to));
}
// FIXME: This is temporary until animations are fixed.
await Task.Delay(1);
await Task.WhenAll(tasks);
if (from != null)
{
from.IsVisible = false;
from.Opacity = 1;
}
if (to != null)
@ -99,7 +139,7 @@ namespace Avalonia.Animation
/// <returns>
/// A <see cref="Task"/> that tracks the progress of the animation.
/// </returns>
Task IPageTransition.Start(IVisual from, IVisual to, bool forward)
Task IPageTransition.Start(Visual from, Visual to, bool forward)
{
return Start(from, to);
}

2
src/Avalonia.Visuals/Animation/IPageTransition.cs

@ -26,6 +26,6 @@ namespace Avalonia.Animation
/// <returns>
/// A <see cref="Task"/> that tracks the progress of the animation.
/// </returns>
Task Start(IVisual from, IVisual to, bool forward);
Task Start(Visual from, Visual to, bool forward);
}
}

64
src/Avalonia.Visuals/Animation/PageSlide.cs

@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Reactive.Threading.Tasks;
using System.Threading.Tasks;
using Avalonia.Media;
using Avalonia.Styling;
using Avalonia.VisualTree;
namespace Avalonia.Animation
@ -67,7 +68,7 @@ namespace Avalonia.Animation
/// <returns>
/// A <see cref="Task"/> that tracks the progress of the animation.
/// </returns>
public async Task Start(IVisual from, IVisual to, bool forward)
public async Task Start(Visual from, Visual to, bool forward)
{
var tasks = new List<Task>();
var parent = GetVisualParent(from, to);
@ -79,16 +80,69 @@ namespace Avalonia.Animation
// in favor of XAML based transition for pages
if (from != null)
{
var animation = new Animation
{
new KeyFrame
(
new Setter
{
Property = translateProperty,
Value = 0
}
)
{
Cue = new Cue(0.0)
},
new KeyFrame
(
new Setter
{
Property = translateProperty,
Value = forward ? -distance : distance
}
)
{
Cue = new Cue(1.0)
}
};
animation.Duration = Duration;
tasks.Add(animation.RunAsync(from));
}
if (to != null)
{
to.IsVisible = true;
var animation = new Animation
{
new KeyFrame
(
new Setter
{
Property = translateProperty,
Value = forward ? -distance : distance
}
)
{
Cue = new Cue(0.0)
},
new KeyFrame
(
new Setter
{
Property = translateProperty,
Value = 0
}
)
{
Cue = new Cue(1.0)
},
};
animation.Duration = Duration;
tasks.Add(animation.RunAsync(to));
}
// FIXME: This is temporary until animations are fixed.
await Task.Delay(1);
await Task.WhenAll(tasks);
if (from != null)
{

Loading…
Cancel
Save