From 8e0fc4b4cfd2806bbb98a91c5c34facf775e599e Mon Sep 17 00:00:00 2001 From: Willem Leuverink Date: Fri, 4 Feb 2022 17:25:10 +0100 Subject: [PATCH] extract app menu & updater logic to separate modules --- src/app-menu.js | 80 ++++++++++++++++++++++++++++++++++++++++++++++ src/app-updater.js | 59 ++++++++++++++++++++++++++++++++++ src/background.js | 8 +++-- 3 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 src/app-menu.js create mode 100644 src/app-updater.js diff --git a/src/app-menu.js b/src/app-menu.js new file mode 100644 index 0000000..2695be5 --- /dev/null +++ b/src/app-menu.js @@ -0,0 +1,80 @@ +import { app, shell, Menu } from 'electron' +import updater from '@/app-updater' + +// const template = [] +const name = app.getName() +const isMac = process.platform === 'darwin' + +const template = [ + // { role: 'appMenu' } + ...(isMac ? [{ + label: name, + submenu: [ + { + label: 'About ' + name, + role: 'about' + }, + { + label: 'Check for updates', + click: async (menuItem) => { + await updater.checkForUpdates(menuItem) + } + }, + { type: 'separator' }, + { role: 'hide' }, + { role: 'hideOthers' }, + { role: 'unhide' }, + { type: 'separator' }, + { + label: 'Quit', + accelerator: 'Command+Q', + click() { app.quit(); } + }, + ] + }] : []), + // { role: 'viewMenu' } + { + label: 'View', + submenu: [ + { role: 'reload' }, + { role: 'forceReload' }, + { role: 'toggleDevTools' }, + { type: 'separator' }, + { role: 'togglefullscreen' } + ] + }, + // { role: 'windowMenu' } + { + label: 'Window', + submenu: [ + { role: 'minimize' }, + { role: 'zoom' }, + ...(isMac ? [ + { type: 'separator' }, + { role: 'front' }, + { type: 'separator' }, + { role: 'window' } + ] : [ + { role: 'close' } + ]) + ] + }, + { + role: 'help', + submenu: [ + { + label: 'Check out the public repository', + click: async () => await shell.openExternal('https://github.com/gwleuverink/clickup-time-tracker') + }, + { + label: 'Sponsor me on GitHub ❤', + click: async () => await shell.openExternal('https://github.com/sponsors/gwleuverink') + } + ] + } +] + +export async function createMenu() { + const menu = Menu.buildFromTemplate(template); + Menu.setApplicationMenu(menu); +} diff --git a/src/app-updater.js b/src/app-updater.js new file mode 100644 index 0000000..e203d0a --- /dev/null +++ b/src/app-updater.js @@ -0,0 +1,59 @@ +import { dialog } from 'electron' +import { autoUpdater } from 'electron-updater' + + +let updater +autoUpdater.autoDownload = false; + +autoUpdater.on('error', (error) => { + dialog.showErrorBox('Error: ', error == null ? "unknown" : (error.stack || error).toString()) +}) + +autoUpdater.on('update-available', () => { + dialog.showMessageBox({ + type: 'info', + title: 'Found Updates', + message: 'Found updates, do you want update now?', + buttons: ['Sure', 'No'] + }).then((buttonIndex) => { + + if (buttonIndex === 0) { + autoUpdater.downloadUpdate() + } + else { + updater.enabled = true + updater = null + } + }) +}) + +autoUpdater.on('update-not-available', () => { + dialog.showMessageBox({ + title: 'No Updates', + message: 'Current version is up-to-date.' + }) + updater.enabled = true + updater = null +}) + +autoUpdater.on('update-downloaded', () => { + dialog.showMessageBox({ + title: 'Install Updates', + message: 'Updates downloaded, application will be quit for update...' + }).then(() => { + setImmediate(() => autoUpdater.quitAndInstall()) + }) +}) + + +// export this to MenuItem click callback +function checkForUpdates(menuItem) { + updater = menuItem + updater.enabled = false + autoUpdater.checkForUpdates() +} + +export default { + checkForUpdates, + checkForUpdatesAndNotify: autoUpdater.checkForUpdatesAndNotify, +} diff --git a/src/background.js b/src/background.js index 330b390..cb41e04 100644 --- a/src/background.js +++ b/src/background.js @@ -3,12 +3,14 @@ import { app, protocol, ipcMain, BrowserWindow } from 'electron' import { createProtocol } from 'vue-cli-plugin-electron-builder/lib' import installExtension, { VUEJS3_DEVTOOLS } from 'electron-devtools-installer' -import { autoUpdater } from 'electron-updater' import clickupService from '@/clickup-service' +import { createMenu } from '@/app-menu' +import updater from '@/app-updater' import Store from 'electron-store'; Store.initRenderer(); + const isDevelopment = process.env.NODE_ENV !== 'production' // Register background ipc protocol listeners @@ -46,10 +48,11 @@ async function createWindow() { // Load the index.html when not in development win.loadURL('app://./index.html') - autoUpdater.checkForUpdatesAndNotify() + updater.checkForUpdatesAndNotify() } } + // Quit when all windows are closed. app.on('window-all-closed', () => { // On macOS it is common for applications and their menu bar @@ -78,6 +81,7 @@ app.on('ready', async () => { } } createWindow() + createMenu() }) // Exit cleanly on request from parent process in development mode.