From 1aaf5857b65f7c100c87e0209931a3ae0e61da81 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Sun, 27 Nov 2022 14:03:43 -0600 Subject: [PATCH] create menu item for toggling auto update checks --- main.js | 36 ++++++++++++++++++++++++++++-------- preload.js | 39 +++++++++++++++++++++++++++++---------- 2 files changed, 57 insertions(+), 18 deletions(-) diff --git a/main.js b/main.js index af9e201..5e5ae3d 100644 --- a/main.js +++ b/main.js @@ -5,6 +5,7 @@ const path = require('path'); const isMac = process.platform === 'darwin'; const isWin = process.platform === 'win32'; +let autoUpdate = false; let menuObj; let mainWindow; @@ -41,7 +42,7 @@ const menuTemplate = [ id: 'window1', enabled: true, click (menuItem, window, event) { - mainWindow.webContents.send('doSlots1'); + mainWindow.webContents.send('loadSlot',1); } }, { @@ -50,7 +51,7 @@ const menuTemplate = [ id: 'window2', enabled: true, click (menuItem, window, event) { - mainWindow.webContents.send('doSlots2'); + mainWindow.webContents.send('loadSlot',2); } }, { @@ -59,7 +60,7 @@ const menuTemplate = [ id: 'window3', enabled: true, click (menuItem, window, event) { - mainWindow.webContents.send('doSlots3'); + mainWindow.webContents.send('loadSlot',3); } }, { type: 'separator' }, @@ -75,7 +76,7 @@ const menuTemplate = [ id: 'deviceSearch', enabled: true, click (menuItem, window, event) { - mainWindow.webContents.send('doSearch'); + mainWindow.webContents.send('searchAll'); } }, { type: 'separator' }, @@ -99,7 +100,7 @@ const menuTemplate = [ id: 'deviceDelete', enabled: false, click (menuItem, window, event) { - mainWindow.webContents.send('doDelete'); + mainWindow.webContents.send('deleteActive'); } } ] @@ -117,6 +118,12 @@ const menuTemplate = [ click: ()=>{ autoUpdater.checkForUpdates(); } + }, + { + label: 'Enable Auto Update', + click: ()=>{ + mainWindow.webContents.send('setAutoUpdate',!autoUpdate); + } } ] }, @@ -216,18 +223,31 @@ ipcMain.on('setDevicePin', (event, arg) => { // Autoupdate logic +ipcMain.on('checkForUpdates', (event, arg)=>{ + console.log('main process told to check for updates') + autoUpdater.checkForUpdates(); +}) + +ipcMain.on('setAutoUpdate', (event, _autoUpdate)=>{ + autoUpdate = _autoUpdate + + // update menu item for enabling/disabling auto update + menuTemplate[menuTemplate.length-1].submenu[2].label = autoUpdate ? 'Disable Auto Update' : 'Enable Auto Update'; + menuObj = Menu.buildFromTemplate(menuTemplate); + Menu.setApplicationMenu(menuObj); +}) // this can be set to true to bypass the download update dialog and skip straight to install prompt autoUpdater.autoDownload = false; -autoUpdater.on('update-available', (updateInfo)=>{ +autoUpdater.on('update-available', (updateInfo) => { // skip prompting to download if autoDownload is set if(autoUpdater.autoDownload){ return; } - const msg = `Version v${updateInfo.version} is available. Would you like to download?` + const msg = `Version v${updateInfo.version} is available. Would you like to download it?` const title = 'Update Available'; let dialogOpts = {} @@ -266,7 +286,7 @@ autoUpdater.on('update-available', (updateInfo)=>{ autoUpdater.on('update-downloaded',(event)=>{ const title = 'Update Downloaded'; - const msg = `Version ${event.version} has been downloaded. Would you like to install this update now?` + const msg = `Version v${event.version} has been downloaded. Would you like to install this update now?` let dialogOpts = {} diff --git a/preload.js b/preload.js index 783827c..ef24bb9 100644 --- a/preload.js +++ b/preload.js @@ -15,6 +15,16 @@ window.init = function init() { ipcRenderer.send('enableDeviceDropdown'); ipcRenderer.send('enableSearchAll'); + // load autoUpdate setting from storage and send to main process + const autoUpdate = JSON.parse(localStorage.getItem('autoUpdate')) + if(autoUpdate !== undefined && autoUpdate !== null){ + if(autoUpdate){ + ipcRenderer.send('checkForUpdates'); + } + // send message so main process knows the state of autoUpdate + ipcRenderer.send('setAutoUpdate', autoUpdate) + } + PLUGINS.init(() => { VIEW.init(); SAVESLOTS.loadDevices(); @@ -58,6 +68,7 @@ window.init = function init() { } }; + document.getElementById('save-slot-1').onclick = function slot1click(e) { e.stopPropagation(); SAVESLOTS.loadSlot(1); @@ -145,11 +156,11 @@ ipcRenderer.on('setActiveDevicePinned', (event, message) => { } }); -ipcRenderer.on('doSearch', (event, message) => { +ipcRenderer.on('searchAll', (event, message) => { window.searchAll(); }); -ipcRenderer.on('doDelete', (event, message) => { +ipcRenderer.on('deleteActive', (event, message) => { DEVICE.deleteActive(); }); @@ -157,17 +168,25 @@ ipcRenderer.on('resetViews', (event, message) => { SAVESLOTS.resetSlots(); }); -ipcRenderer.on('doSlots1', (event, message) => { - SAVESLOTS.loadSlot(1); +ipcRenderer.on('loadSlot', (event, slot) => { + if(slot){ + SAVESLOTS.loadSlot(slot); + } }); -ipcRenderer.on('doSlots2', (event, message) => { - SAVESLOTS.loadSlot(2); -}); -ipcRenderer.on('doSlots3', (event, message) => { - SAVESLOTS.loadSlot(3); -}); +// message from main process to set autoUpdate state +ipcRenderer.on('setAutoUpdate',(event,autoUpdate)=>{ + console.log('setAutoUpdate called ') + console.log(autoUpdate) + localStorage.setItem('autoUpdate',autoUpdate); + if(autoUpdate){ + ipcRenderer.send('checkForUpdates'); + } + // message to main process that we have updated the state + ipcRenderer.send('setAutoUpdate',autoUpdate); +}) + function switchClass(element, className) { try {