|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation 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.Reflection; |
| 6 | +using Windows.ApplicationModel; |
| 7 | +using Windows.UI.Core; |
| 8 | +using Windows.UI.Xaml; |
| 9 | +using Windows.UI.Xaml.Controls; |
| 10 | +using Windows.UI.Xaml.Navigation; |
| 11 | + |
| 12 | +namespace Microsoft.Toolkit.Uwp.UI.Controls |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Panel that allows for a List/Details pattern. |
| 16 | + /// </summary> |
| 17 | + /// <seealso cref="ItemsControl" /> |
| 18 | + public partial class ListDetailsView |
| 19 | + { |
| 20 | + private AppViewBackButtonVisibility? _previousSystemBackButtonVisibility; |
| 21 | + private bool _previousNavigationViewBackEnabled; |
| 22 | + |
| 23 | + // Int used because the underlying type is an enum, but we don't have access to the enum |
| 24 | + private int _previousNavigationViewBackVisibilty; |
| 25 | + private Button _inlineBackButton; |
| 26 | + private object _navigationView; |
| 27 | + private Frame _frame; |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Sets the back button visibility based on the current visual state and selected item |
| 31 | + /// </summary> |
| 32 | + private void SetBackButtonVisibility(ListDetailsViewState? previousState = null) |
| 33 | + { |
| 34 | + const int backButtonVisible = 1; |
| 35 | + |
| 36 | + if (DesignMode.DesignModeEnabled) |
| 37 | + { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + if (ViewState == ListDetailsViewState.Details) |
| 42 | + { |
| 43 | + if (BackButtonBehavior == BackButtonBehavior.Inline && _inlineBackButton != null) |
| 44 | + { |
| 45 | + _inlineBackButton.Visibility = Visibility.Visible; |
| 46 | + } |
| 47 | + else if (BackButtonBehavior == BackButtonBehavior.Automatic) |
| 48 | + { |
| 49 | + // Continue to support the system back button if it is being used |
| 50 | + SystemNavigationManager navigationManager = SystemNavigationManager.GetForCurrentView(); |
| 51 | + if (navigationManager.AppViewBackButtonVisibility == AppViewBackButtonVisibility.Visible) |
| 52 | + { |
| 53 | + // Setting this indicates that the system back button is being used |
| 54 | + _previousSystemBackButtonVisibility = navigationManager.AppViewBackButtonVisibility; |
| 55 | + } |
| 56 | + else if (_inlineBackButton != null && (_navigationView == null || _frame == null)) |
| 57 | + { |
| 58 | + // We can only use the new NavigationView if we also have a Frame |
| 59 | + // If there is no frame we have to use the inline button |
| 60 | + _inlineBackButton.Visibility = Visibility.Visible; |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + SetNavigationViewBackButtonState(backButtonVisible, true); |
| 65 | + } |
| 66 | + } |
| 67 | + else if (BackButtonBehavior != BackButtonBehavior.Manual) |
| 68 | + { |
| 69 | + SystemNavigationManager navigationManager = SystemNavigationManager.GetForCurrentView(); |
| 70 | + _previousSystemBackButtonVisibility = navigationManager.AppViewBackButtonVisibility; |
| 71 | + |
| 72 | + navigationManager.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible; |
| 73 | + } |
| 74 | + } |
| 75 | + else if (previousState == ListDetailsViewState.Details) |
| 76 | + { |
| 77 | + if (BackButtonBehavior == BackButtonBehavior.Inline && _inlineBackButton != null) |
| 78 | + { |
| 79 | + _inlineBackButton.Visibility = Visibility.Collapsed; |
| 80 | + } |
| 81 | + else if (BackButtonBehavior == BackButtonBehavior.Automatic) |
| 82 | + { |
| 83 | + if (!_previousSystemBackButtonVisibility.HasValue) |
| 84 | + { |
| 85 | + if (_inlineBackButton != null && (_navigationView == null || _frame == null)) |
| 86 | + { |
| 87 | + _inlineBackButton.Visibility = Visibility.Collapsed; |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + SetNavigationViewBackButtonState(_previousNavigationViewBackVisibilty, _previousNavigationViewBackEnabled); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if (_previousSystemBackButtonVisibility.HasValue) |
| 97 | + { |
| 98 | + // Make sure we show the back button if the stack can navigate back |
| 99 | + SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = _previousSystemBackButtonVisibility.Value; |
| 100 | + _previousSystemBackButtonVisibility = null; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private void SetNavigationViewBackButtonState(int visible, bool enabled) |
| 106 | + { |
| 107 | + if (_navigationView == null) |
| 108 | + { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + System.Type navType = _navigationView.GetType(); |
| 113 | + PropertyInfo visibleProperty = navType.GetProperty("IsBackButtonVisible"); |
| 114 | + if (visibleProperty != null) |
| 115 | + { |
| 116 | + _previousNavigationViewBackVisibilty = (int)visibleProperty.GetValue(_navigationView); |
| 117 | + visibleProperty.SetValue(_navigationView, visible); |
| 118 | + } |
| 119 | + |
| 120 | + PropertyInfo enabledProperty = navType.GetProperty("IsBackEnabled"); |
| 121 | + if (enabledProperty != null) |
| 122 | + { |
| 123 | + _previousNavigationViewBackEnabled = (bool)enabledProperty.GetValue(_navigationView); |
| 124 | + enabledProperty.SetValue(_navigationView, enabled); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Closes the details pane if we are in narrow state |
| 130 | + /// </summary> |
| 131 | + /// <param name="sender">The sender</param> |
| 132 | + /// <param name="args">The event args</param> |
| 133 | + private void OnFrameNavigating(object sender, NavigatingCancelEventArgs args) |
| 134 | + { |
| 135 | + if (args.NavigationMode == NavigationMode.Back && ViewState == ListDetailsViewState.Details) |
| 136 | + { |
| 137 | + ClearSelectedItem(); |
| 138 | + args.Cancel = true; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /// <summary> |
| 143 | + /// Closes the details pane if we are in narrow state |
| 144 | + /// </summary> |
| 145 | + /// <param name="sender">The sender</param> |
| 146 | + /// <param name="args">The event args</param> |
| 147 | + private void OnBackRequested(object sender, BackRequestedEventArgs args) |
| 148 | + { |
| 149 | + if (ViewState == ListDetailsViewState.Details) |
| 150 | + { |
| 151 | + // let the OnFrameNavigating method handle it if |
| 152 | + if (_frame == null || !_frame.CanGoBack) |
| 153 | + { |
| 154 | + ClearSelectedItem(); |
| 155 | + } |
| 156 | + |
| 157 | + args.Handled = true; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private void OnInlineBackButtonClicked(object sender, RoutedEventArgs e) |
| 162 | + { |
| 163 | + ClearSelectedItem(); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments