-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
428 lines (344 loc) · 14.8 KB
/
MainWindow.xaml.cs
File metadata and controls
428 lines (344 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
using Microsoft.Toolkit.Wpf.UI.XamlHost;
using Newtonsoft.Json;
using System;
using System.Collections.Concurrent;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Management;
using System.Reflection;
using System.Runtime.InteropServices;
using System.ServiceModel;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using WindowsInput;
using WindowsInput.Native;
using static System.Management.ManagementObjectCollection;
namespace PocketTDPControl
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private ViewModel ViewModel;
private readonly string FilePath = "tdp.json";
private ConcurrentQueue<int> TDPQueue;
private ConcurrentQueue<int> FanSpeedPrecentageQueue;
private NotifyIcon TrayIcon;
private HardwareMonitor HWM;
private TDPWindow TDPWindowDialog;
private SettingWindow SettingWindowDialog;
private Window MachineWindow;
private MethodInfo GetFanSpeedPrecentage;
private MethodInfo SetFanSpeedPrecentage;
private MethodInfo SetFanSpeedToAutoControl;
private MethodInfo SetFanSpeedToManualControl;
public MainWindow()
{
InitializeComponent();
InitialViewModel();
InitialSensorReading();
InitialTray();
InitialWCFServer();
InitialFanController();
InitialTDPAdjustor();
InitialBatteryStatusReading();
InitialDialog();
}
private void InitialDialog()
{
this.TDPWindowDialog = new TDPWindow();
this.TDPWindowDialog.DataContext = this.ViewModel;
this.SettingWindowDialog = new SettingWindow();
this.SettingWindowDialog.DataContext = this.ViewModel;
}
private void InitialSensorReading()
{
HWM = new HardwareMonitor(this.ViewModel);
Task t = new Task(() =>
{
while (true)
{
HWM.Update();
if(this.ViewModel.Machine == MachineType.None)
this.ViewModel.Machine = Operation.DetermineMachineType(this.ViewModel.MachineName);
if (this.ViewModel.Machine != MachineType.None) {
this.ViewModel.IsSupportedMachine= true;
if(this.GetFanSpeedPrecentage == null)
this.GetFanSpeedPrecentage = typeof(Operation).GetMethod($"Get{this.ViewModel.Machine}FanSpeedPrecentage");
this.ViewModel.FanSpeedPrecentage = (int)(Convert.ToDouble(this.GetFanSpeedPrecentage.Invoke(null, null)) * 100 / (double)byte.MaxValue);
this.ViewModel.FanSpeed = this.ViewModel.FanSpeedPrecentage * Convert.ToInt32(this.ViewModel.Machine) / 100;
if (!this.ViewModel.IsFanSpeedManualControlEnabled) this.ViewModel.ApplyFanSpeedPrecentage = this.ViewModel.FanSpeedPrecentage;
}
else
{
this.ViewModel.IsSupportedMachine = false;
}
Thread.Sleep(1000);
}
});
t.Start();
}
private void InitialBatteryStatusReading()
{
Task t = new Task(() =>
{
while (true)
{
ManagementObjectEnumerator mom = new ManagementClass("Win32_Battery").GetInstances().GetEnumerator();
if (mom.MoveNext())
{
this.ViewModel.EstimatedChargeRemaining = int.Parse(mom.Current.Properties["EstimatedChargeRemaining"].Value.ToString());
this.ViewModel.EstimatedRunTime = int.Parse(mom.Current.Properties["EstimatedRunTime"].Value.ToString());
}
Thread.Sleep(5000);
}
});
t.Start();
}
private void InitialFanController()
{
this.FanSpeedPrecentageQueue = new ConcurrentQueue<int>();
this.ViewModel.IsFanSpeedManualControlEnabled = false;
Task t = new Task(() =>
{
while (true)
{
if (this.ViewModel.Machine == MachineType.None || FanSpeedPrecentageQueue.IsEmpty)
{
Thread.Sleep(500);
continue;
}
Thread.Sleep(100);
if (FanSpeedPrecentageQueue.TryDequeue(out var fanSpeedPrecentage) && this.ViewModel.IsFanSpeedManualControlEnabled)
{
if (this.SetFanSpeedPrecentage == null)
this.SetFanSpeedPrecentage = typeof(Operation).GetMethod($"Set{this.ViewModel.Machine}FanSpeedPrecentage");
this.SetFanSpeedPrecentage.Invoke(null, new object[] { (byte)fanSpeedPrecentage });
}
}
});
t.Start();
}
private void InitialViewModel()
{
if (File.Exists(this.FilePath))
{
this.ViewModel = JsonConvert.DeserializeObject<ViewModel>(File.ReadAllText(FilePath));
}
else
{
this.ViewModel = new ViewModel();
File.WriteAllText(this.FilePath, JsonConvert.SerializeObject(this.ViewModel));
}
this.ViewModel.IsEditModeEnabled = false;
this.DataContext = this.ViewModel;
this.ViewModel.PropertyChanged += OnPropertyChanged;
}
private void InitialTDPAdjustor()
{
this.TDPQueue = new ConcurrentQueue<int>();
Task t = new Task(() =>
{
while (true)
{
if (TDPQueue.IsEmpty)
{
Thread.Sleep(500);
continue;
}
if(TDPQueue.TryDequeue(out var tdp)) {
Operation.Adjust(
new string[]{ "a", "b", "c"},
new int[] { this.ViewModel.ApplyTDP, this.ViewModel.ApplyTDP, this.ViewModel.ApplyTDP });
}
}
});
t.Start();
}
private void InitialWCFServer()
{
ServiceHost host = new ServiceHost(typeof(MainService));
host.Open();
}
private void InitialTray()
{
TrayIcon = new NotifyIcon();
TrayIcon.Text = this.Title;
TrayIcon.Visible = true;
TrayIcon.Icon = new Icon(@"./TrayIcon.ico");
TrayIcon.Click += TrayIcon_Click;
}
private void ChangeSudokuButtonStyle(System.Windows.Controls.Button button)
{
var grid = button.Parent as System.Windows.Controls.Grid;
foreach (var child in grid.Children)
{
var sudokuButton = child as System.Windows.Controls.Button;
sudokuButton.Background = System.Windows.Media.Brushes.Transparent;
}
button.Background = System.Windows.Media.Brushes.Aqua;
}
private void CenterizeWindowRelativeToMainWindow(Window window)
{
double[] d = new double[2];
d[0] = this.Top + this.Height / 2;
d[1] = this.Left + this.Width / 2;
window.WindowStartupLocation = WindowStartupLocation.Manual;
window.Top = d[0] - window.Height / 2;
window.Left = d[1] - window.Width / 2;
}
private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if(e.PropertyName == nameof(this.ViewModel.ApplyTDP))
TDPQueue.Enqueue(this.ViewModel.ApplyTDP);
if(e.PropertyName == nameof(this.ViewModel.ApplyFanSpeedPrecentage) && this.ViewModel.IsFanSpeedManualControlEnabled)
FanSpeedPrecentageQueue.Enqueue(this.ViewModel.ApplyFanSpeedPrecentage);
}
private void AdjustButton_Click(object sender, RoutedEventArgs e)
{
System.Windows.Controls.Button clickedButton = sender as System.Windows.Controls.Button;
int tdp = Convert.ToInt32(clickedButton.Content.ToString());
this.ViewModel.ApplyTDP = tdp;
}
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == System.Windows.Input.MouseButton.Left)
this.DragMove();
}
private void Window_Closing(object sender, CancelEventArgs e)
{
this.TrayIcon.Visible = false;
this.TrayIcon.Dispose();
Operation.DisposeKeyboardHook();
File.WriteAllText(this.FilePath, JsonConvert.SerializeObject(this.ViewModel));
}
private void MinimizeButton_Click(object sender, RoutedEventArgs e)
{
this.Visibility = Visibility.Hidden;
this.TrayIcon.Visible = true;
}
private void TrayIcon_Click(object sender, EventArgs e)
{
this.Visibility = Visibility.Visible;
this.TrayIcon.Visible = false;
this.Activate();
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
File.WriteAllText(this.FilePath, JsonConvert.SerializeObject(this.ViewModel));
this.TDPWindowDialog?.Close();
this.MachineWindow?.Close();
this.SettingWindowDialog?.Close();
this.Close();
}
private void SudokuButton_Click(object sender, RoutedEventArgs e)
{
var button = e.OriginalSource as System.Windows.Controls.Button;
ChangeSudokuButtonStyle(button);
this.ViewModel.SelectedPresetTDPIndex = int.Parse(button.Name.Split('_')[1]);
if (this.ViewModel.IsEditModeEnabled)
{
this.TDPWindowDialog.WindowStartupLocation = WindowStartupLocation.Manual;
this.TDPWindowDialog.Top = this.Top;
this.TDPWindowDialog.Left = this.Left + this.Width + 5;
}
else {
this.ViewModel.ApplyTDP = this.ViewModel.PresetTDP[this.ViewModel.SelectedPresetTDPIndex];
}
}
private void SortButton_Click(object sender, RoutedEventArgs e)
{
var newPresetTDP = this.ViewModel.PresetTDP.ToArray<int>();
Array.Sort(newPresetTDP);
this.ViewModel.PresetTDP = new ObservableCollection<int>(newPresetTDP);
}
private void EditModeCheckBox_Checked(object sender, RoutedEventArgs e)
{
this.TDPWindowDialog.Top = this.Top;
this.TDPWindowDialog.Left = this.Left + this.Width + 5;
}
private void GlobalSettingButton_Click(object sender, RoutedEventArgs e)
{
CenterizeWindowRelativeToMainWindow(SettingWindowDialog);
this.SettingWindowDialog.Visibility = Visibility.Visible;
this.SettingWindowDialog.Show();
this.SettingWindowDialog.Activate();
}
private void MachineSettingButton_Click(object sender, RoutedEventArgs e)
{
if (this.ViewModel.Machine != MachineType.None)
{
if (this.MachineWindow == null)
{
Type machineWindowType =
Type.GetType($"{MethodBase.GetCurrentMethod().DeclaringType.Namespace}.{this.ViewModel.Machine}Window");
this.MachineWindow = (Window)Activator.CreateInstance(machineWindowType);
this.MachineWindow.DataContext = this.ViewModel;
}
CenterizeWindowRelativeToMainWindow(this.MachineWindow);
this.MachineWindow.Visibility = Visibility.Visible;
this.MachineWindow.Show();
this.MachineWindow.Activate();
}
}
private void FanSpeedControlCheckBox_Checked(object sender, RoutedEventArgs e)
{
if (this.SetFanSpeedToManualControl == null)
this.SetFanSpeedToManualControl = typeof(Operation).GetMethod($"Set{this.ViewModel.Machine}FanSpeedToManualControl");
this.SetFanSpeedToManualControl.Invoke(null, null);
}
private void FanSpeedControlCheckBox_Unchecked(object sender, RoutedEventArgs e)
{
if (this.SetFanSpeedToAutoControl == null)
this.SetFanSpeedToAutoControl = typeof(Operation).GetMethod($"Set{this.ViewModel.Machine}FanSpeedToAutoControl");
this.SetFanSpeedToAutoControl.Invoke(null, null);
}
private void CurveButton_Click(object sender, RoutedEventArgs e)
{
CurveWindow curveWindow = new CurveWindow();
curveWindow.Show();
}
private void TargetFPSModelRadioButton_Checked(object sender, RoutedEventArgs e)
{
var radioButton = e.Source as System.Windows.Controls.RadioButton;
int targetFPS = 0;
if (radioButton.Content.ToString() != "∞")
int.TryParse(radioButton.Content.ToString(), out targetFPS);
this.ViewModel.ApplyTargetFPS = targetFPS;
var from = Operation.GetTargetFPS();
Operation.SetTargetFPS(this.ViewModel.ApplyTargetFPS);
Console.WriteLine($"{from} => {Operation.GetTargetFPS()}");
}
private void TargetFPSModeCheckBox_Checked(object sender, RoutedEventArgs e)
{
if (!Operation.IsRTSSExisted() || !Operation.IsRTSSRunning())
{
if (System.Windows.MessageBox.Show(
"RTSS is not installed on this machine or not running properly.", "warning", MessageBoxButton.OK) == MessageBoxResult.OK)
{
this.ViewModel.IsTargetFPSModelEnabled = false;
}
}
}
//private void WindowsXamlHost_ChildChanged(object sender, EventArgs e)
//{
// var host = (WindowsXamlHost)sender;
// var slider = (Windows.UI.Xaml.Controls.Slider)host.Child;
// slider.Orientation = Windows.UI.Xaml.Controls.Orientation.Horizontal;
//}
private void TargetFPSModeCheckBox_Unchecked(object sender, RoutedEventArgs e)
{
this.ViewModel.ApplyTargetFPS = 0;
if (Operation.IsRTSSExisted() && Operation.IsRTSSRunning())
Operation.SetTargetFPS(this.ViewModel.ApplyTargetFPS);
}
}
}