Files
PowerToys/src/settings-ui/Settings.UI/Behaviors/NavigationViewHeaderBehavior.cs

138 lines
4.8 KiB
C#
Raw Normal View History

// 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;
[settings-ui] Settings WinUI3 (#17797) * Add Settings.WinUI3 project * New namespace * Activation and Services * Assets and Behaviors * Converters and Helpers * Controls * View and ViewModels * Styles and Themes * OOBE * Strings * Small App moves * [check] Project files - publish profiles and launchSettings.json * [using:Microsoft.UI.Xaml.Automation]AutomationProperties.Name workaround * [WIP] Workarounds to make it work * Fix suppressed warnings - naming * Add code analysis * Fix KBMPage and App dispatcher Fix MessageBox - replace with MessageDialog * Fix ImageResizerPage & mark ColorPickerButton with TODO * Add icon to windows Cleanup MainWindow.xaml.cs and OobeWindow.xaml.cs MainWindows and OobeWindow management * App Icon No framework and runtime subdirs * Remove PowerToys.Settings and Settings.UI from solution Update output paths * Installer work & publish.cmd * Fix dispatcher crashes * Fix crashes * Add all dlls to installer Cleanup installer Add OpenOOBE and OpenScoobe logic Fix minor issues Fix update scenario - REINSTALLMODE * Rename back namespaces, project name and project dir * [wip] move to winappsdk 1.1 * Fix propagating isElevated & installer runtimes dlls * Remove obsolete dir/file * PowerToys.Interop to netstandard2.0 * Move everything to .Net6 * [Settings] Always launch settings process non-elevated (#17791) * Move back to WinAppSdk 1.0.1 * Add Settings.WinUI3 project * New namespace * Activation and Services * Assets and Behaviors * Converters and Helpers * Controls * View and ViewModels * Styles and Themes * OOBE * Strings * Small App moves * [check] Project files - publish profiles and launchSettings.json * [using:Microsoft.UI.Xaml.Automation]AutomationProperties.Name workaround * [WIP] Workarounds to make it work * Fix suppressed warnings - naming * Add code analysis * Fix KBMPage and App dispatcher Fix MessageBox - replace with MessageDialog * Fix ImageResizerPage & mark ColorPickerButton with TODO * Add icon to windows Cleanup MainWindow.xaml.cs and OobeWindow.xaml.cs MainWindows and OobeWindow management * App Icon No framework and runtime subdirs * Remove PowerToys.Settings and Settings.UI from solution Update output paths * Installer work & publish.cmd * Fix dispatcher crashes * Fix crashes * Add all dlls to installer Cleanup installer Add OpenOOBE and OpenScoobe logic Fix minor issues Fix update scenario - REINSTALLMODE * Rename back namespaces, project name and project dir * [wip] move to winappsdk 1.1 * Fix propagating isElevated & installer runtimes dlls * Remove obsolete dir/file * PowerToys.Interop to netstandard2.0 * Move everything to .Net6 * [Settings] Always launch settings process non-elevated (#17791) * Move back to WinAppSdk 1.0.1 * Revert merge conflict ARM64 removal * Fix KBM Browse overlay image button * Bring back settings publish profile * Update release.yml * Change target frameworkd windows version * [Setup] Add Windows Application Runtime SDK (#17809) * Update requirements doc * Update compiling docs * Fix signing * Fix Settings exe and dll versions * Add exception for dlls that have version 1.0.0.0 * Fix powershell condition Co-authored-by: Andrey Nekrasov <yuyoyuppe@users.noreply.github.com>
2022-04-19 22:00:28 +02:00
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Navigation;
2020-03-11 10:43:32 -07:00
using Microsoft.Xaml.Interactivity;
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;
2020-03-11 10:43:32 -07:00
public DataTemplate DefaultHeaderTemplate { get; set; }
public object DefaultHeader
{
get { return GetValue(DefaultHeaderProperty); }
set { SetValue(DefaultHeaderProperty, value); }
2020-03-11 10:43:32 -07:00
}
public static readonly DependencyProperty DefaultHeaderProperty = DependencyProperty.Register("DefaultHeader", typeof(object), typeof(NavigationViewHeaderBehavior), new PropertyMetadata(null, (d, e) => current.UpdateHeader()));
2020-03-11 10:43:32 -07:00
public static NavigationViewHeaderMode GetHeaderMode(Page item)
{
return (NavigationViewHeaderMode)item?.GetValue(HeaderModeProperty);
2020-03-11 10:43:32 -07:00
}
public static void SetHeaderMode(Page item, NavigationViewHeaderMode value)
{
item?.SetValue(HeaderModeProperty, value);
2020-03-11 10:43:32 -07:00
}
public static readonly DependencyProperty HeaderModeProperty =
DependencyProperty.RegisterAttached("HeaderMode", typeof(bool), typeof(NavigationViewHeaderBehavior), new PropertyMetadata(NavigationViewHeaderMode.Always, (d, e) => current.UpdateHeader()));
2020-03-11 10:43:32 -07:00
public static object GetHeaderContext(Page item)
{
return item?.GetValue(HeaderContextProperty);
2020-03-11 10:43:32 -07:00
}
public static void SetHeaderContext(Page item, object value)
{
item?.SetValue(HeaderContextProperty, value);
2020-03-11 10:43:32 -07:00
}
public static readonly DependencyProperty HeaderContextProperty =
DependencyProperty.RegisterAttached("HeaderContext", typeof(object), typeof(NavigationViewHeaderBehavior), new PropertyMetadata(null, (d, e) => current.UpdateHeader()));
2020-03-11 10:43:32 -07:00
public static DataTemplate GetHeaderTemplate(Page item)
{
return (DataTemplate)item?.GetValue(HeaderTemplateProperty);
2020-03-11 10:43:32 -07:00
}
public static void SetHeaderTemplate(Page item, DataTemplate value)
{
item?.SetValue(HeaderTemplateProperty, value);
2020-03-11 10:43:32 -07:00
}
public static readonly DependencyProperty HeaderTemplateProperty =
DependencyProperty.RegisterAttached("HeaderTemplate", typeof(DataTemplate), typeof(NavigationViewHeaderBehavior), new PropertyMetadata(null, (d, e) => current.UpdateHeaderTemplate()));
2020-03-11 10:43:32 -07:00
protected override void OnAttached()
{
base.OnAttached();
current = this;
NavigationService.Navigated += OnNavigated;
2020-03-11 10:43:32 -07:00
}
protected override void OnDetaching()
{
base.OnDetaching();
NavigationService.Navigated -= OnNavigated;
2020-03-11 10:43:32 -07:00
}
private void OnNavigated(object sender, NavigationEventArgs e)
{
var frame = sender as Frame;
if (frame.Content is Page page)
{
currentPage = page;
2020-03-11 10:43:32 -07:00
UpdateHeader();
UpdateHeaderTemplate();
2020-03-11 10:43:32 -07:00
}
}
private void UpdateHeader()
{
if (currentPage != null)
2020-03-11 10:43:32 -07:00
{
var headerMode = GetHeaderMode(currentPage);
2020-03-11 10:43:32 -07:00
if (headerMode == NavigationViewHeaderMode.Never)
{
AssociatedObject.Header = null;
AssociatedObject.AlwaysShowHeader = false;
2020-03-11 10:43:32 -07:00
}
else
{
var headerFromPage = GetHeaderContext(currentPage);
2020-03-11 10:43:32 -07:00
if (headerFromPage != null)
{
AssociatedObject.Header = headerFromPage;
2020-03-11 10:43:32 -07:00
}
else
{
AssociatedObject.Header = DefaultHeader;
2020-03-11 10:43:32 -07:00
}
if (headerMode == NavigationViewHeaderMode.Always)
{
AssociatedObject.AlwaysShowHeader = true;
2020-03-11 10:43:32 -07:00
}
else
{
AssociatedObject.AlwaysShowHeader = false;
2020-03-11 10:43:32 -07:00
}
}
}
}
private void UpdateHeaderTemplate()
{
if (currentPage != null)
2020-03-11 10:43:32 -07:00
{
var headerTemplate = GetHeaderTemplate(currentPage);
AssociatedObject.HeaderTemplate = headerTemplate ?? DefaultHeaderTemplate;
2020-03-11 10:43:32 -07:00
}
}
}
}