Skip to content

Commit

Permalink
Make SplitView transitions smoother
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinnara committed Apr 20, 2020
1 parent 2f4b9df commit fb9e75c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
1 change: 1 addition & 0 deletions ModernWpf.Controls/SplitView/SplitView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public override void OnApplyTemplate()
{
_displayModeStates.CurrentStateChanging += OnDisplayModeStatesCurrentStateChanging;
_displayModeStates.CurrentStateChanged += OnDisplayModeStatesCurrentStateChanged;
AnimationHelper.DeferTransitions(_displayModeStates);
}

UpdateTemplateSettings();
Expand Down
11 changes: 1 addition & 10 deletions ModernWpf.MahApps/HamburgerMenu/HamburgerMenuEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using SplitView = MahApps.Metro.Controls.SplitView;
using SplitViewDisplayMode = MahApps.Metro.Controls.SplitViewDisplayMode;

Expand Down Expand Up @@ -788,16 +787,8 @@ private void PlayIndicatorAnimations(UIElement indicator, double from, double to

var indicatorAsFE = (FrameworkElement)indicator;
SetElementAnimation(indicatorAsFE, storyboard);
AnimationHelper.DeferBegin(storyboard);
storyboard.Begin(indicatorAsFE, true);
storyboard.Pause(indicatorAsFE);
Dispatcher.BeginInvoke(() =>
{
var animation = GetElementAnimation(indicatorAsFE);
if (animation == storyboard)
{
animation.Resume(indicatorAsFE);
}
}, DispatcherPriority.Render);
}

private void OnAnimationComplete()
Expand Down
47 changes: 47 additions & 0 deletions ModernWpf/Helpers/AnimationHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Threading;

namespace ModernWpf
{
internal static class AnimationHelper
{
public static void DeferBegin(Storyboard storyboard)
{
storyboard.CurrentStateInvalidated += OnStoryboardCurrentStateInvalidated;

static void OnStoryboardCurrentStateInvalidated(object sender, EventArgs e)
{
if (sender is ClockGroup clock &&
clock.HasControllableRoot &&
clock.CurrentState == ClockState.Active &&
!clock.IsPaused)
{
clock.Controller.Pause();
clock.Dispatcher.BeginInvoke(() =>
{
Debug.Assert(clock.IsPaused || clock.CurrentState != ClockState.Active);
if (clock.IsPaused)
{
clock.Controller.Resume();
}
}, DispatcherPriority.Loaded);
}
}
}

public static void DeferTransitions(VisualStateGroup group)
{
foreach (VisualTransition transition in group.Transitions)
{
var storyboard = transition.Storyboard;
if (storyboard != null)
{
DeferBegin(storyboard);
}
}
}
}
}
1 change: 1 addition & 0 deletions ModernWpf/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
)]

[assembly: InternalsVisibleTo("ModernWpf.Controls")]
[assembly: InternalsVisibleTo("ModernWpf.MahApps")]

[assembly: XmlnsPrefix("http://schemas.modernwpf.com/2019", "ui")]
[assembly: XmlnsDefinition("http://schemas.modernwpf.com/2019", "ModernWpf")]
Expand Down

0 comments on commit fb9e75c

Please sign in to comment.