| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | |
| 3 | using Microsoft.UI.Xaml.Controls; |
| 4 | using System.Diagnostics.CodeAnalysis; |
| 5 | using WslSettings.Contracts.Services; |
| 6 | |
| 7 | namespace WslSettings.Services; |
| 8 | |
| 9 | public class NavigationViewService : INavigationViewService |
| 10 | { |
| 11 | private readonly INavigationService _navigationService; |
| 12 | |
| 13 | private readonly IPageService _pageService; |
| 14 | |
| 15 | private NavigationView? _navigationView; |
| 16 | |
| 17 | public IList<object>? MenuItems => _navigationView?.MenuItems; |
| 18 | |
| 19 | public object? SettingsItem => _navigationView?.SettingsItem; |
| 20 | |
| 21 | public NavigationViewService(INavigationService navigationService, IPageService pageService) |
| 22 | { |
| 23 | _navigationService = navigationService; |
| 24 | _pageService = pageService; |
| 25 | } |
| 26 | |
| 27 | [MemberNotNull(nameof(_navigationView))] |
| 28 | public void Initialize(NavigationView navigationView) |
| 29 | { |
| 30 | _navigationView = navigationView; |
| 31 | _navigationView.BackRequested += OnBackRequested; |
| 32 | _navigationView.ItemInvoked += OnItemInvoked; |
| 33 | } |
| 34 | |
| 35 | public void UnregisterEvents() |
| 36 | { |
| 37 | if (_navigationView != null) |
| 38 | { |
| 39 | _navigationView.BackRequested -= OnBackRequested; |
| 40 | _navigationView.ItemInvoked -= OnItemInvoked; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | public NavigationViewItem? GetSelectedItem(Type pageType) |
| 45 | { |
| 46 | if (_navigationView != null) |
| 47 | { |
| 48 | return GetSelectedItem(_navigationView.MenuItems, pageType) ?? GetSelectedItem(_navigationView.FooterMenuItems, pageType); |
| 49 | } |
| 50 | |
| 51 | return null; |
| 52 | } |
| 53 | |
| 54 | private void OnBackRequested(NavigationView sender, NavigationViewBackRequestedEventArgs args) => _navigationService.GoBack(); |
| 55 | |
| 56 | private void OnItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args) |
| 57 | { |
| 58 | if (args.IsSettingsInvoked) |
| 59 | { |
| 60 | // Navigate to the settings page. |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | var selectedItem = args.InvokedItemContainer as NavigationViewItem; |
| 65 | |
| 66 | if (selectedItem?.GetValue(NavigationHelper.NavigateToProperty) is string pageKey) |
| 67 | { |
| 68 | _navigationService.NavigateTo(pageKey); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | private NavigationViewItem? GetSelectedItem(IEnumerable<object> menuItems, Type pageType) |
| 74 | { |
| 75 | foreach (var item in menuItems.OfType<NavigationViewItem>()) |
| 76 | { |
| 77 | if (IsMenuItemForPageType(item, pageType)) |
| 78 | { |
| 79 | return item; |
| 80 | } |
| 81 | |
| 82 | var selectedChild = GetSelectedItem(item.MenuItems, pageType); |
| 83 | if (selectedChild != null) |
| 84 | { |
| 85 | return selectedChild; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return null; |
| 90 | } |
| 91 | |
| 92 | private bool IsMenuItemForPageType(NavigationViewItem menuItem, Type sourcePageType) |
| 93 | { |
| 94 | if (menuItem.GetValue(NavigationHelper.NavigateToProperty) is string pageKey) |
| 95 | { |
| 96 | return _pageService.GetPageType(pageKey) == sourcePageType; |
| 97 | } |
| 98 | |
| 99 | return false; |
| 100 | } |
| 101 | } |