claude-hook-notify 1.3.0 → 1.4.1

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/README.md CHANGED
@@ -89,7 +89,7 @@ npx claude-hook-notify setup --events Stop,TaskCompleted,Notification,PostToolUs
89
89
  | macOS | `osascript` | 无(系统自带) |
90
90
  | macOS | `terminal-notifier` | 可选: `brew install terminal-notifier`(更好) |
91
91
  | Linux | `notify-send` | `sudo apt-get install libnotify-bin` |
92
- | Windows | Toast 通知 (PowerShell) | 无(Windows 10+ 系统自带) |
92
+ | Windows | 桌面通知 (内置工具) | 无(Windows 10+ 系统自带,点击通知跳转终端) |
93
93
 
94
94
  ## 原理
95
95
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-hook-notify",
3
- "version": "1.3.0",
3
+ "version": "1.4.1",
4
4
  "description": "🔔 Claude Code 任务完成桌面通知 — 一键安装,跨平台支持",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -0,0 +1,82 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * 后台点击监听脚本
5
+ *
6
+ * 由主进程 spawn 为 detached 后台进程。
7
+ * 执行通知命令(带等待标志),检测用户点击后激活终端窗口。
8
+ *
9
+ * 参数格式:
10
+ * node activate-watcher.js <JSON配置>
11
+ *
12
+ * JSON 配置:
13
+ * {
14
+ * "platform": "win32" | "linux",
15
+ * "notifyCommand": "...",
16
+ * "notifyArgs": ["..."],
17
+ * "activateCommand": "...",
18
+ * "activateArgs": ["..."],
19
+ * "activateFallbackCommand": "...", // 可选
20
+ * "activateFallbackArgs": ["..."], // 可选
21
+ * "timeout": 120000
22
+ * }
23
+ */
24
+
25
+ const { spawn } = require("child_process");
26
+
27
+ // 120 秒超时自动退出,防僵尸进程
28
+ const config = parseConfig();
29
+ const selfTimeout = setTimeout(() => process.exit(0), config.timeout || 120000);
30
+ selfTimeout.unref();
31
+
32
+ function parseConfig() {
33
+ try {
34
+ return JSON.parse(process.argv[2] || "{}");
35
+ } catch {
36
+ process.exit(1);
37
+ }
38
+ }
39
+
40
+ function activateTerminal() {
41
+ const { activateCommand, activateArgs, activateFallbackCommand, activateFallbackArgs } = config;
42
+ if (!activateCommand) return;
43
+
44
+ try {
45
+ execFileSync(activateCommand, activateArgs || [], { stdio: "ignore", timeout: 5000 });
46
+ } catch {
47
+ // 主命令失败,尝试回退
48
+ if (activateFallbackCommand) {
49
+ try {
50
+ execFileSync(activateFallbackCommand, activateFallbackArgs || [], { stdio: "ignore", timeout: 5000 });
51
+ } catch {
52
+ // 回退也失败,静默退出
53
+ }
54
+ }
55
+ }
56
+ }
57
+
58
+ async function main() {
59
+ const { platform, notifyCommand, notifyArgs } = config;
60
+ if (!notifyCommand) process.exit(1);
61
+
62
+ if (platform === "linux") {
63
+ // notify-send --action 模式:stdout 输出 action 名
64
+ const child = spawn(notifyCommand, notifyArgs, {
65
+ stdio: ["ignore", "pipe", "ignore"],
66
+ timeout: 120000,
67
+ });
68
+
69
+ let output = "";
70
+ child.stdout.on("data", (chunk) => {
71
+ output += chunk.toString();
72
+ });
73
+
74
+ child.on("close", () => {
75
+ if (output.trim() === "default") {
76
+ activateTerminal();
77
+ }
78
+ });
79
+ }
80
+ }
81
+
82
+ main().catch(() => process.exit(1));
@@ -0,0 +1,215 @@
1
+ const { execSync, execFileSync } = require("child_process");
2
+ const os = require("os");
3
+
4
+ /**
5
+ * 终端进程名到 macOS bundleId 的映射
6
+ */
7
+ const MAC_BUNDLE_MAP = {
8
+ "Terminal": "com.apple.Terminal",
9
+ "iTerm2": "com.googlecode.iterm2",
10
+ "iTerm2-v3": "com.googlecode.iterm2",
11
+ "Electron": null, // 需进一步判断
12
+ "Code": "com.microsoft.VSCode",
13
+ "Code - Insiders": "com.microsoft.VSCodeInsiders",
14
+ "Cursor": "com.todesktop.230313mzl4w4u92",
15
+ "WarpTerminal": "dev.warp.Warp-Stable",
16
+ "Alacritty": "org.alacritty",
17
+ "kitty": "net.kovidgoyal.kitty",
18
+ "Hyper": "co.zeit.hyper",
19
+ "Tabby": "org.tabby",
20
+ };
21
+
22
+ /**
23
+ * Windows 终端进程名(小写,不含 .exe)
24
+ */
25
+ const WIN_TERMINAL_NAMES = new Set([
26
+ "windowsterminal",
27
+ "code",
28
+ "cursor",
29
+ "cmd",
30
+ "powershell",
31
+ "pwsh",
32
+ "mintty",
33
+ "alacritty",
34
+ "wezterm-gui",
35
+ "hyper",
36
+ "tabby",
37
+ "conhost",
38
+ ]);
39
+
40
+ /**
41
+ * Linux 终端进程名
42
+ */
43
+ const LINUX_TERMINALS = new Set([
44
+ "gnome-terminal-server",
45
+ "gnome-terminal",
46
+ "konsole",
47
+ "xfce4-terminal",
48
+ "mate-terminal",
49
+ "tilix",
50
+ "terminator",
51
+ "alacritty",
52
+ "kitty",
53
+ "wezterm-gui",
54
+ "foot",
55
+ "code",
56
+ "cursor",
57
+ "hyper",
58
+ "tabby",
59
+ ]);
60
+
61
+ /**
62
+ * 沿进程树向上查找终端进程
63
+ */
64
+ function detectTerminal() {
65
+ const platform = os.platform();
66
+ try {
67
+ if (platform === "darwin") return detectMac();
68
+ if (platform === "win32") return detectWindows();
69
+ if (platform === "linux") return detectLinux();
70
+ } catch {
71
+ // 检测失败
72
+ }
73
+ return null;
74
+ }
75
+
76
+ function detectMac() {
77
+ let pid = process.ppid;
78
+ for (let i = 0; i < 20 && pid > 1; i++) {
79
+ try {
80
+ const info = execSync(`ps -o ppid=,comm= -p ${pid}`, { encoding: "utf-8" }).trim();
81
+ const match = info.match(/^\s*(\d+)\s+(.+)$/);
82
+ if (!match) break;
83
+ const ppid = parseInt(match[1], 10);
84
+ const comm = match[2].trim().split("/").pop();
85
+
86
+ if (MAC_BUNDLE_MAP[comm] !== undefined) {
87
+ const bundleId = MAC_BUNDLE_MAP[comm];
88
+ if (bundleId) {
89
+ return { name: comm, bundleId, pid, processName: comm };
90
+ }
91
+ }
92
+ pid = ppid;
93
+ } catch {
94
+ break;
95
+ }
96
+ }
97
+ // 回退:检查 TERM_PROGRAM 环境变量
98
+ const termProgram = process.env.TERM_PROGRAM;
99
+ if (termProgram) {
100
+ const map = {
101
+ "Apple_Terminal": { name: "Terminal", bundleId: "com.apple.Terminal" },
102
+ "iTerm.app": { name: "iTerm2", bundleId: "com.googlecode.iterm2" },
103
+ "vscode": { name: "Code", bundleId: "com.microsoft.VSCode" },
104
+ "WarpTerminal": { name: "Warp", bundleId: "dev.warp.Warp-Stable" },
105
+ "Hyper": { name: "Hyper", bundleId: "co.zeit.hyper" },
106
+ };
107
+ if (map[termProgram]) {
108
+ return { ...map[termProgram], pid: 0, processName: termProgram };
109
+ }
110
+ }
111
+ return null;
112
+ }
113
+
114
+ /**
115
+ * Windows: 优先环境变量,回退单次 PowerShell 批量查询
116
+ */
117
+ function detectWindows() {
118
+ // 1. 环境变量快速检测(仅保留精确 PID 的路径)
119
+ if (process.env.VSCODE_PID) {
120
+ const pid = parseInt(process.env.VSCODE_PID, 10);
121
+ if (pid > 0) return { name: "Code", pid, processName: "code.exe" };
122
+ }
123
+
124
+ // 2. WMI 遍历进程树(OS 级 ParentProcessId 始终完整,不受 spawn 方式影响)
125
+ try {
126
+ const script = [
127
+ "$cpid=" + process.pid,
128
+ "for($i=0;$i -lt 30 -and $cpid -gt 0;$i++){",
129
+ " $w=Get-CimInstance Win32_Process -Filter \"ProcessId=$cpid\" -EA SilentlyContinue",
130
+ " if(-not $w){break}",
131
+ " Write-Output \"$($w.Name)|$($w.ProcessId)|$($w.ParentProcessId)\"",
132
+ " $cpid=$w.ParentProcessId",
133
+ "}",
134
+ ].join(";");
135
+ const output = execFileSync("powershell", ["-NoProfile", "-Command", script], {
136
+ encoding: "utf-8",
137
+ timeout: 8000,
138
+ }).trim();
139
+
140
+ for (const line of output.split("\n")) {
141
+ const parts = line.trim().split("|");
142
+ if (parts.length < 3) continue;
143
+ // WMI 返回的 Name 带 .exe 后缀,去掉再匹配
144
+ const name = parts[0].replace(/\.exe$/i, "").toLowerCase();
145
+ const pid = parseInt(parts[1], 10);
146
+ if (WIN_TERMINAL_NAMES.has(name)) {
147
+ return { name: parts[0].replace(/\.exe$/i, ""), pid, processName: name + ".exe" };
148
+ }
149
+ }
150
+ } catch {
151
+ // PowerShell 调用失败
152
+ }
153
+ return null;
154
+ }
155
+
156
+ function detectLinux() {
157
+ // 1. 环境变量快速检测
158
+ if (process.env.VSCODE_PID) {
159
+ const pid = parseInt(process.env.VSCODE_PID, 10);
160
+ if (pid > 0) return { name: "code", pid, processName: "code" };
161
+ }
162
+
163
+ // 2. 进程树遍历
164
+ let pid = process.ppid;
165
+ for (let i = 0; i < 20 && pid > 1; i++) {
166
+ try {
167
+ const info = execSync(`ps -o ppid=,comm= -p ${pid}`, { encoding: "utf-8" }).trim();
168
+ const match = info.match(/^\s*(\d+)\s+(.+)$/);
169
+ if (!match) break;
170
+ const ppid = parseInt(match[1], 10);
171
+ const comm = match[2].trim();
172
+
173
+ if (LINUX_TERMINALS.has(comm)) {
174
+ return { name: comm, pid, processName: comm };
175
+ }
176
+ pid = ppid;
177
+ } catch {
178
+ break;
179
+ }
180
+ }
181
+ return null;
182
+ }
183
+
184
+ /**
185
+ * 获取激活终端窗口的命令和参数
186
+ */
187
+ function getActivateCommand(terminalInfo) {
188
+ if (!terminalInfo) return null;
189
+ const platform = os.platform();
190
+
191
+ if (platform === "darwin") {
192
+ if (terminalInfo.bundleId) {
193
+ return {
194
+ command: "osascript",
195
+ args: ["-e", `tell application id "${terminalInfo.bundleId}" to activate`],
196
+ };
197
+ }
198
+ return null;
199
+ }
200
+
201
+ // Windows 使用 notify-helper.exe 自行处理激活,不需要此函数
202
+
203
+ if (platform === "linux") {
204
+ return {
205
+ command: "xdotool",
206
+ args: ["search", "--pid", String(terminalInfo.pid), "--onlyvisible", "windowactivate"],
207
+ fallbackCommand: "wmctrl",
208
+ fallbackArgs: ["-ia", String(terminalInfo.pid)],
209
+ };
210
+ }
211
+
212
+ return null;
213
+ }
214
+
215
+ module.exports = { detectTerminal, getActivateCommand };
package/src/cli.js CHANGED
@@ -24,6 +24,7 @@ const HELP = `
24
24
  --title <标题> 自定义通知标题
25
25
  --message <消息> 自定义通知消息
26
26
  --sound <音效> macOS 音效名称 (默认: Glass)
27
+ --no-activate 禁用点击通知后激活终端窗口
27
28
  --dry-run 仅打印通知内容,不实际发送
28
29
 
29
30
  示例:
@@ -39,6 +40,7 @@ function parseArgs(args) {
39
40
  const key = args[i].slice(2);
40
41
  if (
41
42
  key === "dry-run" ||
43
+ key === "no-activate" ||
42
44
  key === "global" ||
43
45
  key === "local" ||
44
46
  key === "help"
@@ -101,6 +103,7 @@ async function main() {
101
103
  title: args.title,
102
104
  message: args.message,
103
105
  sound: args.sound,
106
+ activate: !args["no-activate"],
104
107
  dryRun: !!args["dry-run"],
105
108
  hookInput: input,
106
109
  });
package/src/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const { sendNotification, EVENT_CONFIG } = require("./notify");
2
2
  const { setup, uninstall } = require("./setup");
3
+ const { detectTerminal } = require("./activate");
3
4
 
4
- module.exports = { sendNotification, EVENT_CONFIG, setup, uninstall };
5
+ module.exports = { sendNotification, EVENT_CONFIG, setup, uninstall, detectTerminal };
package/src/notify.js CHANGED
@@ -1,7 +1,8 @@
1
- const { execSync } = require("child_process");
1
+ const { execSync, spawn } = require("child_process");
2
2
  const os = require("os");
3
3
  const fs = require("fs");
4
4
  const path = require("path");
5
+ const { detectTerminal, getActivateCommand } = require("./activate");
5
6
 
6
7
  const EVENT_CONFIG = {
7
8
  Stop: {
@@ -120,16 +121,16 @@ function buildNotification({ event, title, message, hookInput }) {
120
121
  const config = EVENT_CONFIG[event] || EVENT_CONFIG.Stop;
121
122
  const ctx = extractContext(hookInput || {});
122
123
 
123
- let finalTitle = title || `${config.title} (${ctx.project})`;
124
+ let finalTitle = title || config.title;
124
125
  let finalMessage = message;
125
126
 
126
127
  if (!finalMessage) {
127
128
  if (event === "StopFailure" && ctx.errorType) {
128
129
  const label = ERROR_TYPE_LABELS[ctx.errorType] || ctx.errorType;
129
- finalTitle = `${config.title}: ${label} (${ctx.project})`;
130
+ finalTitle = `${config.title}: ${label}`;
130
131
  finalMessage = ctx.errorMessage || config.message;
131
132
  } else if (event === "Stop" && ctx.stopReason === "max_tokens") {
132
- finalTitle = `Claude Code 响应截断 (${ctx.project})`;
133
+ finalTitle = "Claude Code 响应截断";
133
134
  finalMessage = "响应达到最大 token 限制被截断";
134
135
  } else if (event === "TaskCompleted" && ctx.taskSubject) {
135
136
  finalMessage = ctx.taskSubject;
@@ -171,6 +172,7 @@ async function sendNotification(options = {}) {
171
172
  sound: customSound,
172
173
  dryRun = false,
173
174
  hookInput = {},
175
+ activate = true,
174
176
  } = options;
175
177
 
176
178
  const {
@@ -189,7 +191,11 @@ async function sendNotification(options = {}) {
189
191
  let method = "unknown";
190
192
  let command = "";
191
193
  let args = [];
192
- let tmpFile = null;
194
+ let useWatcher = false;
195
+
196
+ // 检测终端信息(用于点击激活)
197
+ const terminalInfo = activate ? detectTerminal() : null;
198
+ const activateCmd = terminalInfo ? getActivateCommand(terminalInfo) : null;
193
199
 
194
200
  if (platform === "darwin") {
195
201
  // macOS
@@ -206,6 +212,10 @@ async function sendNotification(options = {}) {
206
212
  "-group",
207
213
  `claude-code-${extractContext(hookInput).project}`,
208
214
  ];
215
+ // terminal-notifier 内置点击激活:添加 -activate bundleId
216
+ if (activate && terminalInfo && terminalInfo.bundleId) {
217
+ args.push("-activate", terminalInfo.bundleId);
218
+ }
209
219
  } else {
210
220
  method = "osascript";
211
221
  command = "osascript";
@@ -215,6 +225,7 @@ async function sendNotification(options = {}) {
215
225
  "-e",
216
226
  `display notification "${escaped}" with title "${escapedTitle}" sound name "${sound}"`,
217
227
  ];
228
+ // osascript 不支持点击回调,跳过激活
218
229
  }
219
230
  } else if (platform === "linux") {
220
231
  method = "notify-send";
@@ -234,20 +245,26 @@ async function sendNotification(options = {}) {
234
245
  }
235
246
  return result;
236
247
  }
248
+ // Linux: 使用后台 watcher 处理点击激活
249
+ if (activate && activateCmd) {
250
+ useWatcher = true;
251
+ args.push("--action=default=click");
252
+ }
237
253
  } else if (platform === "win32") {
238
- // SnoreToast: 原生 Windows Toast 通知,UTF-16 支持,不抢焦点
239
- method = "snoretoast";
240
- const arch = os.arch() === "x64" ? "x64" : "x86";
241
- command = path.join(__dirname, "..", "vendor", "snoretoast", `snoretoast-${arch}.exe`);
242
- args = ["-t", title, "-m", message, "-appID", "Claude.Code"];
254
+ // Windows: notify-helper.exe 通知 + 点击激活终端(无控制台窗口)
255
+ method = "notify-helper";
256
+ command = path.join(__dirname, "..", "vendor", "notify-helper", "ClaudeCodeNotify.exe");
257
+ const targetNames = ["WindowsTerminal", "Code", "cursor", "cmd", "pwsh", "powershell", "mintty"];
258
+ const pid = (terminalInfo && terminalInfo.pid) ? String(terminalInfo.pid) : "0";
259
+ args = [title, message, targetNames.join(","), pid];
243
260
  }
244
261
 
245
- const result = { sent: !dryRun, method, command, args };
262
+ const result = { sent: !dryRun, method, command, args, activate: !!activateCmd };
263
+ if (terminalInfo) {
264
+ result.terminal = terminalInfo.name;
265
+ }
246
266
 
247
267
  if (dryRun) {
248
- if (tmpFile) {
249
- try { fs.unlinkSync(tmpFile); } catch {}
250
- }
251
268
  result.sent = false;
252
269
  console.log(JSON.stringify(result, null, 2));
253
270
  return result;
@@ -255,16 +272,36 @@ async function sendNotification(options = {}) {
255
272
 
256
273
  try {
257
274
  if (command) {
258
- const { execFileSync } = require("child_process");
259
- execFileSync(command, args, { stdio: "ignore", timeout: 8000 });
275
+ if (method === "notify-helper") {
276
+ // Windows: notify-helper.exe 后台运行,等待点击后激活终端
277
+ const child = spawn(command, args, { detached: true, stdio: "ignore", windowsHide: true });
278
+ child.unref();
279
+ } else if (useWatcher && activateCmd) {
280
+ // Linux: 启动后台 watcher 发送通知 + 等待点击 + 激活终端
281
+ const watcherConfig = {
282
+ platform,
283
+ notifyCommand: command,
284
+ notifyArgs: args,
285
+ activateCommand: activateCmd.command,
286
+ activateArgs: activateCmd.args,
287
+ activateFallbackCommand: activateCmd.fallbackCommand || null,
288
+ activateFallbackArgs: activateCmd.fallbackArgs || null,
289
+ timeout: 120000,
290
+ };
291
+ const watcher = spawn(
292
+ process.execPath,
293
+ [path.join(__dirname, "activate-watcher.js"), JSON.stringify(watcherConfig)],
294
+ { detached: true, stdio: "ignore", windowsHide: true }
295
+ );
296
+ watcher.unref();
297
+ } else {
298
+ const { execFileSync } = require("child_process");
299
+ execFileSync(command, args, { stdio: "ignore", timeout: 8000 });
300
+ }
260
301
  }
261
302
  } catch (err) {
262
303
  result.sent = false;
263
304
  result.error = err.message;
264
- } finally {
265
- if (tmpFile) {
266
- try { fs.unlinkSync(tmpFile); } catch {}
267
- }
268
305
  }
269
306
 
270
307
  return result;
@@ -0,0 +1,160 @@
1
+ using System;
2
+ using System.Diagnostics;
3
+ using System.Drawing;
4
+ using System.IO;
5
+ using System.Runtime.InteropServices;
6
+ using System.Windows.Forms;
7
+
8
+ [assembly: System.Reflection.AssemblyTitle("Claude Code 完成通知")]
9
+ [assembly: System.Reflection.AssemblyProduct("Claude Code Notify")]
10
+
11
+ class NotifyHelper
12
+ {
13
+ [DllImport("user32.dll")]
14
+ static extern bool SetForegroundWindow(IntPtr hWnd);
15
+
16
+ [DllImport("user32.dll")]
17
+ static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
18
+
19
+ delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
20
+
21
+ [DllImport("user32.dll")]
22
+ static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
23
+
24
+ [DllImport("user32.dll")]
25
+ static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
26
+
27
+ [DllImport("user32.dll")]
28
+ static extern bool IsWindowVisible(IntPtr hWnd);
29
+
30
+ // Claude sparkle 图标 PNG(内嵌,无需外部文件)
31
+ static string IconBase64 = "iVBORw0KGgoAAAANSUhEUgAAAIAA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swFyScSv02gXJEiQ7o/E0oBoj35fl1EM67RAN61DUEKd8dWmfM2TQH/DPSMGb8AKQlDlmyzBwAj/EQTQANZjT/aaqPlGo4Zk6kAaHcoBAhU1QqpbQhBERouLlqSWqp/h/2kdfTBBNsnRtCRpYIdJQMIyRh9BE1NmmnqsOpAwYiYq6aPwBQ2w/NKs6/ZcHcRRD9AwWCzs2OYQSFscZNqcMQyZFwUY2zP4WW7EllhKso+Y3ghtieJqAhsFmpSzo8ikG2/KSXJEcqZkFIRJWNibAGOMBtqQYhkNMXEU5stBElDujckSqTeuPGcIZUhFRVXKRZgp5KGkTrzdpLWlEZmaVcm0aBg29jQ6zh7JS3PAWOSAK0w3mgBI4hSWRI2kxDt8C/jpMFVqYod1tbHLCaOXYRZYsrNJCs3rPr4i3akVmW6j5NdVBdks+NzkFoyVS5FLPT2TJvdXbFI4XSbjMaiguLzpJMXVJSoSpKLdjVKv8mH5jRC5t1ox3ZwGygMkWInNSJIPom4FSQZKfcNhqnRNMVBt2ETU6RLhAEXc9GdpeHkuZ5D87ETwkfNCsPM6UqPT5Th+Xis8R74i5P5XTpB6im8tj5EGgWhjU+j8NIyIbPE2A27mk0Lf8VryABzB2qlOzYtQn3jkuEASwOHrDyGkfn27YFOHL3xTRbX29XrVNiscYMgX5djGyTl42fM/Wl1f1muarlQAAAAASUVORK5CYII=";
32
+
33
+ static IntPtr targetHandle = IntPtr.Zero;
34
+ static NotifyIcon notify;
35
+
36
+ static void Main(string[] args)
37
+ {
38
+ string title = args.Length > 0 ? args[0] : "Claude Code";
39
+ string message = args.Length > 1 ? args[1] : "任务完成";
40
+ string targetName = args.Length > 2 ? args[2] : "";
41
+ int targetPid = 0;
42
+ if (args.Length > 3) int.TryParse(args[3], out targetPid);
43
+
44
+ // 策略 1:通过 PID 精确定位终端窗口(多实例场景)
45
+ if (targetPid > 0)
46
+ {
47
+ try
48
+ {
49
+ var proc = Process.GetProcessById(targetPid);
50
+ if (proc.MainWindowHandle != IntPtr.Zero)
51
+ {
52
+ targetHandle = proc.MainWindowHandle;
53
+ }
54
+ else
55
+ {
56
+ // MainWindowHandle 可能为零(如 Windows Terminal),用 EnumWindows 查找
57
+ targetHandle = FindWindowByPid(targetPid);
58
+ }
59
+ }
60
+ catch { /* 进程可能已退出,回退到名称查找 */ }
61
+ }
62
+
63
+ // 策略 2:通过进程名查找(回退方案)
64
+ if (targetHandle == IntPtr.Zero && !string.IsNullOrEmpty(targetName))
65
+ {
66
+ foreach (var name in targetName.Split(','))
67
+ {
68
+ try
69
+ {
70
+ var procs = Process.GetProcessesByName(name.Trim());
71
+ foreach (var p in procs)
72
+ {
73
+ if (p.MainWindowHandle != IntPtr.Zero)
74
+ {
75
+ targetHandle = p.MainWindowHandle;
76
+ break;
77
+ }
78
+ }
79
+ if (targetHandle != IntPtr.Zero) break;
80
+ }
81
+ catch { }
82
+ }
83
+ }
84
+
85
+ notify = new NotifyIcon();
86
+ notify.Icon = LoadEmbeddedIcon();
87
+ notify.Visible = true;
88
+ notify.BalloonTipTitle = title;
89
+ notify.BalloonTipText = message;
90
+ notify.BalloonTipIcon = ToolTipIcon.None;
91
+
92
+ notify.BalloonTipClicked += delegate { ActivateAndExit(); };
93
+ notify.Click += delegate { ActivateAndExit(); };
94
+ notify.BalloonTipClosed += delegate { Cleanup(); };
95
+
96
+ notify.ShowBalloonTip(15000);
97
+
98
+ var timer = new Timer();
99
+ timer.Interval = 60000;
100
+ timer.Tick += delegate { Cleanup(); };
101
+ timer.Start();
102
+
103
+ Application.Run();
104
+ }
105
+
106
+ static IntPtr FindWindowByPid(int pid)
107
+ {
108
+ IntPtr found = IntPtr.Zero;
109
+ EnumWindows((hWnd, lParam) =>
110
+ {
111
+ uint procId;
112
+ GetWindowThreadProcessId(hWnd, out procId);
113
+ if ((int)procId == pid && IsWindowVisible(hWnd))
114
+ {
115
+ found = hWnd;
116
+ return false;
117
+ }
118
+ return true;
119
+ }, IntPtr.Zero);
120
+ return found;
121
+ }
122
+
123
+ static Icon LoadEmbeddedIcon()
124
+ {
125
+ try
126
+ {
127
+ var bytes = Convert.FromBase64String(IconBase64);
128
+ using (var ms = new MemoryStream(bytes))
129
+ {
130
+ var bmp = new Bitmap(ms);
131
+ var resized = new Bitmap(bmp, 32, 32);
132
+ return Icon.FromHandle(resized.GetHicon());
133
+ }
134
+ }
135
+ catch
136
+ {
137
+ return SystemIcons.Information;
138
+ }
139
+ }
140
+
141
+ static void ActivateAndExit()
142
+ {
143
+ if (targetHandle != IntPtr.Zero)
144
+ {
145
+ ShowWindow(targetHandle, 9);
146
+ SetForegroundWindow(targetHandle);
147
+ }
148
+ Cleanup();
149
+ }
150
+
151
+ static void Cleanup()
152
+ {
153
+ if (notify != null)
154
+ {
155
+ notify.Visible = false;
156
+ notify.Dispose();
157
+ }
158
+ Application.ExitThread();
159
+ }
160
+ }
@@ -1,166 +0,0 @@
1
- // Retrieved from https://github.com/KDE/snoretoast/blob/master/COPYING.LGPL-3 version 0.7.0
2
- GNU LESSER GENERAL PUBLIC LICENSE
3
- Version 3, 29 June 2007
4
-
5
- Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
6
- Everyone is permitted to copy and distribute verbatim copies
7
- of this license document, but changing it is not allowed.
8
-
9
-
10
- This version of the GNU Lesser General Public License incorporates
11
- the terms and conditions of version 3 of the GNU General Public
12
- License, supplemented by the additional permissions listed below.
13
-
14
- 0. Additional Definitions.
15
-
16
- As used herein, "this License" refers to version 3 of the GNU Lesser
17
- General Public License, and the "GNU GPL" refers to version 3 of the GNU
18
- General Public License.
19
-
20
- "The Library" refers to a covered work governed by this License,
21
- other than an Application or a Combined Work as defined below.
22
-
23
- An "Application" is any work that makes use of an interface provided
24
- by the Library, but which is not otherwise based on the Library.
25
- Defining a subclass of a class defined by the Library is deemed a mode
26
- of using an interface provided by the Library.
27
-
28
- A "Combined Work" is a work produced by combining or linking an
29
- Application with the Library. The particular version of the Library
30
- with which the Combined Work was made is also called the "Linked
31
- Version".
32
-
33
- The "Minimal Corresponding Source" for a Combined Work means the
34
- Corresponding Source for the Combined Work, excluding any source code
35
- for portions of the Combined Work that, considered in isolation, are
36
- based on the Application, and not on the Linked Version.
37
-
38
- The "Corresponding Application Code" for a Combined Work means the
39
- object code and/or source code for the Application, including any data
40
- and utility programs needed for reproducing the Combined Work from the
41
- Application, but excluding the System Libraries of the Combined Work.
42
-
43
- 1. Exception to Section 3 of the GNU GPL.
44
-
45
- You may convey a covered work under sections 3 and 4 of this License
46
- without being bound by section 3 of the GNU GPL.
47
-
48
- 2. Conveying Modified Versions.
49
-
50
- If you modify a copy of the Library, and, in your modifications, a
51
- facility refers to a function or data to be supplied by an Application
52
- that uses the facility (other than as an argument passed when the
53
- facility is invoked), then you may convey a copy of the modified
54
- version:
55
-
56
- a) under this License, provided that you make a good faith effort to
57
- ensure that, in the event an Application does not supply the
58
- function or data, the facility still operates, and performs
59
- whatever part of its purpose remains meaningful, or
60
-
61
- b) under the GNU GPL, with none of the additional permissions of
62
- this License applicable to that copy.
63
-
64
- 3. Object Code Incorporating Material from Library Header Files.
65
-
66
- The object code form of an Application may incorporate material from
67
- a header file that is part of the Library. You may convey such object
68
- code under terms of your choice, provided that, if the incorporated
69
- material is not limited to numerical parameters, data structure
70
- layouts and accessors, or small macros, inline functions and templates
71
- (ten or fewer lines in length), you do both of the following:
72
-
73
- a) Give prominent notice with each copy of the object code that the
74
- Library is used in it and that the Library and its use are
75
- covered by this License.
76
-
77
- b) Accompany the object code with a copy of the GNU GPL and this license
78
- document.
79
-
80
- 4. Combined Works.
81
-
82
- You may convey a Combined Work under terms of your choice that,
83
- taken together, effectively do not restrict modification of the
84
- portions of the Library contained in the Combined Work and reverse
85
- engineering for debugging such modifications, if you also do each of
86
- the following:
87
-
88
- a) Give prominent notice with each copy of the Combined Work that
89
- the Library is used in it and that the Library and its use are
90
- covered by this License.
91
-
92
- b) Accompany the Combined Work with a copy of the GNU GPL and this license
93
- document.
94
-
95
- c) For a Combined Work that displays copyright notices during
96
- execution, include the copyright notice for the Library among
97
- these notices, as well as a reference directing the user to the
98
- copies of the GNU GPL and this license document.
99
-
100
- d) Do one of the following:
101
-
102
- 0) Convey the Minimal Corresponding Source under the terms of this
103
- License, and the Corresponding Application Code in a form
104
- suitable for, and under terms that permit, the user to
105
- recombine or relink the Application with a modified version of
106
- the Linked Version to produce a modified Combined Work, in the
107
- manner specified by section 6 of the GNU GPL for conveying
108
- Corresponding Source.
109
-
110
- 1) Use a suitable shared library mechanism for linking with the
111
- Library. A suitable mechanism is one that (a) uses at run time
112
- a copy of the Library already present on the user's computer
113
- system, and (b) will operate properly with a modified version
114
- of the Library that is interface-compatible with the Linked
115
- Version.
116
-
117
- e) Provide Installation Information, but only if you would otherwise
118
- be required to provide such information under section 6 of the
119
- GNU GPL, and only to the extent that such information is
120
- necessary to install and execute a modified version of the
121
- Combined Work produced by recombining or relinking the
122
- Application with a modified version of the Linked Version. (If
123
- you use option 4d0, the Installation Information must accompany
124
- the Minimal Corresponding Source and Corresponding Application
125
- Code. If you use option 4d1, you must provide the Installation
126
- Information in the manner specified by section 6 of the GNU GPL
127
- for conveying Corresponding Source.)
128
-
129
- 5. Combined Libraries.
130
-
131
- You may place library facilities that are a work based on the
132
- Library side by side in a single library together with other library
133
- facilities that are not Applications and are not covered by this
134
- License, and convey such a combined library under terms of your
135
- choice, if you do both of the following:
136
-
137
- a) Accompany the combined library with a copy of the same work based
138
- on the Library, uncombined with any other library facilities,
139
- conveyed under the terms of this License.
140
-
141
- b) Give prominent notice with the combined library that part of it
142
- is a work based on the Library, and explaining where to find the
143
- accompanying uncombined form of the same work.
144
-
145
- 6. Revised Versions of the GNU Lesser General Public License.
146
-
147
- The Free Software Foundation may publish revised and/or new versions
148
- of the GNU Lesser General Public License from time to time. Such new
149
- versions will be similar in spirit to the present version, but may
150
- differ in detail to address new problems or concerns.
151
-
152
- Each version is given a distinguishing version number. If the
153
- Library as you received it specifies that a certain numbered version
154
- of the GNU Lesser General Public License "or any later version"
155
- applies to it, you have the option of following the terms and
156
- conditions either of that published version or of any later version
157
- published by the Free Software Foundation. If the Library as you
158
- received it does not specify a version number of the GNU Lesser
159
- General Public License, you may choose any version of the GNU Lesser
160
- General Public License ever published by the Free Software Foundation.
161
-
162
- If the Library as you received it specifies that a proxy can decide
163
- whether future versions of the GNU Lesser General Public License shall
164
- apply, that proxy's public statement of acceptance of any version is
165
- permanent authorization for you to choose that version for the
166
- Library.