mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-25 09:51:28 +00:00
在「设置 → 系统 → 网络代理」卡片上方新增开机自启动开关,开启后 系统启动时自动运行 Netcatty,并隐藏到系统托盘(不占用任务栏)。 - 新增 electron/autoLaunch.cjs 封装 app.setLoginItemSettings, 通过 --hidden 启动参数标记本次是登录项冷启动;开发环境 (process.defaultApp)下判定为不支持,避免误操作系统登录项。 - 主窗口首次冷启动读取该标志后跳过 win.show(),并在托管桥注册 完成后强制创建托盘图标(不依赖用户是否开启「关闭时最小化到 托盘」),确保隐藏窗口始终可被找到;后续任何窗口复用/深链接/ 托盘点击路径不受影响。 - 开关真实状态以主进程 app.getLoginItemSettings() 为准,设置页 挂载时向主进程查询校准,而非只信任本地缓存。 - 新增单测覆盖 autoLaunch 纯逻辑及 startHidden 透传/托盘保底 行为,过程中发现并修复一处因未走依赖注入直接 require 模块 导致的隐患。 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
76 lines
2.6 KiB
JavaScript
76 lines
2.6 KiB
JavaScript
/**
|
|
* Auto Launch - Registers Netcatty to start at system login, hidden to the
|
|
* tray. Thin wrapper around Electron's app.setLoginItemSettings/
|
|
* getLoginItemSettings so main.cjs and the settings IPC handlers share one
|
|
* source of truth.
|
|
*
|
|
* Development runs (`electron .`) are unsupported: process.execPath points
|
|
* at the transient electron.exe, so a login item registered there would
|
|
* break (or silently do nothing) after the dev process exits.
|
|
*/
|
|
|
|
const HIDDEN_LAUNCH_ARG = "--hidden";
|
|
|
|
function isAutoLaunchSupported({ defaultApp = process.defaultApp } = {}) {
|
|
return !defaultApp;
|
|
}
|
|
|
|
function getAutoLaunchEnabled({ app, defaultApp = process.defaultApp } = {}) {
|
|
if (!isAutoLaunchSupported({ defaultApp })) {
|
|
return { enabled: false, supported: false };
|
|
}
|
|
try {
|
|
const settings = app.getLoginItemSettings();
|
|
return { enabled: Boolean(settings?.openAtLogin), supported: true };
|
|
} catch (err) {
|
|
console.warn("[AutoLaunch] Failed to read login item settings:", err?.message || err);
|
|
return { enabled: false, supported: true };
|
|
}
|
|
}
|
|
|
|
function setAutoLaunchEnabled(enabled, { app, execPath = process.execPath, defaultApp = process.defaultApp } = {}) {
|
|
if (!isAutoLaunchSupported({ defaultApp })) {
|
|
return { success: false, enabled: false, supported: false };
|
|
}
|
|
const wantEnabled = Boolean(enabled);
|
|
try {
|
|
app.setLoginItemSettings({
|
|
openAtLogin: wantEnabled,
|
|
// openAsHidden only applies on macOS; Windows/Linux rely on the
|
|
// --hidden arg below, which main.cjs checks on cold start.
|
|
openAsHidden: wantEnabled,
|
|
path: execPath,
|
|
args: wantEnabled ? [HIDDEN_LAUNCH_ARG] : [],
|
|
});
|
|
const settings = app.getLoginItemSettings();
|
|
return { success: true, enabled: Boolean(settings?.openAtLogin), supported: true };
|
|
} catch (err) {
|
|
console.warn("[AutoLaunch] Failed to update login item settings:", err?.message || err);
|
|
return { success: false, enabled: getAutoLaunchEnabled({ app, defaultApp }).enabled, supported: true };
|
|
}
|
|
}
|
|
|
|
/** True when this process was launched by the OS login item (cold start only). */
|
|
function wasLaunchedHidden(argv = process.argv) {
|
|
return Array.isArray(argv) && argv.includes(HIDDEN_LAUNCH_ARG);
|
|
}
|
|
|
|
function registerHandlers(ipcMain, { app }) {
|
|
ipcMain.handle("netcatty:autoLaunch:get", async () => {
|
|
return getAutoLaunchEnabled({ app });
|
|
});
|
|
|
|
ipcMain.handle("netcatty:autoLaunch:set", async (_event, { enabled }) => {
|
|
return setAutoLaunchEnabled(enabled, { app });
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
HIDDEN_LAUNCH_ARG,
|
|
isAutoLaunchSupported,
|
|
getAutoLaunchEnabled,
|
|
setAutoLaunchEnabled,
|
|
wasLaunchedHidden,
|
|
registerHandlers,
|
|
};
|