From 767cdc1f6f6a63ce997fc9476911e2c361f9d402 Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Wed, 20 Feb 2013 20:33:05 -0500 Subject: Pushing missing changes --- MediaBrowser.UI.Controls/TransitionControl.cs | 147 ++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 MediaBrowser.UI.Controls/TransitionControl.cs (limited to 'MediaBrowser.UI.Controls/TransitionControl.cs') diff --git a/MediaBrowser.UI.Controls/TransitionControl.cs b/MediaBrowser.UI.Controls/TransitionControl.cs new file mode 100644 index 0000000000..d1e5ccf0ae --- /dev/null +++ b/MediaBrowser.UI.Controls/TransitionControl.cs @@ -0,0 +1,147 @@ +using Microsoft.Expression.Media.Effects; +using System.ComponentModel; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Media; +using System.Windows.Media.Animation; + +namespace MediaBrowser.UI.Controls +{ + /// + /// http://victorcher.blogspot.com/2012/02/wpf-transactions.html + /// + public class TransitionControl : ContentControl + { + /// + /// The _content presenter + /// + private ContentPresenter _contentPresenter; + + /// + /// Initializes static members of the class. + /// + static TransitionControl() + { + DefaultStyleKeyProperty.OverrideMetadata( + typeof(TransitionControl), new FrameworkPropertyMetadata(typeof(TransitionControl))); + + ContentProperty.OverrideMetadata( + typeof(TransitionControl), new FrameworkPropertyMetadata(OnContentPropertyChanged)); + } + + /// + /// When overridden in a derived class, is invoked whenever application code or internal processes call . + /// + public override void OnApplyTemplate() + { + base.OnApplyTemplate(); + _contentPresenter = (ContentPresenter)Template.FindName("ContentPresenter", this); + } + + #region DP TransitionType + + /// + /// Gets or sets the type of the transition. + /// + /// The type of the transition. + public TransitionEffect TransitionType + { + get { return (TransitionEffect)GetValue(TransitionTypeProperty); } + set { SetValue(TransitionTypeProperty, value); } + } + + // Using a DependencyProperty as the backing store for TransitionType. This enables animation, styling, binding, etc... + /// + /// The transition type property + /// + public static readonly DependencyProperty TransitionTypeProperty = + DependencyProperty.Register("TransitionType", typeof(TransitionEffect), typeof(TransitionControl), + new UIPropertyMetadata(new BlindsTransitionEffect())); + + #endregion DP TransitionType + + #region DP Transition Animation + + /// + /// Gets or sets the transition animation. + /// + /// The transition animation. + public DoubleAnimation TransitionAnimation + { + get { return (DoubleAnimation)GetValue(TransitionAnimationProperty); } + set { SetValue(TransitionAnimationProperty, value); } + } + + // Using a DependencyProperty as the backing store for TransitionAnimation. This enables animation, styling, binding, etc... + /// + /// The transition animation property + /// + public static readonly DependencyProperty TransitionAnimationProperty = + DependencyProperty.Register("TransitionAnimation", typeof(DoubleAnimation), typeof(TransitionControl), new UIPropertyMetadata(null)); + + #endregion DP Transition Animation + + /// + /// Called when [content property changed]. + /// + /// The dp. + /// The instance containing the event data. + private static void OnContentPropertyChanged(DependencyObject dp, DependencyPropertyChangedEventArgs args) + { + var oldContent = args.OldValue; + var newContent = args.NewValue; + + var transitionControl = (TransitionControl)dp; + + if (DesignerProperties.GetIsInDesignMode(transitionControl)) + return; + + if (oldContent != null && newContent != null && transitionControl.IsVisible) + { + transitionControl.AnimateContent(oldContent, newContent); + } + else if (newContent != null) + { + transitionControl.Content = newContent; + } + } + + /// + /// Animates the content. + /// + /// The old content. + /// The new content. + private void AnimateContent(object oldContent, object newContent) + { + FrameworkElement oldContentVisual; + + try + { + oldContentVisual = VisualTreeHelper.GetChild(_contentPresenter, 0) as FrameworkElement; + } + catch + { + return; + } + + var transitionEffect = TransitionType; + + if (transitionEffect == null) + { + _contentPresenter.Content = newContent; + return; + } + + var da = TransitionAnimation; + da.From = 0; + da.To = 1; + da.FillBehavior = FillBehavior.HoldEnd; + + transitionEffect.OldImage = new VisualBrush(oldContentVisual); + transitionEffect.BeginAnimation(TransitionEffect.ProgressProperty, da); + + _contentPresenter.Effect = transitionEffect; + _contentPresenter.Content = newContent; + } + } +} \ No newline at end of file -- cgit v1.2.3