Merge pull request #178 from jjavierdguezas/feature/check-updates-menu

add check for updates menu item
This commit is contained in:
Manoj Vivek 2020-06-12 18:40:36 +05:30 committed by GitHub
commit 15a2064fa4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 112 additions and 13 deletions

View file

@ -0,0 +1,57 @@
import {autoUpdater} from 'electron-updater';
import log from 'electron-log';
import {pkg} from './utils/generalUtils';
const EventEmitter = require('events').EventEmitter;
const AppUpdaterState = {
Idle: 'idle',
Checking: 'checking',
NoUpdate: 'noUpdate',
Downloading: 'downloading',
Downloaded: 'downloaded',
}
Object.freeze(AppUpdaterState);
class AppUpdater extends EventEmitter {
state: AppUpdaterState;
readyStates: AppUpdaterState[];
constructor() {
super();
log.transports.file.level = 'info';
autoUpdater.logger = log;
this.state = AppUpdaterState.Idle;
this.readyStates = [AppUpdaterState.Idle, AppUpdaterState.NoUpdate, AppUpdaterState.Downloaded];
autoUpdater.on('checking-for-update', () => this.handleStatusChange(AppUpdaterState.Checking));
autoUpdater.on('update-not-available', () => this.handleStatusChange(AppUpdaterState.NoUpdate));
autoUpdater.on('download-progress', () => this.handleStatusChange(AppUpdaterState.Downloading));
autoUpdater.on('update-downloaded', () => this.handleStatusChange(AppUpdaterState.Downloaded));
}
getCurrentState() {
return this.state;
}
readyToCheck() {
return this.readyStates.includes(this.state);
}
checkForUpdatesAndNotify() {
if (this.readyToCheck())
return autoUpdater.checkForUpdatesAndNotify();
}
handleStatusChange(nextStatus: AppUpdaterState) {
const changed = this.state !== nextStatus;
this.state = nextStatus;
if (changed)
this.emit('status-changed', nextStatus);
}
}
const appUpdaterInstance = new AppUpdater();
export { AppUpdaterState, appUpdaterInstance as appUpdater };

View file

@ -12,7 +12,6 @@
*/
require('dotenv').config();
import electron, {app, BrowserWindow, globalShortcut, ipcMain, nativeTheme} from 'electron';
import {autoUpdater} from 'electron-updater';
import settings from 'electron-settings';
import log from 'electron-log';
import MenuBuilder from './menu';
@ -26,6 +25,7 @@ import devtron from 'devtron';
import fs from 'fs';
import {migrateDeviceSchema} from './settings/migration';
import {initMainShortcutManager} from './shortcut-manager/main-shortcut-manager';
import {appUpdater} from './app-updater';
const path = require('path');
@ -41,14 +41,6 @@ const protocol = 'responsively';
let hasActiveWindow = false;
export default class AppUpdater {
constructor() {
log.transports.file.level = 'info';
autoUpdater.logger = log;
autoUpdater.checkForUpdatesAndNotify();
}
}
let mainWindow = null;
let urlToOpen = null;
@ -227,9 +219,12 @@ const createWindow = async () => {
const menuBuilder = new MenuBuilder(mainWindow);
menuBuilder.buildMenu();
appUpdater.on('status-changed', (nextStatus) => {
menuBuilder.buildMenu(true);
// update status bar info
});
// Remove this if your app does not use auto updates
// eslint-disable-next-line
new AppUpdater();
appUpdater.checkForUpdatesAndNotify();
};
app.on('activate', (event, hasVisibleWindows) => {

View file

@ -14,6 +14,10 @@ import {
getAllShortcuts,
registerShortcut,
} from './shortcut-manager/main-shortcut-manager';
import {
appUpdater,
AppUpdaterState
} from './app-updater';
const path = require('path');
@ -96,6 +100,22 @@ export default class MenuBuilder {
});
},
},
{
label: 'Check for Updates...',
id: 'CHECK_FOR_UPDATES',
click() {
appUpdater.checkForUpdatesAndNotify().then(r => {
if (r == null || r.updateInfo == null || r.updateInfo.version === pkg.version) {
dialog
.showMessageBox(BrowserWindow.getAllWindows()[0], {
type: 'info',
title: 'Responsively',
message: 'There are currently no updates available'
});
}
});
},
},
{
label: 'About',
accelerator: 'F1',
@ -167,7 +187,32 @@ export default class MenuBuilder {
],
};
buildMenu() {
getCheckForUpdatesMenuState() {
const updaterState = appUpdater.getCurrentState();
let label = 'Check for Updates...';
let enabled = true;
if (updaterState === AppUpdaterState.Checking) {
enabled = false;
label = 'Checking for Updates...';
}
else if (updaterState === AppUpdaterState.Downloading) {
enabled = false;
label = 'Downloading Update...';
}
return {label, enabled};
}
buildMenu(isUpdate: boolean = false) {
if (isUpdate) {
const chkUpdtMenu = this.subMenuHelp.submenu.find(x => x.id === 'CHECK_FOR_UPDATES');
const {label, enabled} = this.getCheckForUpdatesMenuState();
chkUpdtMenu.label = label;
chkUpdtMenu.enabled = enabled;
}
if (
process.env.NODE_ENV === 'development' ||
process.env.DEBUG_PROD === 'true'
@ -180,7 +225,9 @@ export default class MenuBuilder {
? this.buildDarwinTemplate()
: this.buildDefaultTemplate();
this.registerMenuShortcuts(template);
if (!isUpdate) {
this.registerMenuShortcuts(template);
}
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);