master
cs 191 lines 5.7 KB
Raw
1 // Copyright (C) Microsoft Corporation. All rights reserved.
2
3 using CommunityToolkit.Mvvm.ComponentModel;
4 using CommunityToolkit.Mvvm.Input;
5 using System.Windows.Input;
6 using WslSettings.Contracts.Services;
7
8 namespace WslSettings.ViewModels.Settings;
9
10 public partial class MemAndProcViewModel : WslConfigSettingViewModel
11 {
12 private IWslConfigSetting? _procCount;
13 private IWslConfigSetting? _memorySize;
14 private IWslConfigSetting? _swapSize;
15 private IWslConfigSetting? _swapFilePath;
16 private int _defaultProcCount;
17 private ulong _defaultMemorySize;
18 private ulong _defaultSwapSize;
19 private bool _procCount_ResetEnabled;
20 private bool _memorySize_ResetEnabled;
21 private bool _swapSize_ResetEnabled;
22
23 public MemAndProcViewModel()
24 {
25 InitializeConfigSettings();
26
27 ProcCount_ResetEnabled = !Equals(_defaultProcCount, _procCount!.Int32Value);
28 MemorySize_ResetEnabled = !Equals(_defaultMemorySize, _memorySize!.UInt64Value);
29 SwapSize_ResetEnabled = !Equals(_defaultSwapSize, _swapSize!.UInt64Value);
30 }
31
32 protected override void InitializeConfigSettings()
33 {
34 var wslConfigService = App.GetService<IWslConfigService>();
35 _procCount = wslConfigService.GetWslConfigSetting(WslConfigEntry.ProcessorCount);
36 _memorySize = wslConfigService.GetWslConfigSetting(WslConfigEntry.MemorySizeBytes);
37 _swapSize = wslConfigService.GetWslConfigSetting(WslConfigEntry.SwapSizeBytes);
38 _swapFilePath = wslConfigService.GetWslConfigSetting(WslConfigEntry.SwapFilePath);
39
40 _defaultProcCount = wslConfigService.GetWslConfigSetting(WslConfigEntry.ProcessorCount, true).Int32Value;
41 _defaultMemorySize = wslConfigService.GetWslConfigSetting(WslConfigEntry.MemorySizeBytes, true).UInt64Value;
42 _defaultSwapSize = wslConfigService.GetWslConfigSetting(WslConfigEntry.SwapSizeBytes, true).UInt64Value;
43 }
44
45 public string ProcCount
46 {
47 get
48 {
49 return _procCount!.Int32Value.ToString();
50 }
51 set
52 {
53 if (ValidateInput(value, Constants.IntegerRegex))
54 {
55 if (Int32.TryParse(value, out int parsedValue))
56 {
57 Set(ref _procCount!, parsedValue);
58 }
59 else
60 {
61 OnPropertyChanged();
62 }
63 }
64 }
65 }
66
67 public void SetProcCount_ResetEnabled(string? value)
68 {
69 if (Int32.TryParse(value, out Int32 parseResult))
70 {
71 ProcCount_ResetEnabled = !Equals(_defaultProcCount, parseResult);
72 }
73 else
74 {
75 ProcCount_ResetEnabled = true;
76 }
77 }
78
79 public bool ProcCount_ResetEnabled
80 {
81 get => _procCount_ResetEnabled;
82 set => SetProperty(ref _procCount_ResetEnabled, value);
83 }
84
85 private void ProcCount_ResetExecuted(string? param)
86 {
87 ProcCount = _defaultProcCount.ToString();
88 }
89
90 public ICommand ProcCount_ResetCommand => new RelayCommand<string>(ProcCount_ResetExecuted);
91
92 public string MemorySize
93 {
94 get
95 {
96 return _memorySize!.UInt64Value.ToString();
97 }
98 set
99 {
100 if (ValidateInput(value, Constants.WholeNumberRegex))
101 {
102 if (UInt64.TryParse(value, out ulong parsedValue))
103 {
104 Set(ref _memorySize!, parsedValue);
105 }
106 else
107 {
108 OnPropertyChanged();
109 }
110 }
111 }
112 }
113
114 public void SetMemorySize_ResetEnabled(string? value)
115 {
116 if (UInt64.TryParse(value, out UInt64 parseResult))
117 {
118 MemorySize_ResetEnabled = !Equals(_defaultMemorySize / Constants.MB, parseResult);
119 }
120 else
121 {
122 MemorySize_ResetEnabled = true;
123 }
124 }
125
126 public bool MemorySize_ResetEnabled
127 {
128 get => _memorySize_ResetEnabled;
129 set => SetProperty(ref _memorySize_ResetEnabled, value);
130 }
131
132 private void MemorySize_ResetExecuted(string? param)
133 {
134 MemorySize = _defaultMemorySize.ToString();
135 }
136
137 public ICommand MemorySize_ResetCommand => new RelayCommand<string>(MemorySize_ResetExecuted);
138
139 public string SwapSize
140 {
141 get
142 {
143 return _swapSize!.UInt64Value.ToString();
144 }
145 set
146 {
147 if (ValidateInput(value, Constants.WholeNumberRegex))
148 {
149 if (UInt64.TryParse(value, out ulong parsedValue))
150 {
151 Set(ref _swapSize!, parsedValue);
152 }
153 else
154 {
155 OnPropertyChanged();
156 }
157 }
158 }
159 }
160
161 public void SetSwapSize_ResetEnabled(string? value)
162 {
163 if (UInt64.TryParse(value, out UInt64 parseResult))
164 {
165 SwapSize_ResetEnabled = !Equals(_defaultSwapSize / Constants.MB, parseResult);
166 }
167 else
168 {
169 SwapSize_ResetEnabled = true;
170 }
171 }
172
173 public bool SwapSize_ResetEnabled
174 {
175 get => _swapSize_ResetEnabled;
176 set => SetProperty(ref _swapSize_ResetEnabled, value);
177 }
178
179 private void SwapSize_ResetExecuted(string? param)
180 {
181 SwapSize = _defaultSwapSize.ToString();
182 }
183
184 public ICommand SwapSize_ResetCommand => new RelayCommand<string>(SwapSize_ResetExecuted);
185
186 public string SwapFilePath
187 {
188 get { return _swapFilePath!.StringValue; }
189 set { Set(ref _swapFilePath!, value); }
190 }
191 }