vanspod 0.4.11 → 0.4.15

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,135 @@ 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
+
14
132
  $notify = New-Object System.Windows.Forms.NotifyIcon
15
- $notify.Icon = New-Object System.Drawing.Icon($IconPath)
133
+ $notify.Icon = $offIcon
16
134
  $notify.Text = "VansPod"
17
135
  $notify.Visible = $true
18
136
  $notify.add_MouseClick({ if ($_.Button -eq [System.Windows.Forms.MouseButtons]::Left) { & $openPage } }.GetNewClosure())
@@ -20,6 +138,10 @@ $notify.add_MouseClick({ if ($_.Button -eq [System.Windows.Forms.MouseButtons]::
20
138
  $menu = New-Object System.Windows.Forms.ContextMenuStrip
21
139
  $open = $menu.Items.Add("開啟 VansPod")
22
140
  $open.add_Click($openPage)
141
+ $script:accessItem = New-Object System.Windows.Forms.ToolStripMenuItem("讓 Agent Builder 使用這台電腦")
142
+ $script:accessItem.add_Click({ Send-ToVansPod @{ type = 'access'; on = (-not $script:access) } })
143
+ [void]$menu.Items.Add($script:accessItem)
144
+ [void]$menu.Items.Add((New-Object System.Windows.Forms.ToolStripSeparator))
23
145
  $stop = $menu.Items.Add("結束 VansPod")
24
146
  $stop.add_Click({
25
147
  try {
@@ -35,6 +157,148 @@ $stop.add_Click({
35
157
  })
36
158
  $notify.ContextMenuStrip = $menu
37
159
 
38
- try { [System.Windows.Forms.Application]::Run() }
39
- finally { $notify.Visible = $false; $notify.Icon.Dispose(); $notify.Dispose(); $menu.Dispose() }
160
+ # ---- Approval Popup(確認面板) ----
161
+ $font = New-Object System.Drawing.Font("Microsoft JhengHei UI", 9)
162
+ $bold = New-Object System.Drawing.Font("Microsoft JhengHei UI", 10, [System.Drawing.FontStyle]::Bold)
163
+ $popup = New-Object VansPodTray.PopupForm
164
+ $popup.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
165
+ $popup.ControlBox = $false
166
+ $popup.Text = ""
167
+ $popup.ShowInTaskbar = $false
168
+ $popup.TopMost = $true
169
+ $popup.StartPosition = [System.Windows.Forms.FormStartPosition]::Manual
170
+ $popup.ClientSize = New-Object System.Drawing.Size(400, 318)
171
+ $popup.BackColor = [System.Drawing.Color]::White
172
+ $popup.Font = $font
173
+ $popup.KeyPreview = $true
174
+ $popup.add_KeyDown({ $_.Handled = $true; $_.SuppressKeyPress = $true })
175
+
176
+ $textLeft = 12
177
+ if ($script:mascot) {
178
+ $picture = New-Object System.Windows.Forms.PictureBox
179
+ $picture.Image = $script:mascot
180
+ $picture.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::Zoom
181
+ $picture.SetBounds(12, 10, 44, 44)
182
+ $popup.Controls.Add($picture)
183
+ $textLeft = 64
184
+ }
185
+ $heading = New-Object System.Windows.Forms.Label
186
+ $heading.Text = "Agent Builder 想要使用這台電腦"
187
+ $heading.Font = $bold
188
+ $heading.SetBounds($textLeft, 10, (388 - $textLeft), 22)
189
+ $popup.Controls.Add($heading)
190
+ $script:titleLabel = New-Object System.Windows.Forms.Label
191
+ $script:titleLabel.AutoEllipsis = $true
192
+ $script:titleLabel.SetBounds($textLeft, 34, (388 - $textLeft), 22)
193
+ $popup.Controls.Add($script:titleLabel)
194
+ $script:bodyBox = New-Object System.Windows.Forms.TextBox
195
+ $script:bodyBox.Multiline = $true
196
+ $script:bodyBox.ReadOnly = $true
197
+ $script:bodyBox.TabStop = $false
198
+ $script:bodyBox.ScrollBars = [System.Windows.Forms.ScrollBars]::Vertical
199
+ $script:bodyBox.Font = New-Object System.Drawing.Font("Consolas", 9)
200
+ $script:bodyBox.BackColor = [System.Drawing.Color]::FromArgb(246, 246, 246)
201
+ $script:bodyBox.SetBounds(12, 62, 376, 120)
202
+ $popup.Controls.Add($script:bodyBox)
203
+ $script:whereLabel = New-Object System.Windows.Forms.Label
204
+ $script:whereLabel.AutoEllipsis = $true
205
+ $script:whereLabel.SetBounds(12, 190, 376, 40)
206
+ $popup.Controls.Add($script:whereLabel)
207
+ $script:timeLabel = New-Object System.Windows.Forms.Label
208
+ $script:timeLabel.SetBounds(12, 234, 376, 20)
209
+ $popup.Controls.Add($script:timeLabel)
210
+ $script:denyButton = New-Object VansPodTray.MouseOnlyButton
211
+ $script:denyButton.Text = "拒絕"
212
+ $script:denyButton.SetBounds(176, 266, 100, 36)
213
+ $popup.Controls.Add($script:denyButton)
214
+ $script:approveButton = New-Object VansPodTray.MouseOnlyButton
215
+ $script:approveButton.Text = "允許"
216
+ $script:approveButton.Font = $bold
217
+ $script:approveButton.SetBounds(288, 266, 100, 36)
218
+ $popup.Controls.Add($script:approveButton)
219
+ # 沒有預設按鈕:AcceptButton 與 CancelButton 都不設定。
220
+
221
+ $script:access = $false
222
+ $script:shownId = $null
223
+ $script:deadline = [DateTime]::MinValue
224
+ $script:waiting = 0
225
+
226
+ function Set-Buttons([bool]$enabled) {
227
+ $script:denyButton.Enabled = $enabled
228
+ $script:approveButton.Enabled = $enabled
229
+ }
230
+ function Update-Time {
231
+ $left = [Math]::Max(0, [int][Math]::Ceiling(($script:deadline - [DateTime]::UtcNow).TotalSeconds))
232
+ $text = "剩下 {0}:{1:00}" -f [Math]::Floor($left / 60), ($left % 60)
233
+ if ($script:waiting -gt 0) { $text += " 還有 $($script:waiting) 個在排隊" }
234
+ $script:timeLabel.Text = $text
235
+ if ($left -le 0) { Set-Buttons $false }
236
+ }
237
+ function Send-Decision([string]$decision) {
238
+ if (-not $script:shownId) { return }
239
+ Set-Buttons $false
240
+ Send-ToVansPod @{ type = 'decide'; id = $script:shownId; decision = $decision }
241
+ }
242
+ $script:approveButton.add_Click({ Send-Decision 'approve' })
243
+ $script:denyButton.add_Click({ Send-Decision 'deny' })
40
244
 
245
+ function Show-State($state) {
246
+ $script:access = [bool]$state.access
247
+ $script:accessItem.Checked = $script:access
248
+ if ($script:access) { $notify.Icon = $onIcon; $notify.Text = "VansPod:Agent Builder 可以使用這台電腦" }
249
+ else { $notify.Icon = $offIcon; $notify.Text = "VansPod" }
250
+ $approval = $state.approval
251
+ if (-not $approval) {
252
+ $script:shownId = $null
253
+ $popup.Hide()
254
+ return
255
+ }
256
+ $script:waiting = [int]$approval.waiting
257
+ if ($approval.id -ne $script:shownId) {
258
+ $script:shownId = [string]$approval.id
259
+ $script:deadline = [DateTime]::UtcNow.AddSeconds([int]$approval.secondsLeft)
260
+ $script:titleLabel.Text = [string]$approval.title
261
+ $script:bodyBox.Text = ([string]$approval.body) -replace "`r?`n", "`r`n"
262
+ $script:whereLabel.Text = "電腦:$($approval.device)`r`n資料夾:$($approval.folder)"
263
+ Set-Buttons $true
264
+ }
265
+ Update-Time
266
+ if (-not $popup.Visible) {
267
+ $popup.Location = [VansPodTray.TrayPlace]::PopupLocation([VansPodTray.TrayPlace]::IconBounds($notify), $popup.Size)
268
+ $popup.Show()
269
+ }
270
+ }
271
+
272
+ function Receive-Line([string]$line) {
273
+ try { $message = $line | ConvertFrom-Json } catch { return }
274
+ if ($message.type -eq 'state') { Show-State $message }
275
+ elseif ($message.type -eq 'refused') { $notify.ShowBalloonTip(5000, 'VansPod', [string]$message.message, [System.Windows.Forms.ToolTipIcon]::Info) }
276
+ }
277
+
278
+ # 讀取在背景進行;計時器在 UI 執行緒上取出讀到的每一行。VansPod 關掉管線時,系統匣一起結束。
279
+ $script:pending = $script:reader.ReadLineAsync()
280
+ $pipeTimer = New-Object System.Windows.Forms.Timer
281
+ $pipeTimer.Interval = 100
282
+ $pipeTimer.add_Tick({
283
+ while ($script:pending -and $script:pending.IsCompleted) {
284
+ $line = $null
285
+ try { $line = $script:pending.Result } catch { }
286
+ if ($null -eq $line) { $script:pending = $null; [System.Windows.Forms.Application]::Exit(); return }
287
+ Receive-Line $line
288
+ $script:pending = $script:reader.ReadLineAsync()
289
+ }
290
+ })
291
+ $pipeTimer.Start()
292
+ $clock = New-Object System.Windows.Forms.Timer
293
+ $clock.Interval = 1000
294
+ $clock.add_Tick({ if ($script:shownId) { Update-Time } })
295
+ $clock.Start()
296
+
297
+ try { [System.Windows.Forms.Application]::Run() }
298
+ finally {
299
+ $pipeTimer.Stop(); $clock.Stop()
300
+ $popup.Dispose()
301
+ $notify.Visible = $false; $notify.Dispose(); $menu.Dispose()
302
+ if ($offIcon) { $offIcon.Dispose() }
303
+ if ($script:mascot) { $script:mascot.Dispose() }
304
+ }