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.
@@ -9,385 +9,385 @@ function Get-HudDpi {
9
9
  }
10
10
 
11
11
  try {
12
- if ($form -and -not $form.IsDisposed -and $form.Handle -ne [IntPtr]::Zero) {
13
- $windowDpi = [AiBatteryNative]::GetDpiForWindow($form.Handle)
14
- if ($windowDpi -gt 0) { return [int]$windowDpi }
15
- }
16
- } catch {
17
- # Fall back to the desktop graphics DPI below.
18
- }
19
-
20
- $graphics = $null
21
- try {
22
- $graphics = [System.Drawing.Graphics]::FromHwnd([IntPtr]::Zero)
23
- if ($graphics.DpiX -gt 0) { return [int][math]::Round($graphics.DpiX) }
24
- } catch {
25
- # Use the WinForms baseline when the desktop DPI cannot be queried.
26
- } finally {
27
- if ($graphics) { $graphics.Dispose() }
28
- }
29
-
30
- return 96
31
- }
32
-
33
- function Update-HudDpiScale {
34
- $dpi = [math]::Max(96, (Get-HudDpi))
35
- $script:hudDpi = [int]$dpi
36
- $script:hudScale = [double]$script:hudDpi / 96.0
37
- }
38
-
39
- function Scale-HudValue([double]$Value) {
40
- if ($Value -eq 0) { return 0 }
41
- $scaled = [int][math]::Round($Value * $script:hudScale)
42
- if ($Value -gt 0) { return [math]::Max(1, $scaled) }
43
- return $scaled
44
- }
45
-
46
- function New-HudPadding([int]$Left, [int]$Top, [int]$Right, [int]$Bottom) {
47
- return [System.Windows.Forms.Padding]::new(
48
- (Scale-HudValue $Left),
49
- (Scale-HudValue $Top),
50
- (Scale-HudValue $Right),
51
- (Scale-HudValue $Bottom)
52
- )
53
- }
54
-
55
- function New-HudBitmapSurface([int]$Width, [int]$Height) {
56
- $scale = [math]::Max(1.0, [double]$script:hudScale)
57
- $bitmapWidth = [math]::Max(1, [int][math]::Round($Width * $scale))
58
- $bitmapHeight = [math]::Max(1, [int][math]::Round($Height * $scale))
59
- $bitmap = [System.Drawing.Bitmap]::new($bitmapWidth, $bitmapHeight)
60
- $graphics = [System.Drawing.Graphics]::FromImage($bitmap)
61
- if ([math]::Abs($scale - 1.0) -gt 0.001) {
62
- $graphics.ScaleTransform([single]$scale, [single]$scale)
63
- }
64
- return @{
65
- Bitmap = $bitmap
66
- Graphics = $graphics
67
- }
68
- }
69
-
70
- function Test-HudTransparentSurface {
71
- $value = $Transparent
72
- if (-not $value) { $value = $env:AI_BATTERY_HUD_TRANSPARENT }
73
- if (-not $value) { $value = $env:CLAUDEX_BATTERY_HUD_TRANSPARENT }
74
- if (-not $value) { return $true }
75
- $normalized = ([string]$value).Trim().ToLowerInvariant()
76
- return -not (@("0", "false", "no", "off", "solid") -contains $normalized)
77
- }
78
-
79
- function Test-HudContrastBackdrop {
80
- $normalized = ([string]$Backdrop).Trim().ToLowerInvariant()
81
- if (-not $normalized) { $normalized = "off" }
82
- return @("1", "true", "yes", "on", "solid", "backdrop") -contains $normalized
83
- }
84
-
85
- Update-HudDpiScale
86
- $script:hudDarkText = Test-HudDarkText
87
-
88
- function Get-LineText($Parts) {
89
- return "$($Parts.Prefix)[battery]$($Parts.Suffix)"
90
- }
91
-
92
- function Limit-Text([string]$Text, [int]$MaxLength) {
93
- if ($Text.Length -le $MaxLength) { return $Text }
94
- return $Text.Substring(0, [math]::Max(0, $MaxLength - 3)) + "..."
95
- }
96
-
97
- function Select-TrayPercent($Texts) {
98
- $items = @()
99
- foreach ($result in @($Texts.CodexResult, $Texts.ClaudeResult)) {
100
- if ($result -and $result.ok -and $null -ne $result.percentRemaining) {
101
- $items += [PSCustomObject]@{
102
- Percent = [int]$result.percentRemaining
103
- Running = [bool]$result.running
104
- }
105
- }
106
- }
107
-
108
- $runningItems = @($items | Where-Object { $_.Running })
109
- if ($runningItems.Count -gt 0) {
110
- return [Nullable[int]](@($runningItems | Sort-Object Percent)[0].Percent)
111
- }
112
- if ($items.Count -gt 0) {
113
- return [Nullable[int]](@($items | Sort-Object Percent)[0].Percent)
114
- }
115
- return $null
116
- }
117
-
118
- function New-ProviderTrayIcon([string]$ProviderLabel, [Nullable[int]]$Percent, [bool]$Running) {
119
- $bitmap = [System.Drawing.Bitmap]::new(32, 32)
120
- $graphics = [System.Drawing.Graphics]::FromImage($bitmap)
121
- $graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias
122
- $graphics.TextRenderingHint = [System.Drawing.Text.TextRenderingHint]::AntiAliasGridFit
123
- $graphics.Clear([System.Drawing.Color]::Transparent)
124
-
125
- $outlineColor = [System.Drawing.Color]::FromArgb(170, 170, 170)
126
- $mutedColor = [System.Drawing.Color]::FromArgb(120, 120, 120)
127
- $activeOutlineColor = if ($Running) { $outlineColor } else { $mutedColor }
128
- $fillColor = if ($Running) { Get-PercentColor $Percent } else { $mutedColor }
129
- $textColor = if ($Running) { [System.Drawing.Color]::FromArgb(245, 245, 245) } else { $mutedColor }
130
- $outlinePen = [System.Drawing.Pen]::new($(if ($null -eq $Percent) { $mutedColor } else { $activeOutlineColor }), 1.6)
131
- $fillBrush = [System.Drawing.SolidBrush]::new($fillColor)
132
- $terminalBrush = [System.Drawing.SolidBrush]::new($(if ($null -eq $Percent) { $mutedColor } else { $activeOutlineColor }))
133
- $textBrush = [System.Drawing.SolidBrush]::new($textColor)
134
- $labelFont = [System.Drawing.Font]::new("Segoe UI", 7, [System.Drawing.FontStyle]::Bold)
135
- $percentText = if ($null -eq $Percent) { "--" } else { [string][int]$Percent }
136
- $percentFontSize = if ($percentText.Length -ge 3) { 8.2 } else { 9.2 }
137
- $percentFont = [System.Drawing.Font]::new("Segoe UI", $percentFontSize, [System.Drawing.FontStyle]::Bold)
138
- $centerFormat = [System.Drawing.StringFormat]::new()
139
- $centerFormat.Alignment = [System.Drawing.StringAlignment]::Center
140
- $centerFormat.LineAlignment = [System.Drawing.StringAlignment]::Center
141
-
142
- try {
143
- $graphics.DrawString($ProviderLabel, $labelFont, $textBrush, [System.Drawing.RectangleF]::new(0, -1, 32, 10), $centerFormat)
144
- $graphics.DrawRectangle($outlinePen, 3, 11, 23, 16)
145
- $graphics.FillRectangle($terminalBrush, 27, 16, 3, 6)
146
-
147
- if ($null -eq $Percent) {
148
- $dashPen = [System.Drawing.Pen]::new($mutedColor, 2)
149
- try {
150
- $graphics.DrawLine($dashPen, 8, 19, 21, 19)
151
- } finally {
152
- $dashPen.Dispose()
153
- }
154
- } else {
155
- $clamped = [math]::Max(0, [math]::Min(100, $Percent))
156
- $fillWidth = [math]::Floor(($clamped / 100.0) * 19)
157
- if ($clamped -gt 0 -and $fillWidth -lt 2) { $fillWidth = 2 }
158
- $graphics.FillRectangle($fillBrush, 5, 13, $fillWidth, 12)
159
- }
160
- $graphics.DrawString($percentText, $percentFont, $textBrush, [System.Drawing.RectangleF]::new(4, 11, 22, 16), $centerFormat)
161
- } finally {
162
- $outlinePen.Dispose()
163
- $fillBrush.Dispose()
164
- $terminalBrush.Dispose()
165
- $textBrush.Dispose()
166
- $labelFont.Dispose()
167
- $percentFont.Dispose()
168
- $centerFormat.Dispose()
169
- $graphics.Dispose()
170
- }
171
-
172
- $handle = $bitmap.GetHicon()
173
- $sourceIcon = [System.Drawing.Icon]::FromHandle($handle)
174
- $icon = $sourceIcon.Clone()
175
- $sourceIcon.Dispose()
176
- [AiBatteryNative]::DestroyIcon($handle) | Out-Null
177
- $bitmap.Dispose()
178
- return $icon
179
- }
180
-
181
- function New-SingleBatteryTrayIcon([Nullable[int]]$Percent) {
182
- $bitmap = [System.Drawing.Bitmap]::new(32, 32)
183
- $graphics = [System.Drawing.Graphics]::FromImage($bitmap)
184
- $graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias
185
- $graphics.Clear([System.Drawing.Color]::Transparent)
186
-
187
- $outlineColor = [System.Drawing.Color]::FromArgb(170, 170, 170)
188
- $mutedColor = [System.Drawing.Color]::FromArgb(120, 120, 120)
189
- $fillColor = Get-PercentColor $Percent
190
- $outlinePen = [System.Drawing.Pen]::new($(if ($null -eq $Percent) { $mutedColor } else { $outlineColor }), 2)
191
- $fillBrush = [System.Drawing.SolidBrush]::new($fillColor)
192
- $terminalBrush = [System.Drawing.SolidBrush]::new($(if ($null -eq $Percent) { $mutedColor } else { $outlineColor }))
193
-
194
- try {
195
- $graphics.DrawRectangle($outlinePen, 4, 9, 21, 14)
196
- $graphics.FillRectangle($terminalBrush, 26, 13, 3, 6)
197
-
198
- if ($null -eq $Percent) {
199
- $dashPen = [System.Drawing.Pen]::new($mutedColor, 2)
200
- try {
201
- $graphics.DrawLine($dashPen, 8, 16, 21, 16)
202
- } finally {
203
- $dashPen.Dispose()
204
- }
205
- } else {
206
- $clamped = [math]::Max(0, [math]::Min(100, $Percent))
207
- $fillWidth = [math]::Floor(($clamped / 100.0) * 17)
208
- if ($clamped -gt 0 -and $fillWidth -lt 2) { $fillWidth = 2 }
209
- $graphics.FillRectangle($fillBrush, 7, 12, $fillWidth, 8)
210
- }
211
- } finally {
212
- $outlinePen.Dispose()
213
- $fillBrush.Dispose()
214
- $terminalBrush.Dispose()
215
- $graphics.Dispose()
216
- }
217
-
218
- $handle = $bitmap.GetHicon()
219
- $sourceIcon = [System.Drawing.Icon]::FromHandle($handle)
220
- $icon = $sourceIcon.Clone()
221
- $sourceIcon.Dispose()
222
- [AiBatteryNative]::DestroyIcon($handle) | Out-Null
223
- $bitmap.Dispose()
224
- return $icon
225
- }
226
-
227
- function New-BatteryImage([Nullable[int]]$Percent, [bool]$Running = $true, [int]$Width = 36, [int]$Height = 16, [bool]$DrawText = $true) {
228
- $surface = New-HudBitmapSurface $Width $Height
229
- $bitmap = $surface.Bitmap
230
- $graphics = $surface.Graphics
231
- $graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias
232
- $graphics.TextRenderingHint = [System.Drawing.Text.TextRenderingHint]::ClearTypeGridFit
233
- $graphics.PixelOffsetMode = [System.Drawing.Drawing2D.PixelOffsetMode]::Half
234
- $graphics.Clear([System.Drawing.Color]::Transparent)
235
-
236
- $outlineColor = [System.Drawing.Color]::FromArgb(170, 170, 170)
237
- $mutedColor = [System.Drawing.Color]::FromArgb(120, 120, 120)
238
- if ($script:hudDarkText) {
239
- $outlineColor = [System.Drawing.Color]::FromArgb(58, 58, 58)
240
- $mutedColor = [System.Drawing.Color]::FromArgb(98, 98, 98)
241
- }
242
- $activeOutlineColor = if ($Running) { $outlineColor } else { $mutedColor }
243
- # The fill always keeps its charge color (matching the terminal bar);
244
- # running state is signalled by the outline and text colors instead.
245
- $fillColor = Get-PercentColor $Percent
246
- $outlinePen = [System.Drawing.Pen]::new($(if ($null -eq $Percent) { $mutedColor } else { $activeOutlineColor }), 1.0)
247
- $fillBrush = [System.Drawing.SolidBrush]::new($fillColor)
248
- $interiorColor = if ($script:hudDarkText) {
249
- [System.Drawing.Color]::FromArgb(246, 246, 246)
250
- } else {
251
- # A solid dark interior keeps the desktop from bleeding through the empty
252
- # part of the battery, so the percent text stays readable on any wallpaper.
253
- [System.Drawing.Color]::FromArgb(46, 46, 46)
254
- }
255
- $interiorBrush = [System.Drawing.SolidBrush]::new($interiorColor)
256
- $terminalBrush = [System.Drawing.SolidBrush]::new($(if ($null -eq $Percent) { $mutedColor } else { $activeOutlineColor }))
257
- $textBrush = [System.Drawing.SolidBrush]::new((Get-HudBatteryTextColor $Running))
258
- $batteryText = if ($null -eq $Percent) { "--" } else { [string][int]$Percent }
259
- $fontSize = if ($batteryText.Length -ge 3) { 4.4 } elseif ($batteryText.Length -ge 2) { 5.1 } else { 5.8 }
260
- $batteryFont = $null
261
- $centerFormat = [System.Drawing.StringFormat]::new()
262
- $centerFormat.Alignment = [System.Drawing.StringAlignment]::Center
263
- $centerFormat.LineAlignment = [System.Drawing.StringAlignment]::Center
264
- $centerFormat.Trimming = [System.Drawing.StringTrimming]::None
265
- $centerFormat.FormatFlags = [System.Drawing.StringFormatFlags]::NoWrap
266
-
267
- try {
268
- $bodyX = 1
269
- $bodyY = 3
270
- $bodyW = $Width - 6
271
- $bodyH = $Height - 6
272
- $capW = 2
273
- $capH = [math]::Max(4, [math]::Floor($bodyH / 2))
274
- $capY = $bodyY + [math]::Floor(($bodyH - $capH) / 2)
275
-
276
- $graphics.FillRectangle($interiorBrush, $bodyX + 1, $bodyY + 1, $bodyW - 1, $bodyH - 1)
277
- $graphics.DrawRectangle($outlinePen, $bodyX, $bodyY, $bodyW, $bodyH)
278
- $graphics.FillRectangle($terminalBrush, $bodyX + $bodyW + 1, $capY, $capW, $capH)
279
-
280
- if ($null -ne $Percent) {
281
- # The "--" text alone marks the unknown state; a dash line under it
282
- # just collides with the halo.
283
- $clamped = [math]::Max(0, [math]::Min(100, $Percent))
284
- $innerW = $bodyW - 5
285
- $fillW = [math]::Floor(($clamped / 100.0) * $innerW)
286
- if ($clamped -gt 0 -and $fillW -lt 2) { $fillW = 2 }
287
- $graphics.FillRectangle($fillBrush, $bodyX + 3, $bodyY + 3, $fillW, $bodyH - 5)
288
- }
289
- if ($DrawText) {
290
- $textRect = [System.Drawing.RectangleF]::new($bodyX + 3, $bodyY + 2.5, $bodyW - 6, $bodyH - 4)
291
- do {
292
- if ($batteryFont) { $batteryFont.Dispose() }
293
- $fontFamily = if ($font) { $font.FontFamily.Name } else { "Segoe UI" }
294
- $batteryFont = [System.Drawing.Font]::new($fontFamily, $fontSize, [System.Drawing.FontStyle]::Regular)
295
- $textSize = $graphics.MeasureString($batteryText, $batteryFont)
296
- if ($textSize.Width -le ($textRect.Width + 1) -or $fontSize -le 4.0) { break }
297
- $fontSize -= 0.4
298
- } while ($true)
299
-
300
- $graphics.DrawString($batteryText, $batteryFont, $textBrush, $textRect, $centerFormat)
301
- }
302
- } finally {
303
- $outlinePen.Dispose()
304
- $fillBrush.Dispose()
305
- $interiorBrush.Dispose()
306
- $terminalBrush.Dispose()
307
- $textBrush.Dispose()
308
- if ($batteryFont) { $batteryFont.Dispose() }
309
- $centerFormat.Dispose()
310
- $graphics.Dispose()
311
- }
312
-
313
- return $bitmap
314
- }
315
-
316
- function New-StatusMenuLabel($Font, [int]$Width, $Align = [System.Drawing.ContentAlignment]::MiddleLeft) {
317
- $label = New-Object System.Windows.Forms.Label
318
- $label.AutoSize = $false
319
- $label.Width = $Width
320
- $label.Height = 20
321
- $label.Font = $Font
322
- $label.TextAlign = $Align
323
- $label.Margin = [System.Windows.Forms.Padding]::new(0)
324
- $label.BackColor = [System.Drawing.SystemColors]::Menu
325
- return $label
326
- }
327
-
328
- function New-StatusMenuRow($Font) {
329
- $panel = New-Object System.Windows.Forms.FlowLayoutPanel
330
- $panel.Width = 276
331
- $panel.Height = 22
332
- $panel.FlowDirection = [System.Windows.Forms.FlowDirection]::LeftToRight
333
- $panel.WrapContents = $false
334
- $panel.Margin = [System.Windows.Forms.Padding]::new(0)
335
- $panel.Padding = [System.Windows.Forms.Padding]::new(4, 1, 4, 1)
336
- $panel.BackColor = [System.Drawing.SystemColors]::Menu
337
-
338
- $name = New-StatusMenuLabel $Font 44
339
- $icon = New-Object System.Windows.Forms.PictureBox
340
- $icon.Width = 40
341
- $icon.Height = 20
342
- $icon.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::Zoom
343
- $icon.Margin = [System.Windows.Forms.Padding]::new(0)
344
- $icon.BackColor = [System.Drawing.SystemColors]::Menu
345
- $percent = New-StatusMenuLabel $Font 0 ([System.Drawing.ContentAlignment]::MiddleRight)
346
- $reset = New-StatusMenuLabel $Font 66
347
- $week = New-StatusMenuLabel $Font 56
348
- $extra = New-StatusMenuLabel $Font 58
349
-
350
- foreach ($control in @($name, $icon, $percent, $reset, $week, $extra)) {
351
- $panel.Controls.Add($control) | Out-Null
352
- }
353
-
354
- $rowHost = New-Object System.Windows.Forms.ToolStripControlHost($panel)
355
- $rowHost.AutoSize = $false
356
- $rowHost.Width = $panel.Width
357
- $rowHost.Height = $panel.Height
358
-
359
- return @{
360
- Host = $rowHost
361
- Panel = $panel
362
- Name = $name
363
- Icon = $icon
364
- Percent = $percent
365
- Reset = $reset
366
- Week = $week
367
- Extra = $extra
368
- }
369
- }
370
-
371
- function Set-StatusMenuRow($Row, $Parts) {
372
- $textColor = $Parts.TextColor
373
- $Row.Name.Text = $Parts.Prefix.TrimEnd()
374
- $Row.Percent.Text = $Parts.PercentText
375
- $Row.Reset.Text = $Parts.ResetText
376
- $Row.Week.Text = $Parts.WeekText
377
- $Row.Extra.Text = $Parts.ExtraText
378
-
379
- foreach ($label in @($Row.Name, $Row.Percent, $Row.Reset, $Row.Week, $Row.Extra)) {
380
- $label.ForeColor = $textColor
381
- }
382
-
383
- $oldImage = $Row.Icon.Image
384
- $Row.Icon.Image = New-BatteryImage $Parts.Percent $Parts.Running
385
- if ($oldImage) { $oldImage.Dispose() }
386
- }
387
-
388
- function Dispose-StatusMenuRow($Row) {
389
- if ($Row.Icon.Image) {
390
- $Row.Icon.Image.Dispose()
391
- $Row.Icon.Image = $null
392
- }
393
- }
12
+ if ($form -and -not $form.IsDisposed -and $form.Handle -ne [IntPtr]::Zero) {
13
+ $windowDpi = [AiBatteryNative]::GetDpiForWindow($form.Handle)
14
+ if ($windowDpi -gt 0) { return [int]$windowDpi }
15
+ }
16
+ } catch {
17
+ # Fall back to the desktop graphics DPI below.
18
+ }
19
+
20
+ $graphics = $null
21
+ try {
22
+ $graphics = [System.Drawing.Graphics]::FromHwnd([IntPtr]::Zero)
23
+ if ($graphics.DpiX -gt 0) { return [int][math]::Round($graphics.DpiX) }
24
+ } catch {
25
+ # Use the WinForms baseline when the desktop DPI cannot be queried.
26
+ } finally {
27
+ if ($graphics) { $graphics.Dispose() }
28
+ }
29
+
30
+ return 96
31
+ }
32
+
33
+ function Update-HudDpiScale {
34
+ $dpi = [math]::Max(96, (Get-HudDpi))
35
+ $script:hudDpi = [int]$dpi
36
+ $script:hudScale = [double]$script:hudDpi / 96.0
37
+ }
38
+
39
+ function Scale-HudValue([double]$Value) {
40
+ if ($Value -eq 0) { return 0 }
41
+ $scaled = [int][math]::Round($Value * $script:hudScale)
42
+ if ($Value -gt 0) { return [math]::Max(1, $scaled) }
43
+ return $scaled
44
+ }
45
+
46
+ function New-HudPadding([int]$Left, [int]$Top, [int]$Right, [int]$Bottom) {
47
+ return [System.Windows.Forms.Padding]::new(
48
+ (Scale-HudValue $Left),
49
+ (Scale-HudValue $Top),
50
+ (Scale-HudValue $Right),
51
+ (Scale-HudValue $Bottom)
52
+ )
53
+ }
54
+
55
+ function New-HudBitmapSurface([int]$Width, [int]$Height) {
56
+ $scale = [math]::Max(1.0, [double]$script:hudScale)
57
+ $bitmapWidth = [math]::Max(1, [int][math]::Round($Width * $scale))
58
+ $bitmapHeight = [math]::Max(1, [int][math]::Round($Height * $scale))
59
+ $bitmap = [System.Drawing.Bitmap]::new($bitmapWidth, $bitmapHeight)
60
+ $graphics = [System.Drawing.Graphics]::FromImage($bitmap)
61
+ if ([math]::Abs($scale - 1.0) -gt 0.001) {
62
+ $graphics.ScaleTransform([single]$scale, [single]$scale)
63
+ }
64
+ return @{
65
+ Bitmap = $bitmap
66
+ Graphics = $graphics
67
+ }
68
+ }
69
+
70
+ function Test-HudTransparentSurface {
71
+ $value = $Transparent
72
+ if (-not $value) { $value = $env:AI_BATTERY_HUD_TRANSPARENT }
73
+ if (-not $value) { $value = $env:CLAUDEX_BATTERY_HUD_TRANSPARENT }
74
+ if (-not $value) { return $true }
75
+ $normalized = ([string]$value).Trim().ToLowerInvariant()
76
+ return -not (@("0", "false", "no", "off", "solid") -contains $normalized)
77
+ }
78
+
79
+ function Test-HudContrastBackdrop {
80
+ $normalized = ([string]$Backdrop).Trim().ToLowerInvariant()
81
+ if (-not $normalized) { $normalized = "off" }
82
+ return @("1", "true", "yes", "on", "solid", "backdrop") -contains $normalized
83
+ }
84
+
85
+ Update-HudDpiScale
86
+ $script:hudDarkText = Test-HudDarkText
87
+
88
+ function Get-LineText($Parts) {
89
+ return "$($Parts.Prefix)[battery]$($Parts.Suffix)"
90
+ }
91
+
92
+ function Limit-Text([string]$Text, [int]$MaxLength) {
93
+ if ($Text.Length -le $MaxLength) { return $Text }
94
+ return $Text.Substring(0, [math]::Max(0, $MaxLength - 3)) + "..."
95
+ }
96
+
97
+ function Select-TrayPercent($Texts) {
98
+ $items = @()
99
+ foreach ($result in @($Texts.CodexResult, $Texts.ClaudeResult)) {
100
+ if ($result -and $result.ok -and $null -ne $result.percentRemaining) {
101
+ $items += [PSCustomObject]@{
102
+ Percent = [int]$result.percentRemaining
103
+ Running = [bool]$result.running
104
+ }
105
+ }
106
+ }
107
+
108
+ $runningItems = @($items | Where-Object { $_.Running })
109
+ if ($runningItems.Count -gt 0) {
110
+ return [Nullable[int]](@($runningItems | Sort-Object Percent)[0].Percent)
111
+ }
112
+ if ($items.Count -gt 0) {
113
+ return [Nullable[int]](@($items | Sort-Object Percent)[0].Percent)
114
+ }
115
+ return $null
116
+ }
117
+
118
+ function New-ProviderTrayIcon([string]$ProviderLabel, [Nullable[int]]$Percent, [bool]$Running) {
119
+ $bitmap = [System.Drawing.Bitmap]::new(32, 32)
120
+ $graphics = [System.Drawing.Graphics]::FromImage($bitmap)
121
+ $graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias
122
+ $graphics.TextRenderingHint = [System.Drawing.Text.TextRenderingHint]::AntiAliasGridFit
123
+ $graphics.Clear([System.Drawing.Color]::Transparent)
124
+
125
+ $outlineColor = [System.Drawing.Color]::FromArgb(170, 170, 170)
126
+ $mutedColor = [System.Drawing.Color]::FromArgb(120, 120, 120)
127
+ $activeOutlineColor = if ($Running) { $outlineColor } else { $mutedColor }
128
+ $fillColor = if ($Running) { Get-PercentColor $Percent } else { $mutedColor }
129
+ $textColor = if ($Running) { [System.Drawing.Color]::FromArgb(245, 245, 245) } else { $mutedColor }
130
+ $outlinePen = [System.Drawing.Pen]::new($(if ($null -eq $Percent) { $mutedColor } else { $activeOutlineColor }), 1.6)
131
+ $fillBrush = [System.Drawing.SolidBrush]::new($fillColor)
132
+ $terminalBrush = [System.Drawing.SolidBrush]::new($(if ($null -eq $Percent) { $mutedColor } else { $activeOutlineColor }))
133
+ $textBrush = [System.Drawing.SolidBrush]::new($textColor)
134
+ $labelFont = [System.Drawing.Font]::new("Segoe UI", 7, [System.Drawing.FontStyle]::Bold)
135
+ $percentText = if ($null -eq $Percent) { "--" } else { [string][int]$Percent }
136
+ $percentFontSize = if ($percentText.Length -ge 3) { 8.2 } else { 9.2 }
137
+ $percentFont = [System.Drawing.Font]::new("Segoe UI", $percentFontSize, [System.Drawing.FontStyle]::Bold)
138
+ $centerFormat = [System.Drawing.StringFormat]::new()
139
+ $centerFormat.Alignment = [System.Drawing.StringAlignment]::Center
140
+ $centerFormat.LineAlignment = [System.Drawing.StringAlignment]::Center
141
+
142
+ try {
143
+ $graphics.DrawString($ProviderLabel, $labelFont, $textBrush, [System.Drawing.RectangleF]::new(0, -1, 32, 10), $centerFormat)
144
+ $graphics.DrawRectangle($outlinePen, 3, 11, 23, 16)
145
+ $graphics.FillRectangle($terminalBrush, 27, 16, 3, 6)
146
+
147
+ if ($null -eq $Percent) {
148
+ $dashPen = [System.Drawing.Pen]::new($mutedColor, 2)
149
+ try {
150
+ $graphics.DrawLine($dashPen, 8, 19, 21, 19)
151
+ } finally {
152
+ $dashPen.Dispose()
153
+ }
154
+ } else {
155
+ $clamped = [math]::Max(0, [math]::Min(100, $Percent))
156
+ $fillWidth = [math]::Floor(($clamped / 100.0) * 19)
157
+ if ($clamped -gt 0 -and $fillWidth -lt 2) { $fillWidth = 2 }
158
+ $graphics.FillRectangle($fillBrush, 5, 13, $fillWidth, 12)
159
+ }
160
+ $graphics.DrawString($percentText, $percentFont, $textBrush, [System.Drawing.RectangleF]::new(4, 11, 22, 16), $centerFormat)
161
+ } finally {
162
+ $outlinePen.Dispose()
163
+ $fillBrush.Dispose()
164
+ $terminalBrush.Dispose()
165
+ $textBrush.Dispose()
166
+ $labelFont.Dispose()
167
+ $percentFont.Dispose()
168
+ $centerFormat.Dispose()
169
+ $graphics.Dispose()
170
+ }
171
+
172
+ $handle = $bitmap.GetHicon()
173
+ $sourceIcon = [System.Drawing.Icon]::FromHandle($handle)
174
+ $icon = $sourceIcon.Clone()
175
+ $sourceIcon.Dispose()
176
+ [AiBatteryNative]::DestroyIcon($handle) | Out-Null
177
+ $bitmap.Dispose()
178
+ return $icon
179
+ }
180
+
181
+ function New-SingleBatteryTrayIcon([Nullable[int]]$Percent) {
182
+ $bitmap = [System.Drawing.Bitmap]::new(32, 32)
183
+ $graphics = [System.Drawing.Graphics]::FromImage($bitmap)
184
+ $graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias
185
+ $graphics.Clear([System.Drawing.Color]::Transparent)
186
+
187
+ $outlineColor = [System.Drawing.Color]::FromArgb(170, 170, 170)
188
+ $mutedColor = [System.Drawing.Color]::FromArgb(120, 120, 120)
189
+ $fillColor = Get-PercentColor $Percent
190
+ $outlinePen = [System.Drawing.Pen]::new($(if ($null -eq $Percent) { $mutedColor } else { $outlineColor }), 2)
191
+ $fillBrush = [System.Drawing.SolidBrush]::new($fillColor)
192
+ $terminalBrush = [System.Drawing.SolidBrush]::new($(if ($null -eq $Percent) { $mutedColor } else { $outlineColor }))
193
+
194
+ try {
195
+ $graphics.DrawRectangle($outlinePen, 4, 9, 21, 14)
196
+ $graphics.FillRectangle($terminalBrush, 26, 13, 3, 6)
197
+
198
+ if ($null -eq $Percent) {
199
+ $dashPen = [System.Drawing.Pen]::new($mutedColor, 2)
200
+ try {
201
+ $graphics.DrawLine($dashPen, 8, 16, 21, 16)
202
+ } finally {
203
+ $dashPen.Dispose()
204
+ }
205
+ } else {
206
+ $clamped = [math]::Max(0, [math]::Min(100, $Percent))
207
+ $fillWidth = [math]::Floor(($clamped / 100.0) * 17)
208
+ if ($clamped -gt 0 -and $fillWidth -lt 2) { $fillWidth = 2 }
209
+ $graphics.FillRectangle($fillBrush, 7, 12, $fillWidth, 8)
210
+ }
211
+ } finally {
212
+ $outlinePen.Dispose()
213
+ $fillBrush.Dispose()
214
+ $terminalBrush.Dispose()
215
+ $graphics.Dispose()
216
+ }
217
+
218
+ $handle = $bitmap.GetHicon()
219
+ $sourceIcon = [System.Drawing.Icon]::FromHandle($handle)
220
+ $icon = $sourceIcon.Clone()
221
+ $sourceIcon.Dispose()
222
+ [AiBatteryNative]::DestroyIcon($handle) | Out-Null
223
+ $bitmap.Dispose()
224
+ return $icon
225
+ }
226
+
227
+ function New-BatteryImage([Nullable[int]]$Percent, [bool]$Running = $true, [int]$Width = 36, [int]$Height = 16, [bool]$DrawText = $true) {
228
+ $surface = New-HudBitmapSurface $Width $Height
229
+ $bitmap = $surface.Bitmap
230
+ $graphics = $surface.Graphics
231
+ $graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias
232
+ $graphics.TextRenderingHint = [System.Drawing.Text.TextRenderingHint]::ClearTypeGridFit
233
+ $graphics.PixelOffsetMode = [System.Drawing.Drawing2D.PixelOffsetMode]::Half
234
+ $graphics.Clear([System.Drawing.Color]::Transparent)
235
+
236
+ $outlineColor = [System.Drawing.Color]::FromArgb(170, 170, 170)
237
+ $mutedColor = [System.Drawing.Color]::FromArgb(120, 120, 120)
238
+ if ($script:hudDarkText) {
239
+ $outlineColor = [System.Drawing.Color]::FromArgb(58, 58, 58)
240
+ $mutedColor = [System.Drawing.Color]::FromArgb(98, 98, 98)
241
+ }
242
+ $activeOutlineColor = if ($Running) { $outlineColor } else { $mutedColor }
243
+ # The fill always keeps its charge color (matching the terminal bar);
244
+ # running state is signalled by the outline and text colors instead.
245
+ $fillColor = Get-PercentColor $Percent
246
+ $outlinePen = [System.Drawing.Pen]::new($(if ($null -eq $Percent) { $mutedColor } else { $activeOutlineColor }), 1.0)
247
+ $fillBrush = [System.Drawing.SolidBrush]::new($fillColor)
248
+ $interiorColor = if ($script:hudDarkText) {
249
+ [System.Drawing.Color]::FromArgb(246, 246, 246)
250
+ } else {
251
+ # A solid dark interior keeps the desktop from bleeding through the empty
252
+ # part of the battery, so the percent text stays readable on any wallpaper.
253
+ [System.Drawing.Color]::FromArgb(46, 46, 46)
254
+ }
255
+ $interiorBrush = [System.Drawing.SolidBrush]::new($interiorColor)
256
+ $terminalBrush = [System.Drawing.SolidBrush]::new($(if ($null -eq $Percent) { $mutedColor } else { $activeOutlineColor }))
257
+ $textBrush = [System.Drawing.SolidBrush]::new((Get-HudBatteryTextColor $Running))
258
+ $batteryText = if ($null -eq $Percent) { "--" } else { [string][int]$Percent }
259
+ $fontSize = if ($batteryText.Length -ge 3) { 4.4 } elseif ($batteryText.Length -ge 2) { 5.1 } else { 5.8 }
260
+ $batteryFont = $null
261
+ $centerFormat = [System.Drawing.StringFormat]::new()
262
+ $centerFormat.Alignment = [System.Drawing.StringAlignment]::Center
263
+ $centerFormat.LineAlignment = [System.Drawing.StringAlignment]::Center
264
+ $centerFormat.Trimming = [System.Drawing.StringTrimming]::None
265
+ $centerFormat.FormatFlags = [System.Drawing.StringFormatFlags]::NoWrap
266
+
267
+ try {
268
+ $bodyX = 1
269
+ $bodyY = 3
270
+ $bodyW = $Width - 6
271
+ $bodyH = $Height - 6
272
+ $capW = 2
273
+ $capH = [math]::Max(4, [math]::Floor($bodyH / 2))
274
+ $capY = $bodyY + [math]::Floor(($bodyH - $capH) / 2)
275
+
276
+ $graphics.FillRectangle($interiorBrush, $bodyX + 1, $bodyY + 1, $bodyW - 1, $bodyH - 1)
277
+ $graphics.DrawRectangle($outlinePen, $bodyX, $bodyY, $bodyW, $bodyH)
278
+ $graphics.FillRectangle($terminalBrush, $bodyX + $bodyW + 1, $capY, $capW, $capH)
279
+
280
+ if ($null -ne $Percent) {
281
+ # The "--" text alone marks the unknown state; a dash line under it
282
+ # just collides with the halo.
283
+ $clamped = [math]::Max(0, [math]::Min(100, $Percent))
284
+ $innerW = $bodyW - 5
285
+ $fillW = [math]::Floor(($clamped / 100.0) * $innerW)
286
+ if ($clamped -gt 0 -and $fillW -lt 2) { $fillW = 2 }
287
+ $graphics.FillRectangle($fillBrush, $bodyX + 3, $bodyY + 3, $fillW, $bodyH - 5)
288
+ }
289
+ if ($DrawText) {
290
+ $textRect = [System.Drawing.RectangleF]::new($bodyX + 3, $bodyY + 2.5, $bodyW - 6, $bodyH - 4)
291
+ do {
292
+ if ($batteryFont) { $batteryFont.Dispose() }
293
+ $fontFamily = if ($font) { $font.FontFamily.Name } else { "Segoe UI" }
294
+ $batteryFont = [System.Drawing.Font]::new($fontFamily, $fontSize, [System.Drawing.FontStyle]::Regular)
295
+ $textSize = $graphics.MeasureString($batteryText, $batteryFont)
296
+ if ($textSize.Width -le ($textRect.Width + 1) -or $fontSize -le 4.0) { break }
297
+ $fontSize -= 0.4
298
+ } while ($true)
299
+
300
+ $graphics.DrawString($batteryText, $batteryFont, $textBrush, $textRect, $centerFormat)
301
+ }
302
+ } finally {
303
+ $outlinePen.Dispose()
304
+ $fillBrush.Dispose()
305
+ $interiorBrush.Dispose()
306
+ $terminalBrush.Dispose()
307
+ $textBrush.Dispose()
308
+ if ($batteryFont) { $batteryFont.Dispose() }
309
+ $centerFormat.Dispose()
310
+ $graphics.Dispose()
311
+ }
312
+
313
+ return $bitmap
314
+ }
315
+
316
+ function New-StatusMenuLabel($Font, [int]$Width, $Align = [System.Drawing.ContentAlignment]::MiddleLeft) {
317
+ $label = New-Object System.Windows.Forms.Label
318
+ $label.AutoSize = $false
319
+ $label.Width = $Width
320
+ $label.Height = 20
321
+ $label.Font = $Font
322
+ $label.TextAlign = $Align
323
+ $label.Margin = [System.Windows.Forms.Padding]::new(0)
324
+ $label.BackColor = [System.Drawing.SystemColors]::Menu
325
+ return $label
326
+ }
327
+
328
+ function New-StatusMenuRow($Font) {
329
+ $panel = New-Object System.Windows.Forms.FlowLayoutPanel
330
+ $panel.Width = 276
331
+ $panel.Height = 22
332
+ $panel.FlowDirection = [System.Windows.Forms.FlowDirection]::LeftToRight
333
+ $panel.WrapContents = $false
334
+ $panel.Margin = [System.Windows.Forms.Padding]::new(0)
335
+ $panel.Padding = [System.Windows.Forms.Padding]::new(4, 1, 4, 1)
336
+ $panel.BackColor = [System.Drawing.SystemColors]::Menu
337
+
338
+ $name = New-StatusMenuLabel $Font 44
339
+ $icon = New-Object System.Windows.Forms.PictureBox
340
+ $icon.Width = 40
341
+ $icon.Height = 20
342
+ $icon.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::Zoom
343
+ $icon.Margin = [System.Windows.Forms.Padding]::new(0)
344
+ $icon.BackColor = [System.Drawing.SystemColors]::Menu
345
+ $percent = New-StatusMenuLabel $Font 0 ([System.Drawing.ContentAlignment]::MiddleRight)
346
+ $reset = New-StatusMenuLabel $Font 66
347
+ $week = New-StatusMenuLabel $Font 56
348
+ $extra = New-StatusMenuLabel $Font 58
349
+
350
+ foreach ($control in @($name, $icon, $percent, $reset, $week, $extra)) {
351
+ $panel.Controls.Add($control) | Out-Null
352
+ }
353
+
354
+ $rowHost = New-Object System.Windows.Forms.ToolStripControlHost($panel)
355
+ $rowHost.AutoSize = $false
356
+ $rowHost.Width = $panel.Width
357
+ $rowHost.Height = $panel.Height
358
+
359
+ return @{
360
+ Host = $rowHost
361
+ Panel = $panel
362
+ Name = $name
363
+ Icon = $icon
364
+ Percent = $percent
365
+ Reset = $reset
366
+ Week = $week
367
+ Extra = $extra
368
+ }
369
+ }
370
+
371
+ function Set-StatusMenuRow($Row, $Parts) {
372
+ $textColor = $Parts.TextColor
373
+ $Row.Name.Text = $Parts.Prefix.TrimEnd()
374
+ $Row.Percent.Text = $Parts.PercentText
375
+ $Row.Reset.Text = $Parts.ResetText
376
+ $Row.Week.Text = $Parts.WeekText
377
+ $Row.Extra.Text = $Parts.ExtraText
378
+
379
+ foreach ($label in @($Row.Name, $Row.Percent, $Row.Reset, $Row.Week, $Row.Extra)) {
380
+ $label.ForeColor = $textColor
381
+ }
382
+
383
+ $oldImage = $Row.Icon.Image
384
+ $Row.Icon.Image = New-BatteryImage $Parts.Percent $Parts.Running
385
+ if ($oldImage) { $oldImage.Dispose() }
386
+ }
387
+
388
+ function Dispose-StatusMenuRow($Row) {
389
+ if ($Row.Icon.Image) {
390
+ $Row.Icon.Image.Dispose()
391
+ $Row.Icon.Image = $null
392
+ }
393
+ }