Skip to content

Commit

Permalink
extract app menu & updater logic to separate modules
Browse files Browse the repository at this point in the history
  • Loading branch information
gwleuverink committed Feb 4, 2022
1 parent 73d2cfb commit 8e0fc4b
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 2 deletions.
80 changes: 80 additions & 0 deletions src/app-menu.js
Original file line number Diff line number Diff line change
@@ -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);
}
59 changes: 59 additions & 0 deletions src/app-updater.js
Original file line number Diff line number Diff line change
@@ -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,
}
8 changes: 6 additions & 2 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -78,6 +81,7 @@ app.on('ready', async () => {
}
}
createWindow()
createMenu()
})

// Exit cleanly on request from parent process in development mode.
Expand Down

0 comments on commit 8e0fc4b

Please sign in to comment.