PowerToys/src/settings-ui/Microsoft.PowerToys.Settings.UI/Behaviors/NavigationViewHeaderBehavior.cs
Andrey Nekrasov 212ea2de30
common: refactor common library pt2 (#8588)
- remove common lib
- split settings, remove common-md
- move ipc interop/kb_layout to interop
- rename core -> settings, settings -> old_settings
- os-detect header-only; interop -> PowerToysInterop
- split notifications, move single-use headers where they're used
- winstore lib
- rename com utils
- rename Updating and Telemetry projects
- rename core -> settings-ui and remove examples folder
- rename settings-ui folder + consisent common/version include
2020-12-15 15:16:09 +03:00

139 lines
4.8 KiB
C#

// 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;
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>
{
private static NavigationViewHeaderBehavior current;
private Page currentPage;
public DataTemplate DefaultHeaderTemplate { get; set; }
public object DefaultHeader
{
get { return GetValue(DefaultHeaderProperty); }
set { SetValue(DefaultHeaderProperty, value); }
}
public static readonly DependencyProperty DefaultHeaderProperty = DependencyProperty.Register("DefaultHeader", typeof(object), typeof(NavigationViewHeaderBehavior), new PropertyMetadata(null, (d, e) => current.UpdateHeader()));
public static NavigationViewHeaderMode GetHeaderMode(Page item)
{
return (NavigationViewHeaderMode)item?.GetValue(HeaderModeProperty);
}
public static void SetHeaderMode(Page item, NavigationViewHeaderMode value)
{
item?.SetValue(HeaderModeProperty, value);
}
public static readonly DependencyProperty HeaderModeProperty =
DependencyProperty.RegisterAttached("HeaderMode", typeof(bool), typeof(NavigationViewHeaderBehavior), new PropertyMetadata(NavigationViewHeaderMode.Always, (d, e) => current.UpdateHeader()));
public static object GetHeaderContext(Page item)
{
return item?.GetValue(HeaderContextProperty);
}
public static void SetHeaderContext(Page item, object value)
{
item?.SetValue(HeaderContextProperty, value);
}
public static readonly DependencyProperty HeaderContextProperty =
DependencyProperty.RegisterAttached("HeaderContext", typeof(object), typeof(NavigationViewHeaderBehavior), new PropertyMetadata(null, (d, e) => current.UpdateHeader()));
public static DataTemplate GetHeaderTemplate(Page item)
{
return (DataTemplate)item?.GetValue(HeaderTemplateProperty);
}
public static void SetHeaderTemplate(Page item, DataTemplate value)
{
item?.SetValue(HeaderTemplateProperty, value);
}
public static readonly DependencyProperty HeaderTemplateProperty =
DependencyProperty.RegisterAttached("HeaderTemplate", typeof(DataTemplate), typeof(NavigationViewHeaderBehavior), new PropertyMetadata(null, (d, e) => current.UpdateHeaderTemplate()));
protected override void OnAttached()
{
base.OnAttached();
current = this;
NavigationService.Navigated += OnNavigated;
}
protected override void OnDetaching()
{
base.OnDetaching();
NavigationService.Navigated -= OnNavigated;
}
private void OnNavigated(object sender, NavigationEventArgs e)
{
var frame = sender as Frame;
if (frame.Content is Page page)
{
currentPage = page;
UpdateHeader();
UpdateHeaderTemplate();
}
}
private void UpdateHeader()
{
if (currentPage != null)
{
var headerMode = GetHeaderMode(currentPage);
if (headerMode == NavigationViewHeaderMode.Never)
{
AssociatedObject.Header = null;
AssociatedObject.AlwaysShowHeader = false;
}
else
{
var headerFromPage = GetHeaderContext(currentPage);
if (headerFromPage != null)
{
AssociatedObject.Header = headerFromPage;
}
else
{
AssociatedObject.Header = DefaultHeader;
}
if (headerMode == NavigationViewHeaderMode.Always)
{
AssociatedObject.AlwaysShowHeader = true;
}
else
{
AssociatedObject.AlwaysShowHeader = false;
}
}
}
}
private void UpdateHeaderTemplate()
{
if (currentPage != null)
{
var headerTemplate = GetHeaderTemplate(currentPage);
AssociatedObject.HeaderTemplate = headerTemplate ?? DefaultHeaderTemplate;
}
}
}
}