-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathFanCurveForm.cs
More file actions
524 lines (436 loc) · 17.4 KB
/
Copy pathFanCurveForm.cs
File metadata and controls
524 lines (436 loc) · 17.4 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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace OmenSuperHub {
internal enum FanCurveEditorResult {
Cancelled,
Saved,
SavedAndApplied
}
public sealed class FanCurveForm : Form {
private const string SeriesName = "FanSpeed";
private const int PointHitRadius = 12;
private const int PointHitRadiusSquared = PointHitRadius * PointHitRadius;
private static readonly Color PageBackColor = Color.FromArgb(245, 247, 250);
private static readonly Color CardBackColor = Color.White;
private static readonly Color BorderColor = Color.FromArgb(220, 225, 230);
private static readonly Color AccentColor = Color.FromArgb(0, 122, 204);
private static readonly Color MarkerColor = Color.FromArgb(220, 53, 69);
private static readonly Color GridColor = Color.FromArgb(232, 236, 240);
private static readonly Color AxisLineColor = Color.FromArgb(200, 205, 210);
private readonly Chart cpuChart;
private readonly Chart gpuChart;
private readonly int cpuTemperatureMaximum;
private readonly int gpuTemperatureMaximum;
private readonly int fanSpeedMaximum;
private readonly string customFilePath;
private readonly ToolTip pointToolTip;
private Chart draggingChart;
private DataPoint draggingPoint;
internal FanCurveEditorResult EditorResult { get; private set; }
internal FanCurveForm(
FanCurveProfile initialProfile,
int cpuTemperatureMaximum,
int gpuTemperatureMaximum,
int fanSpeedMaximum,
string customFilePath,
string windowTitle) {
this.cpuTemperatureMaximum = Math.Max(1, cpuTemperatureMaximum);
this.gpuTemperatureMaximum = Math.Max(1, gpuTemperatureMaximum);
this.fanSpeedMaximum = Math.Max(100, fanSpeedMaximum);
this.customFilePath = customFilePath;
pointToolTip = new ToolTip {
AutomaticDelay = 0,
AutoPopDelay = 2000,
InitialDelay = 0,
ReshowDelay = 0,
ShowAlways = true
};
Text = windowTitle;
Icon = Properties.Resources.fan;
StartPosition = FormStartPosition.CenterScreen;
FormBorderStyle = FormBorderStyle.Sizable;
MaximizeBox = true;
MinimizeBox = true;
BackColor = PageBackColor;
Rectangle screenBounds = Screen.PrimaryScreen.Bounds;
Size = new Size(screenBounds.Width * 3 / 4, screenBounds.Height * 3 / 4);
MinimumSize = new Size(1280, 720);
AutoScaleMode = AutoScaleMode.Dpi;
var rootLayout = BuildRootLayout();
cpuChart = CreateChart("CPU", this.cpuTemperatureMaximum);
gpuChart = CreateChart("GPU", this.gpuTemperatureMaximum);
rootLayout.Controls.Add(cpuChart, 0, 1);
rootLayout.Controls.Add(gpuChart, 1, 1);
Panel rightPanel = BuildActionPanel(out Button saveButton, out Button saveAndApplyButton, out Button cancelButton, out Button loadButton);
rootLayout.Controls.Add(rightPanel, 2, 0);
rootLayout.SetRowSpan(rightPanel, 2);
Controls.Add(rootLayout);
AcceptButton = saveButton;
CancelButton = cancelButton;
saveButton.Click += (sender, args) => SaveAndClose(FanCurveEditorResult.Saved);
saveAndApplyButton.Click += (sender, args) => SaveAndClose(FanCurveEditorResult.SavedAndApplied);
cancelButton.Click += (sender, args) => {
DialogResult = DialogResult.Cancel;
Close();
};
loadButton.Click += LoadButton_Click;
ApplyProfile(initialProfile);
}
protected override CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // WS_EX_COMPOSITED
return cp;
}
}
private TableLayoutPanel BuildRootLayout() {
var rootLayout = new TableLayoutPanel {
Dock = DockStyle.Fill,
ColumnCount = 3,
RowCount = 2,
Padding = new Padding(16),
BackColor = PageBackColor
};
rootLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
rootLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
rootLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 210F));
rootLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, 52F));
rootLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
var instructions = new Label {
AutoSize = false,
Dock = DockStyle.Fill,
Text = Strings.FanCurveInstructions,
TextAlign = ContentAlignment.MiddleLeft,
Padding = new Padding(8, 0, 8, 0),
ForeColor = Color.FromArgb(70, 70, 70),
Font = new Font(Font.FontFamily, 12F, FontStyle.Regular)
};
rootLayout.Controls.Add(instructions, 0, 0);
rootLayout.SetColumnSpan(instructions, 2);
return rootLayout;
}
private Panel BuildActionPanel(
out Button saveButton,
out Button saveAndApplyButton,
out Button cancelButton,
out Button loadButton) {
var panel = new Panel {
Dock = DockStyle.Fill,
Padding = new Padding(12, 8, 0, 0),
BackColor = PageBackColor
};
var inner = new FlowLayoutPanel {
Dock = DockStyle.Top,
FlowDirection = FlowDirection.TopDown,
WrapContents = false,
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowAndShrink,
BackColor = PageBackColor
};
saveButton = CreateActionButton(Strings.FanCurveSave);
saveAndApplyButton = CreateActionButton(Strings.FanCurveSaveAndApply);
cancelButton = CreateActionButton(Strings.FanCurveCancel);
loadButton = CreateActionButton(Strings.FanCurveLoad);
inner.Controls.Add(saveButton);
inner.Controls.Add(saveAndApplyButton);
inner.Controls.Add(cancelButton);
inner.Controls.Add(loadButton);
panel.Controls.Add(inner);
return panel;
}
private Button CreateActionButton(string text) {
return new Button {
Text = text,
Width = 190,
Height = 60,
Margin = new Padding(0, 0, 0, 20),
FlatStyle = FlatStyle.Flat,
BackColor = CardBackColor,
ForeColor = Color.FromArgb(40, 40, 40),
Font = new Font(Font.FontFamily, 12F, FontStyle.Regular),
Cursor = Cursors.Hand
}.Also(button => {
button.FlatAppearance.BorderColor = BorderColor;
button.FlatAppearance.BorderSize = 1;
button.FlatAppearance.MouseOverBackColor = Color.FromArgb(238, 243, 248);
button.FlatAppearance.MouseDownBackColor = Color.FromArgb(225, 232, 240);
});
}
private Chart CreateChart(string cpuOrGpu, int temperatureMaximum) {
var chart = new Chart {
Dock = DockStyle.Fill,
BackColor = PageBackColor,
Margin = new Padding(4, 4, 8, 4),
Cursor = Cursors.Cross
};
chart.AntiAliasing = AntiAliasingStyles.All;
chart.TextAntiAliasingQuality =
TextAntiAliasingQuality.High;
var chartArea = new ChartArea("FanSpeedArea") {
BackColor = CardBackColor
};
chartArea.AxisX.Minimum = 0;
chartArea.AxisX.Maximum = temperatureMaximum;
chartArea.AxisX.Interval = 10;
chartArea.AxisX.Title = $"{cpuOrGpu} {Strings.FanCurveTemperatureAxis}";
chartArea.AxisX.LineColor = AxisLineColor;
chartArea.AxisX.MajorGrid.LineColor = GridColor;
chartArea.AxisX.LabelStyle.ForeColor = Color.FromArgb(90, 90, 90);
chartArea.AxisX.TitleForeColor = Color.FromArgb(80, 80, 80);
chartArea.AxisX.TitleFont = new Font(Font.FontFamily, 12F, FontStyle.Bold);
chartArea.AxisY.Minimum = 0;
chartArea.AxisY.Maximum = fanSpeedMaximum;
chartArea.AxisY.Interval = GetFanSpeedInterval(fanSpeedMaximum);
chartArea.AxisY.Title = Strings.FanCurveFanSpeedAxis;
chartArea.AxisY.LabelStyle.Format = "0";
chartArea.AxisY.LineColor = AxisLineColor;
chartArea.AxisY.MajorGrid.LineColor = GridColor;
chartArea.AxisY.LabelStyle.ForeColor = Color.FromArgb(90, 90, 90);
chartArea.AxisY.TitleForeColor = Color.FromArgb(80, 80, 80);
chartArea.AxisY.TitleFont = new Font(Font.FontFamily, 12F, FontStyle.Bold);
chart.ChartAreas.Add(chartArea);
var series = new Series(SeriesName) {
ChartType = SeriesChartType.Line,
BorderWidth = 3,
Color = AccentColor,
MarkerStyle = MarkerStyle.Circle,
MarkerSize = 8,
MarkerColor = MarkerColor,
MarkerBorderColor = CardBackColor,
MarkerBorderWidth = 1,
ToolTip = "#VALX°C, #VALY RPM"
};
chart.Series.Add(series);
chart.MouseDown += Chart_MouseDown;
chart.MouseMove += Chart_MouseMove;
chart.MouseUp += Chart_MouseUp;
chart.MouseLeave += (sender, args) => pointToolTip.Hide(chart);
return chart;
}
private static int GetFanSpeedInterval(int maximum) {
if (maximum <= 3000) return 500;
if (maximum <= 7000) return 1000;
return 2000;
}
private void ApplyProfile(FanCurveProfile profile) {
ValidateProfileForMachine(profile);
SetSeriesPoints(cpuChart, profile.CpuPoints);
SetSeriesPoints(gpuChart, profile.GpuPoints);
}
private static void SetSeriesPoints(Chart chart, IEnumerable<FanCurvePoint> points) {
Series series = chart.Series[SeriesName];
series.Points.Clear();
foreach (FanCurvePoint point in points.OrderBy(item => item.Temperature)) {
series.Points.Add(CreateDataPoint(point.Temperature, point.FanSpeed));
}
chart.Invalidate();
}
private static DataPoint CreateDataPoint(int temperature, int fanSpeed) {
var point = new DataPoint {
XValue = temperature
};
point.YValues = new double[] { fanSpeed };
return point;
}
private void Chart_MouseDown(object sender, MouseEventArgs e) {
Chart chart = sender as Chart;
if (chart == null) return;
DataPoint point = FindPoint(chart, e.Location);
if (e.Button == MouseButtons.Right) {
if (point != null && chart.Series[SeriesName].Points.Count > 2) {
chart.Series[SeriesName].Points.Remove(point);
chart.Invalidate();
}
return;
}
if (e.Button != MouseButtons.Left) return;
if (point != null) {
draggingChart = chart;
draggingPoint = point;
chart.Cursor = Cursors.SizeAll;
return;
}
double temperature;
double fanSpeed;
if (!TryGetChartValues(chart, e.Location, out temperature, out fanSpeed)) return;
int roundedTemperature = Clamp((int)Math.Round(temperature), 0, GetTemperatureMaximum(chart));
int roundedFanSpeed = Clamp(RoundFanSpeed(fanSpeed), 0, fanSpeedMaximum);
DataPoint existingPoint = chart.Series[SeriesName].Points
.FirstOrDefault(candidate => (int)Math.Round(candidate.XValue) == roundedTemperature);
if (existingPoint != null) {
draggingChart = chart;
draggingPoint = existingPoint;
chart.Cursor = Cursors.SizeAll;
return;
}
DataPoint newPoint = CreateDataPoint(roundedTemperature, roundedFanSpeed);
InsertPointSorted(chart.Series[SeriesName].Points, newPoint);
chart.Invalidate();
}
private void Chart_MouseMove(object sender, MouseEventArgs e) {
Chart chart = sender as Chart;
if (chart == null) return;
if (draggingChart == chart && draggingPoint != null) {
DragPoint(chart, e.Location);
return;
}
DataPoint point = FindPoint(chart, e.Location);
if (point == null) {
pointToolTip.Hide(chart);
chart.Cursor = Cursors.Cross;
return;
}
chart.Cursor = Cursors.Hand;
pointToolTip.Show(
$"{(int)Math.Round(point.XValue)}°C, {(int)Math.Round(point.YValues[0])} RPM",
chart,
e.X + 14,
e.Y - 28,
800);
}
private void Chart_MouseUp(object sender, MouseEventArgs e) {
Chart chart = sender as Chart;
if (chart != null) chart.Cursor = Cursors.Cross;
draggingChart = null;
draggingPoint = null;
}
private void DragPoint(Chart chart, Point location) {
double temperature;
double fanSpeed;
if (!TryGetChartValues(chart, location, out temperature, out fanSpeed)) return;
Series series = chart.Series[SeriesName];
List<DataPoint> orderedPoints = series.Points.OrderBy(item => item.XValue).ToList();
int pointIndex = orderedPoints.IndexOf(draggingPoint);
if (pointIndex < 0) return;
int minimumTemperature = pointIndex == 0
? 0
: (int)Math.Round(orderedPoints[pointIndex - 1].XValue) + 1;
int maximumTemperature = pointIndex == orderedPoints.Count - 1
? GetTemperatureMaximum(chart)
: (int)Math.Round(orderedPoints[pointIndex + 1].XValue) - 1;
draggingPoint.XValue = Clamp((int)Math.Round(temperature), minimumTemperature, maximumTemperature);
draggingPoint.YValues[0] = Clamp(RoundFanSpeed(fanSpeed), 0, fanSpeedMaximum);
series.Points.Remove(draggingPoint);
InsertPointSorted(series.Points, draggingPoint);
chart.Invalidate();
}
private static void InsertPointSorted(DataPointCollection points, DataPoint newPoint) {
int insertIndex = 0;
while (insertIndex < points.Count && points[insertIndex].XValue < newPoint.XValue) {
insertIndex++;
}
points.Insert(insertIndex, newPoint);
}
private DataPoint FindPoint(Chart chart, Point mouseLocation) {
foreach (DataPoint point in chart.Series[SeriesName].Points) {
double pointX = chart.ChartAreas[0].AxisX.ValueToPixelPosition(point.XValue);
double pointY = chart.ChartAreas[0].AxisY.ValueToPixelPosition(point.YValues[0]);
double dx = pointX - mouseLocation.X;
double dy = pointY - mouseLocation.Y;
double distanceSquared = dx * dx + dy * dy;
if (distanceSquared <= PointHitRadiusSquared) {
return point;
}
}
return null;
}
private static bool TryGetChartValues(Chart chart, Point location, out double xValue, out double yValue) {
xValue = 0;
yValue = 0;
try {
ChartArea area = chart.ChartAreas[0];
xValue = area.AxisX.PixelPositionToValue(location.X);
yValue = area.AxisY.PixelPositionToValue(location.Y);
return xValue >= area.AxisX.Minimum &&
xValue <= area.AxisX.Maximum &&
yValue >= area.AxisY.Minimum &&
yValue <= area.AxisY.Maximum;
} catch (ArgumentException) {
return false;
}
}
private void LoadButton_Click(object sender, EventArgs e) {
using (var dialog = new OpenFileDialog {
Filter = Strings.FanCurveFileFilter,
InitialDirectory = AppDomain.CurrentDomain.BaseDirectory,
CheckFileExists = true,
Multiselect = false
}) {
if (dialog.ShowDialog(this) != DialogResult.OK) return;
try {
ApplyProfile(FanCurveProfile.Load(dialog.FileName));
} catch (Exception ex) when (
ex is IOException ||
ex is UnauthorizedAccessException ||
ex is InvalidDataException) {
ShowError(Strings.FanCurveLoadFailed, ex);
}
}
}
private void SaveAndClose(FanCurveEditorResult result) {
try {
FanCurveProfile profile = GetProfile();
ValidateProfileForMachine(profile);
profile.Save(customFilePath);
EditorResult = result;
DialogResult = DialogResult.OK;
Close();
} catch (Exception ex) when (
ex is IOException ||
ex is UnauthorizedAccessException ||
ex is InvalidDataException) {
ShowError(Strings.FanCurveSaveFailed, ex);
}
}
private void ShowError(string message, Exception ex) {
MessageBox.Show(
this,
message + Environment.NewLine + ex.Message,
Strings.Error,
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
private FanCurveProfile GetProfile() {
return new FanCurveProfile(GetPoints(cpuChart), GetPoints(gpuChart));
}
private static List<FanCurvePoint> GetPoints(Chart chart) {
return chart.Series[SeriesName].Points
.Select(point => new FanCurvePoint(
(int)Math.Round(point.XValue),
(int)Math.Round(point.YValues[0])))
.OrderBy(point => point.Temperature)
.ToList();
}
private void ValidateProfileForMachine(FanCurveProfile profile) {
ValidatePointsForMachine(profile.CpuPoints, cpuTemperatureMaximum);
ValidatePointsForMachine(profile.GpuPoints, gpuTemperatureMaximum);
}
private void ValidatePointsForMachine(IList<FanCurvePoint> points, int temperatureMaximum) {
if (points.Count < 2 ||
points.Any(point => point.FanSpeed < 0) ||
points.GroupBy(point => point.Temperature).Any(group => group.Count() > 1)) {
throw new InvalidDataException(Strings.FanCurveOutOfRange);
}
}
private int GetTemperatureMaximum(Chart chart) {
return chart == cpuChart ? cpuTemperatureMaximum : gpuTemperatureMaximum;
}
private static int RoundFanSpeed(double fanSpeed) {
return (int)(Math.Round(fanSpeed / 100D) * 100D);
}
private static int Clamp(int value, int minimum, int maximum) {
return Math.Max(minimum, Math.Min(maximum, value));
}
}
internal static class ObjectExtensions {
internal static T Also<T>(this T value, Action<T> action) {
action(value);
return value;
}
}
}