From 3d7b34b40dbf268c835814ef1358cfe4280768f2 Mon Sep 17 00:00:00 2001 From: mrvnklm <24477241+mrvnklm@users.noreply.github.com> Date: Wed, 20 May 2020 12:21:46 +0200 Subject: [PATCH] v1.2.0 - multiple instance support --- .github/workflows/build.yml | 2 +- app.js | 101 ++++++++++++++++++++++++++++++------ web/index.html | 4 +- 3 files changed, 87 insertions(+), 20 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1d310a1..dee06a3 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -3,7 +3,7 @@ name: Build/release on: push: tags: - - v** + - '*' jobs: release: diff --git a/app.js b/app.js index 29dca0f..1c076cd 100644 --- a/app.js +++ b/app.js @@ -4,6 +4,8 @@ const AutoLaunch = require('auto-launch'); const Positioner = require('electron-traywindow-positioner'); const Store = require('electron-store'); +const indexFile = `file://${__dirname}/web/index.html`; + app.allowRendererProcessReuse = true; // prevent multiple instances @@ -32,12 +34,12 @@ const useAutoUpdater = () => { setInterval(() => { autoUpdater.checkForUpdates(); - }, 1000 * 60 * 30) + }, 1000 * 60 * 30); autoUpdater.on('update-downloaded', () => { autoUpdater.quitAndInstall(); }); -} +}; const checkAutoStart = () => { autoLauncher.isEnabled().then((isEnabled) => { @@ -48,18 +50,57 @@ const checkAutoStart = () => { }; const getMenu = () => { - return Menu.buildFromTemplate([ + let instancesMenu = [{ + label: 'Open in Browser', + enabled: currentInstance(), + click: () => { + shell.openExternal(currentInstance()) + } + }, { - label: (store.has('instance') ? store.get('instance').name : 'Not connected...'), - enabled: !!store.has('instance'), - click: () => { - shell.openExternal(store.get('instance').url) + type: 'separator' + } + ]; + + const allInstances = store.get('allInstances'); + + if (allInstances) { + allInstances.forEach((e) => { + instancesMenu.push({ + label: e, + type: 'checkbox', + checked: currentInstance() === e, + click: () => { + currentInstance(e); + window.loadURL(e); + window.show(); + } + }); + }); + + instancesMenu.push( + { + type: 'separator' + }, + { + label: 'Add another Instance...', click: () => { + store.delete('currentInstance'); + window.loadURL(indexFile); + window.show(); + } } - }, { + ) + } else { + instancesMenu.push({label: 'Not Connected...', enabled: false}) + } + + return Menu.buildFromTemplate([...instancesMenu, + { type: 'separator' }, { label: 'Hover to Show', + enabled: !store.get('detachedMode'), type: 'checkbox', checked: !store.get('disableHover'), click: () => { @@ -92,6 +133,7 @@ const getMenu = () => { checked: store.get('detachedMode'), click: () => { store.set('detachedMode', !store.get('detachedMode')); + window.hide(); createMainWindow(store.get('detachedMode')) } }, @@ -161,7 +203,7 @@ const createMainWindow = (show = false) => { } }); - window.loadURL(`file://${__dirname}/web/index.html`) + window.loadURL(indexFile); window.webContents.on('did-finish-load', function () { window.webContents.insertCSS('::-webkit-scrollbar { display: none; } body { -webkit-user-select: none; }'); @@ -236,9 +278,9 @@ const createTray = () => { if (!window.isVisible()) { showWindow(); } - if (timer) clearTimeout(timer) + if (timer) clearTimeout(timer); timer = setTimeout(() => { - let mousePos = screen.getCursorScreenPoint() + let mousePos = screen.getCursorScreenPoint(); let trayBounds = tray.getBounds(); if (!(mousePos.x >= trayBounds.x && mousePos.x <= trayBounds.x + trayBounds.width) || !(mousePos.y >= trayBounds.y && mousePos.y <= trayBounds.y + trayBounds.height)) { setWindowFocusTimer() @@ -263,16 +305,41 @@ const setWindowFocusTimer = () => { }; app.on('ready', () => { + // temporary migration of instances to avoid breaking active instance + if (store.has('instance')) { + store.set('allInstances', [store.get('instance').url]); + store.set('currentInstance', 0); + store.delete('instance'); + } checkAutoStart(); useAutoUpdater(); createTray(); - createMainWindow(!store.has('instance')); + createMainWindow(!store.has('currentInstance')); }); -ipcMain.on('ha-instance', (event, args) => { - if (args) { - store.set('instance', args) - } else { - event.reply('ha-instance', store.get('instance')) +const currentInstance = (url = null) => { + if (url) { + store.set('currentInstance', store.get('allInstances').indexOf(url)); } + if (store.has('currentInstance')) { + return store.get('allInstances')[store.get('currentInstance')]; + } + return false; +}; + +const addInstance = (url) => { + if (!store.has('allInstances')) store.set('allInstances', []); + let instances = store.get('allInstances'); + if (instances.find(e => e === url)) { + return; + } + instances.push(url); + store.set('allInstances', instances); + currentInstance(url) +}; + +ipcMain.on('ha-instance', (event, args) => { + if (args) addInstance(args.url); + console.log(currentInstance()); + if (currentInstance()) event.reply('ha-instance', currentInstance()) }); diff --git a/web/index.html b/web/index.html index 6423700..c0e4c55 100644 --- a/web/index.html +++ b/web/index.html @@ -12,8 +12,8 @@ ipcRenderer.send('ha-instance'); - ipcRenderer.on('ha-instance', function (event, arg) { - window.location.href = arg.url + ipcRenderer.on('ha-instance', function (event, url) { + window.location.href = url }); function isValidUrl(string) {