dsh-reminder-notify 0.10.2 → 0.11.0

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.
@@ -5,13 +5,20 @@
5
5
  SmartScreen / 杀软拦。本脚本用系统自带组件画同一个提醒窗口,仓库里不出现任何二进制,
6
6
  也不需要买代码签名证书、不需要编译环境。
7
7
 
8
- 行为(对齐原 exe):
8
+ 行为(对齐原 exe,并加了 0.11 的新东西):
9
9
  · 右下角(可换角)悬浮卡片,圆角、无边框、置顶
10
10
  · 不抢焦点(ShowWithoutActivation + WS_EX_NOACTIVATE),不进 Alt+Tab
11
- · 按秒自动淡出关闭;点一下立即关
12
- · 可选播放 wav(System.Media.SoundPlayer,异步,不阻塞)
13
-
14
- 参数:-Title -Msg -Seconds -Pos(br|tr|tl|bc|center) -Audio -Accent -NoSound -Log
11
+ · 按秒自动淡出关闭;点空白处立即关
12
+ · 可选播放 wav / mp3(System.Media.SoundPlayer / WMP COM,异步)
13
+ · 卡片上有两个按钮:「知道了」「N 分钟后再提醒」(-NoButtons 可关)
14
+ · 关掉时把用户的选择写进 -ResultPath,插件据此决定要不要重排
15
+ · -Speak:用系统语音把这句话念出来(System.Speech,异步)
16
+ · -AutoCompact:检测到前台是全屏程序时自动缩成小条,不打扰
17
+ · -QueueLeft:还有几条在排队,卡片底部显示「还有 N 条」
18
+
19
+ 参数:-Title -Msg -Seconds -Pos(br|tr|tl|bc|center) -Audio -Accent -Background -Icon
20
+ -NoSound -Speak -ResultPath -SnoozeMinutes -NoButtons -Compact -AutoCompact
21
+ -QueueLeft -Preview -Log
15
22
  #>
16
23
  [CmdletBinding()]
17
24
  param(
@@ -25,6 +32,13 @@ param(
25
32
  [string]$Background = '',
26
33
  [string]$Icon = '',
27
34
  [switch]$NoSound,
35
+ [string]$Speak = '',
36
+ [string]$ResultPath = '',
37
+ [int]$SnoozeMinutes = 10,
38
+ [switch]$NoButtons,
39
+ [switch]$Compact,
40
+ [switch]$AutoCompact,
41
+ [int]$QueueLeft = 0,
28
42
  [switch]$Preview,
29
43
  [string]$Log = ''
30
44
  )
@@ -49,11 +63,22 @@ using System.Drawing;
49
63
  using System.Runtime.InteropServices;
50
64
  using System.Windows.Forms;
51
65
 
52
- public sealed class ReminderWindow : Form
66
+ public struct DshRect { public int Left, Top, Right, Bottom; }
67
+
68
+ public static class DshWin
53
69
  {
54
70
  [DllImport("user32.dll")]
55
- private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
71
+ public static extern IntPtr GetForegroundWindow();
72
+
73
+ [DllImport("user32.dll")]
74
+ public static extern bool GetWindowRect(IntPtr hWnd, out DshRect rect);
75
+
76
+ [DllImport("user32.dll")]
77
+ public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
78
+ }
56
79
 
80
+ public sealed class ReminderWindow : Form
81
+ {
57
82
  private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
58
83
  private const uint SWP_NOSIZE = 0x0001;
59
84
  private const uint SWP_NOMOVE = 0x0002;
@@ -84,16 +109,38 @@ public sealed class ReminderWindow : Form
84
109
 
85
110
  public void RaiseWithoutActivating()
86
111
  {
87
- SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0,
88
- SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_SHOWWINDOW);
112
+ DshWin.SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0,
113
+ SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_SHOWWINDOW);
89
114
  }
90
115
  }
91
116
  '@
92
117
 
93
118
  Add-Type -TypeDefinition $source -ReferencedAssemblies System.Windows.Forms, System.Drawing
94
119
 
120
+ # ---- 全屏检测(-AutoCompact 时用来降级成小条) -------------------------------
121
+ function Test-ForegroundFullscreen {
122
+ try {
123
+ $hwnd = [DshWin]::GetForegroundWindow()
124
+ if ($hwnd -eq [IntPtr]::Zero) { return $false }
125
+ $r = New-Object DshRect
126
+ if (-not [DshWin]::GetWindowRect($hwnd, [ref]$r)) { return $false }
127
+ $screen = [System.Windows.Forms.Screen]::FromHandle($hwnd)
128
+ $b = $screen.Bounds
129
+ $winW = $r.Right - $r.Left
130
+ $winH = $r.Bottom - $r.Top
131
+ # 窗口盖住整块屏幕(允许 2px 误差)且没有可见边框留白,就当作全屏
132
+ return (($winW -ge ($b.Width - 2)) -and ($winH -ge ($b.Height - 2)))
133
+ } catch {
134
+ return $false
135
+ }
136
+ }
137
+
138
+ if ($AutoCompact -and (Test-ForegroundFullscreen)) {
139
+ $Compact = $true
140
+ Write-Log 'auto-compact (foreground is fullscreen)'
141
+ }
142
+
95
143
  # ---- 外观常量 ---------------------------------------------------------------
96
- $width = 380
97
144
  $radius = 12
98
145
  $padX = 16
99
146
  $accentWidth = 4
@@ -102,14 +149,28 @@ $lineColor = [System.Drawing.Color]::FromArgb(48, 54, 66) # 细边框
102
149
  $titleColor = [System.Drawing.Color]::FromArgb(255, 255, 255)
103
150
  $bodyColor = [System.Drawing.Color]::FromArgb(201, 209, 217)
104
151
  $mutedColor = [System.Drawing.Color]::FromArgb(139, 148, 158)
152
+ $btnColor = [System.Drawing.Color]::FromArgb(45, 52, 63)
153
+ $btnHoverColor = [System.Drawing.Color]::FromArgb(62, 71, 85)
105
154
  # 允许调用方传 "2ECC71"(不带 #):从 node 里 spawn 时,# 开头的 token 会被
106
155
  # PowerShell 当成注释,导致 -Accent 后面没有值。这里统一补上 #。
107
156
  if ($Accent -and -not $Accent.StartsWith('#')) { $Accent = '#' + $Accent }
108
157
  $accentColor = [System.Drawing.ColorTranslator]::FromHtml($Accent)
109
158
 
110
- $titleFont = New-Object System.Drawing.Font('Microsoft YaHei UI', 11.5, [System.Drawing.FontStyle]::Bold, [System.Drawing.GraphicsUnit]::Point)
111
- $bodyFont = New-Object System.Drawing.Font('Microsoft YaHei UI', 9.5, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point)
159
+ $showButtons = -not $NoButtons
160
+ if ($Compact) {
161
+ # 小条模式:正文和按钮都不画,只留一行标题 + 剩余条数
162
+ $width = 300
163
+ $showButtons = $false
164
+ } else {
165
+ $width = 380
166
+ }
167
+
168
+ $titleSize = 11.5
169
+ $bodySize = 9.5
170
+ $titleFont = New-Object System.Drawing.Font('Microsoft YaHei UI', $titleSize, [System.Drawing.FontStyle]::Bold, [System.Drawing.GraphicsUnit]::Point)
171
+ $bodyFont = New-Object System.Drawing.Font('Microsoft YaHei UI', $bodySize, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point)
112
172
  $hintFont = New-Object System.Drawing.Font('Microsoft YaHei UI', 8, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point)
173
+ $btnFont = New-Object System.Drawing.Font('Microsoft YaHei UI', 8.5, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point)
113
174
 
114
175
  # ---- 先量文字,再定窗口高度(正文自动折行) --------------------------------
115
176
  $measureBmp = New-Object System.Drawing.Bitmap 8, 8
@@ -144,18 +205,41 @@ if (-not [string]::IsNullOrEmpty($Icon) -and (Test-Path -LiteralPath $Icon)) {
144
205
  }
145
206
  $hasIcon = $iconBitmap -ne $null
146
207
  if ($hasIcon) { $textWidth = $textWidth - $iconSize - $iconGap }
147
- $titleSize = $measureGfx.MeasureString($Title, $titleFont, $textWidth)
148
- $bodyText = if ([string]::IsNullOrWhiteSpace($Msg)) { '' } else { $Msg }
149
- $bodySize = if ($bodyText -eq '') { New-Object System.Drawing.SizeF 0, 0 } else { $measureGfx.MeasureString($bodyText, $bodyFont, $textWidth) }
208
+ $titleSizeF = $measureGfx.MeasureString($Title, $titleFont, $textWidth)
209
+ $bodyText = if ($Compact -or [string]::IsNullOrWhiteSpace($Msg)) { '' } else { $Msg }
210
+ $bodySizeF = if ($bodyText -eq '') { New-Object System.Drawing.SizeF 0, 0 } else { $measureGfx.MeasureString($bodyText, $bodyFont, $textWidth) }
211
+
212
+ # ---- 按钮尺寸(先量出来,才知道窗口要多高) ---------------------------------
213
+ $buttonHeight = 26
214
+ $buttonGap = 8
215
+ $okText = '知道了'
216
+ $snoozeText = ('{0} 分钟后再提醒' -f [Math]::Max(1, $SnoozeMinutes))
217
+ $okSize = $measureGfx.MeasureString($okText, $btnFont)
218
+ $snoozeSize = $measureGfx.MeasureString($snoozeText, $btnFont)
219
+ $okWidth = [int][Math]::Ceiling($okSize.Width) + 20
220
+ $snoozeWidth = [int][Math]::Ceiling($snoozeSize.Width) + 20
150
221
  $measureGfx.Dispose()
151
222
  $measureBmp.Dispose()
152
223
 
153
- $headerHeight = [int][Math]::Ceiling($titleSize.Height)
154
- $bodyHeight = [int][Math]::Ceiling($bodySize.Height)
224
+ $headerHeight = [int][Math]::Ceiling($titleSizeF.Height)
225
+ $bodyHeight = [int][Math]::Ceiling($bodySizeF.Height)
155
226
  $hintHeight = 16
156
- $height = $padX + $headerHeight + $(if ($bodyHeight -gt 0) { 4 + $bodyHeight } else { 0 }) + 6 + $hintHeight + $padX
227
+ $buttonsHeight = if ($showButtons) { $buttonHeight + 10 } else { 0 }
228
+ $height = $padX + $headerHeight + $(if ($bodyHeight -gt 0) { 4 + $bodyHeight } else { 0 }) + 6 + $hintHeight + $buttonsHeight + $padX
157
229
  if ($height -lt 96) { $height = 96 }
158
- if ($height -gt 260) { $height = 260 }
230
+ if ($height -gt 320) { $height = 320 }
231
+
232
+ # 按钮矩形(相对窗口,右下角排列)
233
+ $script:btnRects = @{}
234
+ $script:hover = ''
235
+ if ($showButtons) {
236
+ $btnRight = $width - $padX
237
+ $btnTop = $height - $padX - $buttonHeight
238
+ $snoozeRect = New-Object System.Drawing.Rectangle ($btnRight - $snoozeWidth), $btnTop, $snoozeWidth, $buttonHeight
239
+ $okRect = New-Object System.Drawing.Rectangle ($snoozeRect.Left - $buttonGap - $okWidth), $btnTop, $okWidth, $buttonHeight
240
+ $script:btnRects['snooze'] = $snoozeRect
241
+ $script:btnRects['ok'] = $okRect
242
+ }
159
243
 
160
244
  # ---- 定位:光标所在屏幕的工作区 ---------------------------------------------
161
245
  $screen = [System.Windows.Forms.Screen]::FromPoint([System.Windows.Forms.Cursor]::Position)
@@ -230,7 +314,7 @@ $form.Add_Paint({
230
314
  $pen.Dispose()
231
315
 
232
316
  $textLeft = $accentWidth + $padX
233
- if ($hasIcon) {
317
+ if ($hasIcon -and -not $Compact) {
234
318
  $g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
235
319
  $iconRect = New-Object System.Drawing.Rectangle $textLeft, ($padX - 1), $iconSize, $iconSize
236
320
  $g.DrawImage($iconBitmap, $iconRect)
@@ -247,17 +331,49 @@ $form.Add_Paint({
247
331
  $bodyBrush.Dispose()
248
332
  }
249
333
 
250
- $hint = ('{0} 秒后自动关闭 · 点一下关掉' -f $Seconds)
334
+ $hint = ('{0} 秒后自动关闭' -f $Seconds)
335
+ if ($QueueLeft -gt 0) { $hint = $hint + (' · 还有 {0} 条' -f $QueueLeft) }
336
+ if (-not $showButtons) { $hint = $hint + ' · 点一下关掉' }
251
337
  $hintBrush = New-Object System.Drawing.SolidBrush $mutedColor
252
- $g.DrawString($hint, $hintFont, $hintBrush, $textLeft, ($height - $padX - 12))
338
+ $g.DrawString($hint, $hintFont, $hintBrush, $textLeft, ($height - $padX - 12 - $buttonsHeight))
253
339
  $hintBrush.Dispose()
340
+
341
+ # 按钮
342
+ if ($showButtons) {
343
+ foreach ($key in @('ok', 'snooze')) {
344
+ $rect = $script:btnRects[$key]
345
+ if ($rect -eq $null) { continue }
346
+ $fill = if ($script:hover -eq $key) { $btnHoverColor } else { $btnColor }
347
+ $btnPath = New-Object System.Drawing.Drawing2D.GraphicsPath
348
+ $r2 = 8
349
+ $btnPath.AddArc($rect.X, $rect.Y, $r2 * 2, $r2 * 2, 180, 90)
350
+ $btnPath.AddArc($rect.Right - $r2 * 2, $rect.Y, $r2 * 2, $r2 * 2, 270, 90)
351
+ $btnPath.AddArc($rect.Right - $r2 * 2, $rect.Bottom - $r2 * 2, $r2 * 2, $r2 * 2, 0, 90)
352
+ $btnPath.AddArc($rect.X, $rect.Bottom - $r2 * 2, $r2 * 2, $r2 * 2, 90, 90)
353
+ $btnPath.CloseFigure()
354
+ $btnBrush = New-Object System.Drawing.SolidBrush $fill
355
+ $g.FillPath($btnBrush, $btnPath)
356
+ $btnBrush.Dispose()
357
+ $btnPath.Dispose()
358
+ $label = if ($key -eq 'ok') { $okText } else { $snoozeText }
359
+ $fmt = New-Object System.Drawing.StringFormat
360
+ $fmt.Alignment = [System.Drawing.StringAlignment]::Center
361
+ $fmt.LineAlignment = [System.Drawing.StringAlignment]::Center
362
+ $labelBrush = New-Object System.Drawing.SolidBrush $bodyColor
363
+ $g.DrawString($label, $btnFont, $labelBrush, (New-Object System.Drawing.RectangleF $rect.X, $rect.Y, $rect.Width, $rect.Height), $fmt)
364
+ $labelBrush.Dispose()
365
+ $fmt.Dispose()
366
+ }
367
+ }
254
368
  })
255
369
 
256
370
  # ---- 淡入 / 停留 / 淡出 ------------------------------------------------------
257
371
  $script:phase = 'in'
258
372
  $script:ticks = 0
373
+ $script:action = 'timeout'
259
374
  $fadeTicks = 12 # ~180ms
260
375
  $script:deadline = (Get-Date).AddSeconds([Math]::Max(1, $Seconds))
376
+ $script:holdStart = Get-Date
261
377
 
262
378
  $timer = New-Object System.Windows.Forms.Timer
263
379
  $timer.Interval = 10
@@ -267,17 +383,21 @@ $timer.Add_Tick({
267
383
  switch ($script:phase) {
268
384
  'in' {
269
385
  $form.Opacity = [Math]::Min(1.0, $script:ticks / $fadeTicks)
270
- if ($script:ticks -ge $fadeTicks) { $script:phase = 'hold'; $script:ticks = 0 }
386
+ if ($script:ticks -ge $fadeTicks) { $script:phase = 'hold'; $script:ticks = 0; $script:holdStart = Get-Date }
271
387
  }
272
388
  'hold' {
273
389
  # 用真实时钟而不是 tick 数:WinForms 计时器最细约 15ms,数 tick 会明显偏长
274
- if ((Get-Date) -ge $script:deadline) { $script:phase = 'out'; $script:ticks = 0 }
390
+ if ((Get-Date) -ge $script:deadline) {
391
+ $script:phase = 'out'
392
+ $script:ticks = 0
393
+ if ($script:action -eq 'timeout') { $script:action = 'timeout' }
394
+ }
275
395
  }
276
396
  'out' {
277
397
  $form.Opacity = [Math]::Max(0.0, 1.0 - ($script:ticks / $fadeTicks))
278
398
  if ($script:ticks -ge $fadeTicks) {
279
399
  $timer.Stop()
280
- Write-Log 'closed'
400
+ Write-Log ('closed action=' + $script:action)
281
401
  $form.Close()
282
402
  [System.Windows.Forms.Application]::Exit()
283
403
  }
@@ -285,11 +405,40 @@ $timer.Add_Tick({
285
405
  }
286
406
  })
287
407
 
288
- $form.Add_Click({
289
- if ($script:phase -ne 'out') {
290
- $script:phase = 'out'
291
- $script:ticks = 0
292
- Write-Log 'clicked'
408
+ function Close-With([string]$action) {
409
+ if ($script:phase -eq 'out') { return }
410
+ $script:action = $action
411
+ $script:phase = 'out'
412
+ $script:ticks = 0
413
+ Write-Log ('action ' + $action)
414
+ }
415
+
416
+ $form.Add_MouseDown({
417
+ param($sender, $e)
418
+ if ($script:phase -eq 'out') { return }
419
+ foreach ($key in @('ok', 'snooze')) {
420
+ $rect = $script:btnRects[$key]
421
+ if ($rect -ne $null -and $rect.Contains($e.X, $e.Y)) {
422
+ Close-With $key
423
+ return
424
+ }
425
+ }
426
+ Close-With 'dismiss'
427
+ })
428
+
429
+ $form.Add_MouseMove({
430
+ param($sender, $e)
431
+ if (-not $showButtons) { return }
432
+ $next = ''
433
+ foreach ($key in @('ok', 'snooze')) {
434
+ $rect = $script:btnRects[$key]
435
+ if ($rect -ne $null -and $rect.Contains($e.X, $e.Y)) { $next = $key }
436
+ }
437
+ if ($next -ne $script:hover) {
438
+ $script:hover = $next
439
+ if ($next -eq '') { $form.Cursor = [System.Windows.Forms.Cursors]::Default }
440
+ else { $form.Cursor = [System.Windows.Forms.Cursors]::Hand }
441
+ $form.Invalidate()
293
442
  }
294
443
  })
295
444
 
@@ -317,6 +466,20 @@ if (-not $NoSound -and -not [string]::IsNullOrEmpty($Audio) -and (Test-Path -Lit
317
466
  }
318
467
  }
319
468
 
469
+ # ---- 语音朗读(System.Speech,异步,缺语音包就跳过) -------------------------
470
+ $script:synth = $null
471
+ if (-not [string]::IsNullOrWhiteSpace($Speak)) {
472
+ try {
473
+ Add-Type -AssemblyName System.Speech
474
+ $script:synth = New-Object System.Speech.Synthesis.SpeechSynthesizer
475
+ $script:synth.SpeakAsync($Speak)
476
+ Write-Log ('speak ' + $Speak)
477
+ } catch {
478
+ $script:synth = $null
479
+ Write-Log ('speak-failed ' + $_.Exception.Message)
480
+ }
481
+ }
482
+
320
483
  # ---- 试听:只放声音,不弹卡 ----------------------------------------------------
321
484
  # 面板里的「试听」按钮走这条分支:播完(或最多 8 秒)就退出,不留窗口。
322
485
  if ($Preview) {
@@ -339,18 +502,41 @@ if ($Preview) {
339
502
  # ---- 显示并进入消息循环 -----------------------------------------------------
340
503
  $form.Show()
341
504
  $form.RaiseWithoutActivating()
342
- Write-Log ('shown hwnd=' + $form.Handle + ' bounds=' + $form.Bounds)
505
+ Write-Log ('shown hwnd=' + $form.Handle + ' bounds=' + $form.Bounds + ' compact=' + $Compact + ' buttons=' + $showButtons)
343
506
  $timer.Start()
344
507
  [System.Windows.Forms.Application]::Run()
345
508
 
509
+ $elapsedMs = [int]((Get-Date) - $script:holdStart).TotalMilliseconds
510
+
511
+ # ---- 把用户的选择写回去(插件据此决定要不要重排) ---------------------------
512
+ if (-not [string]::IsNullOrEmpty($ResultPath)) {
513
+ try {
514
+ $payload = [ordered]@{
515
+ action = $script:action
516
+ at = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds()
517
+ elapsedMs = $elapsedMs
518
+ snooze = $(if ($script:action -eq 'snooze') { [Math]::Max(1, $SnoozeMinutes) } else { 0 })
519
+ }
520
+ $json = ($payload | ConvertTo-Json -Compress)
521
+ [System.IO.File]::WriteAllText($ResultPath, $json, (New-Object System.Text.UTF8Encoding($false)))
522
+ Write-Log ('result ' + $json)
523
+ } catch {
524
+ Write-Log ('result-failed ' + $_.Exception.Message)
525
+ }
526
+ }
527
+
346
528
  if ($script:player -ne $null) { $script:player.Dispose() }
347
529
  if ($script:wmp -ne $null) {
348
530
  try { $script:wmp.controls.stop(); $script:wmp.close() } catch { }
349
531
  try { [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($script:wmp) } catch { }
350
532
  }
533
+ if ($script:synth -ne $null) {
534
+ try { $script:synth.Dispose() } catch { }
535
+ }
351
536
  $titleFont.Dispose()
352
537
  $bodyFont.Dispose()
353
538
  $hintFont.Dispose()
539
+ $btnFont.Dispose()
354
540
  if ($bgBitmap -ne $null) { $bgBitmap.Dispose() }
355
541
  if ($iconBitmap -ne $null) { $iconBitmap.Dispose() }
356
542
  $path.Dispose()