Repository navigation
Expand file tree
/
Copy pathForm1.cs
More file actions
703 lines (624 loc) · 24.3 KB
/
Copy pathForm1.cs
File metadata and controls
703 lines (624 loc) · 24.3 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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
using ClaudeUsageMonitor.Models;
using ClaudeUsageMonitor.Services;
using LiveChartsCore;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Drawing.Geometries;
using LiveChartsCore.SkiaSharpView.Painting;
using LiveChartsCore.SkiaSharpView.VisualElements;
using LiveChartsCore.SkiaSharpView.WinForms;
using SkiaSharp;
using Align = LiveChartsCore.Drawing.Align;
namespace ClaudeUsageMonitor;
public partial class Form1 : Form
{
private readonly SessionParser _parser = new();
private readonly System.Windows.Forms.Timer _refreshTimer = new();
private FileSystemWatcher? _watcher;
// UI Controls
private TabControl _tabControl = null!;
private Label _lblTotalTokens = null!;
private Label _lblTotalCost = null!;
private Label _lblTotalCalls = null!;
private Label _lblActiveSessions = null!;
private Label _lblLastRefresh = null!;
private CartesianChart _dailyCostChart = null!;
private PieChart _modelPieChart = null!;
private CartesianChart _tokenChart = null!;
private DataGridView _sessionGrid = null!;
private DataGridView _detailGrid = null!;
private ComboBox _cmbDateRange = null!;
private CheckBox _chkAutoStart = null!;
private NumericUpDown _nudRefreshInterval = null!;
private NotifyIcon _trayIcon = null!;
private Button _btnRefresh = null!;
// Data
private List<UsageRecord> _allRecords = new();
public Form1()
{
InitializeComponent();
BuildUI();
SetupTrayIcon();
SetupFileWatcher();
SetupRefreshTimer();
LoadData();
}
private void BuildUI()
{
Text = "Claude Usage Monitor";
Size = new Size(1200, 800);
MinimumSize = new Size(900, 600);
StartPosition = FormStartPosition.CenterScreen;
BackColor = Color.FromArgb(30, 30, 30);
ForeColor = Color.White;
Font = new Font("Segoe UI", 9.5f);
// Top bar with summary cards
var topPanel = new Panel
{
Dock = DockStyle.Top,
Height = 100,
BackColor = Color.FromArgb(40, 40, 40),
Padding = new Padding(15, 10, 15, 10)
};
_lblTotalTokens = CreateSummaryCard("Total Tokens", "0", 0);
_lblTotalCost = CreateSummaryCard("Est. Cost (USD)", "$0.00", 1);
_lblTotalCalls = CreateSummaryCard("API Calls", "0", 2);
_lblActiveSessions = CreateSummaryCard("Sessions", "0", 3);
topPanel.Controls.AddRange(new Control[] { _lblTotalTokens.Parent!, _lblTotalCost.Parent!, _lblTotalCalls.Parent!, _lblActiveSessions.Parent! });
// Toolbar
var toolbar = new Panel
{
Dock = DockStyle.Top,
Height = 45,
BackColor = Color.FromArgb(35, 35, 35),
Padding = new Padding(10, 5, 10, 5)
};
_cmbDateRange = new ComboBox
{
Items = { "Today", "Last 7 Days", "Last 30 Days", "All Time" },
SelectedIndex = 3,
DropDownStyle = ComboBoxStyle.DropDownList,
Width = 140,
Location = new Point(10, 10),
BackColor = Color.FromArgb(50, 50, 50),
ForeColor = Color.White,
FlatStyle = FlatStyle.Flat
};
_cmbDateRange.SelectedIndexChanged += (_, _) => RefreshView();
_btnRefresh = new Button
{
Text = "Refresh Now",
Location = new Point(160, 8),
Size = new Size(100, 28),
FlatStyle = FlatStyle.Flat,
BackColor = Color.FromArgb(88, 101, 242),
ForeColor = Color.White
};
_btnRefresh.FlatAppearance.BorderSize = 0;
_btnRefresh.Click += (_, _) => LoadData();
_chkAutoStart = new CheckBox
{
Text = "Start with Windows",
Location = new Point(280, 12),
AutoSize = true,
ForeColor = Color.White,
Checked = StartupManager.IsStartupEnabled()
};
_chkAutoStart.CheckedChanged += (_, _) =>
{
if (_chkAutoStart.Checked)
StartupManager.EnableStartup();
else
StartupManager.DisableStartup();
};
var lblInterval = new Label
{
Text = "Auto-refresh (sec):",
Location = new Point(460, 12),
AutoSize = true,
ForeColor = Color.LightGray
};
_nudRefreshInterval = new NumericUpDown
{
Minimum = 5,
Maximum = 3600,
Value = 60,
Location = new Point(590, 9),
Width = 70,
BackColor = Color.FromArgb(50, 50, 50),
ForeColor = Color.White
};
_nudRefreshInterval.ValueChanged += (_, _) =>
{
_refreshTimer.Interval = (int)_nudRefreshInterval.Value * 1000;
};
_lblLastRefresh = new Label
{
Text = "Last refresh: --",
Location = new Point(680, 12),
AutoSize = true,
ForeColor = Color.Gray
};
toolbar.Controls.AddRange(new Control[] { _cmbDateRange, _btnRefresh, _chkAutoStart, lblInterval, _nudRefreshInterval, _lblLastRefresh });
// Tab control
_tabControl = new TabControl
{
Dock = DockStyle.Fill,
BackColor = Color.FromArgb(30, 30, 30),
Font = new Font("Segoe UI", 10f)
};
// Dashboard tab
var dashTab = new TabPage("Dashboard") { BackColor = Color.FromArgb(30, 30, 30) };
BuildDashboardTab(dashTab);
_tabControl.TabPages.Add(dashTab);
// Sessions tab
var sessionsTab = new TabPage("Sessions") { BackColor = Color.FromArgb(30, 30, 30) };
BuildSessionsTab(sessionsTab);
_tabControl.TabPages.Add(sessionsTab);
// Detail tab
var detailTab = new TabPage("Detailed Log") { BackColor = Color.FromArgb(30, 30, 30) };
BuildDetailTab(detailTab);
_tabControl.TabPages.Add(detailTab);
Controls.Add(_tabControl);
Controls.Add(toolbar);
Controls.Add(topPanel);
}
private Label CreateSummaryCard(string title, string value, int index)
{
var card = new Panel
{
Size = new Size(220, 75),
Location = new Point(15 + index * 235, 10),
BackColor = Color.FromArgb(55, 55, 55),
Padding = new Padding(12)
};
var lblTitle = new Label
{
Text = title,
Dock = DockStyle.Top,
Height = 22,
ForeColor = Color.FromArgb(160, 160, 160),
Font = new Font("Segoe UI", 9f)
};
var lblValue = new Label
{
Text = value,
Dock = DockStyle.Fill,
ForeColor = Color.FromArgb(88, 101, 242),
Font = new Font("Segoe UI Semibold", 18f),
TextAlign = ContentAlignment.MiddleLeft
};
card.Controls.Add(lblValue);
card.Controls.Add(lblTitle);
return lblValue;
}
private void BuildDashboardTab(TabPage tab)
{
// Top row: daily cost chart (50% height)
_dailyCostChart = new CartesianChart
{
Dock = DockStyle.Top,
Height = 300,
BackColor = Color.FromArgb(30, 30, 30)
};
// Bottom panel for token chart + model pie side by side
var bottomPanel = new Panel
{
Dock = DockStyle.Fill,
BackColor = Color.FromArgb(30, 30, 30)
};
_tokenChart = new CartesianChart
{
Dock = DockStyle.Left,
Width = 600,
BackColor = Color.FromArgb(30, 30, 30)
};
_modelPieChart = new PieChart
{
Dock = DockStyle.Fill,
BackColor = Color.FromArgb(30, 30, 30)
};
bottomPanel.Controls.Add(_modelPieChart);
bottomPanel.Controls.Add(_tokenChart);
tab.Controls.Add(bottomPanel);
tab.Controls.Add(_dailyCostChart);
// Resize token chart width proportionally
tab.Resize += (_, _) =>
{
_dailyCostChart.Height = tab.Height / 2;
_tokenChart.Width = (int)(tab.Width * 0.6);
};
}
private void BuildSessionsTab(TabPage tab)
{
_sessionGrid = CreateStyledGrid();
_sessionGrid.Dock = DockStyle.Fill;
_sessionGrid.Columns.AddRange(
new DataGridViewTextBoxColumn { HeaderText = "Project", Width = 200 },
new DataGridViewTextBoxColumn { HeaderText = "Session ID", Width = 120 },
new DataGridViewTextBoxColumn { HeaderText = "Started", Width = 150 },
new DataGridViewTextBoxColumn { HeaderText = "Last Activity", Width = 150 },
new DataGridViewTextBoxColumn { HeaderText = "Model", Width = 140 },
new DataGridViewTextBoxColumn { HeaderText = "API Calls", Width = 80 },
new DataGridViewTextBoxColumn { HeaderText = "Input Tokens", Width = 100, DefaultCellStyle = new DataGridViewCellStyle { Alignment = DataGridViewContentAlignment.MiddleRight } },
new DataGridViewTextBoxColumn { HeaderText = "Output Tokens", Width = 100, DefaultCellStyle = new DataGridViewCellStyle { Alignment = DataGridViewContentAlignment.MiddleRight } },
new DataGridViewTextBoxColumn { HeaderText = "Est. Cost", Width = 90, DefaultCellStyle = new DataGridViewCellStyle { Alignment = DataGridViewContentAlignment.MiddleRight } }
);
tab.Controls.Add(_sessionGrid);
}
private void BuildDetailTab(TabPage tab)
{
_detailGrid = CreateStyledGrid();
_detailGrid.Dock = DockStyle.Fill;
_detailGrid.Columns.AddRange(
new DataGridViewTextBoxColumn { HeaderText = "Timestamp", Width = 170 },
new DataGridViewTextBoxColumn { HeaderText = "Model", Width = 160 },
new DataGridViewTextBoxColumn { HeaderText = "Project", Width = 180 },
new DataGridViewTextBoxColumn { HeaderText = "Input", Width = 90, DefaultCellStyle = new DataGridViewCellStyle { Alignment = DataGridViewContentAlignment.MiddleRight } },
new DataGridViewTextBoxColumn { HeaderText = "Output", Width = 90, DefaultCellStyle = new DataGridViewCellStyle { Alignment = DataGridViewContentAlignment.MiddleRight } },
new DataGridViewTextBoxColumn { HeaderText = "Cache Create", Width = 100, DefaultCellStyle = new DataGridViewCellStyle { Alignment = DataGridViewContentAlignment.MiddleRight } },
new DataGridViewTextBoxColumn { HeaderText = "Cache Read", Width = 100, DefaultCellStyle = new DataGridViewCellStyle { Alignment = DataGridViewContentAlignment.MiddleRight } },
new DataGridViewTextBoxColumn { HeaderText = "Total", Width = 100, DefaultCellStyle = new DataGridViewCellStyle { Alignment = DataGridViewContentAlignment.MiddleRight } },
new DataGridViewTextBoxColumn { HeaderText = "Est. Cost", Width = 90, DefaultCellStyle = new DataGridViewCellStyle { Alignment = DataGridViewContentAlignment.MiddleRight } }
);
tab.Controls.Add(_detailGrid);
}
private static DataGridView CreateStyledGrid()
{
return new DataGridView
{
ReadOnly = true,
AllowUserToAddRows = false,
AllowUserToDeleteRows = false,
SelectionMode = DataGridViewSelectionMode.FullRowSelect,
RowHeadersVisible = false,
BackgroundColor = Color.FromArgb(30, 30, 30),
GridColor = Color.FromArgb(60, 60, 60),
DefaultCellStyle = new DataGridViewCellStyle
{
BackColor = Color.FromArgb(35, 35, 35),
ForeColor = Color.White,
SelectionBackColor = Color.FromArgb(88, 101, 242),
SelectionForeColor = Color.White,
Font = new Font("Segoe UI", 9f)
},
ColumnHeadersDefaultCellStyle = new DataGridViewCellStyle
{
BackColor = Color.FromArgb(50, 50, 50),
ForeColor = Color.White,
Font = new Font("Segoe UI Semibold", 9f)
},
EnableHeadersVisualStyles = false,
AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None,
RowTemplate = { Height = 28 }
};
}
private static Icon CreateAppIcon()
{
using var bmp = new Bitmap(32, 32);
using var g = Graphics.FromImage(bmp);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
// Accent-colored rounded background
using var bgBrush = new SolidBrush(Color.FromArgb(88, 101, 242));
g.FillEllipse(bgBrush, 1, 1, 30, 30);
// "C" letter in white
using var font = new Font("Segoe UI", 18f, FontStyle.Bold, GraphicsUnit.Pixel);
using var textBrush = new SolidBrush(Color.White);
var sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center };
g.DrawString("C", font, textBrush, new RectangleF(0, 0, 32, 32), sf);
return Icon.FromHandle(bmp.GetHicon());
}
private void SetupTrayIcon()
{
var appIcon = CreateAppIcon();
Icon = appIcon; // Also set the form title bar icon
_trayIcon = new NotifyIcon
{
Text = "Claude Usage Monitor",
Icon = appIcon,
Visible = true
};
var trayMenu = new ContextMenuStrip();
trayMenu.Items.Add("Open Dashboard", null, (_, _) => { Show(); WindowState = FormWindowState.Normal; Activate(); });
trayMenu.Items.Add("Refresh Now", null, (_, _) => LoadData());
trayMenu.Items.Add("-");
trayMenu.Items.Add("Exit", null, (_, _) => { _trayIcon.Visible = false; Application.Exit(); });
_trayIcon.ContextMenuStrip = trayMenu;
_trayIcon.DoubleClick += (_, _) => { Show(); WindowState = FormWindowState.Normal; Activate(); };
}
private void SetupFileWatcher()
{
var projectsDir = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".claude", "projects");
if (!Directory.Exists(projectsDir)) return;
_watcher = new FileSystemWatcher(projectsDir)
{
Filter = "*.jsonl",
IncludeSubdirectories = true,
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size,
EnableRaisingEvents = true
};
_watcher.Changed += (_, _) =>
{
// Debounce: only refresh if last refresh was > 5 seconds ago
if (DateTime.Now - _lastRefresh > TimeSpan.FromSeconds(5))
{
BeginInvoke(LoadData);
}
};
}
private DateTime _lastRefresh = DateTime.MinValue;
private void SetupRefreshTimer()
{
_refreshTimer.Interval = 60000;
_refreshTimer.Tick += (_, _) => LoadData();
_refreshTimer.Start();
}
private void LoadData()
{
try
{
_btnRefresh.Enabled = false;
_btnRefresh.Text = "Loading...";
Application.DoEvents();
_allRecords = _parser.ParseAllSessions();
_lastRefresh = DateTime.Now;
RefreshView();
_lblLastRefresh.Text = $"Last refresh: {DateTime.Now:HH:mm:ss}";
}
catch (Exception ex)
{
_lblLastRefresh.Text = $"Error: {ex.Message}";
}
finally
{
_btnRefresh.Enabled = true;
_btnRefresh.Text = "Refresh Now";
}
}
private void RefreshView()
{
var filtered = FilterByDateRange(_allRecords);
UpdateSummaryCards(filtered);
UpdateDailyCostChart(filtered);
UpdateTokenChart(filtered);
UpdateModelPieChart(filtered);
UpdateSessionGrid(filtered);
UpdateDetailGrid(filtered);
}
private List<UsageRecord> FilterByDateRange(List<UsageRecord> records)
{
var cutoff = _cmbDateRange.SelectedIndex switch
{
0 => DateTime.Today,
1 => DateTime.Today.AddDays(-7),
2 => DateTime.Today.AddDays(-30),
_ => DateTime.MinValue
};
return records.Where(r => r.Timestamp >= cutoff).ToList();
}
private void UpdateSummaryCards(List<UsageRecord> records)
{
var totalTokens = records.Sum(r => (long)r.TotalTokens);
var totalCost = records.Sum(r => r.EstimatedCostUsd);
var totalCalls = records.Count;
var sessions = records.Select(r => r.SessionId).Distinct().Count();
_lblTotalTokens.Text = FormatNumber(totalTokens);
_lblTotalCost.Text = $"${totalCost:F2}";
_lblTotalCalls.Text = FormatNumber(totalCalls);
_lblActiveSessions.Text = sessions.ToString();
// Update tray icon tooltip
_trayIcon.Text = $"Claude: {FormatNumber(totalTokens)} tokens | ${totalCost:F2}";
}
private void UpdateDailyCostChart(List<UsageRecord> records)
{
var daily = _parser.GetDailyUsage(records);
_dailyCostChart.Series = new ISeries[]
{
new ColumnSeries<decimal>
{
Values = daily.Select(d => d.TotalCostUsd).ToArray(),
Name = "Daily Cost (USD)",
Fill = new SolidColorPaint(new SKColor(88, 101, 242)),
MaxBarWidth = 30
}
};
_dailyCostChart.XAxes = new Axis[]
{
new Axis
{
Labels = daily.Select(d => d.Date.ToString("MM/dd")).ToArray(),
LabelsRotation = 45,
LabelsPaint = new SolidColorPaint(new SKColor(180, 180, 180)),
Name = "Date",
NamePaint = new SolidColorPaint(new SKColor(180, 180, 180))
}
};
_dailyCostChart.YAxes = new Axis[]
{
new Axis
{
Name = "Cost (USD)",
NamePaint = new SolidColorPaint(new SKColor(180, 180, 180)),
LabelsPaint = new SolidColorPaint(new SKColor(180, 180, 180)),
Labeler = v => $"${v:F2}"
}
};
_dailyCostChart.Title = new DrawnLabelVisual(new LabelGeometry
{
Text = "Daily Cost",
TextSize = 16,
Paint = new SolidColorPaint(new SKColor(220, 220, 220)),
HorizontalAlign = Align.Start,
VerticalAlign = Align.Start
});
}
private void UpdateTokenChart(List<UsageRecord> records)
{
var daily = _parser.GetDailyUsage(records);
var dailyInput = records
.GroupBy(r => DateOnly.FromDateTime(r.Timestamp))
.OrderBy(g => g.Key)
.Select(g => (double)g.Sum(r => r.InputTokens + r.CacheReadTokens))
.ToArray();
var dailyOutput = records
.GroupBy(r => DateOnly.FromDateTime(r.Timestamp))
.OrderBy(g => g.Key)
.Select(g => (double)g.Sum(r => r.OutputTokens))
.ToArray();
_tokenChart.Series = new ISeries[]
{
new StackedColumnSeries<double>
{
Values = dailyInput,
Name = "Input + Cache Read",
Fill = new SolidColorPaint(new SKColor(59, 130, 246)),
MaxBarWidth = 25
},
new StackedColumnSeries<double>
{
Values = dailyOutput,
Name = "Output",
Fill = new SolidColorPaint(new SKColor(168, 85, 247)),
MaxBarWidth = 25
}
};
_tokenChart.XAxes = new Axis[]
{
new Axis
{
Labels = daily.Select(d => d.Date.ToString("MM/dd")).ToArray(),
LabelsRotation = 45,
LabelsPaint = new SolidColorPaint(new SKColor(180, 180, 180))
}
};
_tokenChart.YAxes = new Axis[]
{
new Axis
{
Name = "Tokens",
NamePaint = new SolidColorPaint(new SKColor(180, 180, 180)),
LabelsPaint = new SolidColorPaint(new SKColor(180, 180, 180)),
Labeler = v => FormatNumber((long)v)
}
};
_tokenChart.Title = new DrawnLabelVisual(new LabelGeometry
{
Text = "Daily Token Usage",
TextSize = 16,
Paint = new SolidColorPaint(new SKColor(220, 220, 220)),
HorizontalAlign = Align.Start,
VerticalAlign = Align.Start
});
}
private void UpdateModelPieChart(List<UsageRecord> records)
{
var modelGroups = records
.GroupBy(r => r.Model)
.Select(g => new { Model = FormatModelName(g.Key), Cost = g.Sum(r => r.EstimatedCostUsd) })
.OrderByDescending(g => g.Cost)
.ToList();
var colors = new SKColor[]
{
new(88, 101, 242), // Purple
new(59, 130, 246), // Blue
new(168, 85, 247), // Violet
new(236, 72, 153), // Pink
new(34, 197, 94), // Green
};
_modelPieChart.Series = modelGroups.Select((g, i) =>
new PieSeries<decimal>
{
Values = new[] { g.Cost },
Name = $"{g.Model} (${g.Cost:F2})",
Fill = new SolidColorPaint(colors[i % colors.Length]),
DataLabelsSize = 12,
DataLabelsPaint = new SolidColorPaint(new SKColor(220, 220, 220))
} as ISeries).ToArray();
_modelPieChart.Title = new DrawnLabelVisual(new LabelGeometry
{
Text = "Cost by Model",
TextSize = 16,
Paint = new SolidColorPaint(new SKColor(220, 220, 220)),
HorizontalAlign = Align.Start,
VerticalAlign = Align.Start
});
}
private void UpdateSessionGrid(List<UsageRecord> records)
{
var summaries = _parser.GetSessionSummaries(records);
_sessionGrid.Rows.Clear();
foreach (var s in summaries)
{
_sessionGrid.Rows.Add(
s.Project,
s.SessionId[..Math.Min(8, s.SessionId.Length)] + "...",
s.StartTime.ToString("yyyy-MM-dd HH:mm"),
s.LastActivity.ToString("yyyy-MM-dd HH:mm"),
FormatModelName(s.PrimaryModel),
s.ApiCalls,
FormatNumber(s.TotalInputTokens),
FormatNumber(s.TotalOutputTokens),
$"${s.TotalCostUsd:F4}"
);
}
}
private void UpdateDetailGrid(List<UsageRecord> records)
{
_detailGrid.Rows.Clear();
foreach (var r in records.OrderByDescending(r => r.Timestamp).Take(500))
{
_detailGrid.Rows.Add(
r.Timestamp.ToString("yyyy-MM-dd HH:mm:ss"),
FormatModelName(r.Model),
r.Project,
FormatNumber(r.InputTokens),
FormatNumber(r.OutputTokens),
FormatNumber(r.CacheCreationTokens),
FormatNumber(r.CacheReadTokens),
FormatNumber(r.TotalTokens),
$"${r.EstimatedCostUsd:F4}"
);
}
}
private static string FormatNumber(long n) => n switch
{
>= 1_000_000 => $"{n / 1_000_000.0:F1}M",
>= 1_000 => $"{n / 1_000.0:F1}K",
_ => n.ToString("N0")
};
private static string FormatModelName(string model) => model switch
{
var m when m.Contains("opus") => "Opus 4.6",
var m when m.Contains("sonnet") => "Sonnet 4.6",
var m when m.Contains("haiku") => "Haiku 4.5",
_ => model
};
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
Hide();
_trayIcon.ShowBalloonTip(2000, "Claude Usage Monitor",
"Running in background. Double-click tray icon to open.", ToolTipIcon.Info);
}
else
{
_trayIcon.Visible = false;
_watcher?.Dispose();
_refreshTimer.Stop();
}
base.OnFormClosing(e);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// If started with --minimized flag, hide to tray
if (Environment.GetCommandLineArgs().Contains("--minimized"))
{
WindowState = FormWindowState.Minimized;
Hide();
}
}
}