ai-battery 0.1.19 → 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,331 +1,331 @@
1
- function Limit-Number([int]$Value, [int]$Min, [int]$Max) {
2
- if ($Max -lt $Min) { return $Min }
3
- return [math]::Max($Min, [math]::Min($Max, $Value))
4
- }
5
-
6
- function Set-HudAnchorFromPlacement($Location, [Nullable[int]]$Width, [Nullable[int]]$Height) {
7
- $placementWidth = if ($null -ne $Width -and $Width -gt 0) { [int]$Width } else { [int]$form.Width }
8
- $placementHeight = if ($null -ne $Height -and $Height -gt 0) { [int]$Height } else { [int]$form.Height }
9
- $center = [System.Drawing.Point]::new(
10
- [int]($Location.X + [math]::Floor($placementWidth / 2)),
11
- [int]($Location.Y + [math]::Floor($placementHeight / 2))
12
- )
13
- $bounds = [System.Windows.Forms.Screen]::FromPoint($center).Bounds
14
- $leftDistance = [math]::Abs($Location.X - $bounds.Left)
15
- $rightDistance = [math]::Abs($bounds.Right - ($Location.X + $placementWidth))
16
- $topDistance = [math]::Abs($Location.Y - $bounds.Top)
17
- $bottomDistance = [math]::Abs($bounds.Bottom - ($Location.Y + $placementHeight))
18
-
19
- $script:hudAnchorX = if ($rightDistance -lt $leftDistance) { "right" } else { "left" }
20
- $script:hudAnchorY = if ($bottomDistance -lt $topDistance) { "bottom" } else { "top" }
21
- }
22
-
23
- function Set-HudAnchorFromPositionKeyword {
24
- $script:hudAnchorX = if ($Position -like "*left*") { "left" } else { "right" }
25
- $script:hudAnchorY = if ($Position -like "top*") { "top" } else { "bottom" }
26
- }
27
-
28
- function Get-WindowRectObject([IntPtr]$Handle) {
29
- if ($Handle -eq [IntPtr]::Zero) { return $null }
30
- $rect = New-Object AiBatteryRect
31
- if (-not [AiBatteryNative]::GetWindowRect($Handle, [ref]$rect)) { return $null }
32
- return [PSCustomObject]@{
33
- Left = [int]$rect.Left
34
- Top = [int]$rect.Top
35
- Right = [int]$rect.Right
36
- Bottom = [int]$rect.Bottom
37
- Width = [int]($rect.Right - $rect.Left)
38
- Height = [int]($rect.Bottom - $rect.Top)
39
- }
40
- }
41
-
42
- function Get-MonitorRectObject([IntPtr]$Monitor) {
43
- if ($Monitor -eq [IntPtr]::Zero) { return $null }
44
- $info = New-Object AiBatteryMonitorInfo
45
- $info.CbSize = [System.Runtime.InteropServices.Marshal]::SizeOf($info)
46
- if (-not [AiBatteryNative]::GetMonitorInfo($Monitor, [ref]$info)) { return $null }
47
- return [PSCustomObject]@{
48
- Left = [int]$info.Monitor.Left
49
- Top = [int]$info.Monitor.Top
50
- Right = [int]$info.Monitor.Right
51
- Bottom = [int]$info.Monitor.Bottom
52
- Width = [int]($info.Monitor.Right - $info.Monitor.Left)
53
- Height = [int]($info.Monitor.Bottom - $info.Monitor.Top)
54
- }
55
- }
56
-
57
- function Test-RectCoversMonitor($WindowRect, $MonitorRect) {
58
- if (-not $WindowRect -or -not $MonitorRect) { return $false }
59
- $tolerance = 2
60
- return (
61
- $WindowRect.Left -le ($MonitorRect.Left + $tolerance) -and
62
- $WindowRect.Top -le ($MonitorRect.Top + $tolerance) -and
63
- $WindowRect.Right -ge ($MonitorRect.Right - $tolerance) -and
64
- $WindowRect.Bottom -ge ($MonitorRect.Bottom - $tolerance)
65
- )
66
- }
67
-
68
- function Test-RectIsSubstantialOnMonitor($WindowRect, $MonitorRect) {
69
- if (-not $WindowRect -or -not $MonitorRect) { return $false }
70
- $left = [math]::Max([int]$WindowRect.Left, [int]$MonitorRect.Left)
71
- $top = [math]::Max([int]$WindowRect.Top, [int]$MonitorRect.Top)
72
- $right = [math]::Min([int]$WindowRect.Right, [int]$MonitorRect.Right)
73
- $bottom = [math]::Min([int]$WindowRect.Bottom, [int]$MonitorRect.Bottom)
74
- $width = [math]::Max(0, $right - $left)
75
- $height = [math]::Max(0, $bottom - $top)
76
- $monitorArea = [math]::Max(1, [int]$MonitorRect.Width * [int]$MonitorRect.Height)
77
- $windowArea = $width * $height
78
- return (($windowArea / $monitorArea) -ge 0.20)
79
- }
80
-
81
- function Test-HudWindowHandle([IntPtr]$Handle) {
82
- if ($Handle -eq [IntPtr]::Zero) { return $false }
83
- if ($form -and -not $form.IsDisposed -and $Handle -eq $form.Handle) { return $true }
84
- if ($hitForm -and -not $hitForm.IsDisposed -and $Handle -eq $hitForm.Handle) { return $true }
85
- return $false
86
- }
87
-
88
- function Get-WindowClassName([IntPtr]$Handle) {
89
- if ($Handle -eq [IntPtr]::Zero) { return "" }
90
- $builder = New-Object System.Text.StringBuilder 256
91
- [AiBatteryNative]::GetClassName($Handle, $builder, $builder.Capacity) | Out-Null
92
- return $builder.ToString()
93
- }
94
-
95
- function Test-WindowCloaked([IntPtr]$Handle) {
96
- if ($Handle -eq [IntPtr]::Zero) { return $false }
97
- $value = 0
98
- try {
99
- $hr = [AiBatteryNative]::DwmGetWindowAttribute($Handle, [AiBatteryNative]::DWMWA_CLOAKED, [ref]$value, 4)
100
- } catch {
101
- return $false
102
- }
103
- if ($hr -ne 0) { return $false }
104
- return ($value -ne 0)
105
- }
106
-
107
- # Desktop and shell surfaces span the monitor but must never hide the HUD.
108
- $script:hudSkipWindowClasses = @(
109
- "Shell_TrayWnd",
110
- "Shell_SecondaryTrayWnd",
111
- "NotifyIconOverflowWindow",
112
- "Progman",
113
- "WorkerW"
114
- )
115
-
116
- function Test-FullscreenOnHudMonitor {
117
- if (-not $form -or $form.IsDisposed) { return $false }
118
- $hudMonitor = [AiBatteryNative]::MonitorFromWindow($form.Handle, [AiBatteryNative]::MONITOR_DEFAULTTONEAREST)
119
- if ($hudMonitor -eq [IntPtr]::Zero) { return $false }
120
- $monitorRect = Get-MonitorRectObject $hudMonitor
121
- if (-not $monitorRect) { return $false }
122
-
123
- # Walk the Z-order from the top down. Small transient overlays (volume, IME,
124
- # Game Bar, browser controls) are ignored so we can still see the fullscreen
125
- # window beneath them. A substantial normal window stops the search: any
126
- # fullscreen window below it is not the visible fullscreen surface.
127
- $handle = [AiBatteryNative]::GetTopWindow([IntPtr]::Zero)
128
- for ($scanned = 0; $scanned -lt 400 -and $handle -ne [IntPtr]::Zero; $scanned += 1) {
129
- $next = [AiBatteryNative]::GetWindow($handle, [AiBatteryNative]::GW_HWNDNEXT)
130
- # Filter cheapest-first: most windows live on another monitor, so skip the
131
- # DWM cloak and class-name lookups until a window is on the HUD monitor.
132
- if ((-not (Test-HudWindowHandle $handle)) -and
133
- [AiBatteryNative]::IsWindowVisible($handle) -and
134
- ([AiBatteryNative]::MonitorFromWindow($handle, [AiBatteryNative]::MONITOR_DEFAULTTONEAREST) -eq $hudMonitor) -and
135
- (-not (Test-WindowCloaked $handle)) -and
136
- ($script:hudSkipWindowClasses -notcontains (Get-WindowClassName $handle))) {
137
- $windowRect = Get-WindowRectObject $handle
138
- if ($windowRect -and $windowRect.Width -gt 0 -and $windowRect.Height -gt 0) {
139
- if (Test-RectCoversMonitor $windowRect $monitorRect) {
140
- return $true
141
- }
142
- if (Test-RectIsSubstantialOnMonitor $windowRect $monitorRect) {
143
- return $false
144
- }
145
- }
146
- }
147
- $handle = $next
148
- }
149
-
150
- return $false
151
- }
152
-
153
- function Set-TaskbarPosition {
154
- $taskbarHandle = [AiBatteryNative]::FindWindow("Shell_TrayWnd", $null)
155
- if ($taskbarHandle -eq [IntPtr]::Zero) { return $false }
156
-
157
- $taskbarRect = Get-WindowRectObject $taskbarHandle
158
- if (-not $taskbarRect) { return $false }
159
-
160
- $trayHandle = [AiBatteryNative]::FindWindowEx($taskbarHandle, [IntPtr]::Zero, "TrayNotifyWnd", $null)
161
- $trayRect = Get-WindowRectObject $trayHandle
162
- if (-not $trayRect) {
163
- $trayRect = [PSCustomObject]@{
164
- Left = $taskbarRect.Right
165
- Top = $taskbarRect.Bottom
166
- Right = $taskbarRect.Right
167
- Bottom = $taskbarRect.Bottom
168
- Width = 0
169
- Height = 0
170
- }
171
- }
172
-
173
- $screen = [System.Windows.Forms.Screen]::FromHandle($taskbarHandle)
174
- $bounds = $screen.Bounds
175
- $margin = 6
176
- $isHorizontal = $taskbarRect.Width -ge $taskbarRect.Height
177
-
178
- if ($isHorizontal) {
179
- $x = $trayRect.Left - $form.Width - $margin
180
- $y = $taskbarRect.Top + [math]::Floor(($taskbarRect.Height - $form.Height) / 2)
181
- } else {
182
- $isLeft = $taskbarRect.Left -le $bounds.Left
183
- $x = if ($isLeft) { $taskbarRect.Right + $margin } else { $taskbarRect.Left - $form.Width - $margin }
184
- $y = $trayRect.Top - $form.Height - $margin
185
- }
186
-
187
- $x = Limit-Number $x ($bounds.Left + $margin) ($bounds.Right - $form.Width - $margin)
188
- $y = Limit-Number $y ($bounds.Top + $margin) ($bounds.Bottom - $form.Height - $margin)
189
- $form.Location = [System.Drawing.Point]::new([int]$x, [int]$y)
190
- Set-HudAnchorFromPlacement $form.Location ([Nullable[int]]$form.Width) ([Nullable[int]]$form.Height)
191
- return $true
192
- }
193
-
194
- function Get-OnScreenPoint([int]$X, [int]$Y, [int]$W, [int]$H) {
195
- $rect = [System.Drawing.Rectangle]::new($X, $Y, [math]::Max(1, $W), [math]::Max(1, $H))
196
- foreach ($screen in [System.Windows.Forms.Screen]::AllScreens) {
197
- # Compare against the full monitor bounds, not the working area: sitting
198
- # on top of the taskbar is this HUD's primary use case, and the taskbar
199
- # band is outside every working area.
200
- $overlap = [System.Drawing.Rectangle]::Intersect($screen.Bounds, $rect)
201
- if ($overlap.Width -ge 24 -and $overlap.Height -ge 10) {
202
- return [System.Drawing.Point]::new($X, $Y)
203
- }
204
- }
205
-
206
- # The saved spot is no longer on any screen (monitor unplugged or layout
207
- # changed), so pull the HUD back into the primary working area.
208
- $area = [System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea
209
- $margin = 18
210
- $newX = Limit-Number $X ($area.Left + $margin) ($area.Right - $W - $margin)
211
- $newY = Limit-Number $Y ($area.Top + $margin) ($area.Bottom - $H - $margin)
212
- return [System.Drawing.Point]::new([int]$newX, [int]$newY)
213
- }
214
-
215
- function Set-Position {
216
- if ($DockWindowHandle -ne 0) {
217
- Update-HudDockPlacement
218
- return
219
- }
220
- if ($Position -eq "saved") {
221
- $savedPlacement = Read-HudPlacement
222
- if ($savedPlacement) {
223
- if ($null -ne $savedPlacement.Width -and $savedPlacement.Width -gt 0) {
224
- $form.Width = [int]$savedPlacement.Width
225
- }
226
- if ($null -ne $savedPlacement.Height -and $savedPlacement.Height -gt 0) {
227
- $form.Height = [int]$savedPlacement.Height
228
- }
229
- $form.Location = Get-OnScreenPoint ([int]$savedPlacement.X) ([int]$savedPlacement.Y) ([int]$form.Width) ([int]$form.Height)
230
- Set-HudAnchorFromPlacement $form.Location ([Nullable[int]]$form.Width) ([Nullable[int]]$form.Height)
231
- return
232
- }
233
- }
234
-
235
- if ($Position -eq "taskbar") {
236
- if (Set-TaskbarPosition) { return }
237
- }
238
-
239
- $area = [System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea
240
- $margin = if ($Mode -eq "statusline") { 8 } else { 18 }
241
- $x = if ($Position -like "*left*") {
242
- $area.Left + $margin
243
- } elseif ($Position -like "*center*") {
244
- $area.Left + [math]::Floor(($area.Width - $form.Width) / 2)
245
- } else {
246
- $area.Right - $form.Width - $margin
247
- }
248
- $y = if ($Position -like "top*") { $area.Top + $margin } else { $area.Bottom - $form.Height - $margin }
249
- $form.Location = [System.Drawing.Point]::new($x, $y)
250
- Set-HudAnchorFromPositionKeyword
251
- }
252
-
253
- function Ensure-HudTopMost {
254
- if (-not $form -or $form.IsDisposed) { return }
255
- if ($script:hudHiddenForFullscreen -or $script:hudHiddenForDock) { return }
256
- if ($null -ne $script:dockTargetHandle -and $script:dockTargetHandle -ne [IntPtr]::Zero) {
257
- # Docked HUDs are z-ordered against the terminal window instead of the
258
- # topmost band; Sync-HudDockZOrder maintains that on every placement tick.
259
- return
260
- }
261
- if ($hitForm -and -not $hitForm.IsDisposed -and $hitForm.Visible) {
262
- $hitForm.TopMost = $true
263
- [AiBatteryNative]::SetWindowPos(
264
- $hitForm.Handle,
265
- [AiBatteryNative]::HWND_TOPMOST,
266
- 0,
267
- 0,
268
- 0,
269
- 0,
270
- [AiBatteryNative]::SWP_NOMOVE -bor [AiBatteryNative]::SWP_NOSIZE -bor [AiBatteryNative]::SWP_NOACTIVATE -bor [AiBatteryNative]::SWP_SHOWWINDOW
271
- ) | Out-Null
272
- }
273
- $form.TopMost = $true
274
- [AiBatteryNative]::SetWindowPos(
275
- $form.Handle,
276
- [AiBatteryNative]::HWND_TOPMOST,
277
- 0,
278
- 0,
279
- 0,
280
- 0,
281
- [AiBatteryNative]::SWP_NOMOVE -bor [AiBatteryNative]::SWP_NOSIZE -bor [AiBatteryNative]::SWP_NOACTIVATE -bor [AiBatteryNative]::SWP_SHOWWINDOW
282
- ) | Out-Null
283
- }
284
-
285
- $script:hudHiddenForDock = $false
286
-
287
- function Apply-HudHiddenState {
288
- $hidden = $script:hudHiddenForFullscreen -or $script:hudHiddenForDock
289
- if ($env:AI_BATTERY_HUD_DEBUG_LOG) {
290
- try { Add-Content $env:AI_BATTERY_HUD_DEBUG_LOG "apply hidden=$hidden fullscreen=$($script:hudHiddenForFullscreen) dock=$($script:hudHiddenForDock) formVisible=$($form -and -not $form.IsDisposed -and $form.Visible)" } catch { }
291
- }
292
- if ($hidden) {
293
- Hide-HudMenu
294
- if ($hitForm -and -not $hitForm.IsDisposed -and $hitForm.Visible) {
295
- [AiBatteryNative]::ShowWindow($hitForm.Handle, [AiBatteryNative]::SW_HIDE) | Out-Null
296
- $hitForm.Hide()
297
- }
298
- if ($form -and -not $form.IsDisposed -and $form.Visible) {
299
- [AiBatteryNative]::ShowWindow($form.Handle, [AiBatteryNative]::SW_HIDE) | Out-Null
300
- $form.Hide()
301
- }
302
- return
303
- }
304
-
305
- if ($form -and -not $form.IsDisposed) {
306
- if (-not $form.Visible) { $form.Show() }
307
- # Always issue the native show: the managed Visible flag reports true even
308
- # when the first Show() was suppressed by an SW_HIDE STARTUPINFO.
309
- [AiBatteryNative]::ShowWindow($form.Handle, [AiBatteryNative]::SW_SHOWNOACTIVATE) | Out-Null
310
- }
311
- Show-HitForm
312
- Sync-HitFormBounds
313
- }
314
-
315
- function Set-HudHiddenForFullscreen([bool]$Hidden) {
316
- if ($script:hudHiddenForFullscreen -eq $Hidden) { return }
317
- $script:hudHiddenForFullscreen = $Hidden
318
- Apply-HudHiddenState
319
- }
320
-
321
- function Set-HudHiddenForDock([bool]$Hidden) {
322
- if ($script:hudHiddenForDock -eq $Hidden) { return }
323
- $script:hudHiddenForDock = $Hidden
324
- Apply-HudHiddenState
325
- }
326
-
327
- # --- Terminal-docked placement -------------------------------------------
328
- # When -DockWindowHandle is given, the terminal is shortened by one status
329
- # row and a borderless HUD strip occupies the released pixels immediately
330
- # below it. Codex keeps its native status line and owns its viewport directly;
331
- # the HUD supplies only the second AI Battery row.
1
+ function Limit-Number([int]$Value, [int]$Min, [int]$Max) {
2
+ if ($Max -lt $Min) { return $Min }
3
+ return [math]::Max($Min, [math]::Min($Max, $Value))
4
+ }
5
+
6
+ function Set-HudAnchorFromPlacement($Location, [Nullable[int]]$Width, [Nullable[int]]$Height) {
7
+ $placementWidth = if ($null -ne $Width -and $Width -gt 0) { [int]$Width } else { [int]$form.Width }
8
+ $placementHeight = if ($null -ne $Height -and $Height -gt 0) { [int]$Height } else { [int]$form.Height }
9
+ $center = [System.Drawing.Point]::new(
10
+ [int]($Location.X + [math]::Floor($placementWidth / 2)),
11
+ [int]($Location.Y + [math]::Floor($placementHeight / 2))
12
+ )
13
+ $bounds = [System.Windows.Forms.Screen]::FromPoint($center).Bounds
14
+ $leftDistance = [math]::Abs($Location.X - $bounds.Left)
15
+ $rightDistance = [math]::Abs($bounds.Right - ($Location.X + $placementWidth))
16
+ $topDistance = [math]::Abs($Location.Y - $bounds.Top)
17
+ $bottomDistance = [math]::Abs($bounds.Bottom - ($Location.Y + $placementHeight))
18
+
19
+ $script:hudAnchorX = if ($rightDistance -lt $leftDistance) { "right" } else { "left" }
20
+ $script:hudAnchorY = if ($bottomDistance -lt $topDistance) { "bottom" } else { "top" }
21
+ }
22
+
23
+ function Set-HudAnchorFromPositionKeyword {
24
+ $script:hudAnchorX = if ($Position -like "*left*") { "left" } else { "right" }
25
+ $script:hudAnchorY = if ($Position -like "top*") { "top" } else { "bottom" }
26
+ }
27
+
28
+ function Get-WindowRectObject([IntPtr]$Handle) {
29
+ if ($Handle -eq [IntPtr]::Zero) { return $null }
30
+ $rect = New-Object AiBatteryRect
31
+ if (-not [AiBatteryNative]::GetWindowRect($Handle, [ref]$rect)) { return $null }
32
+ return [PSCustomObject]@{
33
+ Left = [int]$rect.Left
34
+ Top = [int]$rect.Top
35
+ Right = [int]$rect.Right
36
+ Bottom = [int]$rect.Bottom
37
+ Width = [int]($rect.Right - $rect.Left)
38
+ Height = [int]($rect.Bottom - $rect.Top)
39
+ }
40
+ }
41
+
42
+ function Get-MonitorRectObject([IntPtr]$Monitor) {
43
+ if ($Monitor -eq [IntPtr]::Zero) { return $null }
44
+ $info = New-Object AiBatteryMonitorInfo
45
+ $info.CbSize = [System.Runtime.InteropServices.Marshal]::SizeOf($info)
46
+ if (-not [AiBatteryNative]::GetMonitorInfo($Monitor, [ref]$info)) { return $null }
47
+ return [PSCustomObject]@{
48
+ Left = [int]$info.Monitor.Left
49
+ Top = [int]$info.Monitor.Top
50
+ Right = [int]$info.Monitor.Right
51
+ Bottom = [int]$info.Monitor.Bottom
52
+ Width = [int]($info.Monitor.Right - $info.Monitor.Left)
53
+ Height = [int]($info.Monitor.Bottom - $info.Monitor.Top)
54
+ }
55
+ }
56
+
57
+ function Test-RectCoversMonitor($WindowRect, $MonitorRect) {
58
+ if (-not $WindowRect -or -not $MonitorRect) { return $false }
59
+ $tolerance = 2
60
+ return (
61
+ $WindowRect.Left -le ($MonitorRect.Left + $tolerance) -and
62
+ $WindowRect.Top -le ($MonitorRect.Top + $tolerance) -and
63
+ $WindowRect.Right -ge ($MonitorRect.Right - $tolerance) -and
64
+ $WindowRect.Bottom -ge ($MonitorRect.Bottom - $tolerance)
65
+ )
66
+ }
67
+
68
+ function Test-RectIsSubstantialOnMonitor($WindowRect, $MonitorRect) {
69
+ if (-not $WindowRect -or -not $MonitorRect) { return $false }
70
+ $left = [math]::Max([int]$WindowRect.Left, [int]$MonitorRect.Left)
71
+ $top = [math]::Max([int]$WindowRect.Top, [int]$MonitorRect.Top)
72
+ $right = [math]::Min([int]$WindowRect.Right, [int]$MonitorRect.Right)
73
+ $bottom = [math]::Min([int]$WindowRect.Bottom, [int]$MonitorRect.Bottom)
74
+ $width = [math]::Max(0, $right - $left)
75
+ $height = [math]::Max(0, $bottom - $top)
76
+ $monitorArea = [math]::Max(1, [int]$MonitorRect.Width * [int]$MonitorRect.Height)
77
+ $windowArea = $width * $height
78
+ return (($windowArea / $monitorArea) -ge 0.20)
79
+ }
80
+
81
+ function Test-HudWindowHandle([IntPtr]$Handle) {
82
+ if ($Handle -eq [IntPtr]::Zero) { return $false }
83
+ if ($form -and -not $form.IsDisposed -and $Handle -eq $form.Handle) { return $true }
84
+ if ($hitForm -and -not $hitForm.IsDisposed -and $Handle -eq $hitForm.Handle) { return $true }
85
+ return $false
86
+ }
87
+
88
+ function Get-WindowClassName([IntPtr]$Handle) {
89
+ if ($Handle -eq [IntPtr]::Zero) { return "" }
90
+ $builder = New-Object System.Text.StringBuilder 256
91
+ [AiBatteryNative]::GetClassName($Handle, $builder, $builder.Capacity) | Out-Null
92
+ return $builder.ToString()
93
+ }
94
+
95
+ function Test-WindowCloaked([IntPtr]$Handle) {
96
+ if ($Handle -eq [IntPtr]::Zero) { return $false }
97
+ $value = 0
98
+ try {
99
+ $hr = [AiBatteryNative]::DwmGetWindowAttribute($Handle, [AiBatteryNative]::DWMWA_CLOAKED, [ref]$value, 4)
100
+ } catch {
101
+ return $false
102
+ }
103
+ if ($hr -ne 0) { return $false }
104
+ return ($value -ne 0)
105
+ }
106
+
107
+ # Desktop and shell surfaces span the monitor but must never hide the HUD.
108
+ $script:hudSkipWindowClasses = @(
109
+ "Shell_TrayWnd",
110
+ "Shell_SecondaryTrayWnd",
111
+ "NotifyIconOverflowWindow",
112
+ "Progman",
113
+ "WorkerW"
114
+ )
115
+
116
+ function Test-FullscreenOnHudMonitor {
117
+ if (-not $form -or $form.IsDisposed) { return $false }
118
+ $hudMonitor = [AiBatteryNative]::MonitorFromWindow($form.Handle, [AiBatteryNative]::MONITOR_DEFAULTTONEAREST)
119
+ if ($hudMonitor -eq [IntPtr]::Zero) { return $false }
120
+ $monitorRect = Get-MonitorRectObject $hudMonitor
121
+ if (-not $monitorRect) { return $false }
122
+
123
+ # Walk the Z-order from the top down. Small transient overlays (volume, IME,
124
+ # Game Bar, browser controls) are ignored so we can still see the fullscreen
125
+ # window beneath them. A substantial normal window stops the search: any
126
+ # fullscreen window below it is not the visible fullscreen surface.
127
+ $handle = [AiBatteryNative]::GetTopWindow([IntPtr]::Zero)
128
+ for ($scanned = 0; $scanned -lt 400 -and $handle -ne [IntPtr]::Zero; $scanned += 1) {
129
+ $next = [AiBatteryNative]::GetWindow($handle, [AiBatteryNative]::GW_HWNDNEXT)
130
+ # Filter cheapest-first: most windows live on another monitor, so skip the
131
+ # DWM cloak and class-name lookups until a window is on the HUD monitor.
132
+ if ((-not (Test-HudWindowHandle $handle)) -and
133
+ [AiBatteryNative]::IsWindowVisible($handle) -and
134
+ ([AiBatteryNative]::MonitorFromWindow($handle, [AiBatteryNative]::MONITOR_DEFAULTTONEAREST) -eq $hudMonitor) -and
135
+ (-not (Test-WindowCloaked $handle)) -and
136
+ ($script:hudSkipWindowClasses -notcontains (Get-WindowClassName $handle))) {
137
+ $windowRect = Get-WindowRectObject $handle
138
+ if ($windowRect -and $windowRect.Width -gt 0 -and $windowRect.Height -gt 0) {
139
+ if (Test-RectCoversMonitor $windowRect $monitorRect) {
140
+ return $true
141
+ }
142
+ if (Test-RectIsSubstantialOnMonitor $windowRect $monitorRect) {
143
+ return $false
144
+ }
145
+ }
146
+ }
147
+ $handle = $next
148
+ }
149
+
150
+ return $false
151
+ }
152
+
153
+ function Set-TaskbarPosition {
154
+ $taskbarHandle = [AiBatteryNative]::FindWindow("Shell_TrayWnd", $null)
155
+ if ($taskbarHandle -eq [IntPtr]::Zero) { return $false }
156
+
157
+ $taskbarRect = Get-WindowRectObject $taskbarHandle
158
+ if (-not $taskbarRect) { return $false }
159
+
160
+ $trayHandle = [AiBatteryNative]::FindWindowEx($taskbarHandle, [IntPtr]::Zero, "TrayNotifyWnd", $null)
161
+ $trayRect = Get-WindowRectObject $trayHandle
162
+ if (-not $trayRect) {
163
+ $trayRect = [PSCustomObject]@{
164
+ Left = $taskbarRect.Right
165
+ Top = $taskbarRect.Bottom
166
+ Right = $taskbarRect.Right
167
+ Bottom = $taskbarRect.Bottom
168
+ Width = 0
169
+ Height = 0
170
+ }
171
+ }
172
+
173
+ $screen = [System.Windows.Forms.Screen]::FromHandle($taskbarHandle)
174
+ $bounds = $screen.Bounds
175
+ $margin = 6
176
+ $isHorizontal = $taskbarRect.Width -ge $taskbarRect.Height
177
+
178
+ if ($isHorizontal) {
179
+ $x = $trayRect.Left - $form.Width - $margin
180
+ $y = $taskbarRect.Top + [math]::Floor(($taskbarRect.Height - $form.Height) / 2)
181
+ } else {
182
+ $isLeft = $taskbarRect.Left -le $bounds.Left
183
+ $x = if ($isLeft) { $taskbarRect.Right + $margin } else { $taskbarRect.Left - $form.Width - $margin }
184
+ $y = $trayRect.Top - $form.Height - $margin
185
+ }
186
+
187
+ $x = Limit-Number $x ($bounds.Left + $margin) ($bounds.Right - $form.Width - $margin)
188
+ $y = Limit-Number $y ($bounds.Top + $margin) ($bounds.Bottom - $form.Height - $margin)
189
+ $form.Location = [System.Drawing.Point]::new([int]$x, [int]$y)
190
+ Set-HudAnchorFromPlacement $form.Location ([Nullable[int]]$form.Width) ([Nullable[int]]$form.Height)
191
+ return $true
192
+ }
193
+
194
+ function Get-OnScreenPoint([int]$X, [int]$Y, [int]$W, [int]$H) {
195
+ $rect = [System.Drawing.Rectangle]::new($X, $Y, [math]::Max(1, $W), [math]::Max(1, $H))
196
+ foreach ($screen in [System.Windows.Forms.Screen]::AllScreens) {
197
+ # Compare against the full monitor bounds, not the working area: sitting
198
+ # on top of the taskbar is this HUD's primary use case, and the taskbar
199
+ # band is outside every working area.
200
+ $overlap = [System.Drawing.Rectangle]::Intersect($screen.Bounds, $rect)
201
+ if ($overlap.Width -ge 24 -and $overlap.Height -ge 10) {
202
+ return [System.Drawing.Point]::new($X, $Y)
203
+ }
204
+ }
205
+
206
+ # The saved spot is no longer on any screen (monitor unplugged or layout
207
+ # changed), so pull the HUD back into the primary working area.
208
+ $area = [System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea
209
+ $margin = 18
210
+ $newX = Limit-Number $X ($area.Left + $margin) ($area.Right - $W - $margin)
211
+ $newY = Limit-Number $Y ($area.Top + $margin) ($area.Bottom - $H - $margin)
212
+ return [System.Drawing.Point]::new([int]$newX, [int]$newY)
213
+ }
214
+
215
+ function Set-Position {
216
+ if ($DockWindowHandle -ne 0) {
217
+ Update-HudDockPlacement
218
+ return
219
+ }
220
+ if ($Position -eq "saved") {
221
+ $savedPlacement = Read-HudPlacement
222
+ if ($savedPlacement) {
223
+ if ($null -ne $savedPlacement.Width -and $savedPlacement.Width -gt 0) {
224
+ $form.Width = [int]$savedPlacement.Width
225
+ }
226
+ if ($null -ne $savedPlacement.Height -and $savedPlacement.Height -gt 0) {
227
+ $form.Height = [int]$savedPlacement.Height
228
+ }
229
+ $form.Location = Get-OnScreenPoint ([int]$savedPlacement.X) ([int]$savedPlacement.Y) ([int]$form.Width) ([int]$form.Height)
230
+ Set-HudAnchorFromPlacement $form.Location ([Nullable[int]]$form.Width) ([Nullable[int]]$form.Height)
231
+ return
232
+ }
233
+ }
234
+
235
+ if ($Position -eq "taskbar") {
236
+ if (Set-TaskbarPosition) { return }
237
+ }
238
+
239
+ $area = [System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea
240
+ $margin = if ($Mode -eq "statusline") { 8 } else { 18 }
241
+ $x = if ($Position -like "*left*") {
242
+ $area.Left + $margin
243
+ } elseif ($Position -like "*center*") {
244
+ $area.Left + [math]::Floor(($area.Width - $form.Width) / 2)
245
+ } else {
246
+ $area.Right - $form.Width - $margin
247
+ }
248
+ $y = if ($Position -like "top*") { $area.Top + $margin } else { $area.Bottom - $form.Height - $margin }
249
+ $form.Location = [System.Drawing.Point]::new($x, $y)
250
+ Set-HudAnchorFromPositionKeyword
251
+ }
252
+
253
+ function Ensure-HudTopMost {
254
+ if (-not $form -or $form.IsDisposed) { return }
255
+ if ($script:hudHiddenForFullscreen -or $script:hudHiddenForDock) { return }
256
+ if ($null -ne $script:dockTargetHandle -and $script:dockTargetHandle -ne [IntPtr]::Zero) {
257
+ # Docked HUDs are z-ordered against the terminal window instead of the
258
+ # topmost band; Sync-HudDockZOrder maintains that on every placement tick.
259
+ return
260
+ }
261
+ if ($hitForm -and -not $hitForm.IsDisposed -and $hitForm.Visible) {
262
+ $hitForm.TopMost = $true
263
+ [AiBatteryNative]::SetWindowPos(
264
+ $hitForm.Handle,
265
+ [AiBatteryNative]::HWND_TOPMOST,
266
+ 0,
267
+ 0,
268
+ 0,
269
+ 0,
270
+ [AiBatteryNative]::SWP_NOMOVE -bor [AiBatteryNative]::SWP_NOSIZE -bor [AiBatteryNative]::SWP_NOACTIVATE -bor [AiBatteryNative]::SWP_SHOWWINDOW
271
+ ) | Out-Null
272
+ }
273
+ $form.TopMost = $true
274
+ [AiBatteryNative]::SetWindowPos(
275
+ $form.Handle,
276
+ [AiBatteryNative]::HWND_TOPMOST,
277
+ 0,
278
+ 0,
279
+ 0,
280
+ 0,
281
+ [AiBatteryNative]::SWP_NOMOVE -bor [AiBatteryNative]::SWP_NOSIZE -bor [AiBatteryNative]::SWP_NOACTIVATE -bor [AiBatteryNative]::SWP_SHOWWINDOW
282
+ ) | Out-Null
283
+ }
284
+
285
+ $script:hudHiddenForDock = $false
286
+
287
+ function Apply-HudHiddenState {
288
+ $hidden = $script:hudHiddenForFullscreen -or $script:hudHiddenForDock
289
+ if ($env:AI_BATTERY_HUD_DEBUG_LOG) {
290
+ try { Add-Content $env:AI_BATTERY_HUD_DEBUG_LOG "apply hidden=$hidden fullscreen=$($script:hudHiddenForFullscreen) dock=$($script:hudHiddenForDock) formVisible=$($form -and -not $form.IsDisposed -and $form.Visible)" } catch { }
291
+ }
292
+ if ($hidden) {
293
+ Hide-HudMenu
294
+ if ($hitForm -and -not $hitForm.IsDisposed -and $hitForm.Visible) {
295
+ [AiBatteryNative]::ShowWindow($hitForm.Handle, [AiBatteryNative]::SW_HIDE) | Out-Null
296
+ $hitForm.Hide()
297
+ }
298
+ if ($form -and -not $form.IsDisposed -and $form.Visible) {
299
+ [AiBatteryNative]::ShowWindow($form.Handle, [AiBatteryNative]::SW_HIDE) | Out-Null
300
+ $form.Hide()
301
+ }
302
+ return
303
+ }
304
+
305
+ if ($form -and -not $form.IsDisposed) {
306
+ if (-not $form.Visible) { $form.Show() }
307
+ # Always issue the native show: the managed Visible flag reports true even
308
+ # when the first Show() was suppressed by an SW_HIDE STARTUPINFO.
309
+ [AiBatteryNative]::ShowWindow($form.Handle, [AiBatteryNative]::SW_SHOWNOACTIVATE) | Out-Null
310
+ }
311
+ Show-HitForm
312
+ Sync-HitFormBounds
313
+ }
314
+
315
+ function Set-HudHiddenForFullscreen([bool]$Hidden) {
316
+ if ($script:hudHiddenForFullscreen -eq $Hidden) { return }
317
+ $script:hudHiddenForFullscreen = $Hidden
318
+ Apply-HudHiddenState
319
+ }
320
+
321
+ function Set-HudHiddenForDock([bool]$Hidden) {
322
+ if ($script:hudHiddenForDock -eq $Hidden) { return }
323
+ $script:hudHiddenForDock = $Hidden
324
+ Apply-HudHiddenState
325
+ }
326
+
327
+ # --- Terminal-docked placement -------------------------------------------
328
+ # When -DockWindowHandle is given, the terminal is shortened by one status
329
+ # row and a borderless HUD strip occupies the released pixels immediately
330
+ # below it. Codex keeps its native status line and owns its viewport directly;
331
+ # the HUD supplies only the second AI Battery row.