ai-battery 0.1.20 → 0.1.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,409 +1,409 @@
1
- function Set-HudBatteryImage($Box, [Nullable[int]]$Percent, [bool]$Running) {
2
- $oldImage = $Box.Image
3
- $Box.Image = New-BatteryImage $Percent $Running 36 16 $false
4
- $Box.AccessibleName = if ($null -eq $Percent) { "--" } else { [string][int]$Percent }
5
- $Box.ForeColor = Get-HudBatteryTextColor $Running
6
- $Box.Visible = $true
7
- $Box.Tag = $true
8
- $Box.Invalidate()
9
- if ($oldImage) { $oldImage.Dispose() }
10
- }
11
-
12
- function New-HudGlyphImage([string]$Kind, [System.Drawing.Color]$Color, [int]$Width = 21, [int]$Height = 18) {
13
- $surface = New-HudBitmapSurface $Width $Height
14
- $bitmap = $surface.Bitmap
15
- $graphics = $surface.Graphics
16
- $graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias
17
- $graphics.TextRenderingHint = [System.Drawing.Text.TextRenderingHint]::AntiAliasGridFit
18
- $graphics.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
19
- $graphics.PixelOffsetMode = [System.Drawing.Drawing2D.PixelOffsetMode]::Half
20
- $graphics.Clear([System.Drawing.Color]::Transparent)
21
-
22
- if ($Kind -eq "reset") {
23
- $pen = [System.Drawing.Pen]::new($Color, 2.1)
24
- $pen.StartCap = [System.Drawing.Drawing2D.LineCap]::Round
25
- $pen.EndCap = [System.Drawing.Drawing2D.LineCap]::Round
26
- $brush = [System.Drawing.SolidBrush]::new($Color)
27
- try {
28
- $arcX = 3.6
29
- $arcY = 2.5
30
- $arcW = 13.0
31
- $arcH = 13.0
32
- $startAngle = 150.0
33
- $sweepAngle = 190.0
34
- $endAngle = ($startAngle + $sweepAngle) * [math]::PI / 180.0
35
- $centerX = $arcX + ($arcW / 2.0)
36
- $centerY = $arcY + ($arcH / 2.0)
37
- $radiusX = $arcW / 2.0
38
- $radiusY = $arcH / 2.0
39
-
40
- $graphics.DrawArc($pen, [single]$arcX, [single]$arcY, [single]$arcW, [single]$arcH, [single]$startAngle, [single]$sweepAngle)
41
-
42
- $tipX = $centerX + ($radiusX * [math]::Cos($endAngle))
43
- $tipY = $centerY + ($radiusY * [math]::Sin($endAngle))
44
- $dirX = -[math]::Sin($endAngle)
45
- $dirY = [math]::Cos($endAngle)
46
- $length = [math]::Sqrt(($dirX * $dirX) + ($dirY * $dirY))
47
- if ($length -gt 0) {
48
- $dirX = $dirX / $length
49
- $dirY = $dirY / $length
50
- }
51
- $normalX = -$dirY
52
- $normalY = $dirX
53
- $headLength = 4.8
54
- $headWidth = 3.3
55
- $baseX = $tipX - ($dirX * $headLength)
56
- $baseY = $tipY - ($dirY * $headLength)
57
-
58
- $points = @(
59
- [System.Drawing.PointF]::new([single]$tipX, [single]$tipY),
60
- [System.Drawing.PointF]::new([single]($baseX + ($normalX * $headWidth)), [single]($baseY + ($normalY * $headWidth))),
61
- [System.Drawing.PointF]::new([single]($baseX - ($normalX * $headWidth)), [single]($baseY - ($normalY * $headWidth)))
62
- )
63
- $graphics.FillPolygon($brush, $points)
64
- } finally {
65
- $pen.Dispose()
66
- $brush.Dispose()
67
- $graphics.Dispose()
68
- }
69
- return $bitmap
70
- }
71
-
72
- $sourceSize = 48
73
- $source = [System.Drawing.Bitmap]::new($sourceSize, $sourceSize)
74
- $sourceGraphics = [System.Drawing.Graphics]::FromImage($source)
75
- $sourceGraphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias
76
- $sourceGraphics.TextRenderingHint = [System.Drawing.Text.TextRenderingHint]::AntiAliasGridFit
77
- $sourceGraphics.PixelOffsetMode = [System.Drawing.Drawing2D.PixelOffsetMode]::Half
78
- $sourceGraphics.Clear([System.Drawing.Color]::Transparent)
79
-
80
- $brush = [System.Drawing.SolidBrush]::new($Color)
81
- $format = [System.Drawing.StringFormat]::new()
82
- $format.Alignment = [System.Drawing.StringAlignment]::Center
83
- $format.LineAlignment = [System.Drawing.StringAlignment]::Near
84
-
85
- try {
86
- $glyph = [string][char]0x2466
87
- $maxIconWidth = 12
88
- $maxIconHeight = 12
89
- $sourceGraphics.DrawString(
90
- $glyph,
91
- $symbolFont,
92
- $brush,
93
- [System.Drawing.RectangleF]::new([single]0, [single]0, [single]$sourceSize, [single]$sourceSize),
94
- $format
95
- )
96
-
97
- $left = $sourceSize
98
- $top = $sourceSize
99
- $right = -1
100
- $bottom = -1
101
- for ($y = 0; $y -lt $sourceSize; $y += 1) {
102
- for ($x = 0; $x -lt $sourceSize; $x += 1) {
103
- if ($source.GetPixel($x, $y).A -gt 0) {
104
- if ($x -lt $left) { $left = $x }
105
- if ($x -gt $right) { $right = $x }
106
- if ($y -lt $top) { $top = $y }
107
- if ($y -gt $bottom) { $bottom = $y }
108
- }
109
- }
110
- }
111
-
112
- if ($right -ge $left -and $bottom -ge $top) {
113
- $sourceRect = [System.Drawing.Rectangle]::new($left, $top, ($right - $left + 1), ($bottom - $top + 1))
114
- $scale = [math]::Min(1.0, [math]::Min(($maxIconWidth / [double]$sourceRect.Width), ($maxIconHeight / [double]$sourceRect.Height)))
115
- $targetWidth = [math]::Max(1, [int][math]::Round($sourceRect.Width * $scale))
116
- $targetHeight = [math]::Max(1, [int][math]::Round($sourceRect.Height * $scale))
117
- $targetX = [math]::Floor(($Width - $targetWidth) / 2)
118
- $targetY = [math]::Floor(($Height - $targetHeight) / 2)
119
- $targetRect = [System.Drawing.Rectangle]::new([int]$targetX, [int]$targetY, $targetWidth, $targetHeight)
120
- $graphics.DrawImage($source, $targetRect, $sourceRect, [System.Drawing.GraphicsUnit]::Pixel)
121
- }
122
- } finally {
123
- $brush.Dispose()
124
- $format.Dispose()
125
- $sourceGraphics.Dispose()
126
- $source.Dispose()
127
- $graphics.Dispose()
128
- }
129
-
130
- return $bitmap
131
- }
132
-
133
- function Set-HudLineIconImage($Box, [string]$Kind, [string]$Value, [System.Drawing.Color]$Color) {
134
- $oldImage = $Box.Image
135
- if ([string]::IsNullOrWhiteSpace($Value)) {
136
- $Box.Image = $null
137
- $Box.Visible = $false
138
- } else {
139
- $Box.Image = New-HudGlyphImage $Kind $Color
140
- $Box.Visible = $true
141
- }
142
- if ($oldImage) { $oldImage.Dispose() }
143
- }
144
-
145
- function Set-HudMetricLabel($Label, [string]$Value, [System.Drawing.Color]$Color, [Nullable[int]]$FixedWidth = $null) {
146
- $Label.Text = $Value
147
- $Label.ForeColor = $Color
148
- if ([string]::IsNullOrWhiteSpace($Value)) {
149
- $Label.Width = 0
150
- $Label.Visible = $false
151
- $Label.Tag = $false
152
- } else {
153
- $Label.Width = if ($null -ne $FixedWidth -and $FixedWidth -gt 0) {
154
- [int]$FixedWidth
155
- } else {
156
- (Get-LabelTextWidth $Label) + (Scale-HudValue 1)
157
- }
158
- $Label.Visible = $true
159
- $Label.Tag = $true
160
- }
161
- }
162
-
163
- function Set-HudDivider($Divider, [string]$Value) {
164
- if ([string]::IsNullOrWhiteSpace($Value)) {
165
- $Divider.Visible = $false
166
- $Divider.Width = 0
167
- $Divider.Tag = $false
168
- } else {
169
- $Divider.Visible = $true
170
- $Divider.Width = Scale-HudValue 1
171
- $Divider.Tag = $true
172
- }
173
- }
174
-
175
- function Get-HudTextWidth([string]$Value, $Font = $font) {
176
- if ([string]::IsNullOrWhiteSpace($Value)) { return 0 }
177
- return [System.Windows.Forms.TextRenderer]::MeasureText(
178
- $Value,
179
- $Font,
180
- [System.Drawing.Size]::new(1000, 1000),
181
- [System.Windows.Forms.TextFormatFlags]::NoPadding
182
- ).Width
183
- }
184
-
185
- function Get-SharedHudColumnWidth([string[]]$Values, [int]$Minimum = 0) {
186
- $width = Scale-HudValue $Minimum
187
- foreach ($value in $Values) {
188
- $width = [math]::Max($width, (Get-HudTextWidth $value) + (Scale-HudValue 1))
189
- }
190
- return [int]$width
191
- }
192
-
193
- function Set-HudParts($Parts, $PrefixLabel, $BatteryBox, $Divider1, $ResetLabel, $Divider2, $WeekLabel, $ExtraLabel, [Nullable[int]]$ResetWidth = $null, [Nullable[int]]$WeekWidth = $null) {
194
- $PrefixLabel.Text = $Parts.Prefix
195
- $PrefixLabel.ForeColor = $Parts.TextColor
196
- $PrefixLabel.Width = Scale-HudValue 48
197
- $PrefixLabel.Visible = $true
198
- $PrefixLabel.Tag = $true
199
- Set-HudBatteryImage $BatteryBox $Parts.Percent $Parts.Running
200
- Set-HudDivider $Divider1 $Parts.ResetValue
201
- Set-HudMetricLabel $ResetLabel $Parts.ResetValue $Parts.TextColor $ResetWidth
202
- Set-HudDivider $Divider2 $Parts.WeekValue
203
- Set-HudMetricLabel $WeekLabel $Parts.WeekValue $Parts.TextColor $WeekWidth
204
- Set-HudMetricLabel $ExtraLabel $Parts.ExtraText $Parts.TextColor
205
- }
206
-
207
- function Set-HudControlsVisible($Controls, [bool]$Visible) {
208
- foreach ($control in $Controls) {
209
- $control.Visible = $Visible
210
- $control.Tag = $Visible
211
- }
212
- }
213
-
214
- function Get-LabelTextWidth($Label) {
215
- if (-not $Label.Text) { return 0 }
216
- return [System.Windows.Forms.TextRenderer]::MeasureText(
217
- $Label.Text,
218
- $Label.Font,
219
- [System.Drawing.Size]::new(1000, 1000),
220
- [System.Windows.Forms.TextFormatFlags]::NoPadding
221
- ).Width
222
- }
223
-
224
- function Get-ControlLayoutWidth($Control) {
225
- # Control.Visible reads as $false while the form is still hidden, so track
226
- # the intended visibility in Tag to keep pre-show layout math correct.
227
- if ($Control.Tag -ne $true) { return 0 }
228
- $baseWidth = $Control.Width
229
- return $baseWidth + $Control.Margin.Left + $Control.Margin.Right
230
- }
231
-
232
- function Get-HudControlsWidth($Controls) {
233
- $width = 0
234
- foreach ($control in $Controls) {
235
- $width += Get-ControlLayoutWidth $control
236
- }
237
- return $width
238
- }
239
-
240
- function Resize-HudToContent {
241
- if ($Mode -eq "tray") { return }
242
-
243
- $oldRight = $form.Left + $form.Width
244
- $oldBottom = $form.Top + $form.Height
245
- $codexWidth = $(if ($script:codexRowVisible) { Get-HudControlsWidth $codexHudControls } else { 0 })
246
- $claudeWidth = $(if ($script:claudeRowVisible) { Get-HudControlsWidth $claudeHudControls } else { 0 })
247
- $bothRows = $script:codexRowVisible -and $script:claudeRowVisible
248
- if ($providerDivider) {
249
- $providerDivider.Visible = $bothRows
250
- $providerDivider.Tag = $bothRows
251
- }
252
- if ($script:dockTuiStatusline) {
253
- Set-DockedTuiWindowRegion
254
- Sync-HitFormBounds
255
- return
256
- }
257
- $contentWidth = [math]::Max($codexWidth, $claudeWidth)
258
- $desiredWidth = $panel.Padding.Left + $panel.Padding.Right +
259
- $contentWidth +
260
- (Scale-HudValue 1)
261
- $desiredWidth = [math]::Max((Scale-HudValue 150), [int][math]::Ceiling($desiredWidth))
262
-
263
- $desiredHeight = [int]$(if ($bothRows) { $script:hudTwoRowHeight } else { $script:hudOneRowHeight })
264
- if ($bothRows) {
265
- $panel.RowStyles[0] = [System.Windows.Forms.RowStyle]::new([System.Windows.Forms.SizeType]::Percent, 50)
266
- $panel.RowStyles[1] = [System.Windows.Forms.RowStyle]::new([System.Windows.Forms.SizeType]::Percent, 50)
267
- } elseif ($script:claudeRowVisible) {
268
- $panel.RowStyles[0] = [System.Windows.Forms.RowStyle]::new([System.Windows.Forms.SizeType]::Absolute, 0)
269
- $panel.RowStyles[1] = [System.Windows.Forms.RowStyle]::new([System.Windows.Forms.SizeType]::Percent, 100)
270
- } else {
271
- $panel.RowStyles[0] = [System.Windows.Forms.RowStyle]::new([System.Windows.Forms.SizeType]::Percent, 100)
272
- $panel.RowStyles[1] = [System.Windows.Forms.RowStyle]::new([System.Windows.Forms.SizeType]::Absolute, 0)
273
- }
274
-
275
- if (([math]::Abs($form.Width - $desiredWidth) -gt 1) -or ($form.Height -ne $desiredHeight)) {
276
- $form.Width = $desiredWidth
277
- $form.Height = $desiredHeight
278
- $newX = if ($script:hudAnchorX -eq "right") { $oldRight - $form.Width } else { $form.Left }
279
- $newY = if ($script:hudAnchorY -eq "bottom") { $oldBottom - $form.Height } else { $form.Top }
280
- $form.Location = [System.Drawing.Point]::new([int]$newX, [int]$newY)
281
- Sync-HitFormBounds
282
- Save-HudPositionIfReady
283
- }
284
- }
285
-
286
- function Show-HudMessage([string]$Message) {
287
- $codexPrefixLabel.Text = $Message
288
- $codexPrefixLabel.ForeColor = Get-HudMutedTextColor
289
- $codexPrefixLabel.Width = (Get-LabelTextWidth $codexPrefixLabel) + (Scale-HudValue 1)
290
- $codexPrefixLabel.Visible = $true
291
- $codexPrefixLabel.Tag = $true
292
- foreach ($label in @($codexResetLabel, $codexWeekLabel, $codexExtraLabel, $claudePrefixLabel, $claudeResetLabel, $claudeWeekLabel, $claudeExtraLabel)) {
293
- $label.Text = ""
294
- $label.Width = 0
295
- $label.Visible = $false
296
- $label.Tag = $false
297
- }
298
- foreach ($divider in @($codexDivider1, $codexDivider2, $claudeDivider1, $claudeDivider2)) {
299
- $divider.Visible = $false
300
- $divider.Width = 0
301
- $divider.Tag = $false
302
- }
303
- Set-HudBatteryImage $codexIconLabel $null $false
304
- $oldImage = $claudeIconLabel.Image
305
- $claudeIconLabel.Image = $null
306
- $claudeIconLabel.Visible = $false
307
- $claudeIconLabel.Tag = $false
308
- if ($oldImage) { $oldImage.Dispose() }
309
- $script:codexRowVisible = $true
310
- $script:claudeRowVisible = $false
311
- $codexRow.Visible = $true
312
- $claudeRow.Visible = $false
313
- Resize-HudToContent
314
- }
315
-
316
- function Update-HudFromSnapshot($Snapshot) {
317
- if (-not $Snapshot) {
318
- Show-HudMessage "AI Battery starting..."
319
- Invalidate-DockedTuiIfChanged $Snapshot
320
- Update-HudVisibilityForFullscreen
321
- return
322
- }
323
-
324
- try {
325
- $texts = ConvertTo-HudTexts $Snapshot
326
- $script:codexRowVisible = [bool]$texts.CodexVisible
327
- $script:claudeRowVisible = [bool]$texts.ClaudeVisible
328
- $resetValues = @()
329
- $weekValues = @()
330
- if ($texts.CodexVisible) {
331
- if ($texts.Codex.ResetValue) { $resetValues += $texts.Codex.ResetValue }
332
- if ($texts.Codex.WeekValue) { $weekValues += $texts.Codex.WeekValue }
333
- }
334
- if ($texts.ClaudeVisible) {
335
- if ($texts.Claude.ResetValue) { $resetValues += $texts.Claude.ResetValue }
336
- if ($texts.Claude.WeekValue) { $weekValues += $texts.Claude.WeekValue }
337
- }
338
- $resetWidth = Get-SharedHudColumnWidth $resetValues
339
- $weekWidth = Get-SharedHudColumnWidth $weekValues
340
- if ($texts.CodexVisible) {
341
- $codexRow.Visible = $true
342
- Set-HudParts $texts.Codex $codexPrefixLabel $codexIconLabel $codexDivider1 $codexResetLabel $codexDivider2 $codexWeekLabel $codexExtraLabel $resetWidth $weekWidth
343
- } else {
344
- $codexRow.Visible = $false
345
- Set-HudControlsVisible $codexHudControls $false
346
- }
347
- if ($texts.ClaudeVisible) {
348
- $claudeRow.Visible = $true
349
- Set-HudParts $texts.Claude $claudePrefixLabel $claudeIconLabel $claudeDivider1 $claudeResetLabel $claudeDivider2 $claudeWeekLabel $claudeExtraLabel $resetWidth $weekWidth
350
- } else {
351
- $claudeRow.Visible = $false
352
- Set-HudControlsVisible $claudeHudControls $false
353
- }
354
- Resize-HudToContent
355
- } catch {
356
- Show-HudMessage "AI Battery unavailable"
357
- }
358
- Invalidate-DockedTuiIfChanged $Snapshot
359
- Update-HudVisibilityForFullscreen
360
- }
361
-
362
- function Invoke-HudPump {
363
- # Everything here must return quickly: this runs on the UI thread, and the
364
- # actual data fetch happens on a background runspace.
365
- if ($script:fetchPowerShell -and -not $script:fetchHandle.IsCompleted -and
366
- ([datetime]::UtcNow - $script:lastFetchStartUtc).TotalSeconds -gt 30) {
367
- Stop-SnapshotFetch
368
- }
369
-
370
- $fresh = Complete-SnapshotFetch
371
- if ($fresh) {
372
- $merged = Merge-HudSnapshot $fresh $script:latestSnapshot
373
- $script:latestSnapshot = $merged
374
- Write-HudSnapshot $merged
375
- Update-HudFromSnapshot $merged
376
-
377
- $missingWeekly = @($merged.results | Where-Object { $_.ok -and $null -ne $_.percentRemaining -and $null -eq $_.secondary })
378
- if ($missingWeekly.Count -gt 0 -and $script:weeklyRetryCount -lt 6) {
379
- $script:weeklyRetryCount += 1
380
- Start-SnapshotFetch
381
- return
382
- }
383
- $script:weeklyRetryCount = 0
384
- return
385
- }
386
-
387
- if (-not $script:fetchPowerShell -and
388
- ([datetime]::UtcNow - $script:lastFetchStartUtc).TotalSeconds -ge [math]::Max(1, $Interval)) {
389
- Start-SnapshotFetch
390
- }
391
- }
392
-
393
- $timer = New-Object System.Windows.Forms.Timer
394
- $timer.Interval = 1000
395
- $timer.add_Tick({ Invoke-HudPump })
396
- # WinEvent hooks wake fullscreen checks immediately on foreground/Z-order/show
397
- # changes. The UI timer is only a fallback for Windows builds or shells that do
398
- # not deliver every event we subscribe to.
399
- $topMostTimer = New-Object System.Windows.Forms.Timer
400
- $topMostTimer.Interval = $script:dockIdleTrackingInterval
1
+ function Set-HudBatteryImage($Box, [Nullable[int]]$Percent, [bool]$Running) {
2
+ $oldImage = $Box.Image
3
+ $Box.Image = New-BatteryImage $Percent $Running 36 16 $false
4
+ $Box.AccessibleName = if ($null -eq $Percent) { "--" } else { [string][int]$Percent }
5
+ $Box.ForeColor = Get-HudBatteryTextColor $Running
6
+ $Box.Visible = $true
7
+ $Box.Tag = $true
8
+ $Box.Invalidate()
9
+ if ($oldImage) { $oldImage.Dispose() }
10
+ }
11
+
12
+ function New-HudGlyphImage([string]$Kind, [System.Drawing.Color]$Color, [int]$Width = 21, [int]$Height = 18) {
13
+ $surface = New-HudBitmapSurface $Width $Height
14
+ $bitmap = $surface.Bitmap
15
+ $graphics = $surface.Graphics
16
+ $graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias
17
+ $graphics.TextRenderingHint = [System.Drawing.Text.TextRenderingHint]::AntiAliasGridFit
18
+ $graphics.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
19
+ $graphics.PixelOffsetMode = [System.Drawing.Drawing2D.PixelOffsetMode]::Half
20
+ $graphics.Clear([System.Drawing.Color]::Transparent)
21
+
22
+ if ($Kind -eq "reset") {
23
+ $pen = [System.Drawing.Pen]::new($Color, 2.1)
24
+ $pen.StartCap = [System.Drawing.Drawing2D.LineCap]::Round
25
+ $pen.EndCap = [System.Drawing.Drawing2D.LineCap]::Round
26
+ $brush = [System.Drawing.SolidBrush]::new($Color)
27
+ try {
28
+ $arcX = 3.6
29
+ $arcY = 2.5
30
+ $arcW = 13.0
31
+ $arcH = 13.0
32
+ $startAngle = 150.0
33
+ $sweepAngle = 190.0
34
+ $endAngle = ($startAngle + $sweepAngle) * [math]::PI / 180.0
35
+ $centerX = $arcX + ($arcW / 2.0)
36
+ $centerY = $arcY + ($arcH / 2.0)
37
+ $radiusX = $arcW / 2.0
38
+ $radiusY = $arcH / 2.0
39
+
40
+ $graphics.DrawArc($pen, [single]$arcX, [single]$arcY, [single]$arcW, [single]$arcH, [single]$startAngle, [single]$sweepAngle)
41
+
42
+ $tipX = $centerX + ($radiusX * [math]::Cos($endAngle))
43
+ $tipY = $centerY + ($radiusY * [math]::Sin($endAngle))
44
+ $dirX = -[math]::Sin($endAngle)
45
+ $dirY = [math]::Cos($endAngle)
46
+ $length = [math]::Sqrt(($dirX * $dirX) + ($dirY * $dirY))
47
+ if ($length -gt 0) {
48
+ $dirX = $dirX / $length
49
+ $dirY = $dirY / $length
50
+ }
51
+ $normalX = -$dirY
52
+ $normalY = $dirX
53
+ $headLength = 4.8
54
+ $headWidth = 3.3
55
+ $baseX = $tipX - ($dirX * $headLength)
56
+ $baseY = $tipY - ($dirY * $headLength)
57
+
58
+ $points = @(
59
+ [System.Drawing.PointF]::new([single]$tipX, [single]$tipY),
60
+ [System.Drawing.PointF]::new([single]($baseX + ($normalX * $headWidth)), [single]($baseY + ($normalY * $headWidth))),
61
+ [System.Drawing.PointF]::new([single]($baseX - ($normalX * $headWidth)), [single]($baseY - ($normalY * $headWidth)))
62
+ )
63
+ $graphics.FillPolygon($brush, $points)
64
+ } finally {
65
+ $pen.Dispose()
66
+ $brush.Dispose()
67
+ $graphics.Dispose()
68
+ }
69
+ return $bitmap
70
+ }
71
+
72
+ $sourceSize = 48
73
+ $source = [System.Drawing.Bitmap]::new($sourceSize, $sourceSize)
74
+ $sourceGraphics = [System.Drawing.Graphics]::FromImage($source)
75
+ $sourceGraphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias
76
+ $sourceGraphics.TextRenderingHint = [System.Drawing.Text.TextRenderingHint]::AntiAliasGridFit
77
+ $sourceGraphics.PixelOffsetMode = [System.Drawing.Drawing2D.PixelOffsetMode]::Half
78
+ $sourceGraphics.Clear([System.Drawing.Color]::Transparent)
79
+
80
+ $brush = [System.Drawing.SolidBrush]::new($Color)
81
+ $format = [System.Drawing.StringFormat]::new()
82
+ $format.Alignment = [System.Drawing.StringAlignment]::Center
83
+ $format.LineAlignment = [System.Drawing.StringAlignment]::Near
84
+
85
+ try {
86
+ $glyph = [string][char]0x2466
87
+ $maxIconWidth = 12
88
+ $maxIconHeight = 12
89
+ $sourceGraphics.DrawString(
90
+ $glyph,
91
+ $symbolFont,
92
+ $brush,
93
+ [System.Drawing.RectangleF]::new([single]0, [single]0, [single]$sourceSize, [single]$sourceSize),
94
+ $format
95
+ )
96
+
97
+ $left = $sourceSize
98
+ $top = $sourceSize
99
+ $right = -1
100
+ $bottom = -1
101
+ for ($y = 0; $y -lt $sourceSize; $y += 1) {
102
+ for ($x = 0; $x -lt $sourceSize; $x += 1) {
103
+ if ($source.GetPixel($x, $y).A -gt 0) {
104
+ if ($x -lt $left) { $left = $x }
105
+ if ($x -gt $right) { $right = $x }
106
+ if ($y -lt $top) { $top = $y }
107
+ if ($y -gt $bottom) { $bottom = $y }
108
+ }
109
+ }
110
+ }
111
+
112
+ if ($right -ge $left -and $bottom -ge $top) {
113
+ $sourceRect = [System.Drawing.Rectangle]::new($left, $top, ($right - $left + 1), ($bottom - $top + 1))
114
+ $scale = [math]::Min(1.0, [math]::Min(($maxIconWidth / [double]$sourceRect.Width), ($maxIconHeight / [double]$sourceRect.Height)))
115
+ $targetWidth = [math]::Max(1, [int][math]::Round($sourceRect.Width * $scale))
116
+ $targetHeight = [math]::Max(1, [int][math]::Round($sourceRect.Height * $scale))
117
+ $targetX = [math]::Floor(($Width - $targetWidth) / 2)
118
+ $targetY = [math]::Floor(($Height - $targetHeight) / 2)
119
+ $targetRect = [System.Drawing.Rectangle]::new([int]$targetX, [int]$targetY, $targetWidth, $targetHeight)
120
+ $graphics.DrawImage($source, $targetRect, $sourceRect, [System.Drawing.GraphicsUnit]::Pixel)
121
+ }
122
+ } finally {
123
+ $brush.Dispose()
124
+ $format.Dispose()
125
+ $sourceGraphics.Dispose()
126
+ $source.Dispose()
127
+ $graphics.Dispose()
128
+ }
129
+
130
+ return $bitmap
131
+ }
132
+
133
+ function Set-HudLineIconImage($Box, [string]$Kind, [string]$Value, [System.Drawing.Color]$Color) {
134
+ $oldImage = $Box.Image
135
+ if ([string]::IsNullOrWhiteSpace($Value)) {
136
+ $Box.Image = $null
137
+ $Box.Visible = $false
138
+ } else {
139
+ $Box.Image = New-HudGlyphImage $Kind $Color
140
+ $Box.Visible = $true
141
+ }
142
+ if ($oldImage) { $oldImage.Dispose() }
143
+ }
144
+
145
+ function Set-HudMetricLabel($Label, [string]$Value, [System.Drawing.Color]$Color, [Nullable[int]]$FixedWidth = $null) {
146
+ $Label.Text = $Value
147
+ $Label.ForeColor = $Color
148
+ if ([string]::IsNullOrWhiteSpace($Value)) {
149
+ $Label.Width = 0
150
+ $Label.Visible = $false
151
+ $Label.Tag = $false
152
+ } else {
153
+ $Label.Width = if ($null -ne $FixedWidth -and $FixedWidth -gt 0) {
154
+ [int]$FixedWidth
155
+ } else {
156
+ (Get-LabelTextWidth $Label) + (Scale-HudValue 1)
157
+ }
158
+ $Label.Visible = $true
159
+ $Label.Tag = $true
160
+ }
161
+ }
162
+
163
+ function Set-HudDivider($Divider, [string]$Value) {
164
+ if ([string]::IsNullOrWhiteSpace($Value)) {
165
+ $Divider.Visible = $false
166
+ $Divider.Width = 0
167
+ $Divider.Tag = $false
168
+ } else {
169
+ $Divider.Visible = $true
170
+ $Divider.Width = Scale-HudValue 1
171
+ $Divider.Tag = $true
172
+ }
173
+ }
174
+
175
+ function Get-HudTextWidth([string]$Value, $Font = $font) {
176
+ if ([string]::IsNullOrWhiteSpace($Value)) { return 0 }
177
+ return [System.Windows.Forms.TextRenderer]::MeasureText(
178
+ $Value,
179
+ $Font,
180
+ [System.Drawing.Size]::new(1000, 1000),
181
+ [System.Windows.Forms.TextFormatFlags]::NoPadding
182
+ ).Width
183
+ }
184
+
185
+ function Get-SharedHudColumnWidth([string[]]$Values, [int]$Minimum = 0) {
186
+ $width = Scale-HudValue $Minimum
187
+ foreach ($value in $Values) {
188
+ $width = [math]::Max($width, (Get-HudTextWidth $value) + (Scale-HudValue 1))
189
+ }
190
+ return [int]$width
191
+ }
192
+
193
+ function Set-HudParts($Parts, $PrefixLabel, $BatteryBox, $Divider1, $ResetLabel, $Divider2, $WeekLabel, $ExtraLabel, [Nullable[int]]$ResetWidth = $null, [Nullable[int]]$WeekWidth = $null) {
194
+ $PrefixLabel.Text = $Parts.Prefix
195
+ $PrefixLabel.ForeColor = $Parts.TextColor
196
+ $PrefixLabel.Width = Scale-HudValue 48
197
+ $PrefixLabel.Visible = $true
198
+ $PrefixLabel.Tag = $true
199
+ Set-HudBatteryImage $BatteryBox $Parts.Percent $Parts.Running
200
+ Set-HudDivider $Divider1 $Parts.ResetValue
201
+ Set-HudMetricLabel $ResetLabel $Parts.ResetValue $Parts.TextColor $ResetWidth
202
+ Set-HudDivider $Divider2 $Parts.WeekValue
203
+ Set-HudMetricLabel $WeekLabel $Parts.WeekValue $Parts.TextColor $WeekWidth
204
+ Set-HudMetricLabel $ExtraLabel $Parts.ExtraText $Parts.TextColor
205
+ }
206
+
207
+ function Set-HudControlsVisible($Controls, [bool]$Visible) {
208
+ foreach ($control in $Controls) {
209
+ $control.Visible = $Visible
210
+ $control.Tag = $Visible
211
+ }
212
+ }
213
+
214
+ function Get-LabelTextWidth($Label) {
215
+ if (-not $Label.Text) { return 0 }
216
+ return [System.Windows.Forms.TextRenderer]::MeasureText(
217
+ $Label.Text,
218
+ $Label.Font,
219
+ [System.Drawing.Size]::new(1000, 1000),
220
+ [System.Windows.Forms.TextFormatFlags]::NoPadding
221
+ ).Width
222
+ }
223
+
224
+ function Get-ControlLayoutWidth($Control) {
225
+ # Control.Visible reads as $false while the form is still hidden, so track
226
+ # the intended visibility in Tag to keep pre-show layout math correct.
227
+ if ($Control.Tag -ne $true) { return 0 }
228
+ $baseWidth = $Control.Width
229
+ return $baseWidth + $Control.Margin.Left + $Control.Margin.Right
230
+ }
231
+
232
+ function Get-HudControlsWidth($Controls) {
233
+ $width = 0
234
+ foreach ($control in $Controls) {
235
+ $width += Get-ControlLayoutWidth $control
236
+ }
237
+ return $width
238
+ }
239
+
240
+ function Resize-HudToContent {
241
+ if ($Mode -eq "tray") { return }
242
+
243
+ $oldRight = $form.Left + $form.Width
244
+ $oldBottom = $form.Top + $form.Height
245
+ $codexWidth = $(if ($script:codexRowVisible) { Get-HudControlsWidth $codexHudControls } else { 0 })
246
+ $claudeWidth = $(if ($script:claudeRowVisible) { Get-HudControlsWidth $claudeHudControls } else { 0 })
247
+ $bothRows = $script:codexRowVisible -and $script:claudeRowVisible
248
+ if ($providerDivider) {
249
+ $providerDivider.Visible = $bothRows
250
+ $providerDivider.Tag = $bothRows
251
+ }
252
+ if ($script:dockTuiStatusline) {
253
+ Set-DockedTuiWindowRegion
254
+ Sync-HitFormBounds
255
+ return
256
+ }
257
+ $contentWidth = [math]::Max($codexWidth, $claudeWidth)
258
+ $desiredWidth = $panel.Padding.Left + $panel.Padding.Right +
259
+ $contentWidth +
260
+ (Scale-HudValue 1)
261
+ $desiredWidth = [math]::Max((Scale-HudValue 150), [int][math]::Ceiling($desiredWidth))
262
+
263
+ $desiredHeight = [int]$(if ($bothRows) { $script:hudTwoRowHeight } else { $script:hudOneRowHeight })
264
+ if ($bothRows) {
265
+ $panel.RowStyles[0] = [System.Windows.Forms.RowStyle]::new([System.Windows.Forms.SizeType]::Percent, 50)
266
+ $panel.RowStyles[1] = [System.Windows.Forms.RowStyle]::new([System.Windows.Forms.SizeType]::Percent, 50)
267
+ } elseif ($script:claudeRowVisible) {
268
+ $panel.RowStyles[0] = [System.Windows.Forms.RowStyle]::new([System.Windows.Forms.SizeType]::Absolute, 0)
269
+ $panel.RowStyles[1] = [System.Windows.Forms.RowStyle]::new([System.Windows.Forms.SizeType]::Percent, 100)
270
+ } else {
271
+ $panel.RowStyles[0] = [System.Windows.Forms.RowStyle]::new([System.Windows.Forms.SizeType]::Percent, 100)
272
+ $panel.RowStyles[1] = [System.Windows.Forms.RowStyle]::new([System.Windows.Forms.SizeType]::Absolute, 0)
273
+ }
274
+
275
+ if (([math]::Abs($form.Width - $desiredWidth) -gt 1) -or ($form.Height -ne $desiredHeight)) {
276
+ $form.Width = $desiredWidth
277
+ $form.Height = $desiredHeight
278
+ $newX = if ($script:hudAnchorX -eq "right") { $oldRight - $form.Width } else { $form.Left }
279
+ $newY = if ($script:hudAnchorY -eq "bottom") { $oldBottom - $form.Height } else { $form.Top }
280
+ $form.Location = [System.Drawing.Point]::new([int]$newX, [int]$newY)
281
+ Sync-HitFormBounds
282
+ Save-HudPositionIfReady
283
+ }
284
+ }
285
+
286
+ function Show-HudMessage([string]$Message) {
287
+ $codexPrefixLabel.Text = $Message
288
+ $codexPrefixLabel.ForeColor = Get-HudMutedTextColor
289
+ $codexPrefixLabel.Width = (Get-LabelTextWidth $codexPrefixLabel) + (Scale-HudValue 1)
290
+ $codexPrefixLabel.Visible = $true
291
+ $codexPrefixLabel.Tag = $true
292
+ foreach ($label in @($codexResetLabel, $codexWeekLabel, $codexExtraLabel, $claudePrefixLabel, $claudeResetLabel, $claudeWeekLabel, $claudeExtraLabel)) {
293
+ $label.Text = ""
294
+ $label.Width = 0
295
+ $label.Visible = $false
296
+ $label.Tag = $false
297
+ }
298
+ foreach ($divider in @($codexDivider1, $codexDivider2, $claudeDivider1, $claudeDivider2)) {
299
+ $divider.Visible = $false
300
+ $divider.Width = 0
301
+ $divider.Tag = $false
302
+ }
303
+ Set-HudBatteryImage $codexIconLabel $null $false
304
+ $oldImage = $claudeIconLabel.Image
305
+ $claudeIconLabel.Image = $null
306
+ $claudeIconLabel.Visible = $false
307
+ $claudeIconLabel.Tag = $false
308
+ if ($oldImage) { $oldImage.Dispose() }
309
+ $script:codexRowVisible = $true
310
+ $script:claudeRowVisible = $false
311
+ $codexRow.Visible = $true
312
+ $claudeRow.Visible = $false
313
+ Resize-HudToContent
314
+ }
315
+
316
+ function Update-HudFromSnapshot($Snapshot) {
317
+ if (-not $Snapshot) {
318
+ Show-HudMessage "AI Battery starting..."
319
+ Invalidate-DockedTuiIfChanged $Snapshot
320
+ Update-HudVisibilityForFullscreen
321
+ return
322
+ }
323
+
324
+ try {
325
+ $texts = ConvertTo-HudTexts $Snapshot
326
+ $script:codexRowVisible = [bool]$texts.CodexVisible
327
+ $script:claudeRowVisible = [bool]$texts.ClaudeVisible
328
+ $resetValues = @()
329
+ $weekValues = @()
330
+ if ($texts.CodexVisible) {
331
+ if ($texts.Codex.ResetValue) { $resetValues += $texts.Codex.ResetValue }
332
+ if ($texts.Codex.WeekValue) { $weekValues += $texts.Codex.WeekValue }
333
+ }
334
+ if ($texts.ClaudeVisible) {
335
+ if ($texts.Claude.ResetValue) { $resetValues += $texts.Claude.ResetValue }
336
+ if ($texts.Claude.WeekValue) { $weekValues += $texts.Claude.WeekValue }
337
+ }
338
+ $resetWidth = Get-SharedHudColumnWidth $resetValues
339
+ $weekWidth = Get-SharedHudColumnWidth $weekValues
340
+ if ($texts.CodexVisible) {
341
+ $codexRow.Visible = $true
342
+ Set-HudParts $texts.Codex $codexPrefixLabel $codexIconLabel $codexDivider1 $codexResetLabel $codexDivider2 $codexWeekLabel $codexExtraLabel $resetWidth $weekWidth
343
+ } else {
344
+ $codexRow.Visible = $false
345
+ Set-HudControlsVisible $codexHudControls $false
346
+ }
347
+ if ($texts.ClaudeVisible) {
348
+ $claudeRow.Visible = $true
349
+ Set-HudParts $texts.Claude $claudePrefixLabel $claudeIconLabel $claudeDivider1 $claudeResetLabel $claudeDivider2 $claudeWeekLabel $claudeExtraLabel $resetWidth $weekWidth
350
+ } else {
351
+ $claudeRow.Visible = $false
352
+ Set-HudControlsVisible $claudeHudControls $false
353
+ }
354
+ Resize-HudToContent
355
+ } catch {
356
+ Show-HudMessage "AI Battery unavailable"
357
+ }
358
+ Invalidate-DockedTuiIfChanged $Snapshot
359
+ Update-HudVisibilityForFullscreen
360
+ }
361
+
362
+ function Invoke-HudPump {
363
+ # Everything here must return quickly: this runs on the UI thread, and the
364
+ # actual data fetch happens on a background runspace.
365
+ if ($script:fetchPowerShell -and -not $script:fetchHandle.IsCompleted -and
366
+ ([datetime]::UtcNow - $script:lastFetchStartUtc).TotalSeconds -gt 30) {
367
+ Stop-SnapshotFetch
368
+ }
369
+
370
+ $fresh = Complete-SnapshotFetch
371
+ if ($fresh) {
372
+ $merged = Merge-HudSnapshot $fresh $script:latestSnapshot
373
+ $script:latestSnapshot = $merged
374
+ Write-HudSnapshot $merged
375
+ Update-HudFromSnapshot $merged
376
+
377
+ $missingWeekly = @($merged.results | Where-Object { $_.ok -and $null -ne $_.percentRemaining -and $null -eq $_.secondary })
378
+ if ($missingWeekly.Count -gt 0 -and $script:weeklyRetryCount -lt 6) {
379
+ $script:weeklyRetryCount += 1
380
+ Start-SnapshotFetch
381
+ return
382
+ }
383
+ $script:weeklyRetryCount = 0
384
+ return
385
+ }
386
+
387
+ if (-not $script:fetchPowerShell -and
388
+ ([datetime]::UtcNow - $script:lastFetchStartUtc).TotalSeconds -ge [math]::Max(1, $Interval)) {
389
+ Start-SnapshotFetch
390
+ }
391
+ }
392
+
393
+ $timer = New-Object System.Windows.Forms.Timer
394
+ $timer.Interval = 1000
395
+ $timer.add_Tick({ Invoke-HudPump })
396
+ # WinEvent hooks wake fullscreen checks immediately on foreground/Z-order/show
397
+ # changes. The UI timer is only a fallback for Windows builds or shells that do
398
+ # not deliver every event we subscribe to.
399
+ $topMostTimer = New-Object System.Windows.Forms.Timer
400
+ $topMostTimer.Interval = $script:dockIdleTrackingInterval
401
401
  $topMostTimer.add_Tick({
402
402
  Update-HudVisibilityForFullscreen
403
403
  Update-HudDockRequest
404
404
  Update-DockSessionLifetime
405
405
  Update-HudDockPlacement
406
406
  })
407
- $menuAutoHideTimer = New-Object System.Windows.Forms.Timer
408
- $menuAutoHideTimer.Interval = 50
409
- $menuAutoHideTimer.add_Tick({ Update-HudMenuAutoHide })
407
+ $menuAutoHideTimer = New-Object System.Windows.Forms.Timer
408
+ $menuAutoHideTimer.Interval = 50
409
+ $menuAutoHideTimer.add_Tick({ Update-HudMenuAutoHide })