2020-04-11 06:22:07 +08:00
|
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Services;
|
2020-03-12 01:43:32 +08:00
|
|
|
|
using Microsoft.Xaml.Interactivity;
|
|
|
|
|
|
|
|
|
|
using Windows.UI.Xaml;
|
|
|
|
|
using Windows.UI.Xaml.Controls;
|
|
|
|
|
using Windows.UI.Xaml.Navigation;
|
|
|
|
|
|
|
|
|
|
using WinUI = Microsoft.UI.Xaml.Controls;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.PowerToys.Settings.UI.Behaviors
|
|
|
|
|
{
|
|
|
|
|
public class NavigationViewHeaderBehavior : Behavior<WinUI.NavigationView>
|
|
|
|
|
{
|
2020-04-08 01:19:14 +08:00
|
|
|
|
private static NavigationViewHeaderBehavior current;
|
|
|
|
|
private Page currentPage;
|
2020-03-12 01:43:32 +08:00
|
|
|
|
|
|
|
|
|
public DataTemplate DefaultHeaderTemplate { get; set; }
|
|
|
|
|
|
|
|
|
|
public object DefaultHeader
|
|
|
|
|
{
|
2020-04-11 06:22:07 +08:00
|
|
|
|
get { return GetValue(DefaultHeaderProperty); }
|
|
|
|
|
set { SetValue(DefaultHeaderProperty, value); }
|
2020-03-12 01:43:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-08 01:19:14 +08:00
|
|
|
|
public static readonly DependencyProperty DefaultHeaderProperty = DependencyProperty.Register("DefaultHeader", typeof(object), typeof(NavigationViewHeaderBehavior), new PropertyMetadata(null, (d, e) => current.UpdateHeader()));
|
2020-03-12 01:43:32 +08:00
|
|
|
|
|
|
|
|
|
public static NavigationViewHeaderMode GetHeaderMode(Page item)
|
|
|
|
|
{
|
2020-10-30 05:24:16 +08:00
|
|
|
|
return (NavigationViewHeaderMode)item?.GetValue(HeaderModeProperty);
|
2020-03-12 01:43:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SetHeaderMode(Page item, NavigationViewHeaderMode value)
|
|
|
|
|
{
|
2020-10-30 05:24:16 +08:00
|
|
|
|
item?.SetValue(HeaderModeProperty, value);
|
2020-03-12 01:43:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty HeaderModeProperty =
|
2020-04-08 01:19:14 +08:00
|
|
|
|
DependencyProperty.RegisterAttached("HeaderMode", typeof(bool), typeof(NavigationViewHeaderBehavior), new PropertyMetadata(NavigationViewHeaderMode.Always, (d, e) => current.UpdateHeader()));
|
2020-03-12 01:43:32 +08:00
|
|
|
|
|
|
|
|
|
public static object GetHeaderContext(Page item)
|
|
|
|
|
{
|
2020-10-30 05:24:16 +08:00
|
|
|
|
return item?.GetValue(HeaderContextProperty);
|
2020-03-12 01:43:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SetHeaderContext(Page item, object value)
|
|
|
|
|
{
|
2020-10-30 05:24:16 +08:00
|
|
|
|
item?.SetValue(HeaderContextProperty, value);
|
2020-03-12 01:43:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty HeaderContextProperty =
|
2020-04-08 01:19:14 +08:00
|
|
|
|
DependencyProperty.RegisterAttached("HeaderContext", typeof(object), typeof(NavigationViewHeaderBehavior), new PropertyMetadata(null, (d, e) => current.UpdateHeader()));
|
2020-03-12 01:43:32 +08:00
|
|
|
|
|
|
|
|
|
public static DataTemplate GetHeaderTemplate(Page item)
|
|
|
|
|
{
|
2020-10-30 05:24:16 +08:00
|
|
|
|
return (DataTemplate)item?.GetValue(HeaderTemplateProperty);
|
2020-03-12 01:43:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SetHeaderTemplate(Page item, DataTemplate value)
|
|
|
|
|
{
|
2020-10-30 05:24:16 +08:00
|
|
|
|
item?.SetValue(HeaderTemplateProperty, value);
|
2020-03-12 01:43:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty HeaderTemplateProperty =
|
2020-04-08 01:19:14 +08:00
|
|
|
|
DependencyProperty.RegisterAttached("HeaderTemplate", typeof(DataTemplate), typeof(NavigationViewHeaderBehavior), new PropertyMetadata(null, (d, e) => current.UpdateHeaderTemplate()));
|
2020-03-12 01:43:32 +08:00
|
|
|
|
|
|
|
|
|
protected override void OnAttached()
|
|
|
|
|
{
|
|
|
|
|
base.OnAttached();
|
2020-04-08 01:19:14 +08:00
|
|
|
|
current = this;
|
2020-04-11 06:22:07 +08:00
|
|
|
|
NavigationService.Navigated += OnNavigated;
|
2020-03-12 01:43:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnDetaching()
|
|
|
|
|
{
|
|
|
|
|
base.OnDetaching();
|
2020-04-11 06:22:07 +08:00
|
|
|
|
NavigationService.Navigated -= OnNavigated;
|
2020-03-12 01:43:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnNavigated(object sender, NavigationEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var frame = sender as Frame;
|
|
|
|
|
if (frame.Content is Page page)
|
|
|
|
|
{
|
2020-04-11 06:22:07 +08:00
|
|
|
|
currentPage = page;
|
2020-03-12 01:43:32 +08:00
|
|
|
|
|
2020-04-11 06:22:07 +08:00
|
|
|
|
UpdateHeader();
|
|
|
|
|
UpdateHeaderTemplate();
|
2020-03-12 01:43:32 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateHeader()
|
|
|
|
|
{
|
2020-04-11 06:22:07 +08:00
|
|
|
|
if (currentPage != null)
|
2020-03-12 01:43:32 +08:00
|
|
|
|
{
|
2020-04-11 06:22:07 +08:00
|
|
|
|
var headerMode = GetHeaderMode(currentPage);
|
2020-03-12 01:43:32 +08:00
|
|
|
|
if (headerMode == NavigationViewHeaderMode.Never)
|
|
|
|
|
{
|
2020-04-11 06:22:07 +08:00
|
|
|
|
AssociatedObject.Header = null;
|
|
|
|
|
AssociatedObject.AlwaysShowHeader = false;
|
2020-03-12 01:43:32 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-11 06:22:07 +08:00
|
|
|
|
var headerFromPage = GetHeaderContext(currentPage);
|
2020-03-12 01:43:32 +08:00
|
|
|
|
if (headerFromPage != null)
|
|
|
|
|
{
|
2020-04-11 06:22:07 +08:00
|
|
|
|
AssociatedObject.Header = headerFromPage;
|
2020-03-12 01:43:32 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-11 06:22:07 +08:00
|
|
|
|
AssociatedObject.Header = DefaultHeader;
|
2020-03-12 01:43:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (headerMode == NavigationViewHeaderMode.Always)
|
|
|
|
|
{
|
2020-04-11 06:22:07 +08:00
|
|
|
|
AssociatedObject.AlwaysShowHeader = true;
|
2020-03-12 01:43:32 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-11 06:22:07 +08:00
|
|
|
|
AssociatedObject.AlwaysShowHeader = false;
|
2020-03-12 01:43:32 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateHeaderTemplate()
|
|
|
|
|
{
|
2020-04-11 06:22:07 +08:00
|
|
|
|
if (currentPage != null)
|
2020-03-12 01:43:32 +08:00
|
|
|
|
{
|
2020-04-11 06:22:07 +08:00
|
|
|
|
var headerTemplate = GetHeaderTemplate(currentPage);
|
|
|
|
|
AssociatedObject.HeaderTemplate = headerTemplate ?? DefaultHeaderTemplate;
|
2020-03-12 01:43:32 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|