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.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Threading;
using AdamsToolkit.Core;
namespace AdamsToolkit.Views;
public partial class DashboardView : UserControl
{
private DispatcherTimer? _monitorTimer;
private DispatcherTimer? _pingTimer;
private DispatcherTimer? _tempTimer;
// 60 amostras por série (1/s no monitor, 1/3s nas temperaturas)
private readonly Queue<double> _cpuHistory = new();
private readonly Queue<double> _gpuHistory = new();
private readonly Queue<double> _ramHistory = new();
private readonly Queue<double> _diskHistory = new();
private readonly Queue<double> _cpuTempHistory = new();
private readonly Queue<double> _gpuTempHistory = new();
private bool _pinging;
private bool _readingTemp;
public DashboardView()
{
InitializeComponent();
// Loaded (não construtor): re-lê a preferência sempre que a view volta a
// aparecer — pode ter mudado no menu da bandeja entretanto.
Loaded += (_, _) => { RefreshStartupCheck(); Refresh(); StartMonitor(); AppState.Changed += OnVisibilityChanged; };
Unloaded += (_, _) => { AppState.Changed -= OnVisibilityChanged; StopMonitor(); };
SizeChanged += (_, _) => RedrawSparks(); // largura dos tiles mudou → refazer curvas
}
// ---- iniciar com o Windows ----
private bool _startupSyncing;
private void RefreshStartupCheck()
{
_startupSyncing = true;
StartupCheck.IsChecked = StartupManager.IsEnabled;
_startupSyncing = false;
}
private void Startup_Toggled(object sender, RoutedEventArgs e)
{
if (_startupSyncing) return;
StartupManager.SetEnabled(StartupCheck.IsChecked == true);
}
// ---- ações rápidas (navegam para a secção respetiva) ----
private static void Nav(string key) =>
(Application.Current.MainWindow as MainWindow)?.NavigateTo(key);
private void QuickOptimize_Click(object sender, RoutedEventArgs e) => Nav("optimizer");
private void QuickClean_Click(object sender, RoutedEventArgs e) => Nav("optimizer");
private void QuickFiveM_Click(object sender, RoutedEventArgs e) => Nav("fivem");
// ---- monitor de desempenho ----
private void StartMonitor()
{
GpuNameText.Text = SystemMonitor.GetGpuName();
OsText.Text = SystemMonitor.GetOsName();
OsDetailText.Text = OsText.Text;
ArchText.Text = Environment.Is64BitOperatingSystem ? "64-bit" : "32-bit";
StopMonitor();
// Janela por trás do jogo (ou na bandeja) = ninguém vê os gráficos: não se
// arranca nada. O AppState avisa quando voltar à frente.
if (!AppState.IsVisible) return;
_monitorTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };
_monitorTimer.Tick += async (_, _) => await SampleAsync();
_monitorTimer.Start();
_ = SampleAsync();
// ping: 10s chega para um número que quase não muda
_pingTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(10) };
_pingTimer.Tick += async (_, _) => await UpdatePingAsync();
_pingTimer.Start();
_ = UpdatePingAsync();
// temperaturas: sensores (LHM/WMI/nvidia-smi) são a leitura mais cara — 5s
_tempTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(5) };
_tempTimer.Tick += async (_, _) => await UpdateTempsAsync();
_tempTimer.Start();
_ = UpdateTempsAsync();
}
private void StopMonitor()
{
_monitorTimer?.Stop(); _monitorTimer = null;
_pingTimer?.Stop(); _pingTimer = null;
_tempTimer?.Stop(); _tempTimer = null;
}
/// <summary>App voltou à frente → religa o monitor; saiu de vista → desliga tudo.</summary>
private void OnVisibilityChanged(bool visible)
{
// Nota: os contadores ficam abertos no alt-tab (não custam nada parados e
// reabri-los perderia a 1ª amostra). Só se largam ao ir para a bandeja.
if (visible) StartMonitor();
else StopMonitor();
}
private async Task UpdateTempsAsync()
{
if (_readingTemp) return;
_readingTemp = true;
try
{
var (cpuT, gpuT) = await Task.Run(SystemMonitor.GetTemps);
GpuTempText.Text = gpuT < 0 ? "N/D" : $"{gpuT:0}°C";
if (gpuT >= 0) Push(_gpuTempHistory, gpuT);
// A temperatura do CPU precisa do driver de kernel (admin) e o Windows 11
// com Integridade da Memória bloqueia-o — em muitos PCs é impossível de ler.
// Em vez de mostrar "N/D" (parece avariado), escondemos o campo TEMP CPU
// quando não há leitura; a GPU ocupa a largura toda. Quem consegue ler
// (admin + sem bloqueio) continua a vê-lo normalmente.
if (cpuT < 0)
{
CpuTempTile.Visibility = Visibility.Collapsed;
TempGrid.Columns = 1;
}
else
{
CpuTempText.Text = $"{cpuT:0}°C";
CpuTempTile.Visibility = Visibility.Visible;
TempGrid.Columns = 2;
Push(_cpuTempHistory, cpuT);
}
TempAdminHint.Visibility = Visibility.Collapsed;
DrawSeries(CpuTempSpark, null, CpuTempSparkHost, _cpuTempHistory, TempScale(_cpuTempHistory));
DrawSeries(GpuTempSpark, null, GpuTempSparkHost, _gpuTempHistory, TempScale(_gpuTempHistory));
}
finally { _readingTemp = false; }
}
private void TempAdmin_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
try
{
var exe = Environment.ProcessPath;
if (exe == null) return;
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(exe)
{
UseShellExecute = true,
Verb = "runas", // pede UAC
});
System.Windows.Application.Current.Shutdown();
}
catch { /* UAC recusado — fica em N/D */ }
}
private async Task SampleAsync()
{
var (cpu, gpu, ram, disk) = await Task.Run(() =>
(SystemMonitor.GetCpuUsage(), SystemMonitor.GetGpuUsage(),
SystemMonitor.GetRam(), SystemMonitor.GetSystemDisk()));
CpuText.Text = cpu < 0 ? "—" : $"{cpu:0.0}%";
GpuText.Text = gpu < 0 ? "—" : $"{gpu:0}%";
if (ram.pct < 0) { RamText.Text = "—"; }
else
{
RamText.Text = $"{ram.pct:0}%";
RamDetailText.Text = $"{ram.usedGb:0.0} / {ram.totalGb:0.0} GB";
}
if (disk.pct < 0) { DiskText.Text = "—"; }
else
{
DiskText.Text = $"{disk.pct:0}%";
DiskDetailText.Text = $"{disk.freeGb:0} GB livres";
}
UptimeText.Text = SystemMonitor.GetUptime();
Push(_cpuHistory, Math.Max(0, cpu));
Push(_gpuHistory, Math.Max(0, gpu));
Push(_ramHistory, Math.Max(0, ram.pct));
Push(_diskHistory, Math.Max(0, disk.pct));
SetGauge(CpuArc, cpu);
SetGauge(GpuArc, gpu);
SetGauge(RamArc, ram.pct);
SetGauge(DiskArc, disk.pct);
RedrawSparks();
}
private static void Push(Queue<double> q, double v)
{
q.Enqueue(v);
while (q.Count > 60) q.Dequeue();
}
// temperaturas não são 0–100%: escala com folga por cima do pico
private static double TempScale(Queue<double> q) =>
q.Count == 0 ? 100 : Math.Max(90, Math.Ceiling(q.Max() / 10) * 10 + 5);
private void RedrawSparks()
{
DrawSeries(GpuSpark, null, GpuSparkHost, _gpuHistory, 100);
DrawSeries(RamSpark, null, RamSparkHost, _ramHistory, 100);
DrawSeries(CpuSpark, null, CpuSparkHost, _cpuHistory, 100);
DrawSeries(DiskSpark, null, DiskSparkHost, _diskHistory, 100);
DrawSeries(ChartStroke, ChartFill, ChartHost, _cpuHistory, 100);
}
// ---- desenho ----
/// <summary>
/// Curva suave (Catmull-Rom → Bézier) dentro do host. Redesenhada a cada
/// amostra (1/s) — nada de render por frame: na bandeja/idle custa zero.
/// </summary>
private static void DrawSeries(Path stroke, Path? fill, FrameworkElement host,
Queue<double> values, double max)
{
var w = host.ActualWidth;
var h = host.ActualHeight;
if (w < 8 || h < 6 || values.Count < 2)
{
stroke.Data = null;
if (fill != null) fill.Data = null;
return;
}
var samples = values.ToArray();
var n = samples.Length;
var step = w / Math.Max(1, n - 1);
var pts = new Point[n];
for (var i = 0; i < n; i++)
{
var y = h - (Math.Clamp(samples[i], 0, max) / max * (h - 5)) - 2.5;
pts[i] = new Point(i * step, y);
}
var sg = new StreamGeometry();
var fg = new StreamGeometry();
using (var sc = sg.Open())
{
var fc = fill != null ? fg.Open() : null;
try
{
sc.BeginFigure(pts[0], false, false);
fc?.BeginFigure(new Point(pts[0].X, h), true, true);
fc?.LineTo(pts[0], false, false);
for (var i = 0; i < n - 1; i++)
{
var p0 = pts[Math.Max(i - 1, 0)];
var p1 = pts[i];
var p2 = pts[i + 1];
var p3 = pts[Math.Min(i + 2, n - 1)];
var c1 = new Point(p1.X + (p2.X - p0.X) / 6.0, p1.Y + (p2.Y - p0.Y) / 6.0);
var c2 = new Point(p2.X - (p3.X - p1.X) / 6.0, p2.Y - (p3.Y - p1.Y) / 6.0);
sc.BezierTo(c1, c2, p2, true, false);
fc?.BezierTo(c1, c2, p2, false, false);
}
fc?.LineTo(new Point(pts[n - 1].X, h), false, false);
}
finally { fc?.Close(); }
}
sg.Freeze();
stroke.Data = sg;
if (fill != null) { fg.Freeze(); fill.Data = fg; }
}
/// <summary>Anel de progresso: arco a começar às 12h, no sentido dos ponteiros.</summary>
private static void SetGauge(Path arc, double pct, double size = 34, double thickness = 3.4)
{
if (pct <= 0.5) { arc.Data = null; return; }
var r = (size - thickness) / 2.0;
var c = new Point(size / 2.0, size / 2.0);
var p = Math.Clamp(pct, 0, 100);
if (p >= 99.5)
{
var full = new EllipseGeometry(c, r, r);
full.Freeze();
arc.Data = full;
return;
}
var a = p / 100.0 * 2 * Math.PI;
var start = new Point(c.X, c.Y - r);
var end = new Point(c.X + r * Math.Sin(a), c.Y - r * Math.Cos(a));
var fig = new PathFigure { StartPoint = start, IsClosed = false, IsFilled = false };
fig.Segments.Add(new ArcSegment(end, new Size(r, r), 0,
p > 50, SweepDirection.Clockwise, true));
var geo = new PathGeometry();
geo.Figures.Add(fig);
geo.Freeze();
arc.Data = geo;
}
private async Task UpdatePingAsync()
{
if (_pinging) return;
_pinging = true;
try
{
var ms = await SystemMonitor.PingAsync();
PingText.Text = ms < 0 ? "—" : ms.ToString();
PingLabel.Text = ms switch
{
< 0 => "Sem ligação",
< 40 => "● Ligação excelente",
< 90 => "● Ligação boa",
_ => "● Ligação fraca",
};
// cor do badge segue o estado (antes ficava sempre verde, mesmo em "fraca")
var (fg, bg) = ms switch
{
< 0 => (Color.FromRgb(0xF8, 0x71, 0x71), Color.FromArgb(0x1A, 0xF8, 0x71, 0x71)),
< 40 => (Color.FromRgb(0x34, 0xD3, 0x99), Color.FromArgb(0x1A, 0x34, 0xD3, 0x99)),
< 90 => (Color.FromRgb(0xFB, 0xBF, 0x24), Color.FromArgb(0x1A, 0xFB, 0xBF, 0x24)),
_ => (Color.FromRgb(0xF8, 0x71, 0x71), Color.FromArgb(0x1A, 0xF8, 0x71, 0x71)),
};
PingLabel.Foreground = new SolidColorBrush(fg);
PingBadge.Background = new SolidColorBrush(bg);
// anel cheio = ligação perfeita; vazio a partir de 200 ms
var quality = ms < 0 ? 0 : (1 - Math.Min(ms, 200) / 200.0) * 100;
SetGauge(PingArc, quality, 84, 3);
}
finally { _pinging = false; }
}
private void Refresh()
{
var cfg = ConfigService.Current;
var user = MainWindow.CurrentUser;
GreetingText.Text = user == null ? "Bem-vindo" : $"Bem-vindo, {user.DisplayName}";
CommunityText.Text = cfg.CommunityStatus;
NewsList.ItemsSource = cfg.News;
Task.Run(() =>
{
InstallDetector.InvalidateCache();
var installed = cfg.Apps.Count(InstallDetector.IsInstalled);
Dispatcher.Invoke(() => InstalledText.Text = $"{installed} / {cfg.Apps.Count}");
});
}
}