using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
namespace MaterialDesignThemes.Wpf.Transitions
{
///
/// The transitioner provides an easy way to move between content with a default in-place circular transition.
///
public class Transitioner : Selector, IZIndexController
{
private Point? _nextTransitionOrigin;
static Transitioner()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(Transitioner), new FrameworkPropertyMetadata(typeof(Transitioner)));
}
///
/// Causes the the next slide to be displayed (affectively increments ).
///
public static RoutedCommand MoveNextCommand = new RoutedCommand();
///
/// Causes the the previous slide to be displayed (affectively decrements ).
///
public static RoutedCommand MovePreviousCommand = new RoutedCommand();
///
/// Moves to the first slide.
///
public static RoutedCommand MoveFirstCommand = new RoutedCommand();
///
/// Moves to the last slide.
///
public static RoutedCommand MoveLastCommand = new RoutedCommand();
public static readonly DependencyProperty AutoApplyTransitionOriginsProperty = DependencyProperty.Register(
"AutoApplyTransitionOrigins", typeof (bool), typeof (Transitioner), new PropertyMetadata(default(bool)));
///
/// If enabled, transition origins will be applied to wipes, according to where a transition was triggered from. For example, the mouse point where a user clicks a button.
///
public bool AutoApplyTransitionOrigins
{
get { return (bool) GetValue(AutoApplyTransitionOriginsProperty); }
set { SetValue(AutoApplyTransitionOriginsProperty, value); }
}
public static readonly DependencyProperty DefaultTransitionOriginProperty = DependencyProperty.Register(
"DefaultTransitionOrigin", typeof(Point), typeof(Transitioner), new PropertyMetadata(new Point(0.5, 0.5)));
public Point DefaultTransitionOrigin
{
get { return (Point)GetValue(DefaultTransitionOriginProperty); }
set { SetValue(DefaultTransitionOriginProperty, value); }
}
public Transitioner()
{
CommandBindings.Add(new CommandBinding(MoveNextCommand, MoveNextHandler));
CommandBindings.Add(new CommandBinding(MovePreviousCommand, MovePreviousHandler));
CommandBindings.Add(new CommandBinding(MoveFirstCommand, MoveFirstHandler));
CommandBindings.Add(new CommandBinding(MoveLastCommand, MoveLastHandler));
AddHandler(TransitionerSlide.InTransitionFinished, new RoutedEventHandler(IsTransitionFinishedHandler));
Loaded += (sender, args) =>
{
if (SelectedIndex != -1)
ActivateFrame(SelectedIndex, -1);
};
}
protected override bool IsItemItsOwnContainerOverride(object item)
{
return item is TransitionerSlide;
}
protected override DependencyObject GetContainerForItemOverride()
{
return new TransitionerSlide();
}
protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
{
if (AutoApplyTransitionOrigins)
_nextTransitionOrigin = GetNavigationSourcePoint(e);
base.OnPreviewMouseLeftButtonDown(e);
}
protected override void OnSelectionChanged(SelectionChangedEventArgs e)
{
var unselectedIndex = -1;
if (e.RemovedItems.Count == 1)
{
unselectedIndex = Items.IndexOf(e.RemovedItems[0]);
}
var selectedIndex = 1;
if (e.AddedItems.Count == 1)
{
selectedIndex = Items.IndexOf(e.AddedItems[0]);
}
ActivateFrame(selectedIndex, unselectedIndex);
base.OnSelectionChanged(e);
}
private void IsTransitionFinishedHandler(object sender, RoutedEventArgs routedEventArgs)
{
foreach (var slide in Items.OfType