| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | |
| 3 | using Microsoft.UI.Xaml; |
| 4 | using Microsoft.UI.Xaml.Automation.Peers; |
| 5 | using Microsoft.UI.Xaml.Controls; |
| 6 | using Microsoft.UI.Xaml.Navigation; |
| 7 | using WslSettings.Contracts.Services; |
| 8 | using WslSettings.ViewModels.Settings; |
| 9 | |
| 10 | namespace WslSettings.Views.Settings; |
| 11 | |
| 12 | public sealed partial class MemAndProcPage : Page |
| 13 | { |
| 14 | public MemAndProcViewModel ViewModel |
| 15 | { |
| 16 | get; |
| 17 | } |
| 18 | |
| 19 | public MemAndProcPage() |
| 20 | { |
| 21 | ViewModel = App.GetService<MemAndProcViewModel>(); |
| 22 | InitializeComponent(); |
| 23 | Settings_ErrorTryAgainLater.RegisterPropertyChangedCallback(TextBlock.VisibilityProperty, (s, e) => |
| 24 | { |
| 25 | if (ViewModel.ErrorVisibility) |
| 26 | { |
| 27 | FrameworkElementAutomationPeer.FromElement(Settings_ErrorTryAgainLater).RaiseAutomationEvent(AutomationEvents.LiveRegionChanged); |
| 28 | } |
| 29 | }); |
| 30 | |
| 31 | this.Loaded += OnPageLoaded; |
| 32 | } |
| 33 | |
| 34 | private void OnPageLoaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e) |
| 35 | { |
| 36 | MemAndProcPageRoot.Focus(FocusState.Programmatic); |
| 37 | RuntimeHelper.SetupExpanderFocusManagementByName(this, "ProcCountExpander", "ProcCountTextBox"); |
| 38 | RuntimeHelper.SetupExpanderFocusManagementByName(this, "MemorySizeExpander", "MemorySizeTextBox"); |
| 39 | RuntimeHelper.SetupExpanderFocusManagementByName(this, "SwapSizeExpander", "SwapSizeTextBox"); |
| 40 | RuntimeHelper.SetupExpanderFocusManagementByName(this, "SwapFilePathExpander", "SwapFilePathTextBox"); |
| 41 | } |
| 42 | |
| 43 | override protected void OnNavigatedFrom(NavigationEventArgs e) |
| 44 | { |
| 45 | App.GetService<IWslConfigService>().WslConfigChanged -= ViewModel.OnConfigChanged; |
| 46 | } |
| 47 | |
| 48 | private void Settings_ResetButton_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e) |
| 49 | { |
| 50 | if (sender == null) |
| 51 | { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | RuntimeHelper.TryMoveFocusPreviousControl(sender as Button); |
| 56 | } |
| 57 | |
| 58 | public async void SwapFilePath_Click(object sender, RoutedEventArgs e) |
| 59 | { |
| 60 | // Open the picker for the user to pick a file |
| 61 | Windows.Storage.StorageFile file = await RuntimeHelper.PickSingleFileAsync([".vhdx"]); |
| 62 | if (file != null) |
| 63 | { |
| 64 | ViewModel.SwapFilePath = file.Path; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | private void ProcCountTextBox_TextChanged(object sender, TextChangedEventArgs e) |
| 69 | { |
| 70 | if (sender == null) |
| 71 | { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | TextBox? textBox = sender as TextBox; |
| 76 | ViewModel.SetProcCount_ResetEnabled(textBox!.Text); |
| 77 | } |
| 78 | |
| 79 | private void MemorySizeTextBox_TextChanged(object sender, TextChangedEventArgs e) |
| 80 | { |
| 81 | if (sender == null) |
| 82 | { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | TextBox? textBox = sender as TextBox; |
| 87 | ViewModel.SetMemorySize_ResetEnabled(textBox!.Text); |
| 88 | } |
| 89 | |
| 90 | private void SwapSizeTextBox_TextChanged(object sender, TextChangedEventArgs e) |
| 91 | { |
| 92 | if (sender == null) |
| 93 | { |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | TextBox? textBox = sender as TextBox; |
| 98 | ViewModel.SetSwapSize_ResetEnabled(textBox!.Text); |
| 99 | } |
| 100 | } |