Files
modernkeepass/WinAppCommon/Common/NavigationHelper.cs

423 lines
18 KiB
C#
Raw Normal View History

2017-09-11 18:25:00 +02:00
using System;
using System.Collections.Generic;
2021-06-14 19:38:48 +02:00
using Windows.Foundation.Metadata;
2017-09-11 18:25:00 +02:00
using Windows.System;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using GalaSoft.MvvmLight.Command;
2017-09-11 18:25:00 +02:00
namespace ModernKeePass.Common
{
/// <summary>
/// NavigationHelper aids in navigation between pages. It provides commands used to
/// navigate back and forward as well as registers for standard mouse and keyboard
/// shortcuts used to go back and forward in Windows and the hardware back button in
/// Windows Phone. In addition it integrates SuspensionManger to handle process lifetime
/// management and state management when navigating between pages.
/// </summary>
/// <example>
/// To make use of NavigationHelper, follow these two steps or
/// start with a BasicPage or any other Page item template other than BlankPage.
///
/// 1) Create an instance of the NavigationHelper somewhere such as in the
/// constructor for the page and register a callback for the LoadState and
/// SaveState events.
/// <code>
/// public MyPage()
/// {
/// this.InitializeComponent();
/// var navigationHelper = new NavigationHelper(this);
/// this.navigationHelper.LoadState += navigationHelper_LoadState;
/// this.navigationHelper.SaveState += navigationHelper_SaveState;
/// }
///
/// private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
/// { }
/// private async void navigationHelper_SaveState(object sender, LoadStateEventArgs e)
/// { }
/// </code>
///
/// 2) Register the page to call into the NavigationHelper whenever the page participates
/// in navigation by overriding the <see cref="Windows.UI.Xaml.Controls.Page.OnNavigatedTo"/>
/// and <see cref="Windows.UI.Xaml.Controls.Page.OnNavigatedFrom"/> events.
/// <code>
/// protected override void OnNavigatedTo(NavigationEventArgs e)
/// {
/// navigationHelper.OnNavigatedTo(e);
/// }
///
/// protected override void OnNavigatedFrom(NavigationEventArgs e)
/// {
/// navigationHelper.OnNavigatedFrom(e);
/// }
/// </code>
/// </example>
2021-06-14 19:38:48 +02:00
[WebHostHidden]
2017-09-11 18:25:00 +02:00
public class NavigationHelper : DependencyObject
{
2021-06-14 19:38:48 +02:00
private Page Page { get; set; }
private Frame Frame => Page.Frame;
2017-09-11 18:25:00 +02:00
/// <summary>
/// Initializes a new instance of the <see cref="NavigationHelper"/> class.
/// </summary>
/// <param name="page">A reference to the current page used for navigation.
/// This reference allows for frame manipulation and to ensure that keyboard
/// navigation requests only occur when the page is occupying the entire window.</param>
public NavigationHelper(Page page)
{
2021-06-14 19:38:48 +02:00
Page = page;
2017-09-11 18:25:00 +02:00
// When this page is part of the visual tree make two changes:
// 1) Map application view state to visual state for the page
// 2) Handle hardware navigation requests
2021-06-14 19:38:48 +02:00
Page.Loaded += (sender, e) =>
2017-09-11 18:25:00 +02:00
{
#if WINDOWS_PHONE_APP
Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
#else
// Keyboard and mouse navigation only apply when occupying the entire window
2021-06-14 19:38:48 +02:00
if (Page.ActualHeight.Equals(Window.Current.Bounds.Height) &&
Page.ActualWidth.Equals(Window.Current.Bounds.Width))
2017-09-11 18:25:00 +02:00
{
// Listen to the window directly so focus isn't required
Window.Current.CoreWindow.Dispatcher.AcceleratorKeyActivated +=
CoreDispatcher_AcceleratorKeyActivated;
Window.Current.CoreWindow.PointerPressed +=
2021-06-14 19:38:48 +02:00
CoreWindow_PointerPressed;
2017-09-11 18:25:00 +02:00
}
#endif
};
// Undo the same changes when the page is no longer visible
2021-06-14 19:38:48 +02:00
Page.Unloaded += (sender, e) =>
2017-09-11 18:25:00 +02:00
{
#if WINDOWS_PHONE_APP
Windows.Phone.UI.Input.HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
#else
Window.Current.CoreWindow.Dispatcher.AcceleratorKeyActivated -=
CoreDispatcher_AcceleratorKeyActivated;
Window.Current.CoreWindow.PointerPressed -=
2021-06-14 19:38:48 +02:00
CoreWindow_PointerPressed;
2017-09-11 18:25:00 +02:00
#endif
};
}
#region Navigation support
2021-06-14 19:38:48 +02:00
private RelayCommand _goBackCommand;
private RelayCommand _goForwardCommand;
2017-09-11 18:25:00 +02:00
/// <summary>
/// <see cref="RelayCommand"/> used to bind to the back Button's Command property
/// for navigating to the most recent item in back navigation history, if a Frame
/// manages its own navigation history.
///
/// The <see cref="RelayCommand"/> is set up to use the virtual method <see cref="GoBack"/>
/// as the Execute Action and <see cref="CanGoBack"/> for CanExecute.
/// </summary>
public RelayCommand GoBackCommand
{
get { return _goBackCommand ?? (_goBackCommand = new RelayCommand(GoBack, CanGoBack)); }
set { _goBackCommand = value; }
2017-09-11 18:25:00 +02:00
}
2017-09-11 18:25:00 +02:00
/// <summary>
/// <see cref="RelayCommand"/> used for navigating to the most recent item in
/// the forward navigation history, if a Frame manages its own navigation history.
///
/// The <see cref="RelayCommand"/> is set up to use the virtual method <see cref="GoForward"/>
/// as the Execute Action and <see cref="CanGoForward"/> for CanExecute.
/// </summary>
2021-06-14 19:38:48 +02:00
public RelayCommand GoForwardCommand => _goForwardCommand ?? (_goForwardCommand = new RelayCommand(GoForward, CanGoForward));
2017-09-11 18:25:00 +02:00
/// <summary>
/// Virtual method used by the <see cref="GoBackCommand"/> property
/// to determine if the <see cref="Frame"/> can go back.
/// </summary>
/// <returns>
/// true if the <see cref="Frame"/> has at least one entry
/// in the back navigation history.
/// </returns>
public virtual bool CanGoBack()
{
2021-06-14 19:38:48 +02:00
return Frame != null && Frame.CanGoBack;
2017-09-11 18:25:00 +02:00
}
2017-09-11 18:25:00 +02:00
/// <summary>
/// Virtual method used by the <see cref="GoForwardCommand"/> property
/// to determine if the <see cref="Frame"/> can go forward.
/// </summary>
/// <returns>
/// true if the <see cref="Frame"/> has at least one entry
/// in the forward navigation history.
/// </returns>
public virtual bool CanGoForward()
{
2021-06-14 19:38:48 +02:00
return Frame != null && Frame.CanGoForward;
2017-09-11 18:25:00 +02:00
}
/// <summary>
/// Virtual method used by the <see cref="GoBackCommand"/> property
/// to invoke the <see cref="Windows.UI.Xaml.Controls.Frame.GoBack"/> method.
/// </summary>
public virtual void GoBack()
{
if (Frame != null && Frame.CanGoBack)
{
Frame.GoBack();
}
2017-09-11 18:25:00 +02:00
}
2017-09-11 18:25:00 +02:00
/// <summary>
/// Virtual method used by the <see cref="GoForwardCommand"/> property
/// to invoke the <see cref="Windows.UI.Xaml.Controls.Frame.GoForward"/> method.
/// </summary>
public virtual void GoForward()
{
if (Frame != null && Frame.CanGoForward)
{
Frame.GoForward();
}
2017-09-11 18:25:00 +02:00
}
#if WINDOWS_PHONE_APP
/// <summary>
/// Invoked when the hardware back button is pressed. For Windows Phone only.
/// </summary>
/// <param name="sender">Instance that triggered the event.</param>
/// <param name="e">Event data describing the conditions that led to the event.</param>
private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
if (this.GoBackCommand.CanExecute(null))
{
e.Handled = true;
this.GoBackCommand.Execute(null);
}
}
#else
/// <summary>
/// Invoked on every keystroke, including system keys such as Alt key combinations, when
/// this page is active and occupies the entire window. Used to detect keyboard navigation
/// between pages even when the page itself doesn't have focus.
/// </summary>
/// <param name="sender">Instance that triggered the event.</param>
/// <param name="e">Event data describing the conditions that led to the event.</param>
private void CoreDispatcher_AcceleratorKeyActivated(CoreDispatcher sender,
AcceleratorKeyEventArgs e)
2017-09-11 18:25:00 +02:00
{
var virtualKey = e.VirtualKey;
// Only investigate further when Left, Right, or the dedicated Previous or Next keys
// are pressed
if ((e.EventType == CoreAcceleratorKeyEventType.SystemKeyDown ||
e.EventType == CoreAcceleratorKeyEventType.KeyDown) &&
2017-09-11 18:25:00 +02:00
(virtualKey == VirtualKey.Left || virtualKey == VirtualKey.Right ||
(int) virtualKey == 166 || (int) virtualKey == 167))
2017-09-11 18:25:00 +02:00
{
2021-06-14 19:38:48 +02:00
var coreWindow = Window.Current.CoreWindow;
const CoreVirtualKeyStates downState = CoreVirtualKeyStates.Down;
var menuKey = (coreWindow.GetKeyState(VirtualKey.Menu) & downState) == downState;
var controlKey = (coreWindow.GetKeyState(VirtualKey.Control) & downState) == downState;
var shiftKey = (coreWindow.GetKeyState(VirtualKey.Shift) & downState) == downState;
var noModifiers = !menuKey && !controlKey && !shiftKey;
var onlyAlt = menuKey && !controlKey && !shiftKey;
2017-09-11 18:25:00 +02:00
if ((int) virtualKey == 166 && noModifiers ||
2021-06-14 19:38:48 +02:00
virtualKey == VirtualKey.Left && onlyAlt)
2017-09-11 18:25:00 +02:00
{
// When the previous key or Alt+Left are pressed navigate back
e.Handled = true;
2021-06-14 19:38:48 +02:00
GoBackCommand.Execute(null);
2017-09-11 18:25:00 +02:00
}
else if ((int) virtualKey == 167 && noModifiers ||
virtualKey == VirtualKey.Right && onlyAlt)
2017-09-11 18:25:00 +02:00
{
// When the next key or Alt+Right are pressed navigate forward
e.Handled = true;
2021-06-14 19:38:48 +02:00
GoForwardCommand.Execute(null);
2017-09-11 18:25:00 +02:00
}
}
}
/// <summary>
/// Invoked on every mouse click, touch screen tap, or equivalent interaction when this
/// page is active and occupies the entire window. Used to detect browser-style next and
/// previous mouse button clicks to navigate between pages.
/// </summary>
/// <param name="sender">Instance that triggered the event.</param>
/// <param name="e">Event data describing the conditions that led to the event.</param>
private void CoreWindow_PointerPressed(CoreWindow sender,
PointerEventArgs e)
2017-09-11 18:25:00 +02:00
{
var properties = e.CurrentPoint.Properties;
// Ignore button chords with the left, right, and middle buttons
if (properties.IsLeftButtonPressed || properties.IsRightButtonPressed ||
properties.IsMiddleButtonPressed)
{
return;
}
2017-09-11 18:25:00 +02:00
2021-06-14 19:38:48 +02:00
// If back or forward are pressed (but not both) navigate appropriately
var backPressed = properties.IsXButton1Pressed;
2021-06-14 19:38:48 +02:00
var forwardPressed = properties.IsXButton2Pressed;
2017-09-11 18:25:00 +02:00
if (backPressed ^ forwardPressed)
{
e.Handled = true;
if (backPressed)
{
GoBackCommand.Execute(null);
}
if (forwardPressed)
{
GoForwardCommand.Execute(null);
}
2017-09-11 18:25:00 +02:00
}
}
#endif
#endregion
#region Process lifetime management
2021-06-14 19:38:48 +02:00
private string _pageKey;
2017-09-11 18:25:00 +02:00
/// <summary>
/// Register this event on the current page to populate the page
/// with content passed during navigation as well as any saved
/// state provided when recreating a page from a prior session.
/// </summary>
public event LoadStateEventHandler LoadState;
2017-09-11 18:25:00 +02:00
/// <summary>
/// Register this event on the current page to preserve
/// state associated with the current page in case the
/// application is suspended or the page is discarded from
2021-06-14 19:38:48 +02:00
/// the navigation cache.
2017-09-11 18:25:00 +02:00
/// </summary>
public event SaveStateEventHandler SaveState;
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// This method calls <see cref="LoadState"/>, where all page specific
/// navigation and process lifetime management logic should be placed.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property provides the group to be displayed.</param>
public void OnNavigatedTo(NavigationEventArgs e)
{
2021-06-14 19:38:48 +02:00
var frameState = SuspensionManager.SessionStateForFrame(Frame);
_pageKey = "Page-" + Frame.BackStackDepth;
2017-09-11 18:25:00 +02:00
if (e.NavigationMode == NavigationMode.New)
{
// Clear existing state for forward navigation when adding a new page to the
// navigation stack
var nextPageKey = _pageKey;
2021-06-14 19:38:48 +02:00
var nextPageIndex = Frame.BackStackDepth;
2017-09-11 18:25:00 +02:00
while (frameState.Remove(nextPageKey))
{
nextPageIndex++;
nextPageKey = "Page-" + nextPageIndex;
}
// Pass the navigation parameter to the new page
2021-06-14 19:38:48 +02:00
LoadState?.Invoke(this, new LoadStateEventArgs(e.Parameter, null));
2017-09-11 18:25:00 +02:00
}
else
{
// Pass the navigation parameter and preserved page state to the page, using
// the same strategy for loading suspended state and recreating pages discarded
// from cache
LoadState?.Invoke(this, new LoadStateEventArgs(e.Parameter, (Dictionary<string, object>) frameState[_pageKey]));
2017-09-11 18:25:00 +02:00
}
}
/// <summary>
/// Invoked when this page will no longer be displayed in a Frame.
/// This method calls <see cref="SaveState"/>, where all page specific
/// navigation and process lifetime management logic should be placed.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property provides the group to be displayed.</param>
public void OnNavigatedFrom(NavigationEventArgs e)
{
2021-06-14 19:38:48 +02:00
var frameState = SuspensionManager.SessionStateForFrame(Frame);
var pageState = new Dictionary<string, object>();
2021-06-14 19:38:48 +02:00
SaveState?.Invoke(this, new SaveStateEventArgs(pageState));
2017-09-11 18:25:00 +02:00
frameState[_pageKey] = pageState;
}
#endregion
}
/// <summary>
/// Represents the method that will handle the <see cref="NavigationHelper.LoadState"/>event
/// </summary>
public delegate void LoadStateEventHandler(object sender, LoadStateEventArgs e);
2017-09-11 18:25:00 +02:00
/// <summary>
/// Represents the method that will handle the <see cref="NavigationHelper.SaveState"/>event
/// </summary>
public delegate void SaveStateEventHandler(object sender, SaveStateEventArgs e);
/// <summary>
/// Class used to hold the event data required when a page attempts to load state.
/// </summary>
public class LoadStateEventArgs : EventArgs
{
/// <summary>
2021-06-14 19:38:48 +02:00
/// The parameter value passed to <see cref="Frame.Navigate(Type, object)"/>
2017-09-11 18:25:00 +02:00
/// when this page was initially requested.
/// </summary>
2021-06-14 19:38:48 +02:00
public object NavigationParameter { get; private set; }
2017-09-11 18:25:00 +02:00
/// <summary>
/// A dictionary of state preserved by this page during an earlier
/// session. This will be null the first time a page is visited.
/// </summary>
2021-06-14 19:38:48 +02:00
public Dictionary<string, object> PageState { get; private set; }
2017-09-11 18:25:00 +02:00
/// <summary>
/// Initializes a new instance of the <see cref="LoadStateEventArgs"/> class.
/// </summary>
/// <param name="navigationParameter">
2021-06-14 19:38:48 +02:00
/// The parameter value passed to <see cref="Frame.Navigate(Type, object)"/>
2017-09-11 18:25:00 +02:00
/// when this page was initially requested.
/// </param>
/// <param name="pageState">
/// A dictionary of state preserved by this page during an earlier
/// session. This will be null the first time a page is visited.
/// </param>
2021-06-14 19:38:48 +02:00
public LoadStateEventArgs(object navigationParameter, Dictionary<string, object> pageState)
2017-09-11 18:25:00 +02:00
{
2021-06-14 19:38:48 +02:00
NavigationParameter = navigationParameter;
PageState = pageState;
2017-09-11 18:25:00 +02:00
}
}
2017-09-11 18:25:00 +02:00
/// <summary>
/// Class used to hold the event data required when a page attempts to save state.
/// </summary>
public class SaveStateEventArgs : EventArgs
{
/// <summary>
/// An empty dictionary to be populated with serializable state.
/// </summary>
2021-06-14 19:38:48 +02:00
public Dictionary<string, object> PageState { get; private set; }
2017-09-11 18:25:00 +02:00
/// <summary>
/// Initializes a new instance of the <see cref="SaveStateEventArgs"/> class.
/// </summary>
/// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
2021-06-14 19:38:48 +02:00
public SaveStateEventArgs(Dictionary<string, object> pageState)
2017-09-11 18:25:00 +02:00
{
2021-06-14 19:38:48 +02:00
PageState = pageState;
2017-09-11 18:25:00 +02:00
}
}
}