ai-battery 0.2.0 → 0.2.1

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.
Files changed (45) hide show
  1. package/README.md +30 -14
  2. package/bin/ai-battery-macos-status.applescript +5 -12
  3. package/bin/ai-battery-run +701 -31
  4. package/bin/ai-battery-tmux.js +17 -0
  5. package/bin/terminal-compositor.js +111 -0
  6. package/docs/REFERENCE.md +23 -3
  7. package/docs/RELEASING.md +24 -7
  8. package/docs/WINDOWS-TERMINAL-SHARED-BAR.md +74 -0
  9. package/docs/hud-preview.svg +43 -20
  10. package/docs/terminal-vertical-preview.svg +73 -0
  11. package/lib/cli.js +945 -219
  12. package/lib/core/atomic-rename.js +18 -0
  13. package/lib/core/cli-help.js +35 -7
  14. package/lib/core/cli-messages.js +45 -1
  15. package/lib/core/cli-output.js +277 -72
  16. package/lib/core/cli-wrappers.js +410 -0
  17. package/lib/core/config-editor.js +177 -29
  18. package/lib/core/display-config.js +45 -0
  19. package/lib/core/integration-files.js +32 -3
  20. package/lib/core/shared-bar.js +603 -0
  21. package/lib/core/statusline-git.js +179 -15
  22. package/lib/core/terminal.js +25 -7
  23. package/lib/core/tmux-status.js +172 -0
  24. package/lib/core/uninstall.js +20 -7
  25. package/lib/core/usage-store.js +85 -0
  26. package/lib/hud/launcher.js +29 -31
  27. package/lib/platforms/linux/hud.js +1 -1
  28. package/lib/platforms/linux/panel.js +1 -1
  29. package/lib/platforms/macos/hud.js +5 -5
  30. package/lib/platforms/process-scanner.js +77 -29
  31. package/lib/platforms/windows/autostart-cleanup.js +1 -1
  32. package/lib/platforms/windows/codex-runner.js +178 -21
  33. package/lib/platforms/windows/hud/controls.ps1 +44 -10
  34. package/lib/platforms/windows/hud/main.ps1 +350 -43
  35. package/lib/platforms/windows/hud/rendering.ps1 +1227 -1
  36. package/lib/platforms/windows/hud/snapshot.ps1 +26 -46
  37. package/lib/platforms/windows/hud-launcher.js +153 -79
  38. package/lib/platforms/windows/shared-bar-platform.js +972 -0
  39. package/lib/platforms/windows/terminal-compositor.js +210 -25
  40. package/lib/platforms/windows/wt-bar.js +729 -0
  41. package/lib/platforms/windows/wt-fragment.js +665 -0
  42. package/lib/platforms/windows/wt-keybinding.js +300 -0
  43. package/lib/providers/antigravity.js +155 -8
  44. package/package.json +5 -2
  45. package/vendor/PaneContext.cs +875 -0
@@ -1,8 +1,8 @@
1
1
  param(
2
- [int]$Interval = 5,
2
+ [int]$Interval = 2,
3
3
  [string]$Position = $(if ($env:AI_BATTERY_HUD_POSITION) { $env:AI_BATTERY_HUD_POSITION } elseif ($env:CLAUDEX_BATTERY_HUD_POSITION) { $env:CLAUDEX_BATTERY_HUD_POSITION } else { "saved" }),
4
- [ValidateSet("tray", "statusline", "floating")]
5
- [string]$Mode = $(if ($env:AI_BATTERY_HUD_MODE) { $env:AI_BATTERY_HUD_MODE } elseif ($env:CLAUDEX_BATTERY_HUD_MODE) { $env:CLAUDEX_BATTERY_HUD_MODE } else { "floating" }),
4
+ [ValidateSet("tray", "statusline", "floating", "icon", "rail", "horizontal")]
5
+ [string]$Mode = $(if ($env:AI_BATTERY_HUD_MODE) { $env:AI_BATTERY_HUD_MODE } elseif ($env:CLAUDEX_BATTERY_HUD_MODE) { $env:CLAUDEX_BATTERY_HUD_MODE } else { "icon" }),
6
6
  [string]$BatteryCommand = $(if ($env:AI_BATTERY_COMMAND) { $env:AI_BATTERY_COMMAND } elseif ($env:CLAUDEX_BATTERY_COMMAND) { $env:CLAUDEX_BATTERY_COMMAND } else { "ai-battery --json" }),
7
7
  [string]$BatteryCommandBase64 = "",
8
8
  [string]$InitialJsonBase64 = "",
@@ -130,25 +130,44 @@ function Remove-HudDockHostMarker {
130
130
  } catch { }
131
131
  }
132
132
 
133
+ function Test-HudMutexRunning([string]$MutexName = "Local\AiBatteryHud") {
134
+ try {
135
+ $mutex = [System.Threading.Mutex]::OpenExisting($MutexName)
136
+ $mutex.Dispose()
137
+ return $true
138
+ } catch {
139
+ return $false
140
+ }
141
+ }
142
+
133
143
  function Request-HudStop {
144
+ if ($DockWindowHandle -eq 0 -and -not (Test-HudMutexRunning)) {
145
+ return $false
146
+ }
134
147
  $requestPath = Get-HudDockRequestPath
135
148
  try {
136
149
  $request = @{ stop = $true; at = [datetime]::UtcNow.ToString("o") } | ConvertTo-Json -Compress
137
150
  [System.IO.File]::WriteAllText($requestPath, $request, [System.Text.UTF8Encoding]::new($false))
138
151
  } catch {
139
- return
152
+ return $false
140
153
  }
141
154
 
142
155
  for ($wait = 0; $wait -lt 40; $wait += 1) {
143
156
  Start-Sleep -Milliseconds 150
144
- if (-not (Test-Path -LiteralPath $requestPath)) { return }
157
+ if (-not (Test-Path -LiteralPath $requestPath)) { return $true }
158
+ if ($DockWindowHandle -eq 0 -and -not (Test-HudMutexRunning)) {
159
+ Remove-Item -LiteralPath $requestPath -Force -ErrorAction SilentlyContinue
160
+ return $true
161
+ }
145
162
  }
146
163
  Remove-Item -LiteralPath $requestPath -Force -ErrorAction SilentlyContinue
164
+ return $true
147
165
  }
148
166
 
149
167
  if ($StopExisting) {
150
- Request-HudStop
151
- Start-Sleep -Milliseconds 400
168
+ if (Request-HudStop) {
169
+ Start-Sleep -Milliseconds 400
170
+ }
152
171
  Stop-ExistingHudProcesses
153
172
  if (-not $Once) {
154
173
  exit 0
@@ -255,7 +274,20 @@ function Get-HudSnapshotPath {
255
274
  Join-Path $env:TEMP "ai-battery"
256
275
  }
257
276
  New-Item -ItemType Directory -Force -Path $root | Out-Null
258
- return Join-Path $root "hud-snapshot-$(Get-HudSourceId).json"
277
+ return Join-Path $root "hud-snapshot.json"
278
+ }
279
+
280
+ function Get-HudSnapshotCandidatePaths {
281
+ $root = if ($env:LOCALAPPDATA) {
282
+ Join-Path $env:LOCALAPPDATA "ai-battery"
283
+ } else {
284
+ Join-Path $env:TEMP "ai-battery"
285
+ }
286
+ return @(
287
+ (Join-Path $root "hud-snapshot.json"),
288
+ (Join-Path $root "hud-snapshot-windows.json"),
289
+ (Join-Path $root "hud-snapshot-wsl.json")
290
+ )
259
291
  }
260
292
 
261
293
  function Get-LegacyHudStatePath {
@@ -272,17 +304,29 @@ $script:hudAnchorY = "top"
272
304
  $script:codexRowVisible = $true
273
305
  $script:claudeRowVisible = $true
274
306
 
307
+ function Write-HudTextAtomic([string]$Path, [string]$Text) {
308
+ $dir = Split-Path -Parent $Path
309
+ if ($dir) { New-Item -ItemType Directory -Force -Path $dir | Out-Null }
310
+ $tempPath = "$Path.$PID.$([guid]::NewGuid().ToString('N').Substring(0, 8).ToLowerInvariant()).tmp"
311
+ try {
312
+ [System.IO.File]::WriteAllText($tempPath, $Text, [System.Text.UTF8Encoding]::new($false))
313
+ Move-Item -LiteralPath $tempPath -Destination $Path -Force
314
+ } catch {
315
+ Remove-Item -LiteralPath $tempPath -Force -ErrorAction SilentlyContinue
316
+ throw
317
+ }
318
+ }
319
+
275
320
  function Signal-HudReady {
276
321
  if ([string]::IsNullOrWhiteSpace($ReadyPath)) { return }
277
322
  try {
278
- $dir = Split-Path -Parent $ReadyPath
279
- if ($dir) { New-Item -ItemType Directory -Force -Path $dir | Out-Null }
280
- @{
323
+ $payload = @{
281
324
  Ready = $true
282
325
  Pid = $PID
283
326
  At = [datetime]::UtcNow.ToString("o")
284
327
  Mode = $Mode
285
- } | ConvertTo-Json -Compress | Set-Content -Encoding UTF8 -Path $ReadyPath
328
+ } | ConvertTo-Json -Compress
329
+ Write-HudTextAtomic $ReadyPath $payload
286
330
  } catch {
287
331
  # Readiness is diagnostic only.
288
332
  }
@@ -317,12 +361,13 @@ function Write-HudPosition($Point) {
317
361
  $width = [int]$form.Width
318
362
  $height = [int]$form.Height
319
363
  }
320
- @{
364
+ $payload = @{
321
365
  X = [int]$Point.X
322
366
  Y = [int]$Point.Y
323
367
  Width = $width
324
368
  Height = $height
325
- } | ConvertTo-Json -Compress | Set-Content -Encoding UTF8 -Path (Get-HudStatePath)
369
+ } | ConvertTo-Json -Compress
370
+ Write-HudTextAtomic (Get-HudStatePath) $payload
326
371
  } catch {
327
372
  # Position persistence is helpful but not required for the HUD to run.
328
373
  }
@@ -338,17 +383,25 @@ function Get-SnapshotAgeSeconds($Snapshot) {
338
383
  }
339
384
 
340
385
  function Read-HudSnapshot {
341
- try {
342
- $path = Get-HudSnapshotPath
343
- if (-not (Test-Path $path)) { return $null }
344
- $snapshot = Get-Content $path -Raw | ConvertFrom-Json
345
- $age = Get-SnapshotAgeSeconds $snapshot
346
- $maxAge = if ($env:AI_BATTERY_HUD_CACHE_SECONDS) { [int]$env:AI_BATTERY_HUD_CACHE_SECONDS } else { 900 }
347
- if ($null -ne $age -and $age -le $maxAge) { return $snapshot }
348
- } catch {
349
- # Cached data is optional.
386
+ $maxAge = if ($env:AI_BATTERY_HUD_CACHE_SECONDS) { [int]$env:AI_BATTERY_HUD_CACHE_SECONDS } else { 900 }
387
+ $best = $null
388
+ $bestTime = [datetime]::MinValue
389
+ foreach ($path in (Get-HudSnapshotCandidatePaths)) {
390
+ try {
391
+ if (-not (Test-Path -LiteralPath $path)) { continue }
392
+ $snapshot = Get-Content -LiteralPath $path -Raw | ConvertFrom-Json
393
+ $age = Get-SnapshotAgeSeconds $snapshot
394
+ if ($null -eq $age -or $age -gt $maxAge) { continue }
395
+ $generated = ([datetime]$snapshot.generatedAt).ToUniversalTime()
396
+ if ($generated -ge $bestTime) {
397
+ $best = $snapshot
398
+ $bestTime = $generated
399
+ }
400
+ } catch {
401
+ # Cached data is optional.
402
+ }
350
403
  }
351
- return $null
404
+ return $best
352
405
  }
353
406
 
354
407
  function Read-InitialHudSnapshot {
@@ -366,7 +419,8 @@ function Write-HudSnapshot($Snapshot) {
366
419
  try {
367
420
  if (-not $Snapshot) { return }
368
421
  $Snapshot | Add-Member -NotePropertyName "hudSource" -NotePropertyValue (Get-HudSourceId) -Force
369
- $Snapshot | ConvertTo-Json -Depth 20 -Compress | Set-Content -Encoding UTF8 -Path (Get-HudSnapshotPath)
422
+ $payload = $Snapshot | ConvertTo-Json -Depth 20 -Compress
423
+ Write-HudTextAtomic (Get-HudSnapshotPath) $payload
370
424
  } catch {
371
425
  # The HUD can still run without a warm-start cache.
372
426
  }
@@ -386,6 +440,61 @@ if ($Once) {
386
440
 
387
441
  Add-Type -AssemblyName System.Windows.Forms
388
442
  Add-Type -AssemblyName System.Drawing
443
+ if (-not ("AiBatteryHudForm" -as [type])) {
444
+ # Borderless WinForms windows still inherit Win32 SM_CXMINTRACK (~136px at 100% DPI),
445
+ # which stretches slim vertical rail HUDs far wider than their content. Override
446
+ # WM_GETMINMAXINFO so both the HUD and its transparent hit-test form can shrink to fit.
447
+ $hudFormCode = @"
448
+ using System;
449
+ using System.Runtime.InteropServices;
450
+ using System.Windows.Forms;
451
+ public class AiBatteryHudForm : Form {
452
+ private const int WM_GETMINMAXINFO = 0x0024;
453
+ [StructLayout(LayoutKind.Sequential)]
454
+ private struct POINT {
455
+ public int X;
456
+ public int Y;
457
+ }
458
+ [StructLayout(LayoutKind.Sequential)]
459
+ private struct MINMAXINFO {
460
+ public POINT ptReserved;
461
+ public POINT ptMaxSize;
462
+ public POINT ptMaxPosition;
463
+ public POINT ptMinTrackSize;
464
+ public POINT ptMaxTrackSize;
465
+ }
466
+ protected override void WndProc(ref Message m) {
467
+ base.WndProc(ref m);
468
+ if (m.Msg == WM_GETMINMAXINFO && m.LParam != IntPtr.Zero) {
469
+ MINMAXINFO mmi = (MINMAXINFO)Marshal.PtrToStructure(m.LParam, typeof(MINMAXINFO));
470
+ mmi.ptMinTrackSize.X = 1;
471
+ mmi.ptMinTrackSize.Y = 1;
472
+ Marshal.StructureToPtr(mmi, m.LParam, false);
473
+ }
474
+ }
475
+ }
476
+ "@
477
+ try {
478
+ $hudFormRefs = @(
479
+ [System.Windows.Forms.Form].Assembly.Location,
480
+ [System.Windows.Forms.Message].Assembly.Location,
481
+ [System.Drawing.Point].Assembly.Location,
482
+ [System.ComponentModel.Component].Assembly.Location
483
+ ) | Where-Object {
484
+ -not [string]::IsNullOrWhiteSpace($_) -and
485
+ @("System.dll", "mscorlib.dll") -notcontains ([System.IO.Path]::GetFileName($_))
486
+ } | Select-Object -Unique
487
+ Add-Type -TypeDefinition $hudFormCode -ReferencedAssemblies $hudFormRefs
488
+ } catch {
489
+ # Fall back to standard Form if dynamic type compilation is unavailable.
490
+ }
491
+ }
492
+ function New-HudForm {
493
+ if ("AiBatteryHudForm" -as [type]) {
494
+ return New-Object AiBatteryHudForm
495
+ }
496
+ return New-Object System.Windows.Forms.Form
497
+ }
389
498
  try {
390
499
  [System.Windows.Forms.Application]::SetHighDpiMode([System.Windows.Forms.HighDpiMode]::PerMonitorV2) | Out-Null
391
500
  } catch {
@@ -458,6 +567,9 @@ if ($Mode -eq "tray") {
458
567
  Set-SingleNotifyIcon $texts
459
568
  $script:lastBalloonText = $lines -join "`n"
460
569
  } catch {
570
+ $codexRow.Host.Visible = $true
571
+ $claudeRow.Host.Visible = $true
572
+ $antigravityRow.Host.Visible = $false
461
573
  Set-StatusMenuRow $codexRow @{
462
574
  Prefix = "Codex "
463
575
  Percent = $null
@@ -465,6 +577,7 @@ if ($Mode -eq "tray") {
465
577
  ResetText = ""
466
578
  WeekText = ""
467
579
  ExtraText = "unavailable"
580
+ Running = $false
468
581
  TextColor = [System.Drawing.Color]::FromArgb(255, 92, 92)
469
582
  }
470
583
  Set-StatusMenuRow $claudeRow @{
@@ -474,6 +587,7 @@ if ($Mode -eq "tray") {
474
587
  ResetText = ""
475
588
  WeekText = ""
476
589
  ExtraText = ""
590
+ Running = $false
477
591
  TextColor = [System.Drawing.Color]::FromArgb(145, 145, 145)
478
592
  }
479
593
  $newIcon = New-SingleBatteryTrayIcon $null
@@ -519,6 +633,10 @@ if ($Mode -eq "tray") {
519
633
  }
520
634
 
521
635
  $script:dockTuiStatusline = ($DockWindowHandle -ne 0)
636
+ $script:hudCustomLayout = if ($script:dockTuiStatusline) { "dock" } elseif ($Mode -eq "rail") { "rail" } elseif (@("icon", "horizontal") -contains $Mode) { "icon" } else { "stack" }
637
+ $script:hudHorizontalPanel = ($script:hudCustomLayout -eq "icon") -or ($script:hudCustomLayout -eq "rail")
638
+ $script:latestHudTexts = $null
639
+ $script:hudHorizontalMessage = "AI Battery starting..."
522
640
  $script:dockPlacementMode = $DockPlacement
523
641
  $script:dockStripHeight = if ($script:dockTuiStatusline) { Scale-HudValue 20 } else { 0 }
524
642
  $script:dockJoinOverlap = if ($script:dockTuiStatusline -and $script:dockPlacementMode -eq "bottom") { [math]::Max(1, (Scale-HudValue 8)) } else { 0 }
@@ -528,15 +646,15 @@ $script:dockSquareCorners = $false
528
646
  $script:dockTuiResponsiveLayoutName = ""
529
647
  $script:dockTuiResponsiveThresholds = ""
530
648
  $script:dockOuterBorderColor = [System.Drawing.Color]::FromArgb(72, 72, 72)
531
- $form = New-Object System.Windows.Forms.Form
649
+ $form = New-HudForm
532
650
  Enable-HudDoubleBuffering $form
533
651
  $form.Text = "AI Battery"
534
652
  $form.AutoScaleMode = [System.Windows.Forms.AutoScaleMode]::None
535
653
  $form.StartPosition = [System.Windows.Forms.FormStartPosition]::Manual
536
- $form.Width = Scale-HudValue $Width
537
- $form.Height = if ($script:dockTuiStatusline) { $script:dockStripHeight + $script:dockJoinOverlap } elseif ($Mode -eq "floating") { Scale-HudValue 44 } elseif ($Mode -eq "statusline") { Scale-HudValue 44 } else { Scale-HudValue 54 }
538
- $script:hudTwoRowHeight = $form.Height
539
- $script:hudOneRowHeight = [math]::Max((Scale-HudValue 20), $form.Height - (Scale-HudValue 18))
654
+ $form.Width = if ($script:hudCustomLayout -eq "rail") { Scale-HudValue 38 } else { Scale-HudValue $Width }
655
+ $form.Height = if ($script:dockTuiStatusline) { $script:dockStripHeight + $script:dockJoinOverlap } elseif ($script:hudCustomLayout -eq "rail") { Scale-HudValue 140 } elseif ($script:hudHorizontalPanel) { Scale-HudValue 34 } elseif ($Mode -eq "floating") { Scale-HudValue 44 } elseif ($Mode -eq "statusline") { Scale-HudValue 44 } else { Scale-HudValue 54 }
656
+ $script:hudTwoRowHeight = Scale-HudValue 44
657
+ $script:hudOneRowHeight = [math]::Max((Scale-HudValue 20), $script:hudTwoRowHeight - (Scale-HudValue 18))
540
658
  $form.TopMost = ($DockWindowHandle -eq 0)
541
659
  $form.ShowInTaskbar = $false
542
660
  $form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::None
@@ -545,11 +663,13 @@ $script:hudTransparentSurface = if ($script:dockTuiStatusline) { $false } else {
545
663
  $script:hudContrastBackdrop = Test-HudContrastBackdrop
546
664
  $script:hudChromeBackColor = if ($script:dockTuiStatusline) {
547
665
  [System.Drawing.Color]::FromArgb(12, 12, 12)
666
+ } elseif ($script:hudDarkText) {
667
+ [System.Drawing.Color]::FromArgb(244, 244, 248)
548
668
  } else {
549
669
  [System.Drawing.Color]::FromArgb(24, 24, 24)
550
670
  }
551
671
  if ($script:hudTransparentSurface) {
552
- $transparentBackColor = if ($script:hudDarkText -and (-not $script:hudContrastBackdrop)) {
672
+ $transparentBackColor = if ($script:hudDarkText) {
553
673
  [System.Drawing.Color]::FromArgb(254, 254, 254)
554
674
  } else {
555
675
  [System.Drawing.Color]::FromArgb(18, 18, 18)
@@ -559,10 +679,10 @@ if ($script:hudTransparentSurface) {
559
679
  } else {
560
680
  $form.BackColor = $script:hudChromeBackColor
561
681
  }
562
- $script:hudSurfaceBackColor = if ($script:hudContrastBackdrop -or (-not $script:hudTransparentSurface)) {
563
- $script:hudChromeBackColor
564
- } else {
682
+ $script:hudSurfaceBackColor = if ($script:hudTransparentSurface) {
565
683
  $form.BackColor
684
+ } else {
685
+ $script:hudChromeBackColor
566
686
  }
567
687
  $form.Opacity = if ($script:dockTuiStatusline) {
568
688
  1.0
@@ -607,7 +727,7 @@ $panel.ColumnCount = 1
607
727
  $panel.GrowStyle = [System.Windows.Forms.TableLayoutPanelGrowStyle]::FixedSize
608
728
  $panel.Padding = if ($script:dockTuiStatusline) {
609
729
  New-HudPadding 8 2 2 2
610
- } elseif ($Mode -eq "floating" -or $Mode -eq "statusline") {
730
+ } elseif (@("floating", "statusline", "icon", "rail", "horizontal") -contains $Mode) {
611
731
  New-HudPadding 6 4 2 3
612
732
  } else {
613
733
  New-HudPadding 10 7 10 6
@@ -750,9 +870,13 @@ function Get-TuiProviderDisplayValues($Result, [string]$Name, [int]$BarWidth, [b
750
870
  Secondary = ""
751
871
  }
752
872
  if ($ShowWindows -and $Result -and $Result.primary) {
753
- $values.Primary = "$(Get-WindowText $Result.primary.windowMinutes) $(Get-ResetClock $Result.primary)"
873
+ $primaryLabel = Get-WindowText $Result.primary.windowMinutes
874
+ if ($Result.provider -eq "antigravity" -and -not $Result.primary.windowMinutes -and $Result.primary.id) {
875
+ $primaryLabel = [string]$Result.primary.id
876
+ }
877
+ $values.Primary = "$primaryLabel $(Get-ResetClock $Result.primary)"
754
878
  }
755
- if ($ShowWindows -and -not $PrimaryOnly -and $Result -and $Result.secondary) {
879
+ if ($ShowWindows -and -not $PrimaryOnly -and $Result -and $Result.secondary -and $null -ne $Result.secondary.remainingPercent) {
756
880
  $values.Secondary = "$(Get-WindowText $Result.secondary.windowMinutes) $([int]$Result.secondary.remainingPercent)%"
757
881
  if ($Result.secondary.remainingPercent -le 10 -and -not $Result.secondary.resetPassed) {
758
882
  $values.Secondary += " $(Get-ResetClock $Result.secondary)"
@@ -878,7 +1002,11 @@ function Get-DockedTuiRenderKey($Snapshot) {
878
1002
  $name = $result.provider
879
1003
  if (-not $result) { $parts += "$name`:missing"; continue }
880
1004
  $primary = if ($result.primary) {
881
- "$(Get-WindowText $result.primary.windowMinutes):$(Get-ResetClock $result.primary)"
1005
+ $primaryLabel = Get-WindowText $result.primary.windowMinutes
1006
+ if ($result.provider -eq "antigravity" -and -not $result.primary.windowMinutes -and $result.primary.id) {
1007
+ $primaryLabel = [string]$result.primary.id
1008
+ }
1009
+ "${primaryLabel}:$(Get-ResetClock $result.primary)"
882
1010
  } else { "" }
883
1011
  $secondary = if ($result.secondary) {
884
1012
  "$($result.secondary.windowMinutes):$($result.secondary.remainingPercent):$($result.secondary.resetPassed):$(Get-ResetClock $result.secondary)"
@@ -984,6 +1112,29 @@ $menu.AutoClose = $true
984
1112
  $script:hudMenuOpenedAt = [datetime]::UtcNow
985
1113
  $script:hudMenuLastInsideAt = [datetime]::UtcNow
986
1114
  $script:hudMenuReadyForOutsideClick = $false
1115
+ $toggleLayoutItem = $null
1116
+ if (-not $script:dockTuiStatusline) {
1117
+ $setHudLayout = {
1118
+ param([string]$NextLayout)
1119
+ $script:hudCustomLayout = $NextLayout
1120
+ $script:hudHorizontalPanel = ($NextLayout -eq "icon" -or $NextLayout -eq "rail")
1121
+ if ($script:horizontalPaintPanel) {
1122
+ $script:horizontalPaintPanel.Visible = $script:hudHorizontalPanel
1123
+ $panel.Visible = -not $script:hudHorizontalPanel
1124
+ if ($script:hudHorizontalPanel) { $script:horizontalPaintPanel.BringToFront(); $script:horizontalPaintPanel.Invalidate() }
1125
+ else { Hide-HorizontalDetailPopup; $panel.BringToFront() }
1126
+ }
1127
+ $script:hudVisualKey = $null
1128
+ Update-HudFromSnapshot $script:latestSnapshot
1129
+ }
1130
+ $iconModeItem = $menu.Items.Add("Icon Panel")
1131
+ $iconModeItem.add_Click({ & $setHudLayout "icon" })
1132
+ $railModeItem = $menu.Items.Add("Rail Panel")
1133
+ $railModeItem.add_Click({ & $setHudLayout "rail" })
1134
+ $stackModeItem = $menu.Items.Add("Stack Panel")
1135
+ $stackModeItem.add_Click({ & $setHudLayout "stack" })
1136
+ $menu.Items.Add("-") | Out-Null
1137
+ }
987
1138
  $exitItem = $menu.Items.Add("Exit")
988
1139
  $exitItem.add_Click({ $form.Close() })
989
1140
  $menu.add_Opened({
@@ -1017,6 +1168,7 @@ $claudeRow.ContextMenuStrip = $menu
1017
1168
  $providerDivider.ContextMenuStrip = $menu
1018
1169
  $script:dockCombinedRow = $null
1019
1170
  $script:dockTuiPaintPanel = $null
1171
+ $script:horizontalPaintPanel = $null
1020
1172
  if ($script:dockTuiStatusline) {
1021
1173
  $script:dockTuiPaintPanel = New-Object System.Windows.Forms.Panel
1022
1174
  $script:dockTuiPaintPanel.Dock = [System.Windows.Forms.DockStyle]::Fill
@@ -1039,16 +1191,34 @@ if ($script:dockTuiStatusline) {
1039
1191
  $panel.Controls.Add($codexRow, 0, 0) | Out-Null
1040
1192
  $panel.Controls.Add($claudeRow, 0, 1) | Out-Null
1041
1193
  $panel.Controls.Add($antigravityRow, 0, 2) | Out-Null
1194
+ $script:horizontalPaintPanel = New-Object System.Windows.Forms.Panel
1195
+ Enable-HudDoubleBuffering $script:horizontalPaintPanel
1196
+ $script:horizontalPaintPanel.Dock = [System.Windows.Forms.DockStyle]::Fill
1197
+ $script:horizontalPaintPanel.Margin = [System.Windows.Forms.Padding]::new(0)
1198
+ $script:horizontalPaintPanel.BackColor = $script:hudSurfaceBackColor
1199
+ $script:horizontalPaintPanel.ContextMenuStrip = $menu
1200
+ $script:horizontalPaintPanel.Visible = $script:hudHorizontalPanel
1201
+ $panel.Visible = -not $script:hudHorizontalPanel
1202
+ $script:horizontalPaintPanel.add_Paint({
1203
+ param($sender, $event)
1204
+ if ($script:hudCustomLayout -eq "rail") {
1205
+ Draw-VerticalRailHud $event.Graphics $sender.Width $sender.Height $script:latestHudTexts
1206
+ } else {
1207
+ Draw-HorizontalBrandHud $event.Graphics $sender.Width $sender.Height $script:latestHudTexts $font
1208
+ }
1209
+ })
1042
1210
  }
1043
1211
  if ($script:dockTuiStatusline) {
1044
1212
  $form.Controls.Add($script:dockTuiPaintPanel)
1045
1213
  } else {
1046
1214
  $form.Controls.Add($panel)
1215
+ $form.Controls.Add($script:horizontalPaintPanel)
1216
+ if ($script:hudHorizontalPanel) { $script:horizontalPaintPanel.BringToFront() }
1047
1217
  }
1048
1218
 
1049
1219
  $hitForm = $null
1050
1220
  if ((-not $ClickThrough) -and $script:hudTransparentSurface) {
1051
- $hitForm = New-Object System.Windows.Forms.Form
1221
+ $hitForm = New-HudForm
1052
1222
  $hitForm.Text = "AI Battery hit area"
1053
1223
  $hitForm.AutoScaleMode = [System.Windows.Forms.AutoScaleMode]::None
1054
1224
  $hitForm.Width = $form.Width
@@ -1115,13 +1285,127 @@ function Show-HitForm {
1115
1285
  [AiBatteryNative]::SetWindowLong($hitForm.Handle, [AiBatteryNative]::GWL_EXSTYLE, $style) | Out-Null
1116
1286
  }
1117
1287
 
1288
+ $script:detailPopupForm = $null
1289
+ $script:detailPopupOpenedAt = [datetime]::MinValue
1290
+ $script:detailPopupReadyForOutsideClick = $false
1291
+
1292
+ function Hide-HorizontalDetailPopup {
1293
+ $script:detailPopupReadyForOutsideClick = $false
1294
+ if ($script:detailPopupForm -and -not $script:detailPopupForm.IsDisposed -and $script:detailPopupForm.Visible) {
1295
+ $script:detailPopupForm.Hide()
1296
+ }
1297
+ }
1298
+
1299
+ function Update-DetailPopupAutoHide {
1300
+ if (-not $script:detailPopupForm -or $script:detailPopupForm.IsDisposed -or -not $script:detailPopupForm.Visible) { return }
1301
+ $now = [datetime]::UtcNow
1302
+ $buttons = [System.Windows.Forms.Control]::MouseButtons
1303
+ if (-not $script:detailPopupReadyForOutsideClick) {
1304
+ if ($buttons -eq [System.Windows.Forms.MouseButtons]::None -and ($now - $script:detailPopupOpenedAt).TotalMilliseconds -ge 150) {
1305
+ $script:detailPopupReadyForOutsideClick = $true
1306
+ }
1307
+ return
1308
+ }
1309
+ if (([AiBatteryNative]::GetAsyncKeyState(0x1B) -band 0x8000) -ne 0) {
1310
+ Hide-HorizontalDetailPopup
1311
+ return
1312
+ }
1313
+ if ($buttons -ne [System.Windows.Forms.MouseButtons]::None) {
1314
+ $point = [System.Windows.Forms.Cursor]::Position
1315
+ $insidePopup = Test-PointInBounds $script:detailPopupForm.Bounds $point
1316
+ $insideHud = Test-PointInBounds $form.Bounds $point
1317
+ if ($hitForm -and -not $hitForm.IsDisposed) {
1318
+ $insideHud = $insideHud -or (Test-PointInBounds $hitForm.Bounds $point)
1319
+ }
1320
+ if (-not $insidePopup -and -not $insideHud) {
1321
+ Hide-HorizontalDetailPopup
1322
+ }
1323
+ }
1324
+ }
1325
+
1326
+ function Update-HorizontalDetailPopupBounds {
1327
+ if (-not $script:detailPopupForm -or $script:detailPopupForm.IsDisposed) { return }
1328
+ $size = Get-HorizontalDetailCardSize $script:latestHudTexts
1329
+ $script:detailPopupForm.Size = $size
1330
+ $screen = [System.Windows.Forms.Screen]::FromControl($form)
1331
+ $work = $screen.WorkingArea
1332
+ $gap = Scale-HudValue 6
1333
+ $x = $form.Left + [int][math]::Floor(($form.Width - $size.Width) / 2)
1334
+ $x = [math]::Max($work.Left + $gap, [math]::Min($work.Right - $size.Width - $gap, $x))
1335
+ $y = $form.Bottom + $gap
1336
+ if (($y + $size.Height) -gt ($work.Bottom - $gap)) {
1337
+ $y = $form.Top - $size.Height - $gap
1338
+ }
1339
+ $y = [math]::Max($work.Top + $gap, [math]::Min($work.Bottom - $size.Height - $gap, $y))
1340
+ $script:detailPopupForm.Location = [System.Drawing.Point]::new([int]$x, [int]$y)
1341
+ $regionPath = New-RoundedRectPath ([single]0) ([single]0) ([single]$size.Width) ([single]$size.Height) ([single](Scale-HudValue 10))
1342
+ try {
1343
+ $oldRegion = $script:detailPopupForm.Region
1344
+ $script:detailPopupForm.Region = [System.Drawing.Region]::new($regionPath)
1345
+ if ($oldRegion) { $oldRegion.Dispose() }
1346
+ } finally {
1347
+ $regionPath.Dispose()
1348
+ }
1349
+ $script:detailPopupForm.Invalidate()
1350
+ }
1351
+
1352
+ function Toggle-HorizontalDetailPopup {
1353
+ if (-not $script:hudHorizontalPanel) { return }
1354
+ if ($script:detailPopupForm -and -not $script:detailPopupForm.IsDisposed -and $script:detailPopupForm.Visible) {
1355
+ Hide-HorizontalDetailPopup
1356
+ return
1357
+ }
1358
+ if (-not $script:detailPopupForm -or $script:detailPopupForm.IsDisposed) {
1359
+ $popup = New-Object System.Windows.Forms.Form
1360
+ Enable-HudDoubleBuffering $popup
1361
+ $popup.Text = "AI Battery Details"
1362
+ $popup.AutoScaleMode = [System.Windows.Forms.AutoScaleMode]::None
1363
+ $popup.StartPosition = [System.Windows.Forms.FormStartPosition]::Manual
1364
+ $popup.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::None
1365
+ $popup.ShowInTaskbar = $false
1366
+ $popup.TopMost = $true
1367
+ $popup.BackColor = [System.Drawing.Color]::FromArgb(38, 38, 43)
1368
+ $popup.add_Paint({
1369
+ param($sender, $event)
1370
+ Draw-HorizontalDetailCard $event.Graphics $sender.Width $sender.Height $script:latestHudTexts
1371
+ })
1372
+ $popup.add_MouseClick({
1373
+ param($sender, $event)
1374
+ if ($event.Button -ne [System.Windows.Forms.MouseButtons]::Left) { return }
1375
+ $rects = Get-HorizontalDetailFooterRects $sender.Width $sender.Height
1376
+ $pt = [System.Drawing.Point]::new($event.X, $event.Y)
1377
+ if ($rects.Refresh.Contains($pt)) {
1378
+ try { Start-SnapshotFetch } catch { }
1379
+ $sender.Invalidate()
1380
+ } elseif ($rects.Quit.Contains($pt)) {
1381
+ Hide-HorizontalDetailPopup
1382
+ $form.Close()
1383
+ }
1384
+ })
1385
+ $script:detailPopupForm = $popup
1386
+ }
1387
+ Update-HorizontalDetailPopupBounds
1388
+ $script:detailPopupOpenedAt = [datetime]::UtcNow
1389
+ $script:detailPopupReadyForOutsideClick = $false
1390
+ $script:detailPopupForm.Show()
1391
+ $popupStyle = [AiBatteryNative]::GetWindowLong($script:detailPopupForm.Handle, [AiBatteryNative]::GWL_EXSTYLE)
1392
+ $popupStyle = $popupStyle -bor [AiBatteryNative]::WS_EX_TOOLWINDOW -bor [AiBatteryNative]::WS_EX_NOACTIVATE
1393
+ [AiBatteryNative]::SetWindowLong($script:detailPopupForm.Handle, [AiBatteryNative]::GWL_EXSTYLE, $popupStyle) | Out-Null
1394
+ $swpFlags = [AiBatteryNative]::SWP_NOMOVE -bor [AiBatteryNative]::SWP_NOSIZE -bor [AiBatteryNative]::SWP_NOACTIVATE -bor [AiBatteryNative]::SWP_SHOWWINDOW
1395
+ [AiBatteryNative]::SetWindowPos($script:detailPopupForm.Handle, [AiBatteryNative]::HWND_TOPMOST, 0, 0, 0, 0, $swpFlags) | Out-Null
1396
+ }
1397
+
1118
1398
  $dragging = $false
1119
1399
  $dragOffset = [System.Drawing.Point]::new(0, 0)
1400
+ $script:dragStartScreen = [System.Drawing.Point]::new(0, 0)
1401
+ $script:dragMoved = $false
1120
1402
  $mouseDown = {
1121
1403
  param($sender, $event)
1122
1404
  if ($event.Button -eq [System.Windows.Forms.MouseButtons]::Left) {
1123
1405
  $script:dragging = $true
1406
+ $script:dragMoved = $false
1124
1407
  $screenPoint = $sender.PointToScreen([System.Drawing.Point]::new($event.X, $event.Y))
1408
+ $script:dragStartScreen = $screenPoint
1125
1409
  $script:dragOffset = [System.Drawing.Point]::new(
1126
1410
  ($screenPoint.X - $form.Location.X),
1127
1411
  ($screenPoint.Y - $form.Location.Y)
@@ -1132,13 +1416,27 @@ $mouseMove = {
1132
1416
  param($sender, $event)
1133
1417
  if ($script:dragging) {
1134
1418
  $screenPoint = $sender.PointToScreen([System.Drawing.Point]::new($event.X, $event.Y))
1135
- $form.Location = [System.Drawing.Point]::new(($screenPoint.X - $script:dragOffset.X), ($screenPoint.Y - $script:dragOffset.Y))
1136
- Sync-HitFormBounds
1419
+ if ((-not $script:dragMoved) -and (([math]::Abs($screenPoint.X - $script:dragStartScreen.X) -ge 4) -or ([math]::Abs($screenPoint.Y - $script:dragStartScreen.Y) -ge 4))) {
1420
+ $script:dragMoved = $true
1421
+ Hide-HorizontalDetailPopup
1422
+ }
1423
+ if ($script:dragMoved) {
1424
+ $form.Location = [System.Drawing.Point]::new(($screenPoint.X - $script:dragOffset.X), ($screenPoint.Y - $script:dragOffset.Y))
1425
+ Sync-HitFormBounds
1426
+ }
1137
1427
  }
1138
1428
  }
1139
1429
  $mouseUp = {
1430
+ param($sender, $event)
1431
+ $wasDragging = $script:dragging
1432
+ $wasMoved = $script:dragMoved
1140
1433
  $script:dragging = $false
1141
- if ($canMove) {
1434
+ $script:dragMoved = $false
1435
+ if ($wasDragging -and (-not $wasMoved) -and $script:hudHorizontalPanel) {
1436
+ Toggle-HorizontalDetailPopup
1437
+ return
1438
+ }
1439
+ if ($canMove -and $wasMoved) {
1142
1440
  Set-HudAnchorFromPlacement $form.Location ([Nullable[int]]$form.Width) ([Nullable[int]]$form.Height)
1143
1441
  Write-HudPosition $form.Location
1144
1442
  Ensure-HudTopMost
@@ -1146,7 +1444,7 @@ $mouseUp = {
1146
1444
  }
1147
1445
  # A docked HUD is glued to its terminal window; dragging and position saving
1148
1446
  # are floating-mode concepts.
1149
- $canMove = ($DockWindowHandle -eq 0) -and ($Movable -or (($Mode -eq "floating") -and (-not $Locked)))
1447
+ $canMove = ($DockWindowHandle -eq 0) -and ($Movable -or ((@("floating", "icon", "rail", "horizontal") -contains $Mode) -and (-not $Locked)))
1150
1448
  $script:positionSaveReady = $false
1151
1449
  $script:hudHiddenForFullscreen = $false
1152
1450
 
@@ -1158,6 +1456,7 @@ function Save-HudPositionIfReady {
1158
1456
 
1159
1457
  if ($canMove) {
1160
1458
  $dragControls = @($form, $panel, $codexRow, $claudeRow, $antigravityRow) + $codexHudControls + $claudeHudControls + $antigravityHudControls
1459
+ if ($script:horizontalPaintPanel) { $dragControls += $script:horizontalPaintPanel }
1161
1460
  if ($hitForm) { $dragControls += $hitForm }
1162
1461
  foreach ($control in $dragControls) {
1163
1462
  $control.add_MouseDown($mouseDown)
@@ -1337,6 +1636,10 @@ $form.add_FormClosed({
1337
1636
  }
1338
1637
  $font.Dispose()
1339
1638
  $symbolFont.Dispose()
1639
+ if ($script:detailPopupForm -and -not $script:detailPopupForm.IsDisposed) {
1640
+ $script:detailPopupForm.Close()
1641
+ $script:detailPopupForm.Dispose()
1642
+ }
1340
1643
  if ($hitForm -and -not $hitForm.IsDisposed) {
1341
1644
  $hitForm.Close()
1342
1645
  $hitForm.Dispose()
@@ -1354,6 +1657,9 @@ $form.add_SizeChanged({
1354
1657
  Sync-HitFormBounds
1355
1658
  })
1356
1659
  $form.add_Shown({
1660
+ if (-not $script:dockTuiStatusline) {
1661
+ Resize-HudToContent
1662
+ }
1357
1663
  Show-HitForm
1358
1664
  Update-HudVisibilityForFullscreen
1359
1665
  Update-HudDockPlacement
@@ -1371,6 +1677,7 @@ if ($script:initialHudSnapshot) {
1371
1677
  } else {
1372
1678
  $script:latestSnapshot = $diskSnapshot
1373
1679
  }
1680
+ $script:latestSnapshot = Get-CurrentHudSnapshot $script:latestSnapshot
1374
1681
  Update-HudFromSnapshot $script:latestSnapshot
1375
1682
  Start-SnapshotFetch
1376
1683
  $style = [AiBatteryNative]::GetWindowLong($form.Handle, [AiBatteryNative]::GWL_EXSTYLE)