From 087113d8a1214ba4c7df03bfe66747d8d944280c Mon Sep 17 00:00:00 2001 From: Markus Hatvan Date: Tue, 14 Sep 2021 11:03:28 +0200 Subject: chore: convert JS to TS (#1934) --- src/electron/ipc-api/appIndicator.ts | 83 ++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/electron/ipc-api/appIndicator.ts (limited to 'src/electron/ipc-api/appIndicator.ts') diff --git a/src/electron/ipc-api/appIndicator.ts b/src/electron/ipc-api/appIndicator.ts new file mode 100644 index 000000000..5b5f2bac7 --- /dev/null +++ b/src/electron/ipc-api/appIndicator.ts @@ -0,0 +1,83 @@ +import { app, ipcMain } from 'electron'; +import { join } from 'path'; +import { autorun } from 'mobx'; +import { isMac, isWindows, isLinux } from '../../environment'; + +const INDICATOR_TASKBAR = 'taskbar'; +const FILE_EXTENSION = isWindows ? 'ico' : 'png'; + +let isTrayIconEnabled: boolean; + +function getAsset(type: 'tray' | 'taskbar', asset: string) { + return join( + __dirname, + '..', + '..', + 'assets', + 'images', + type, + process.platform, + `${asset}.${FILE_EXTENSION}`, + ); +} + +export default params => { + autorun(() => { + isTrayIconEnabled = params.settings.app.get('enableSystemTray'); + + if (!isTrayIconEnabled) { + params.trayIcon.hide(); + } else if (isTrayIconEnabled) { + params.trayIcon.show(); + } + }); + + ipcMain.on('updateAppIndicator', (_event, args) => { + // Flash TaskBar for windows, bounce Dock on Mac + if (!(app as any).mainWindow.isFocused()) { + if (params.settings.app.get('notifyTaskBarOnMessage')) { + if (isWindows) { + (app as any).mainWindow.flashFrame(true); + (app as any).mainWindow.once('focus', () => + (app as any).mainWindow.flashFrame(false), + ); + } else if (isMac) { + app.dock.bounce('informational'); + } + } + } + + // Update badge + if (isMac && typeof args.indicator === 'string') { + app.dock.setBadge(args.indicator); + } + + if ((isMac || isLinux) && typeof args.indicator === 'number') { + app.badgeCount = args.indicator; + } + + if (isWindows) { + if (typeof args.indicator === 'number' && args.indicator !== 0) { + params.mainWindow.setOverlayIcon( + getAsset( + 'taskbar', + `${INDICATOR_TASKBAR}-${ + args.indicator >= 10 ? 10 : args.indicator + }`, + ), + '', + ); + } else if (typeof args.indicator === 'string') { + params.mainWindow.setOverlayIcon( + getAsset('taskbar', `${INDICATOR_TASKBAR}-alert`), + '', + ); + } else { + params.mainWindow.setOverlayIcon(null, ''); + } + } + + // Update Tray + params.trayIcon.setIndicator(args.indicator); + }); +}; -- cgit v1.2.3-70-g09d2