Repository navigation
Expand file tree
/
Copy pathExtraForm.cs
More file actions
353 lines (319 loc) · 14.4 KB
/
Copy pathExtraForm.cs
File metadata and controls
353 lines (319 loc) · 14.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
using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using MHelper.Hardware;
namespace MHelper
{
public sealed class ExtraForm : Form
{
private static readonly Color BgWindow = Color.FromArgb(24, 26, 31);
private static readonly Color BgCard = Color.FromArgb(34, 38, 46);
private static readonly Color BgButton = Color.FromArgb(46, 51, 62);
private static readonly Color BgButtonHover = Color.FromArgb(58, 64, 78);
private static readonly Color BorderSubtle = Color.FromArgb(56, 62, 74);
private static readonly Color TextPrimary = Color.FromArgb(242, 244, 248);
private static readonly Color TextMuted = Color.FromArgb(156, 163, 175);
private static readonly Color AccentRed = Color.FromArgb(225, 29, 72);
private static readonly Color AccentGreen = Color.FromArgb(16, 185, 129);
private static readonly Color AccentAmber = Color.FromArgb(245, 158, 11);
private readonly MsiAcpi _acpi;
private readonly AppSettings _settings;
private readonly UiCategorySettings _cats;
private readonly float _dpi;
private CheckBox _chkWebcam;
private CheckBox _chkFnWinSwap;
private Label _lblMsiSvc;
private Button _btnStopSvc;
private Button _btnStartSvc;
private Label _lblStatus;
private bool _loading;
private bool _busyServices;
private int _refreshInFlight;
private int S(int px) { return (int)Math.Round(px * _dpi); }
public ExtraForm(MsiAcpi acpi, AppSettings settings)
{
_acpi = acpi;
_settings = settings;
_cats = settings.GetCategories();
AutoScaleMode = AutoScaleMode.None;
_dpi = UiDpi.GetScale(this);
Text = "M-Helper — Extra Settings";
FormBorderStyle = FormBorderStyle.None;
ShowInTaskbar = false;
StartPosition = FormStartPosition.CenterScreen;
TopMost = true;
BackColor = BgWindow;
ForeColor = TextPrimary;
Font = UiFonts.DialogBody;
DoubleBuffered = true;
KeyPreview = true;
KeyDown += (s, e) => { if (e.KeyCode == Keys.Escape && !_busyServices) Close(); };
ClientSize = new Size(S(436), S(320));
BuildUi();
Shown += delegate { RefreshStateAsync(); };
}
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
UiChrome.ApplyRoundedDarkBorder(Handle, BorderSubtle);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
using (var pen = new Pen(BorderSubtle, 1))
e.Graphics.DrawRectangle(pen, 0, 0, Width - 1, Height - 1);
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (_busyServices && e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
return;
}
base.OnFormClosing(e);
}
private void BuildUi()
{
int pad = S(14);
int cpad = S(12);
int w = ClientSize.Width - pad * 2;
// --- CUSTOM DRAGGABLE DARK HEADER ---
var hdr = new Panel { Location = new Point(1, 1), Size = new Size(ClientSize.Width - 2, S(40)), BackColor = BgWindow };
var lblHdr = new Label
{
Text = "Extra Settings",
Font = UiFonts.SubWindowTitle,
ForeColor = TextPrimary,
Location = new Point(pad - 1, S(10)),
AutoSize = true,
UseMnemonic = false
};
var btnClose = new Button
{
Text = "✕",
FlatStyle = FlatStyle.Flat,
ForeColor = TextMuted,
BackColor = BgWindow,
Size = new Size(S(28), S(26)),
Location = new Point(hdr.Width - pad - S(26), S(7)),
Cursor = Cursors.Hand,
TabStop = false,
UseMnemonic = false
};
btnClose.FlatAppearance.BorderSize = 0;
btnClose.FlatAppearance.MouseOverBackColor = BgCard;
btnClose.Click += delegate { if (!_busyServices) Close(); };
hdr.MouseDown += (s, e) => UiChrome.BeginDragWindow(this, e);
lblHdr.MouseDown += (s, e) => UiChrome.BeginDragWindow(this, e);
hdr.Controls.AddRange(new Control[] { lblHdr, btnClose });
Controls.Add(hdr);
int y = hdr.Bottom + S(4);
// --- CARD 1: VERIFIED EC HARDWARE SWITCHES ---
if (_cats.ShowHardwareSwitches)
{
var cardHw = CreateCard(pad, y, w, S(116), "HARDWARE SWITCHES");
_chkWebcam = new CheckBox
{
Text = "Webcam Hardware Power",
ForeColor = TextPrimary,
Location = new Point(cpad, S(32)),
AutoSize = true,
UseMnemonic = false,
Enabled = false
};
_chkWebcam.CheckedChanged += delegate
{
if (_loading || _acpi == null || !_acpi.CanWrite) return;
bool ok = _acpi.SetWebcamEnabled(_chkWebcam.Checked);
SetStatus(ok ? ("Webcam hardware switch " + (_chkWebcam.Checked ? "ON." : "OFF.")) : "Failed to toggle webcam switch.", !ok);
RefreshStateAsync();
};
_chkFnWinSwap = new CheckBox
{
Text = "Swap Fn ↔ Windows Key",
ForeColor = TextPrimary,
Location = new Point(cpad, S(60)),
AutoSize = true,
UseMnemonic = false,
Enabled = false
};
_chkFnWinSwap.CheckedChanged += delegate
{
if (_loading || _acpi == null || !_acpi.CanWrite) return;
bool ok = _acpi.SetFnWinSwap(_chkFnWinSwap.Checked);
SetStatus(ok ? ("Fn ↔ Windows key swap " + (_chkFnWinSwap.Checked ? "ON." : "OFF.")) : "Failed to toggle Fn/Win swap.", !ok);
RefreshStateAsync();
};
var lblKbdNote = new Label
{
Text = "Keyboard RGB is on the main popup (Fn+F8 also works).",
Font = UiFonts.Regular80,
ForeColor = TextMuted,
Location = new Point(cpad, S(89)),
Size = new Size(w - cpad * 2, S(18)),
UseMnemonic = false
};
cardHw.Controls.AddRange(new Control[] { _chkWebcam, _chkFnWinSwap, lblKbdNote });
Controls.Add(cardHw);
y += cardHw.Height + S(10);
}
// --- CARD 2: MSI BACKGROUND SERVICES ---
if (_cats.ShowMsiServices)
{
var cardSvc = CreateCard(pad, y, w, S(102), "MSI CENTER BACKGROUND SERVICES");
_lblMsiSvc = new Label
{
Text = "Checking MSI services...",
ForeColor = TextMuted,
Font = UiFonts.Regular85,
Location = new Point(cpad, S(30)),
Size = new Size(w - cpad * 2, S(18)),
UseMnemonic = false
};
int bw2 = (w - cpad * 2 - S(8)) / 2;
_btnStopSvc = CreateButton("Stop & Disable MSI", cpad, S(56), bw2, S(30));
_btnStopSvc.BackColor = AccentRed;
_btnStopSvc.FlatAppearance.BorderColor = AccentRed;
_btnStopSvc.Click += delegate { RunServiceAction(true); };
_btnStartSvc = CreateButton("Restore MSI Services", cpad + bw2 + S(8), S(56), bw2, S(30));
_btnStartSvc.Click += delegate { RunServiceAction(false); };
cardSvc.Controls.AddRange(new Control[] { _lblMsiSvc, _btnStopSvc, _btnStartSvc });
Controls.Add(cardSvc);
y += cardSvc.Height + S(10);
}
_lblStatus = new Label
{
Text = "",
ForeColor = TextMuted,
Font = UiFonts.Regular82,
Location = new Point(pad, y),
Size = new Size(w, S(20)),
UseMnemonic = false
};
Controls.Add(_lblStatus);
y += S(28);
ClientSize = new Size(ClientSize.Width, y);
}
private void RunServiceAction(bool stopAndDisable)
{
if (_busyServices || _btnStopSvc == null) return;
_busyServices = true;
_btnStopSvc.Enabled = _btnStartSvc.Enabled = false;
SetStatus(stopAndDisable ? "Stopping & disabling MSI services..." : "Restoring MSI services...", false);
ThreadPool.QueueUserWorkItem(delegate
{
int count = stopAndDisable
? SystemTuning.StopAndDisableMsiServices(_settings)
: SystemTuning.EnableAndStartMsiServices(_settings);
if (!IsDisposed && IsHandleCreated)
{
try
{
BeginInvoke((Action)(() =>
{
_busyServices = false;
if (IsDisposed || _btnStopSvc == null) return;
_btnStopSvc.Enabled = _btnStartSvc.Enabled = true;
SetStatus((stopAndDisable ? "Stopped & disabled " : "Restored ") + count + " MSI service(s).", false);
RefreshStateAsync();
}));
}
catch { _busyServices = false; }
}
else
{
_busyServices = false;
}
});
}
private void RefreshStateAsync()
{
if (Interlocked.CompareExchange(ref _refreshInFlight, 1, 0) != 0) return;
ThreadPool.QueueUserWorkItem(delegate
{
try
{
ExtraSwitchState sw = (_cats.ShowHardwareSwitches && _acpi != null)
? _acpi.ReadExtraSwitches()
: new ExtraSwitchState { Ok = false };
int installed = 0;
int running = _cats.ShowMsiServices ? SystemTuning.CountRunningMsiServices(out installed) : 0;
if (!IsDisposed && IsHandleCreated)
{
try
{
BeginInvoke((Action)(() =>
{
if (IsDisposed) return;
_loading = true;
try
{
if (_cats.ShowHardwareSwitches && _chkWebcam != null)
{
bool canEdit = _acpi != null && _acpi.CanWrite && sw.Ok;
if (sw.Ok)
{
_chkWebcam.Checked = sw.WebcamEnabled;
_chkFnWinSwap.Checked = sw.FnWinSwapped;
}
_chkWebcam.Enabled = canEdit;
_chkFnWinSwap.Enabled = canEdit;
}
if (_cats.ShowMsiServices && _lblMsiSvc != null)
{
if (installed == 0)
{
_lblMsiSvc.Text = "No MSI Center services installed (clean!).";
_lblMsiSvc.ForeColor = AccentGreen;
}
else
{
_lblMsiSvc.Text = string.Format("MSI Services running: {0} / {1} installed", running, installed);
_lblMsiSvc.ForeColor = running > 0 ? AccentAmber : AccentGreen;
}
}
}
finally
{
_loading = false;
}
}));
}
catch { }
}
}
finally
{
Interlocked.Exchange(ref _refreshInFlight, 0);
}
});
}
private void SetStatus(string msg, bool warn)
{
if (_lblStatus == null) return;
_lblStatus.Text = msg;
_lblStatus.ForeColor = warn ? AccentAmber : AccentGreen;
}
private Panel CreateCard(int x, int y, int w, int h, string heading)
{
var p = new Panel { Location = new Point(x, y), Size = new Size(w, h), BackColor = BgCard };
p.Paint += (s, e) => { using (var pen = new Pen(BorderSubtle, 1)) e.Graphics.DrawRectangle(pen, 0, 0, p.Width - 1, p.Height - 1); };
p.Controls.Add(new Label { Text = heading, Font = UiFonts.CardHeader, ForeColor = TextMuted, Location = new Point(S(12), S(9)), AutoSize = true, UseMnemonic = false });
return p;
}
private Button CreateButton(string text, int x, int y, int w, int h)
{
var btn = new Button
{
Text = text, Location = new Point(x, y), Size = new Size(w, h),
FlatStyle = FlatStyle.Flat, BackColor = BgButton, ForeColor = TextPrimary,
Font = UiFonts.Semibold84, Cursor = Cursors.Hand, TabStop = false, UseMnemonic = false
};
btn.FlatAppearance.BorderColor = BorderSubtle;
btn.FlatAppearance.BorderSize = 1;
btn.FlatAppearance.MouseOverBackColor = BgButtonHover;
return btn;
}
}
}