Skip to content

Commit 20a5121

Browse files
author
while
committed
mac fix
1 parent f3dca05 commit 20a5121

7 files changed

Lines changed: 156 additions & 30 deletions

File tree

‎.vscode/launch.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
{
55
"type": "cppvsdbg",
66
"request": "launch",
7-
"name": "Tauri Debug (dev) · Windows",
7+
"name": "Launch App · Windows",
88
"preLaunchTask": "tauri:before:dev",
99
"program": "${workspaceFolder}/packages/app/src-tauri/target/debug/sbox.exe",
1010
"cwd": "${workspaceFolder}/packages/app/src-tauri"
1111
},
1212
{
1313
"type": "lldb",
1414
"request": "launch",
15-
"name": "Tauri Debug (dev) · macOS",
15+
"name": "Launch App · macOS",
1616
"preLaunchTask": "tauri:before:dev",
1717
"program": "${workspaceFolder}/packages/app/src-tauri/target/debug/sbox",
1818
"cwd": "${workspaceFolder}/packages/app/src-tauri"

‎.vscode/tasks.json‎

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,22 @@
33
"tasks": [
44
{
55
"label": "ui:kill",
6+
"type": "process",
7+
"command": "node",
8+
"args": ["${workspaceFolder}/scripts/kill-port.cjs", "1400"],
9+
"problemMatcher": []
10+
},
11+
{
12+
"label": "debug:app",
613
"type": "shell",
7-
"command": "npx kill-port 1400",
14+
"command": "pnpm run debug:app",
815
"options": { "cwd": "${workspaceFolder}" },
9-
"problemMatcher": []
16+
"group": {
17+
"kind": "build",
18+
"isDefault": true
19+
},
20+
"problemMatcher": ["$rustc"],
21+
"presentation": { "reveal": "silent", "panel": "dedicated" }
1022
},
1123
{
1224
"label": "ui:dev",
@@ -17,28 +29,18 @@
1729
"options": { "cwd": "${workspaceFolder}/packages/app" },
1830
"problemMatcher": {
1931
"owner": "vite",
20-
"pattern": { "regexp": "" },
32+
"pattern": { "regexp": ".+" },
2133
"background": {
2234
"activeOnStart": true,
23-
"beginsPattern": ".*",
24-
"endsPattern": "(ready in|Local:).*"
35+
"beginsPattern": ".+",
36+
"endsPattern": "Local.*1400"
2537
}
26-
}
27-
},
28-
{
29-
"label": "rust:build:dev",
30-
"type": "shell",
31-
"command": "cargo",
32-
"args": [
33-
"build",
34-
"--manifest-path=${workspaceFolder}/packages/app/src-tauri/Cargo.toml",
35-
"--no-default-features"
36-
],
37-
"problemMatcher": ["$rustc"]
38+
},
39+
"presentation": { "reveal": "always", "panel": "dedicated" }
3840
},
3941
{
4042
"label": "tauri:before:dev",
41-
"dependsOn": ["ui:kill", "rust:build:dev", "ui:dev"],
43+
"dependsOn": ["ui:kill", "debug:app", "ui:dev"],
4244
"dependsOrder": "sequence"
4345
}
4446
]

‎package.json‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,17 @@
99
"build:web": "pnpm --filter @sbox/web build",
1010
"preview:web": "pnpm --filter @sbox/web preview",
1111
"dev": "pnpm --filter @sbox/app dev",
12-
"dev:app": "pnpm --filter @sbox/app dev",
1312
"dev:app:mac": "pnpm --filter @sbox/app macos:dev",
1413
"build:app": "pnpm --filter @sbox/app windows:build",
1514
"build:app:mac": "pnpm --filter @sbox/app macos:build",
1615
"tauri": "pnpm --filter @sbox/app tauri",
16+
"debug:app": "cargo build --manifest-path packages/app/src-tauri/Cargo.toml --no-default-features",
1717
"release": "node scripts/release.cjs",
1818
"docs:dev": "vitepress dev docs-site",
1919
"docs:build": "vitepress build docs-site",
2020
"docs:preview": "vitepress preview docs-site"
2121
},
2222
"devDependencies": {
23-
"typescript": "catalog:",
24-
"vite": "catalog:",
2523
"vitepress": "^1.6.3"
2624
},
2725
"pnpm": {

‎packages/app/src-tauri/build.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44
const KEYS: &[&str] = &["GDRIVE_BUILTIN_CLIENT_ID", "GDRIVE_BUILTIN_CLIENT_SECRET"];
55

66
fn main() {
7+
// macOS:screencapturekit → swift-rs 会把 @rpath/libswiftCore.dylib 等系统 Swift 运行库
8+
// 链进最终二进制,但 rustc 不会像 Xcode 那样自动补 LC_RPATH,缺少 rpath 时应用会在
9+
// 启动阶段被 dyld abort("Library not loaded: @rpath/libswiftCore.dylib, no LC_RPATH's found")。
10+
// /usr/lib/swift 是系统自带 Swift 运行时(dyld shared cache)的解析路径,随系统存在;
11+
// @executable_path/../Frameworks 兜底,以便日后向 .app/Contents/Frameworks 内嵌库时无需改动。
12+
if std::env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("macos") {
13+
println!("cargo:rustc-link-arg=-Wl,-rpath,/usr/lib/swift");
14+
println!("cargo:rustc-link-arg=-Wl,-rpath,@executable_path/../Frameworks");
15+
}
716
for key in KEYS {
817
let value = std::env::var(key)
918
.ok()

‎packages/app/src/tools/media/screenshot/ScreenshotOverlay.vue‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,13 @@ onMounted(async () => {
262262
const drawAt = performance.now()
263263
// 首次挂载时让画布先按已设置的窗口尺寸完成一次布局,再显示原生窗口;
264264
// 避免 Windows 把默认尺寸的 WebView 放大到全屏,形成缩放动画。
265-
await new Promise<void>((resolve) => requestAnimationFrame(() => resolve()))
265+
// macOS 的隐藏窗口会暂停 requestAnimationFrame,必须给超时兜底,否则覆盖层永不显示。
266+
await new Promise<void>((resolve) => {
267+
let done = false
268+
const finish = () => { if (!done) { done = true; resolve() } }
269+
requestAnimationFrame(finish)
270+
setTimeout(finish, 150)
271+
})
266272
if (!(await w.isVisible())) await w.show()
267273
await w.setIgnoreCursorEvents(false)
268274
await w.setFocus()

‎pnpm-lock.yaml‎

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎scripts/kill-port.cjs‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#!/usr/bin/env node
2+
3+
// 释放被占用的端口:找到监听该端口的进程,连同启动它的工具链父进程一起结束。
4+
// 用法: node kill-port.js <端口>
5+
//
6+
// 为什么不直接杀监听进程:`tsx --watch` 这类父进程会在子进程被杀后重新拉起,
7+
// 因此必须沿祖先链把整个工具链一起结束(遇到 shell 终端即停止,不会关闭终端)。
8+
9+
const { execFileSync } = require("node:child_process");
10+
11+
const port = Number(process.argv[2]);
12+
if (!Number.isInteger(port) || port <= 0) {
13+
console.error("用法: node kill-port.js <端口>");
14+
process.exit(1);
15+
}
16+
17+
function findListenerPids() {
18+
if (process.platform === "win32") {
19+
const output = execFileSync("netstat", ["-ano"], { encoding: "utf8" });
20+
const pids = new Set();
21+
for (const line of output.split("\n")) {
22+
const columns = line.trim().split(/\s+/);
23+
// 协议 本地地址 远程地址 状态 PID
24+
if (columns.length === 5 && columns[3] === "LISTENING" && columns[1].endsWith(`:${port}`)) {
25+
pids.add(Number(columns[4]));
26+
}
27+
}
28+
return [...pids];
29+
}
30+
// lsof 在没有找到监听进程时以退出码 1 结束,属正常情况,按空结果处理
31+
let output = "";
32+
try {
33+
output = execFileSync("lsof", [`-ti:${port}`, "-sTCP:LISTEN"], { encoding: "utf8" });
34+
} catch (error) {
35+
output = error.stdout || "";
36+
}
37+
return [...new Set(output.split("\n").map(Number).filter(Boolean))];
38+
}
39+
40+
const TOOLCHAIN_NAMES = new Set(["node", "node.exe", "pnpm", "npm", "npx", "cross-env", "tsx", "shx"]);
41+
const TOOLCHAIN_CMD = /pnpm|tsx|cross-env/;
42+
43+
function isToolchainProcess(name, commandLine) {
44+
if (TOOLCHAIN_NAMES.has(name)) return true;
45+
// Windows 上 pnpm 会包一层 cmd.exe /c pnpm ...
46+
return name === "cmd.exe" && TOOLCHAIN_CMD.test(commandLine);
47+
}
48+
49+
function findWinProcess(procId) {
50+
const script = `Get-CimInstance Win32_Process -Filter "ProcessId=${procId}" | Select-Object -First 1 | ForEach-Object { "$($_.ParentProcessId)|$($_.Name)|$($_.CommandLine)" }`;
51+
try {
52+
const output = execFileSync("powershell", ["-NoProfile", "-Command", script], { encoding: "utf8" }).trim();
53+
if (!output) return null;
54+
const separator = output.indexOf("|");
55+
const parent = Number(output.slice(0, separator));
56+
const rest = output.slice(separator + 1);
57+
const nameSeparator = rest.indexOf("|");
58+
return { parent, name: rest.slice(0, nameSeparator), commandLine: rest.slice(nameSeparator + 1) };
59+
} catch {
60+
return null;
61+
}
62+
}
63+
64+
function collectAncestorTree(pid) {
65+
const pids = [pid];
66+
let current = pid;
67+
for (let depth = 0; depth < 15; depth += 1) {
68+
if (process.platform === "win32") {
69+
const info = findWinProcess(current);
70+
if (!info || !info.parent || info.parent <= 4) break;
71+
const parent = findWinProcess(info.parent);
72+
if (!parent || !isToolchainProcess(parent.name, parent.commandLine)) break;
73+
pids.push(info.parent);
74+
current = info.parent;
75+
} else {
76+
let parentPid;
77+
let parentName;
78+
try {
79+
parentPid = Number(execFileSync("ps", ["-o", "ppid=", "-p", String(current)], { encoding: "utf8" }).trim());
80+
parentName = execFileSync("ps", ["-o", "comm=", "-p", String(parentPid)], { encoding: "utf8" }).trim();
81+
} catch {
82+
break;
83+
}
84+
if (!parentPid || parentPid <= 1) break;
85+
if (!isToolchainProcess(parentName.split("/").pop(), "")) break;
86+
pids.push(parentPid);
87+
current = parentPid;
88+
}
89+
}
90+
return pids;
91+
}
92+
93+
function killProcess(target) {
94+
try {
95+
if (process.platform === "win32") {
96+
execFileSync("taskkill", ["/PID", String(target), "/F"], { stdio: "ignore" });
97+
} else {
98+
process.kill(target, "SIGKILL");
99+
}
100+
} catch {
101+
// 进程可能已经退出,忽略
102+
}
103+
}
104+
105+
const pids = findListenerPids();
106+
if (pids.length === 0) {
107+
console.log(`端口 ${port} 已空闲`);
108+
process.exit(0);
109+
}
110+
for (const pid of pids) {
111+
const tree = collectAncestorTree(pid);
112+
for (const target of tree) {
113+
killProcess(target);
114+
}
115+
console.log(`已结束进程: ${tree.join(" <- ")}`);
116+
}
117+
console.log(`已释放端口 ${port}`);

0 commit comments

Comments
 (0)