Files
Netcatty/electron/main/appLockLifecycle.cjs
T
bincxz 25d6646ffe fix(app-lock): re-lock on macOS reopen after last window closes
On darwin, closing every window leaves the process alive while app-lock
runtime stays unlocked. Re-apply startup lock when the last window closes
and before creating a fresh main window with no app-content windows.
2026-08-06 00:13:51 +08:00

252 lines
7.0 KiB
JavaScript

"use strict";
function emitAppLockReopen(windows) {
const seen = new Set();
for (const win of Array.isArray(windows) ? windows : []) {
try {
if (!win || win.isDestroyed?.()) continue;
const id = win.webContents?.id;
if (id && seen.has(id)) continue;
if (id) seen.add(id);
win.webContents?.send?.("netcatty:app-lock:reopen");
} catch {
// ignore
}
}
}
function shouldBackgroundLockOnHide(appLockController) {
return Boolean(appLockController && typeof appLockController.setLocked === "function");
}
function handleAppHide(appLockController) {
if (!shouldBackgroundLockOnHide(appLockController)) return false;
try {
appLockController.setLocked("background");
return true;
} catch {
return false;
}
}
/**
* macOS keeps the process alive after the last BrowserWindow closes
* (`window-all-closed` only quits on non-darwin). App-lock runtime is shared
* for the process lifetime, so an unlock can otherwise survive a full
* close → Dock reopen cycle. Re-apply lock with `startup` (same reason as
* cold start) so a fresh renderer withholds content until unlock. Prefer this
* over `background`, which waits on a reopen signal that a brand-new window
* never receives.
*/
function ensureAppLockForFreshSession(appLockController, reason = "startup") {
if (!shouldBackgroundLockOnHide(appLockController)) return false;
const nextReason = typeof reason === "string" && reason.trim() !== "" ? reason : "startup";
try {
appLockController.setLocked(nextReason);
return true;
} catch {
return false;
}
}
/**
* True when no usable app-content windows remain (main / settings / etc.).
* Used to decide whether recreating a main window is a "fresh session" reopen
* after the last window was closed, vs. adding another window while one is open.
*/
function hasNoUsableAppContentWindows(appContentWindows) {
if (!Array.isArray(appContentWindows) || appContentWindows.length === 0) return true;
return !appContentWindows.some((win) => {
try {
return Boolean(win && !(typeof win.isDestroyed === "function" && win.isDestroyed()));
} catch {
return false;
}
});
}
function handleActivateWithMainWindow({
app,
mainWindow,
globalShortcutBridge,
windowManager,
reopenWindows,
}) {
if (!mainWindow || mainWindow.isDestroyed?.()) return false;
try {
if (mainWindow.webContents?.isCrashed?.()) {
mainWindow.destroy?.();
return false;
}
} catch {
// ignore
}
try {
globalShortcutBridge?.clearPendingFullscreenHide?.(mainWindow);
} catch {
// ignore
}
if (typeof windowManager?.showAndFocusMainWindow === "function") {
windowManager.showAndFocusMainWindow(mainWindow);
} else {
try {
if (mainWindow.isMinimized?.()) mainWindow.restore?.();
} catch {
// ignore
}
try {
mainWindow.show?.();
} catch {
// ignore
}
try {
mainWindow.focus?.();
} catch {
// ignore
}
}
emitAppLockReopen(reopenWindows);
try {
app?.focus?.({ steal: true });
} catch {
// ignore
}
return true;
}
function shouldCommitQuitWithoutDirtyCheck({
reachableMainWindows,
queryableWebContents,
}) {
const hasReachableMainWindows = Array.isArray(reachableMainWindows) && reachableMainWindows.length > 0;
if (!hasReachableMainWindows) return true;
const hasQueryableWebContents = Array.isArray(queryableWebContents) && queryableWebContents.length > 0;
return !hasQueryableWebContents;
}
async function handleBeforeQuit({
event,
mainWindows,
queryDirtyEditors,
appLockController,
windowManager,
app,
ipcMain,
quitConfirmed,
quitGuardChannelBusy,
timeoutMs,
setQuitGuardChannelBusy,
setQuitConfirmed,
}) {
if (quitConfirmed) return { committed: false, skipped: "quit-confirmed" };
if (quitGuardChannelBusy) {
event?.preventDefault?.();
return { committed: false, skipped: "busy" };
}
const reachableMainWindows = (Array.isArray(mainWindows) ? mainWindows : []).filter((candidate) => (
candidate && !candidate.isDestroyed?.()
));
// Keep the window alongside webContents so a dirty result can bring the
// owning window forward (hidden-to-tray / unfocused) before aborting quit.
const queryableWindows = reachableMainWindows.filter((candidate) => {
const wc = candidate.webContents;
return wc && !wc.isDestroyed?.() && !wc.isCrashed?.();
});
const queryableWebContents = queryableWindows
.map((candidate) => candidate.webContents)
.filter(Boolean);
if (shouldCommitQuitWithoutDirtyCheck({ reachableMainWindows, queryableWebContents })) {
if (shouldBackgroundLockOnHide(appLockController)) {
appLockController.setLocked("background");
}
windowManager?.setIsQuitting?.(true);
setQuitConfirmed?.(true);
app?.quit?.();
return { committed: true, skipped: "fast-path" };
}
setQuitGuardChannelBusy?.(true);
event?.preventDefault?.();
try {
const dirtyResults = await Promise.all(
queryableWindows.map((win) =>
queryDirtyEditors(win.webContents, timeoutMs, { ipcMain }).then((hasDirty) => ({
win,
hasDirty: Boolean(hasDirty),
})),
),
);
setQuitGuardChannelBusy?.(false);
const dirtyWindows = dirtyResults.filter((result) => result.hasDirty).map((result) => result.win);
if (dirtyWindows.length === 0) {
if (shouldBackgroundLockOnHide(appLockController)) {
appLockController.setLocked("background");
}
windowManager?.setIsQuitting?.(true);
setQuitConfirmed?.(true);
app?.quit?.();
return { committed: true, skipped: null };
}
// Renderer only surfaces the dirty toast in its own window. Focus every
// dirty owner so a tray-hidden or backgrounded window is visible before
// we refuse to quit.
for (const win of dirtyWindows) {
try {
if (typeof windowManager?.showAndFocusMainWindow === "function") {
windowManager.showAndFocusMainWindow(win);
} else {
try {
if (win.isMinimized?.()) win.restore?.();
} catch {
// ignore
}
try {
win.show?.();
} catch {
// ignore
}
try {
win.focus?.();
} catch {
// ignore
}
}
} catch {
// ignore
}
}
if (windowManager?.isQuittingForUpdate?.()) {
windowManager.setQuittingForUpdate?.(false);
}
return { committed: false, skipped: "dirty" };
} catch {
setQuitGuardChannelBusy?.(false);
if (shouldBackgroundLockOnHide(appLockController)) {
appLockController.setLocked("background");
}
windowManager?.setIsQuitting?.(true);
setQuitConfirmed?.(true);
app?.quit?.();
return { committed: true, skipped: "error" };
}
}
module.exports = {
emitAppLockReopen,
ensureAppLockForFreshSession,
handleAppHide,
handleActivateWithMainWindow,
handleBeforeQuit,
hasNoUsableAppContentWindows,
shouldBackgroundLockOnHide,
shouldCommitQuitWithoutDirtyCheck,
};