| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | |
| 3 | using Microsoft.UI.Xaml; |
| 4 | using Microsoft.UI.Xaml.Controls; |
| 5 | using Microsoft.UI.Xaml.Input; |
| 6 | |
| 7 | using Windows.System; |
| 8 | |
| 9 | using WslSettings.Contracts.Services; |
| 10 | using WslSettings.ViewModels; |
| 11 | |
| 12 | namespace WslSettings.Views.OOBE; |
| 13 | |
| 14 | public sealed partial class ShellPage : Page |
| 15 | { |
| 16 | private readonly Microsoft.UI.Dispatching.DispatcherQueue _dispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread(); |
| 17 | |
| 18 | private void RegisterNavigationService() |
| 19 | { |
| 20 | ViewModel.NavigationViewService.UnregisterEvents(); |
| 21 | ViewModel.NavigationService.Frame = NavigationFrame; |
| 22 | ViewModel.NavigationViewService.Initialize(NavigationViewControl); |
| 23 | } |
| 24 | |
| 25 | public ShellViewModel ViewModel |
| 26 | { |
| 27 | get; |
| 28 | } |
| 29 | |
| 30 | public ShellPage(ShellViewModel viewModel) |
| 31 | { |
| 32 | ViewModel = viewModel; |
| 33 | InitializeComponent(); |
| 34 | |
| 35 | RegisterNavigationService(); |
| 36 | |
| 37 | // TODO: Set the title bar icon by updating /Assets/wsl.ico. |
| 38 | // A custom title bar is required for full window theme and Mica support. |
| 39 | // https://docs.microsoft.com/windows/apps/develop/title-bar?tabs=winui3#full-customization |
| 40 | App.OOBEWindow!.ExtendsContentIntoTitleBar = true; |
| 41 | App.OOBEWindow.SetTitleBar(AppTitleBar); |
| 42 | App.OOBEWindow.Activated += OOBEWindow_Activated; |
| 43 | AppTitleBarText.Text = "Settings_OOBEDisplayName".GetLocalized(); |
| 44 | } |
| 45 | |
| 46 | private void OnLoaded(object sender, RoutedEventArgs e) |
| 47 | { |
| 48 | TitleBarHelper.UpdateTitleBar(App.OOBEWindow!, RequestedTheme); |
| 49 | |
| 50 | KeyboardAccelerators.Add(BuildKeyboardAccelerator(VirtualKey.Left, VirtualKeyModifiers.Menu)); |
| 51 | KeyboardAccelerators.Add(BuildKeyboardAccelerator(VirtualKey.GoBack)); |
| 52 | } |
| 53 | |
| 54 | private void OOBEWindow_Activated(object sender, WindowActivatedEventArgs args) |
| 55 | { |
| 56 | App.AppTitlebar = AppTitleBarText as UIElement; |
| 57 | RegisterNavigationService(); |
| 58 | if (ViewModel.NavigationService.Frame?.Content == null) |
| 59 | { |
| 60 | ViewModel.NavigationService.NavigateTo(typeof(ViewModels.OOBE.GeneralViewModel).FullName!); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | private void NavigationViewControl_DisplayModeChanged(NavigationView sender, NavigationViewDisplayModeChangedEventArgs args) |
| 65 | { |
| 66 | if (args.DisplayMode == NavigationViewDisplayMode.Compact || args.DisplayMode == NavigationViewDisplayMode.Minimal) |
| 67 | { |
| 68 | PaneToggleBtn.Visibility = Visibility.Visible; |
| 69 | AppTitleBar.Margin = new Thickness(48, 0, 0, 0); |
| 70 | AppTitleBarText.Margin = new Thickness(12, 0, 0, 0); |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | PaneToggleBtn.Visibility = Visibility.Collapsed; |
| 75 | AppTitleBar.Margin = new Thickness(16, 0, 0, 0); |
| 76 | AppTitleBarText.Margin = new Thickness(16, 0, 0, 0); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | private void PaneToggleBtn_Click(object sender, RoutedEventArgs e) |
| 81 | { |
| 82 | NavigationViewControl.IsPaneOpen = !NavigationViewControl.IsPaneOpen; |
| 83 | } |
| 84 | |
| 85 | private async void NavigationViewControl_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args) |
| 86 | { |
| 87 | switch (args.InvokedItemContainer.Tag) |
| 88 | { |
| 89 | case "Settings": |
| 90 | await Task.Run(() => |
| 91 | { |
| 92 | _dispatcherQueue.TryEnqueue(() => |
| 93 | { |
| 94 | App.GetService<IWindowService>().CreateOrGetWindow(IWindowService.WindowId.MainWindow).Activate(); |
| 95 | }); |
| 96 | }); |
| 97 | break; |
| 98 | default: |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | private static KeyboardAccelerator BuildKeyboardAccelerator(VirtualKey key, VirtualKeyModifiers? modifiers = null) |
| 104 | { |
| 105 | var keyboardAccelerator = new KeyboardAccelerator() { Key = key }; |
| 106 | |
| 107 | if (modifiers.HasValue) |
| 108 | { |
| 109 | keyboardAccelerator.Modifiers = modifiers.Value; |
| 110 | } |
| 111 | |
| 112 | keyboardAccelerator.Invoked += OnKeyboardAcceleratorInvoked; |
| 113 | |
| 114 | return keyboardAccelerator; |
| 115 | } |
| 116 | |
| 117 | private static void OnKeyboardAcceleratorInvoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args) |
| 118 | { |
| 119 | var navigationService = App.GetService<INavigationService>(); |
| 120 | |
| 121 | var result = navigationService.GoBack(); |
| 122 | |
| 123 | args.Handled = result; |
| 124 | } |
| 125 | } |