import electron from "electron"; const fakeWindow = { setMenu(menu: Electron.Menu) {}, } as Electron.BrowserWindow; export const BrowserWindow = { fromId(id: number): electron.BrowserWindow { if (!process.type) { return fakeWindow; } return electron.BrowserWindow.fromId(id); }, };
app.on('before-quit', () => { isQuitting = true; config.set('lastWindowState', mainWindow.getNormalBounds()); });
notification.on('click', () => { mainWindow.show(); sendAction('notification-callback', {callbackName: 'onclick', id}); notifications.delete(id); });
click: () => { mainWindow.show(); sendAction('jump-to-conversation', index + 1); }
app.on('activate', () => { if (mainWindow) { mainWindow.show(); } });
ipcMain.on('update-overlay-icon', (_event: ElectronEvent, data: string, text: string) => { const img = nativeImage.createFromDataURL(data); mainWindow.setOverlayIcon(img, text); });
function createMainWindow(): BrowserWindow { const lastWindowState = config.get('lastWindowState'); const isDarkMode = config.get('darkMode'); // Messenger or Work Chat const mainURL = config.get('useWorkChat') ? 'https://work.facebook.com/chat' : 'https://www.messenger.com/login/'; const win = new BrowserWindow({ title: app.getName(), show: false, x: lastWindowState.x, y: lastWindowState.y, width: lastWindowState.width, height: lastWindowState.height, icon: is.linux ? path.join(__dirname, '..', 'static', 'Icon.png') : undefined, minWidth: 400, minHeight: 200, alwaysOnTop: config.get('alwaysOnTop'), titleBarStyle: 'hiddenInset', autoHideMenuBar: config.get('autoHideMenuBar'), darkTheme: isDarkMode, // GTK+3 webPreferences: { preload: path.join(__dirname, 'browser.js'), nodeIntegration: false, contextIsolation: true, plugins: true } }); setUserLocale(); initRequestsFiltering(); darkMode.onChange(() => { win.webContents.send('set-dark-mode'); }); if (is.macos) { win.setSheetOffset(40); } win.loadURL(mainURL); win.on('close', e => { if (config.get('quitOnWindowClose')) { app.quit(); return; } if (!isQuitting) { e.preventDefault(); // Workaround for electron/electron#10023 win.blur(); win.hide(); } }); win.on('focus', () => { if (config.get('flashWindowOnMessage')) { // This is a security in the case where messageCount is not reset by page title update win.flashFrame(false); } }); return win; }
export const loadFile = async (appPath: string) => { mainWindow = await createWindow() mainWindow.loadFile(appPath) mainWindow.focus() }
export const loadURL = async (appUrl: string) => { mainWindow = await createWindow() mainWindow.loadURL(appUrl) mainWindow.focus() }
function createWindow(config: Config, icon_path: string) { const display_size = screen.getPrimaryDisplay().workAreaSize as DisplaySize; function getConfigLength(key: 'width'|'height'): number { const len = config[key]; const default_len = default_config[key]; switch (typeof len) { case 'string': { if (len === 'max') { return display_size[key]; } return default_len; } case 'number': { return len; } default: { return default_len; } } } const config_width = getConfigLength('width'); const config_height = getConfigLength('height'); const win_state = windowState({ defaultWidth: config_width, defaultHeight: config_height, }); let options: Electron.BrowserWindowOptions; if (config.restore_window_state) { options = { x: win_state.x, y: win_state.y, width: win_state.width, height: win_state.height, }; } else { options = { width: config_width, height: config_height, }; } options.icon = icon_path; options.autoHideMenuBar = config.hide_menu_bar; if (config.hide_title_bar) { options.titleBarStyle = 'hidden-inset'; } const win = new BrowserWindow(options); if (config.restore_window_state) { if (win_state.isFullScreen) { win.setFullScreen(true); } else if (win_state.isMaximized) { win.maximize(); } win_state.manage(win); } return win; }