/* Based on Windows UI Library */
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Navigation;
using WPFluent.Abstractions;
using WPFluent.Animations;
// ReSharper disable once CheckNamespace
namespace WPFluent.Controls;
public class NavigationViewContentPresenter : Frame
{
///
/// Identifies the dependency property.
///
public static readonly DependencyProperty IsDynamicScrollViewerEnabledProperty =
DependencyProperty.Register(
nameof(IsDynamicScrollViewerEnabled),
typeof(bool),
typeof(NavigationViewContentPresenter),
new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsMeasure));
///
/// Identifies the dependency property.
///
public static readonly DependencyProperty TransitionDurationProperty = DependencyProperty.Register(
nameof(TransitionDuration),
typeof(int),
typeof(NavigationViewContentPresenter),
new FrameworkPropertyMetadata(200));
///
/// Identifies the dependency property.
///
public static readonly DependencyProperty TransitionProperty = DependencyProperty.Register(
nameof(Transition),
typeof(Transition),
typeof(NavigationViewContentPresenter),
new FrameworkPropertyMetadata(Transition.FadeInWithSlide));
static NavigationViewContentPresenter()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof(NavigationViewContentPresenter),
new FrameworkPropertyMetadata(typeof(NavigationViewContentPresenter)));
NavigationUIVisibilityProperty.OverrideMetadata(
typeof(NavigationViewContentPresenter),
new FrameworkPropertyMetadata(NavigationUIVisibility.Hidden));
SandboxExternalContentProperty.OverrideMetadata(
typeof(NavigationViewContentPresenter),
new FrameworkPropertyMetadata(true));
JournalOwnershipProperty.OverrideMetadata(
typeof(NavigationViewContentPresenter),
new FrameworkPropertyMetadata(JournalOwnership.UsesParentJournal));
ScrollViewer.CanContentScrollProperty.OverrideMetadata(typeof(Page), new FrameworkPropertyMetadata(true));
}
public NavigationViewContentPresenter()
{
Navigating += static (sender, eventArgs) =>
{
if (eventArgs.Content is null)
{
return;
}
var self = (NavigationViewContentPresenter)sender;
self.OnNavigating(eventArgs);
};
Navigated += static (sender, eventArgs) =>
{
var self = (NavigationViewContentPresenter)sender;
if (eventArgs.Content is null)
{
return;
}
self.OnNavigated(eventArgs);
};
}
private void ApplyTransitionEffectToNavigatedPage(object content)
{
if (TransitionDuration < 1)
{
return;
}
TransitionAnimationProvider.ApplyTransition(content, Transition, TransitionDuration);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage(
"ReSharper",
"SuspiciousTypeConversion.Global",
Justification = "The library user might make a class inherit from both FrameworkElement and INavigationAware at the same time.")]
private static void NotifyContentAboutNavigating(object content, Func function)
{
// The order in which the OnNavigatedToAsync/OnNavigatedFromAsync methods of View and ViewModel are called
// is not guaranteed
if (content is INavigationAware navigationAwareContent)
{
function(navigationAwareContent);
}
if (content is INavigableView