Files
client/app/main/os-settings-main.js
T

190 lines
7.6 KiB
JavaScript

'use strict';
const path = require('node:path');
const { app, Menu, Notification, Tray, powerMonitor } = require('electron');
/**
* Official Settings → OS Specific (settings.os.windows.*), account-free: Start with the OS, flash the taskbar on
* messages / pokes, hide in the taskbar on minimize or close (with a tray icon to come back), mute the microphone
* while the computer is locked; plus the "Server Private Chat" system notification.
*
* Every item is explicit opt-in (all defaults off) and reports what the platform API actually supports:
* - autostart: Electron app.setLoginItemSettings exists on Windows and macOS only; installed builds only (a dev
* run would register the Electron binary). The OS login item is the source of truth, never a stored pref.
* - lock events: powerMonitor 'lock-screen' / 'unlock-screen' are emitted on Windows and macOS only.
* - setSkipTaskbar: Windows and Linux (no-op on macOS, where the Dock is not a taskbar).
* In TEAMSPEACH_TEST_OFFSCREEN nothing native is touched: no login item, tray, flash or notification.
*/
const HIDE_MODES = new Set(['never', 'minimize', 'close', 'both']);
function capabilities({ testOffscreen }) {
const platform = process.platform;
const loginItems = platform === 'win32' || platform === 'darwin';
const autoStart = testOffscreen
? { supported: false, reason: 'test_mode' }
: !loginItems
? { supported: false, reason: 'platform' }
: !app.isPackaged
? { supported: false, reason: 'not_packaged' }
: { supported: true, enabled: Boolean(app.getLoginItemSettings().openAtLogin) };
return {
platform,
autoStart,
flashFrame: { supported: !testOffscreen },
hideInTaskbar: { supported: !testOffscreen && (platform === 'win32' || platform === 'linux') },
lockMute: { supported: !testOffscreen && (platform === 'win32' || platform === 'darwin') },
notifications: { supported: !testOffscreen && Notification.isSupported() },
};
}
/**
* @param {object} o
* @param {Electron.IpcMain} o.ipcMain
* @param {(event) => boolean} o.trustedSender
* @param {() => Electron.BrowserWindow | null} o.getWindow
* @param {boolean} o.testOffscreen
* @param {{ get(): object }} o.prefs
* @param {(key: string) => string} o.translate main-side translator for tray labels
* @param {string} o.iconPath tray icon file
* @returns {{ attachWindow(win): void, interceptClose(event): boolean, markQuitting(): void }}
*/
function registerOsSettings({ ipcMain, trustedSender, getWindow, testOffscreen, prefs, translate, iconPath }) {
let tray = null;
let quitting = false;
let lockListenersOn = false;
const caps = () => capabilities({ testOffscreen });
const hideMode = () => (HIDE_MODES.has(prefs.get().osHideInTaskbar) ? prefs.get().osHideInTaskbar : 'never');
function showWindow() {
const win = getWindow();
if (!win || win.isDestroyed()) return;
win.setSkipTaskbar(false);
if (win.isMinimized()) win.restore();
win.show();
win.focus();
}
function ensureTray() {
if (tray || testOffscreen) return;
tray = new Tray(iconPath);
tray.setToolTip('TeamsPeach');
tray.setContextMenu(Menu.buildFromTemplate([
{ label: translate('os.tray.show'), click: showWindow },
{ type: 'separator' },
{ label: translate('os.tray.quit'), click: () => { quitting = true; app.quit(); } },
]));
tray.on('click', showWindow);
}
function dropTrayIfUnused() {
if (tray && hideMode() === 'never') {
const win = getWindow();
if (win && !win.isDestroyed()) {
win.setSkipTaskbar(false);
if (!win.isVisible()) win.show();
}
tray.destroy();
tray = null;
}
}
function updateLockListeners() {
const want = !testOffscreen && caps().lockMute.supported && prefs.get().osMuteWhenLocked === true;
if (want === lockListenersOn) return;
lockListenersOn = want;
if (want) {
powerMonitor.on('lock-screen', onLock);
powerMonitor.on('unlock-screen', onUnlock);
} else {
powerMonitor.off('lock-screen', onLock);
powerMonitor.off('unlock-screen', onUnlock);
}
}
function send(locked) {
const win = getWindow();
if (win && !win.isDestroyed()) win.webContents.send('os:lock', locked);
}
function onLock() { send(true); }
function onUnlock() { send(false); }
const forbidden = { ok: false, error: { code: 'forbidden', message: 'forbidden' } };
ipcMain.handle('os:capabilities', (event) => (trustedSender(event) ? caps() : null));
ipcMain.handle('os:set-autostart', (event, enabled) => {
if (!trustedSender(event)) return forbidden;
if (typeof enabled !== 'boolean') return { ok: false, error: { code: 'bad_request', message: 'expected true/false' } };
const c = caps().autoStart;
if (!c.supported) return { ok: false, error: { code: 'unsupported', message: c.reason } };
try {
app.setLoginItemSettings({ openAtLogin: enabled });
return { ok: true, enabled: Boolean(app.getLoginItemSettings().openAtLogin) };
} catch (error) {
return { ok: false, error: { code: 'os_error', message: String(error.message ?? error) } };
}
});
// Called by the renderer after it saved osHideInTaskbar / osMuteWhenLocked, so main applies them at once.
ipcMain.handle('os:apply', (event) => {
if (!trustedSender(event)) return forbidden;
updateLockListeners();
dropTrayIfUnused();
return { ok: true };
});
ipcMain.handle('os:flash', (event, kind) => {
if (!trustedSender(event) || testOffscreen) return false;
const p = prefs.get();
const allowed = (kind === 'message' && p.osFlashOnMessage === true) || (kind === 'poke' && p.osFlashOnPoke === true);
const win = getWindow();
if (!allowed || !win || win.isDestroyed() || win.isFocused()) return false;
win.flashFrame(true);
win.once('focus', () => { if (!win.isDestroyed()) win.flashFrame(false); });
return true;
});
ipcMain.handle('os:notify', (event, title, body, kind = 'private') => {
if (!trustedSender(event) || testOffscreen) return false;
if (kind !== 'private' && kind !== 'channel') return false;
const preference = kind === 'channel' ? 'systemNotifyChannelChat' : 'systemNotifyServerPrivate';
if (prefs.get()[preference] !== true || !Notification.isSupported()) return false;
if (typeof title !== 'string' || typeof body !== 'string' || title.length > 200) return false;
const win = getWindow();
if (win && !win.isDestroyed() && win.isFocused()) return false;
const n = new Notification({ title, body: body.slice(0, 500), silent: true });
n.on('click', showWindow);
n.show();
return true;
});
updateLockListeners();
return {
attachWindow(win) {
if (testOffscreen) return;
win.on('minimize', () => {
if (!['minimize', 'both'].includes(hideMode()) || !caps().hideInTaskbar.supported) return;
ensureTray();
win.setSkipTaskbar(true);
});
win.on('restore', () => win.setSkipTaskbar(false));
win.on('show', () => win.setSkipTaskbar(false));
},
/** Call first in the window 'close' handler; true = the window was hidden instead of closed. */
interceptClose(event) {
if (quitting || !['close', 'both'].includes(hideMode()) || !caps().hideInTaskbar.supported) return false;
const win = getWindow();
if (!win || win.isDestroyed()) return false;
ensureTray();
event.preventDefault();
win.hide();
return true;
},
markQuitting() { quitting = true; },
};
}
module.exports = { registerOsSettings, capabilities, HIDE_MODES, defaultIconPath: () => path.join(__dirname, '..', 'renderer', 'assets', 'icon.png') };