/ src / settings-ui / Settings.UI / Services / NavigationService.cs
NavigationService.cs
  1  // Copyright (c) Microsoft Corporation
  2  // The Microsoft Corporation licenses this file to you under the MIT license.
  3  // See the LICENSE file in the project root for more information.
  4  
  5  using System;
  6  
  7  using Microsoft.UI.Xaml;
  8  using Microsoft.UI.Xaml.Controls;
  9  using Microsoft.UI.Xaml.Media.Animation;
 10  using Microsoft.UI.Xaml.Navigation;
 11  
 12  namespace Microsoft.PowerToys.Settings.UI.Services
 13  {
 14      public static class NavigationService
 15      {
 16          public static event NavigatedEventHandler Navigated;
 17  
 18          public static event NavigationFailedEventHandler NavigationFailed;
 19  
 20          private static Frame frame;
 21          private static object lastParamUsed;
 22  
 23          public static Frame Frame
 24          {
 25              get
 26              {
 27                  return frame;
 28              }
 29  
 30              set
 31              {
 32                  UnregisterFrameEvents();
 33                  frame = value;
 34                  RegisterFrameEvents();
 35              }
 36          }
 37  
 38          public static bool CanGoBack => Frame.CanGoBack;
 39  
 40          public static bool CanGoForward => Frame.CanGoForward;
 41  
 42          public static bool GoBack()
 43          {
 44              if (CanGoBack)
 45              {
 46                  Frame.GoBack();
 47                  return true;
 48              }
 49  
 50              return false;
 51          }
 52  
 53          public static void GoForward() => Frame.GoForward();
 54  
 55          public static bool Navigate(Type pageType, object parameter = null, NavigationTransitionInfo infoOverride = null)
 56          {
 57              // Don't open the same page multiple times
 58              if (Frame.Content?.GetType() != pageType || (parameter != null && !parameter.Equals(lastParamUsed)))
 59              {
 60                  var navigationResult = Frame.Navigate(pageType, parameter, infoOverride);
 61                  if (navigationResult)
 62                  {
 63                      lastParamUsed = parameter;
 64                  }
 65  
 66                  return navigationResult;
 67              }
 68              else
 69              {
 70                  return false;
 71              }
 72          }
 73  
 74          public static bool Navigate<T>(object parameter = null, NavigationTransitionInfo infoOverride = null)
 75              where T : Page
 76              => Navigate(typeof(T), parameter, infoOverride);
 77  
 78          private static void RegisterFrameEvents()
 79          {
 80              if (frame != null)
 81              {
 82                  frame.Navigated += Frame_Navigated;
 83                  frame.NavigationFailed += Frame_NavigationFailed;
 84              }
 85          }
 86  
 87          private static void UnregisterFrameEvents()
 88          {
 89              if (frame != null)
 90              {
 91                  frame.Navigated -= Frame_Navigated;
 92                  frame.NavigationFailed -= Frame_NavigationFailed;
 93              }
 94          }
 95  
 96          private static void Frame_NavigationFailed(object sender, NavigationFailedEventArgs e) => NavigationFailed?.Invoke(sender, e);
 97  
 98          private static void Frame_Navigated(object sender, NavigationEventArgs e) => Navigated?.Invoke(sender, e);
 99  
100          internal static void EnsurePageIsSelected(Type pageType)
101          {
102              if (Frame.Content == null)
103              {
104                  Frame.Navigate(pageType);
105              }
106          }
107      }
108  }