dsh-reminder-notify 0.10.1 → 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(
@@ -24,8 +31,14 @@ param(
24
31
  [string]$Accent = '#2ECC71',
25
32
  [string]$Background = '',
26
33
  [string]$Icon = '',
27
- [string]$Speak = '',
28
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,
29
42
  [switch]$Preview,
30
43
  [string]$Log = ''
31
44
  )
@@ -50,11 +63,22 @@ using System.Drawing;
50
63
  using System.Runtime.InteropServices;
51
64
  using System.Windows.Forms;
52
65
 
53
- public sealed class ReminderWindow : Form
66
+ public struct DshRect { public int Left, Top, Right, Bottom; }
67
+
68
+ public static class DshWin
54
69
  {
55
70
  [DllImport("user32.dll")]
56
- 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
+ }
57
79
 
80
+ public sealed class ReminderWindow : Form
81
+ {
58
82
  private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
59
83
  private const uint SWP_NOSIZE = 0x0001;
60
84
  private const uint SWP_NOMOVE = 0x0002;
@@ -85,16 +109,38 @@ public sealed class ReminderWindow : Form
85
109
 
86
110
  public void RaiseWithoutActivating()
87
111
  {
88
- SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0,
89
- 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);
90
114
  }
91
115
  }
92
116
  '@
93
117
 
94
118
  Add-Type -TypeDefinition $source -ReferencedAssemblies System.Windows.Forms, System.Drawing
95
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
+
96
143
  # ---- 外观常量 ---------------------------------------------------------------
97
- $width = 380
98
144
  $radius = 12
99
145
  $padX = 16
100
146
  $accentWidth = 4
@@ -103,14 +149,28 @@ $lineColor = [System.Drawing.Color]::FromArgb(48, 54, 66) # 细边框
103
149
  $titleColor = [System.Drawing.Color]::FromArgb(255, 255, 255)
104
150
  $bodyColor = [System.Drawing.Color]::FromArgb(201, 209, 217)
105
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)
106
154
  # 允许调用方传 "2ECC71"(不带 #):从 node 里 spawn 时,# 开头的 token 会被
107
155
  # PowerShell 当成注释,导致 -Accent 后面没有值。这里统一补上 #。
108
156
  if ($Accent -and -not $Accent.StartsWith('#')) { $Accent = '#' + $Accent }
109
157
  $accentColor = [System.Drawing.ColorTranslator]::FromHtml($Accent)
110
158
 
111
- $titleFont = New-Object System.Drawing.Font('Microsoft YaHei UI', 11.5, [System.Drawing.FontStyle]::Bold, [System.Drawing.GraphicsUnit]::Point)
112
- $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)
113
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)
114
174
 
115
175
  # ---- 先量文字,再定窗口高度(正文自动折行) --------------------------------
116
176
  $measureBmp = New-Object System.Drawing.Bitmap 8, 8
@@ -145,18 +205,41 @@ if (-not [string]::IsNullOrEmpty($Icon) -and (Test-Path -LiteralPath $Icon)) {
145
205
  }
146
206
  $hasIcon = $iconBitmap -ne $null
147
207
  if ($hasIcon) { $textWidth = $textWidth - $iconSize - $iconGap }
148
- $titleSize = $measureGfx.MeasureString($Title, $titleFont, $textWidth)
149
- $bodyText = if ([string]::IsNullOrWhiteSpace($Msg)) { '' } else { $Msg }
150
- $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
151
221
  $measureGfx.Dispose()
152
222
  $measureBmp.Dispose()
153
223
 
154
- $headerHeight = [int][Math]::Ceiling($titleSize.Height)
155
- $bodyHeight = [int][Math]::Ceiling($bodySize.Height)
224
+ $headerHeight = [int][Math]::Ceiling($titleSizeF.Height)
225
+ $bodyHeight = [int][Math]::Ceiling($bodySizeF.Height)
156
226
  $hintHeight = 16
157
- $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
158
229
  if ($height -lt 96) { $height = 96 }
159
- 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
+ }
160
243
 
161
244
  # ---- 定位:光标所在屏幕的工作区 ---------------------------------------------
162
245
  $screen = [System.Windows.Forms.Screen]::FromPoint([System.Windows.Forms.Cursor]::Position)
@@ -231,7 +314,7 @@ $form.Add_Paint({
231
314
  $pen.Dispose()
232
315
 
233
316
  $textLeft = $accentWidth + $padX
234
- if ($hasIcon) {
317
+ if ($hasIcon -and -not $Compact) {
235
318
  $g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
236
319
  $iconRect = New-Object System.Drawing.Rectangle $textLeft, ($padX - 1), $iconSize, $iconSize
237
320
  $g.DrawImage($iconBitmap, $iconRect)
@@ -248,17 +331,49 @@ $form.Add_Paint({
248
331
  $bodyBrush.Dispose()
249
332
  }
250
333
 
251
- $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 + ' · 点一下关掉' }
252
337
  $hintBrush = New-Object System.Drawing.SolidBrush $mutedColor
253
- $g.DrawString($hint, $hintFont, $hintBrush, $textLeft, ($height - $padX - 12))
338
+ $g.DrawString($hint, $hintFont, $hintBrush, $textLeft, ($height - $padX - 12 - $buttonsHeight))
254
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
+ }
255
368
  })
256
369
 
257
370
  # ---- 淡入 / 停留 / 淡出 ------------------------------------------------------
258
371
  $script:phase = 'in'
259
372
  $script:ticks = 0
373
+ $script:action = 'timeout'
260
374
  $fadeTicks = 12 # ~180ms
261
375
  $script:deadline = (Get-Date).AddSeconds([Math]::Max(1, $Seconds))
376
+ $script:holdStart = Get-Date
262
377
 
263
378
  $timer = New-Object System.Windows.Forms.Timer
264
379
  $timer.Interval = 10
@@ -268,17 +383,21 @@ $timer.Add_Tick({
268
383
  switch ($script:phase) {
269
384
  'in' {
270
385
  $form.Opacity = [Math]::Min(1.0, $script:ticks / $fadeTicks)
271
- 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 }
272
387
  }
273
388
  'hold' {
274
389
  # 用真实时钟而不是 tick 数:WinForms 计时器最细约 15ms,数 tick 会明显偏长
275
- 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
+ }
276
395
  }
277
396
  'out' {
278
397
  $form.Opacity = [Math]::Max(0.0, 1.0 - ($script:ticks / $fadeTicks))
279
398
  if ($script:ticks -ge $fadeTicks) {
280
399
  $timer.Stop()
281
- Write-Log 'closed'
400
+ Write-Log ('closed action=' + $script:action)
282
401
  $form.Close()
283
402
  [System.Windows.Forms.Application]::Exit()
284
403
  }
@@ -286,11 +405,40 @@ $timer.Add_Tick({
286
405
  }
287
406
  })
288
407
 
289
- $form.Add_Click({
290
- if ($script:phase -ne 'out') {
291
- $script:phase = 'out'
292
- $script:ticks = 0
293
- 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()
294
442
  }
295
443
  })
296
444
 
@@ -318,27 +466,17 @@ if (-not $NoSound -and -not [string]::IsNullOrEmpty($Audio) -and (Test-Path -Lit
318
466
  }
319
467
  }
320
468
 
321
- # ---- 语音朗读(System.Speech,优先中文音色) ---------------------------------
322
- # 和 -Audio 互不冲突:有提示音就放提示音,同时朗读 -Speak 的文本。
323
- # 朗读是异步的(SpeakAsync),不拖慢卡片弹出;读不完就随进程退出被掐断。
469
+ # ---- 语音朗读(System.Speech,异步,缺语音包就跳过) -------------------------
324
470
  $script:synth = $null
325
- if (-not [string]::IsNullOrEmpty($Speak)) {
471
+ if (-not [string]::IsNullOrWhiteSpace($Speak)) {
326
472
  try {
327
473
  Add-Type -AssemblyName System.Speech
328
474
  $script:synth = New-Object System.Speech.Synthesis.SpeechSynthesizer
329
- try {
330
- $zh = $script:synth.GetInstalledVoices() |
331
- Where-Object { $_.VoiceInfo.Culture.Name -like 'zh-*' } |
332
- Select-Object -First 1
333
- if ($zh -ne $null) { $script:synth.SelectVoice($zh.VoiceInfo.Name) }
334
- } catch { }
335
- $script:synth.Rate = 0
336
- $script:synth.Volume = 100
337
- $script:synth.SpeakAsync($Speak) | Out-Null
475
+ $script:synth.SpeakAsync($Speak)
338
476
  Write-Log ('speak ' + $Speak)
339
477
  } catch {
340
- Write-Log ('speak-failed ' + $_.Exception.Message)
341
478
  $script:synth = $null
479
+ Write-Log ('speak-failed ' + $_.Exception.Message)
342
480
  }
343
481
  }
344
482
 
@@ -364,19 +502,41 @@ if ($Preview) {
364
502
  # ---- 显示并进入消息循环 -----------------------------------------------------
365
503
  $form.Show()
366
504
  $form.RaiseWithoutActivating()
367
- Write-Log ('shown hwnd=' + $form.Handle + ' bounds=' + $form.Bounds)
505
+ Write-Log ('shown hwnd=' + $form.Handle + ' bounds=' + $form.Bounds + ' compact=' + $Compact + ' buttons=' + $showButtons)
368
506
  $timer.Start()
369
507
  [System.Windows.Forms.Application]::Run()
370
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
+
371
528
  if ($script:player -ne $null) { $script:player.Dispose() }
372
- if ($script:synth -ne $null) { try { $script:synth.Dispose() } catch { } }
373
529
  if ($script:wmp -ne $null) {
374
530
  try { $script:wmp.controls.stop(); $script:wmp.close() } catch { }
375
531
  try { [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($script:wmp) } catch { }
376
532
  }
533
+ if ($script:synth -ne $null) {
534
+ try { $script:synth.Dispose() } catch { }
535
+ }
377
536
  $titleFont.Dispose()
378
537
  $bodyFont.Dispose()
379
538
  $hintFont.Dispose()
539
+ $btnFont.Dispose()
380
540
  if ($bgBitmap -ne $null) { $bgBitmap.Dispose() }
381
541
  if ($iconBitmap -ne $null) { $iconBitmap.Dispose() }
382
542
  $path.Dispose()