vanspod 0.4.13 → 0.4.16

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.
package/scripts/tray.ps1 CHANGED
@@ -2,17 +2,158 @@ param(
2
2
  [Parameter(Mandatory = $true)]
3
3
  [string]$Url,
4
4
  [Parameter(Mandatory = $true)][string]$IconPath,
5
- [Parameter(Mandatory = $true)][string]$StateFile
5
+ [Parameter(Mandatory = $true)][string]$StateFile,
6
+ # 法鬥超人:Local Tool Access 打開時的系統匣圖示,也放在 Approval Popup 上。
7
+ [string]$MascotPath
6
8
  )
7
9
 
10
+ # VansPod 用這個程序的 stdin 與 stdout 和系統匣溝通(src/trayChannel.ts):每行一個 JSON。
11
+ # 只有 VansPod 自己啟動的這個程序拿得到這兩條管線,所以只有它能從頁面以外決定 Local Tool Approval。
12
+
8
13
  Add-Type -AssemblyName System.Windows.Forms
9
14
  Add-Type -AssemblyName System.Drawing
15
+ Add-Type -ReferencedAssemblies System.Windows.Forms, System.Drawing -TypeDefinition @'
16
+ using System;
17
+ using System.Drawing;
18
+ using System.Reflection;
19
+ using System.Runtime.InteropServices;
20
+ using System.Windows.Forms;
21
+
22
+ namespace VansPodTray {
23
+ // 確認面板:不搶鍵盤(顯示與點擊都不會成為前景視窗)、不在工作列、永遠在最上層。
24
+ public class PopupForm : Form {
25
+ protected override bool ShowWithoutActivation { get { return true; } }
26
+ protected override CreateParams CreateParams {
27
+ get {
28
+ CreateParams p = base.CreateParams;
29
+ p.ExStyle |= 0x08000000 | 0x00000080 | 0x00000008; // WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW | WS_EX_TOPMOST
30
+ return p;
31
+ }
32
+ }
33
+ protected override void WndProc(ref Message m) {
34
+ if (m.Msg == 0x21) { m.Result = (IntPtr)3; return; } // WM_MOUSEACTIVATE -> MA_NOACTIVATE
35
+ base.WndProc(ref m);
36
+ }
37
+ }
38
+
39
+ // 拿不到焦點的按鈕:只能用滑鼠按,Enter 與空白鍵都不會按到它。
40
+ public class MouseOnlyButton : Button {
41
+ public MouseOnlyButton() { SetStyle(ControlStyles.Selectable, false); TabStop = false; }
42
+ protected override bool ShowFocusCues { get { return false; } }
43
+ }
44
+
45
+ public static class TrayPlace {
46
+ [StructLayout(LayoutKind.Sequential)]
47
+ struct NOTIFYICONIDENTIFIER { public uint cbSize; public IntPtr hWnd; public uint uID; public Guid guidItem; }
48
+ [StructLayout(LayoutKind.Sequential)]
49
+ struct RECT { public int Left, Top, Right, Bottom; }
50
+ [DllImport("shell32.dll")]
51
+ static extern int Shell_NotifyIconGetRect(ref NOTIFYICONIDENTIFIER identifier, out RECT iconLocation);
52
+
53
+ // 系統匣圖示在螢幕上的位置;拿不到時是 Rectangle.Empty。
54
+ public static Rectangle IconBounds(NotifyIcon icon) {
55
+ try {
56
+ FieldInfo windowField = typeof(NotifyIcon).GetField("window", BindingFlags.NonPublic | BindingFlags.Instance);
57
+ FieldInfo idField = typeof(NotifyIcon).GetField("id", BindingFlags.NonPublic | BindingFlags.Instance);
58
+ NativeWindow window = (NativeWindow)windowField.GetValue(icon);
59
+ NOTIFYICONIDENTIFIER identifier = new NOTIFYICONIDENTIFIER();
60
+ identifier.cbSize = (uint)Marshal.SizeOf(typeof(NOTIFYICONIDENTIFIER));
61
+ identifier.hWnd = window.Handle;
62
+ identifier.uID = (uint)(int)idField.GetValue(icon);
63
+ RECT r;
64
+ if (Shell_NotifyIconGetRect(ref identifier, out r) != 0) return Rectangle.Empty;
65
+ return Rectangle.FromLTRB(r.Left, r.Top, r.Right, r.Bottom);
66
+ } catch { return Rectangle.Empty; }
67
+ }
68
+
69
+ // 面板放在系統匣圖示上方(工作列在上方時放下方),不超出工作區。
70
+ public static Point PopupLocation(Rectangle icon, Size popup) {
71
+ Screen screen = icon.IsEmpty ? Screen.PrimaryScreen : Screen.FromRectangle(icon);
72
+ Rectangle area = screen.WorkingArea;
73
+ Rectangle bounds = screen.Bounds;
74
+ const int gap = 12;
75
+ bool taskbarTop = area.Top > bounds.Top;
76
+ bool taskbarLeft = area.Left > bounds.Left;
77
+ int x = taskbarLeft ? area.Left + gap : area.Right - popup.Width - gap;
78
+ int y = taskbarTop ? area.Top + gap : area.Bottom - popup.Height - gap;
79
+ if (!icon.IsEmpty && area.IntersectsWith(Rectangle.Inflate(icon, 64, 64))) {
80
+ int center = icon.Left + icon.Width / 2;
81
+ if (center >= area.Left && center <= area.Right) x = center - popup.Width / 2;
82
+ }
83
+ x = Math.Max(area.Left + gap, Math.Min(x, area.Right - popup.Width - gap));
84
+ y = Math.Max(area.Top + gap, Math.Min(y, area.Bottom - popup.Height - gap));
85
+ return new Point(x, y);
86
+ }
87
+ }
88
+ }
89
+ '@
10
90
 
11
91
  $script:OpenUrl = $Url
12
92
  $openPage = { Start-Process $script:OpenUrl }.GetNewClosure()
13
93
 
94
+ # ---- 和 VansPod 的管線 ----
95
+ $utf8 = New-Object System.Text.UTF8Encoding $false
96
+ $script:reader = New-Object System.IO.StreamReader([Console]::OpenStandardInput(), $utf8)
97
+ $script:writer = New-Object System.IO.StreamWriter([Console]::OpenStandardOutput(), $utf8)
98
+ $script:writer.AutoFlush = $true
99
+ function Send-ToVansPod($message) {
100
+ try { $script:writer.WriteLine(($message | ConvertTo-Json -Compress)) } catch { }
101
+ }
102
+
103
+ # ---- 系統匣圖示 ----
104
+ $offIcon = New-Object System.Drawing.Icon($IconPath)
105
+ $script:mascot = $null
106
+ $onIcon = $null
107
+ $size = [System.Windows.Forms.SystemInformation]::SmallIconSize
108
+ if ($MascotPath -and (Test-Path -LiteralPath $MascotPath)) {
109
+ try {
110
+ $script:mascot = [System.Drawing.Image]::FromFile($MascotPath)
111
+ $small = New-Object System.Drawing.Bitmap($script:mascot, $size.Width, $size.Height)
112
+ $onIcon = [System.Drawing.Icon]::FromHandle($small.GetHicon())
113
+ } catch { $onIcon = $null }
114
+ }
115
+ # Without the mascot, the "on" icon is the VansPod icon with a green dot, so "on" still looks different.
116
+ if (-not $onIcon) {
117
+ try {
118
+ $marked = New-Object System.Drawing.Bitmap($size.Width, $size.Height)
119
+ $graphics = [System.Drawing.Graphics]::FromImage($marked)
120
+ $graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias
121
+ $graphics.DrawIcon($offIcon, (New-Object System.Drawing.Rectangle(0, 0, $size.Width, $size.Height)))
122
+ $dot = [Math]::Max(6, [int]($size.Width / 2))
123
+ $x = $size.Width - $dot
124
+ $y = $size.Height - $dot
125
+ $graphics.FillEllipse([System.Drawing.Brushes]::White, $x, $y, $dot, $dot)
126
+ $graphics.FillEllipse([System.Drawing.Brushes]::LimeGreen, $x + 1, $y + 1, $dot - 2, $dot - 2)
127
+ $graphics.Dispose()
128
+ $onIcon = [System.Drawing.Icon]::FromHandle($marked.GetHicon())
129
+ } catch { $onIcon = [System.Drawing.SystemIcons]::Shield }
130
+ }
131
+ # The third look, for Auto Approval (ADR 0014): the "on" icon with a purple badge and a white bolt.
132
+ $autoIcon = $null
133
+ try {
134
+ $badged = New-Object System.Drawing.Bitmap($size.Width, $size.Height)
135
+ $graphics = [System.Drawing.Graphics]::FromImage($badged)
136
+ $graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias
137
+ $graphics.DrawIcon($onIcon, (New-Object System.Drawing.Rectangle(0, 0, $size.Width, $size.Height)))
138
+ $badge = [Math]::Max(8, [int]($size.Width * 0.6))
139
+ $bx = $size.Width - $badge
140
+ $by = $size.Height - $badge
141
+ $purple = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::FromArgb(124, 58, 237))
142
+ $graphics.FillEllipse([System.Drawing.Brushes]::White, $bx, $by, $badge, $badge)
143
+ $graphics.FillEllipse($purple, $bx + 1, $by + 1, $badge - 2, $badge - 2)
144
+ # The bolt, on a 24 x 24 grid: M13 2 4 14h7l-1 8 9-12h-7z
145
+ $unit = ($badge - 2) / 24.0
146
+ $bolt = @(@(13, 2), @(4, 14), @(11, 14), @(10, 22), @(19, 10), @(12, 10)) | ForEach-Object {
147
+ New-Object System.Drawing.PointF(($bx + 1 + $_[0] * $unit), ($by + 1 + $_[1] * $unit))
148
+ }
149
+ $graphics.FillPolygon([System.Drawing.Brushes]::White, [System.Drawing.PointF[]]$bolt)
150
+ $purple.Dispose()
151
+ $graphics.Dispose()
152
+ $autoIcon = [System.Drawing.Icon]::FromHandle($badged.GetHicon())
153
+ } catch { $autoIcon = [System.Drawing.SystemIcons]::Warning }
154
+
14
155
  $notify = New-Object System.Windows.Forms.NotifyIcon
15
- $notify.Icon = New-Object System.Drawing.Icon($IconPath)
156
+ $notify.Icon = $offIcon
16
157
  $notify.Text = "VansPod"
17
158
  $notify.Visible = $true
18
159
  $notify.add_MouseClick({ if ($_.Button -eq [System.Windows.Forms.MouseButtons]::Left) { & $openPage } }.GetNewClosure())
@@ -20,6 +161,15 @@ $notify.add_MouseClick({ if ($_.Button -eq [System.Windows.Forms.MouseButtons]::
20
161
  $menu = New-Object System.Windows.Forms.ContextMenuStrip
21
162
  $open = $menu.Items.Add("開啟 VansPod")
22
163
  $open.add_Click($openPage)
164
+ $script:accessItem = New-Object System.Windows.Forms.ToolStripMenuItem("允許 Agent 使用這台電腦")
165
+ $script:accessItem.add_Click({ Send-ToVansPod @{ type = 'access'; on = (-not $script:access) } })
166
+ [void]$menu.Items.Add($script:accessItem)
167
+ # 只在 Auto Approval 期間出現。
168
+ $script:stopAutoItem = New-Object System.Windows.Forms.ToolStripMenuItem("停止自動允許")
169
+ $script:stopAutoItem.Visible = $false
170
+ $script:stopAutoItem.add_Click({ Send-ToVansPod @{ type = 'auto_approval_stop' } })
171
+ [void]$menu.Items.Add($script:stopAutoItem)
172
+ [void]$menu.Items.Add((New-Object System.Windows.Forms.ToolStripSeparator))
23
173
  $stop = $menu.Items.Add("結束 VansPod")
24
174
  $stop.add_Click({
25
175
  try {
@@ -35,6 +185,250 @@ $stop.add_Click({
35
185
  })
36
186
  $notify.ContextMenuStrip = $menu
37
187
 
38
- try { [System.Windows.Forms.Application]::Run() }
39
- finally { $notify.Visible = $false; $notify.Icon.Dispose(); $notify.Dispose(); $menu.Dispose() }
188
+ # ---- Approval Popup(確認面板) ----
189
+ $font = New-Object System.Drawing.Font("Microsoft JhengHei UI", 9)
190
+ $bold = New-Object System.Drawing.Font("Microsoft JhengHei UI", 10, [System.Drawing.FontStyle]::Bold)
191
+ $popup = New-Object VansPodTray.PopupForm
192
+ $popup.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
193
+ $popup.ControlBox = $false
194
+ $popup.Text = ""
195
+ $popup.ShowInTaskbar = $false
196
+ $popup.TopMost = $true
197
+ $popup.StartPosition = [System.Windows.Forms.FormStartPosition]::Manual
198
+ $popup.ClientSize = New-Object System.Drawing.Size(400, 318)
199
+ $popup.BackColor = [System.Drawing.Color]::White
200
+ $popup.Font = $font
201
+ $popup.KeyPreview = $true
202
+ $popup.add_KeyDown({ $_.Handled = $true; $_.SuppressKeyPress = $true })
203
+
204
+ $textLeft = 12
205
+ if ($script:mascot) {
206
+ $picture = New-Object System.Windows.Forms.PictureBox
207
+ $picture.Image = $script:mascot
208
+ $picture.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::Zoom
209
+ $picture.SetBounds(12, 10, 44, 44)
210
+ $popup.Controls.Add($picture)
211
+ $textLeft = 64
212
+ }
213
+ $autoColor = [System.Drawing.Color]::FromArgb(124, 58, 237)
214
+ $script:heading = New-Object System.Windows.Forms.Label
215
+ $script:heading.Text = "Agent 想要使用這台電腦"
216
+ $script:heading.Font = $bold
217
+ $script:heading.AutoEllipsis = $true
218
+ $script:heading.SetBounds($textLeft, 10, (388 - $textLeft), 22)
219
+ $popup.Controls.Add($script:heading)
220
+ $script:titleLabel = New-Object System.Windows.Forms.Label
221
+ $script:titleLabel.AutoEllipsis = $true
222
+ $script:titleLabel.SetBounds($textLeft, 34, (388 - $textLeft), 22)
223
+ $popup.Controls.Add($script:titleLabel)
224
+ $script:bodyBox = New-Object System.Windows.Forms.TextBox
225
+ $script:bodyBox.Multiline = $true
226
+ $script:bodyBox.ReadOnly = $true
227
+ $script:bodyBox.TabStop = $false
228
+ $script:bodyBox.ScrollBars = [System.Windows.Forms.ScrollBars]::Vertical
229
+ $script:bodyBox.Font = New-Object System.Drawing.Font("Consolas", 9)
230
+ $script:bodyBox.BackColor = [System.Drawing.Color]::FromArgb(246, 246, 246)
231
+ $script:bodyBox.SetBounds(12, 62, 376, 120)
232
+ $popup.Controls.Add($script:bodyBox)
233
+ $script:whereLabel = New-Object System.Windows.Forms.Label
234
+ $script:whereLabel.AutoEllipsis = $true
235
+ $script:whereLabel.SetBounds(12, 190, 376, 40)
236
+ $popup.Controls.Add($script:whereLabel)
237
+ $script:timeLabel = New-Object System.Windows.Forms.Label
238
+ $script:timeLabel.SetBounds(12, 234, 376, 20)
239
+ $popup.Controls.Add($script:timeLabel)
240
+ $script:denyButton = New-Object VansPodTray.MouseOnlyButton
241
+ $script:denyButton.Text = "拒絕"
242
+ $script:denyButton.SetBounds(176, 266, 100, 36)
243
+ $popup.Controls.Add($script:denyButton)
244
+ $script:approveButton = New-Object VansPodTray.MouseOnlyButton
245
+ $script:approveButton.Text = "允許一次"
246
+ $script:approveButton.Font = $bold
247
+ $script:approveButton.SetBounds(288, 266, 100, 36)
248
+ $popup.Controls.Add($script:approveButton)
249
+ # 自動允許 ▾:選一個時間,先看提醒,按「確定打開」才打開 Auto Approval(ADR 0014)。
250
+ # Auto Approval 期間沒有這個按鈕。
251
+ $script:autoButton = New-Object VansPodTray.MouseOnlyButton
252
+ $script:autoButton.Text = "自動允許 ▾"
253
+ $script:autoButton.SetBounds(12, 266, 110, 36)
254
+ $popup.Controls.Add($script:autoButton)
255
+ $script:autoMenu = New-Object System.Windows.Forms.ContextMenuStrip
256
+ foreach ($choice in @(@('10', '10 分鐘'), @('30', '30 分鐘'), @('until_off', '直到關閉開關'))) {
257
+ $item = New-Object System.Windows.Forms.ToolStripMenuItem($choice[1])
258
+ $item.Tag = $choice[0]
259
+ $item.add_Click({ param($sender) Show-Reminder ([string]$sender.Tag) ([string]$sender.Text) })
260
+ [void]$script:autoMenu.Items.Add($item)
261
+ }
262
+ $script:autoButton.add_Click({ $script:autoMenu.Show($script:autoButton, (New-Object System.Drawing.Point(0, $script:autoButton.Height))) })
263
+
264
+ # 提醒:蓋在 Approval Popup 上面,同樣不搶鍵盤、沒有預設按鈕。
265
+ $script:reminder = New-Object System.Windows.Forms.Panel
266
+ $script:reminder.SetBounds(0, 0, 400, 318)
267
+ $script:reminder.BackColor = [System.Drawing.Color]::White
268
+ $script:reminder.Visible = $false
269
+ $popup.Controls.Add($script:reminder)
270
+ $script:reminderTitle = New-Object System.Windows.Forms.Label
271
+ $script:reminderTitle.Font = $bold
272
+ $script:reminderTitle.ForeColor = $autoColor
273
+ $script:reminderTitle.SetBounds(12, 10, 376, 22)
274
+ $script:reminder.Controls.Add($script:reminderTitle)
275
+ $reminderText = New-Object System.Windows.Forms.Label
276
+ $reminderText.Text = "自動允許期間,Agent 可以不經過你,直接執行指令、寫入檔案和儲存圖片。"
277
+ $reminderText.SetBounds(12, 38, 376, 36)
278
+ $script:reminder.Controls.Add($reminderText)
279
+ $reminderWarn = New-Object System.Windows.Forms.Label
280
+ $reminderWarn.Text = "如果你的 Agent 會讀網頁或電子郵件,那些內容可能藏著給 Agent 的指令。"
281
+ $reminderWarn.Font = New-Object System.Drawing.Font("Microsoft JhengHei UI", 9, [System.Drawing.FontStyle]::Bold)
282
+ $reminderWarn.ForeColor = [System.Drawing.Color]::FromArgb(192, 54, 44)
283
+ $reminderWarn.SetBounds(12, 80, 376, 36)
284
+ $script:reminder.Controls.Add($reminderWarn)
285
+ $reminderExceptions = New-Object System.Windows.Forms.Label
286
+ $reminderExceptions.Text = "下面這兩種情況仍然會問你:`r`n・碰到機密檔案,例如 .env、私密金鑰`r`n・刪除專案資料夾以外的檔案`r`n`r`n排隊中的確認也會一起允許。你隨時可以按「停止自動允許」。"
287
+ $reminderExceptions.ForeColor = [System.Drawing.Color]::FromArgb(90, 90, 90)
288
+ $reminderExceptions.SetBounds(12, 122, 376, 132)
289
+ $script:reminder.Controls.Add($reminderExceptions)
290
+ $reminderCancel = New-Object VansPodTray.MouseOnlyButton
291
+ $reminderCancel.Text = "取消"
292
+ $reminderCancel.SetBounds(176, 266, 100, 36)
293
+ $reminderCancel.add_Click({ Hide-Reminder })
294
+ $script:reminder.Controls.Add($reminderCancel)
295
+ $reminderOk = New-Object VansPodTray.MouseOnlyButton
296
+ $reminderOk.Text = "確定打開"
297
+ $reminderOk.Font = $bold
298
+ $reminderOk.SetBounds(288, 266, 100, 36)
299
+ $reminderOk.add_Click({
300
+ if ($script:reminderDuration) { Send-ToVansPod @{ type = 'auto_approval_start'; duration = $script:reminderDuration } }
301
+ Hide-Reminder
302
+ })
303
+ $script:reminder.Controls.Add($reminderOk)
304
+ $script:reminderDuration = $null
305
+ # 沒有預設按鈕:AcceptButton 與 CancelButton 都不設定。
306
+
307
+ $script:access = $false
308
+ $script:shownId = $null
309
+ $script:deadline = [DateTime]::MinValue
310
+ $script:waiting = 0
311
+
312
+ function Set-Buttons([bool]$enabled) {
313
+ $script:denyButton.Enabled = $enabled
314
+ $script:approveButton.Enabled = $enabled
315
+ }
316
+ function Update-Time {
317
+ $left = [Math]::Max(0, [int][Math]::Ceiling(($script:deadline - [DateTime]::UtcNow).TotalSeconds))
318
+ $text = "剩下 {0}:{1:00}" -f [Math]::Floor($left / 60), ($left % 60)
319
+ if ($script:waiting -gt 0) { $text += " 還有 $($script:waiting) 個在排隊" }
320
+ $script:timeLabel.Text = $text
321
+ if ($left -le 0) { Set-Buttons $false }
322
+ }
323
+ function Send-Decision([string]$decision) {
324
+ if (-not $script:shownId) { return }
325
+ Set-Buttons $false
326
+ Send-ToVansPod @{ type = 'decide'; id = $script:shownId; decision = $decision }
327
+ }
328
+ $script:approveButton.add_Click({ Send-Decision 'approve' })
329
+ $script:denyButton.add_Click({ Send-Decision 'deny' })
330
+
331
+ function Show-Reminder([string]$duration, [string]$label) {
332
+ $script:reminderDuration = $duration
333
+ $script:reminderTitle.Text = "打開自動允許($label)"
334
+ $script:reminder.Visible = $true
335
+ $script:reminder.BringToFront()
336
+ }
337
+ function Hide-Reminder {
338
+ $script:reminderDuration = $null
339
+ $script:reminder.Visible = $false
340
+ }
40
341
 
342
+ # Auto Approval:$script:autoOn、結束時間($null 代表直到關閉開關)。
343
+ $script:autoOn = $false
344
+ $script:autoDeadline = $null
345
+ function Update-TrayText {
346
+ if (-not $script:access) { $notify.Text = "VansPod"; return }
347
+ if (-not $script:autoOn) { $notify.Text = "VansPod:Agent 可以使用這台電腦"; return }
348
+ if ($null -eq $script:autoDeadline) { $notify.Text = "VansPod:自動允許中,直到關閉開關"; return }
349
+ $left = [Math]::Max(0, [int][Math]::Ceiling(($script:autoDeadline - [DateTime]::UtcNow).TotalMinutes))
350
+ $notify.Text = "VansPod:自動允許中,剩 $left 分鐘"
351
+ }
352
+
353
+ function Show-State($state) {
354
+ $script:access = [bool]$state.access
355
+ $script:accessItem.Checked = $script:access
356
+ $auto = $state.autoApproval
357
+ $script:autoOn = [bool]$auto
358
+ if ($auto -and $null -ne $auto.secondsLeft) { $script:autoDeadline = [DateTime]::UtcNow.AddSeconds([int]$auto.secondsLeft) }
359
+ else { $script:autoDeadline = $null }
360
+ $script:stopAutoItem.Visible = $script:autoOn
361
+ if (-not $script:access) { $notify.Icon = $offIcon }
362
+ elseif ($script:autoOn) { $notify.Icon = $autoIcon }
363
+ else { $notify.Icon = $onIcon }
364
+ Update-TrayText
365
+ # Auto Approval 期間沒有「自動允許 ▾」,提醒也收起來。
366
+ $script:autoButton.Visible = -not $script:autoOn
367
+ if ($script:autoOn) { Hide-Reminder }
368
+ $approval = $state.approval
369
+ if (-not $approval) {
370
+ $script:shownId = $null
371
+ Hide-Reminder
372
+ $popup.Hide()
373
+ return
374
+ }
375
+ # Auto Approval 期間只有例外會顯示:標題寫出為什麼仍要確認。
376
+ if ($script:autoOn -and $approval.exception) {
377
+ $script:heading.Text = "自動允許期間仍要確認:$($approval.exception)"
378
+ $script:heading.ForeColor = $autoColor
379
+ } else {
380
+ $script:heading.Text = "Agent 想要使用這台電腦"
381
+ $script:heading.ForeColor = [System.Drawing.SystemColors]::ControlText
382
+ }
383
+ $script:waiting = [int]$approval.waiting
384
+ if ($approval.id -ne $script:shownId) {
385
+ $script:shownId = [string]$approval.id
386
+ $script:deadline = [DateTime]::UtcNow.AddSeconds([int]$approval.secondsLeft)
387
+ $script:titleLabel.Text = [string]$approval.title
388
+ $script:bodyBox.Text = ([string]$approval.body) -replace "`r?`n", "`r`n"
389
+ $script:whereLabel.Text = "電腦:$($approval.device)`r`n資料夾:$($approval.folder)"
390
+ Set-Buttons $true
391
+ }
392
+ Update-Time
393
+ if (-not $popup.Visible) {
394
+ $popup.Location = [VansPodTray.TrayPlace]::PopupLocation([VansPodTray.TrayPlace]::IconBounds($notify), $popup.Size)
395
+ $popup.Show()
396
+ }
397
+ }
398
+
399
+ function Receive-Line([string]$line) {
400
+ try { $message = $line | ConvertFrom-Json } catch { return }
401
+ if ($message.type -eq 'state') { Show-State $message }
402
+ elseif ($message.type -eq 'refused') { $notify.ShowBalloonTip(5000, 'VansPod', [string]$message.message, [System.Windows.Forms.ToolTipIcon]::Info) }
403
+ }
404
+
405
+ # 讀取在背景進行;計時器在 UI 執行緒上取出讀到的每一行。VansPod 關掉管線時,系統匣一起結束。
406
+ $script:pending = $script:reader.ReadLineAsync()
407
+ $pipeTimer = New-Object System.Windows.Forms.Timer
408
+ $pipeTimer.Interval = 100
409
+ $pipeTimer.add_Tick({
410
+ while ($script:pending -and $script:pending.IsCompleted) {
411
+ $line = $null
412
+ try { $line = $script:pending.Result } catch { }
413
+ if ($null -eq $line) { $script:pending = $null; [System.Windows.Forms.Application]::Exit(); return }
414
+ Receive-Line $line
415
+ $script:pending = $script:reader.ReadLineAsync()
416
+ }
417
+ })
418
+ $pipeTimer.Start()
419
+ $clock = New-Object System.Windows.Forms.Timer
420
+ $clock.Interval = 1000
421
+ $clock.add_Tick({
422
+ if ($script:shownId) { Update-Time }
423
+ if ($script:autoOn) { Update-TrayText }
424
+ })
425
+ $clock.Start()
426
+
427
+ try { [System.Windows.Forms.Application]::Run() }
428
+ finally {
429
+ $pipeTimer.Stop(); $clock.Stop()
430
+ $popup.Dispose()
431
+ $notify.Visible = $false; $notify.Dispose(); $menu.Dispose(); $script:autoMenu.Dispose()
432
+ if ($offIcon) { $offIcon.Dispose() }
433
+ if ($script:mascot) { $script:mascot.Dispose() }
434
+ }