Show auto-updater dialog as a modal

This commit is contained in:
Yuya Ochiai
2017-09-07 23:51:57 +09:00
parent 7f54d0f01d
commit f7926241ea

View File

@@ -5,21 +5,23 @@ const semver = require('semver');
const INTERVAL_48_HOURS_IN_MS = 172800000; // 48 * 60 * 60 * 1000 [ms] const INTERVAL_48_HOURS_IN_MS = 172800000; // 48 * 60 * 60 * 1000 [ms]
let updaterWindow = null; let updaterModal = null;
function setEvent(win, eventName) { function createEventListener(win, eventName) {
ipcMain.on(eventName, (event) => { return (event) => {
if (event.sender === win.webContents) { if (event.sender === win.webContents) {
win.emit(eventName); win.emit(eventName);
} }
}); };
} }
function createUpdaterWindow(options) { function createUpdaterModal(parentWindow, options) {
const windowWidth = 480; const windowWidth = 480;
const windowHeight = 240; const windowHeight = 240;
const windowOptions = { const windowOptions = {
title: `${app.getName()} Updater`, title: `${app.getName()} Updater`,
parent: parentWindow,
modal: true,
maximizable: false, maximizable: false,
show: false, show: false,
width: windowWidth, width: windowWidth,
@@ -31,19 +33,22 @@ function createUpdaterWindow(options) {
windowOptions.icon = options.linuxAppIcon; windowOptions.icon = options.linuxAppIcon;
} }
const win = new BrowserWindow(windowOptions); const modal = new BrowserWindow(windowOptions);
win.once('ready-to-show', () => { modal.once('ready-to-show', () => {
win.show(); modal.show();
}); });
const updaterURL = (global.isDev ? 'http://localhost:8080' : `file://${app.getAppPath()}`) + '/browser/updater.html'; const updaterURL = (global.isDev ? 'http://localhost:8080' : `file://${app.getAppPath()}`) + '/browser/updater.html';
win.loadURL(updaterURL); modal.loadURL(updaterURL);
setEvent(win, 'click-release-notes'); for (const eventName of ['click-release-notes', 'click-skip', 'click-remind', 'click-install']) {
setEvent(win, 'click-skip'); const listener = createEventListener(modal, eventName);
setEvent(win, 'click-remind'); ipcMain.on(eventName, listener);
setEvent(win, 'click-install'); modal.on('closed', () => {
ipcMain.removeListener(eventName, listener);
});
}
return win; return modal;
} }
function isUpdateApplicable(now, skippedVersion, updateInfo) { function isUpdateApplicable(now, skippedVersion, updateInfo) {
@@ -74,24 +79,24 @@ function initialize(appState, mainWindow) {
console.error('Error in autoUpdater:', err.message); console.error('Error in autoUpdater:', err.message);
}).on('update-available', (info) => { }).on('update-available', (info) => {
if (isUpdateApplicable(new Date(), appState.skippedVersion, info)) { if (isUpdateApplicable(new Date(), appState.skippedVersion, info)) {
updaterWindow = createUpdaterWindow({linuxAppIcon: path.join(assetsDir, 'appicon.png'), nextVersion: '0.0.0'}); updaterModal = createUpdaterModal(mainWindow, {linuxAppIcon: path.join(assetsDir, 'appicon.png'), nextVersion: '0.0.0'});
updaterWindow.on('close', () => { updaterModal.on('closed', () => {
updaterWindow = null; updaterModal = null;
}); });
updaterWindow.on('click-skip', () => { updaterModal.on('click-skip', () => {
appState.skippedVersion = info.version; appState.skippedVersion = info.version;
updaterWindow.close(); updaterModal.close();
}).on('click-remind', () => { }).on('click-remind', () => {
appState.updateCheckedDate = new Date(); appState.updateCheckedDate = new Date();
setTimeout(autoUpdater.checkForUpdates, INTERVAL_48_HOURS_IN_MS); setTimeout(autoUpdater.checkForUpdates, INTERVAL_48_HOURS_IN_MS);
updaterWindow.close(); updaterModal.close();
}).on('click-install', () => { }).on('click-install', () => {
downloadAndInstall(); downloadAndInstall();
updaterWindow.close(); updaterModal.close();
}).on('click-release-notes', () => { }).on('click-release-notes', () => {
shell.openExternal(`https://github.com/mattermost/desktop/releases/v${info.version}`); shell.openExternal(`https://github.com/mattermost/desktop/releases/v${info.version}`);
}); });
updaterWindow.focus(); updaterModal.focus();
} else if (autoUpdater.isManual) { } else if (autoUpdater.isManual) {
autoUpdater.emit('update-not-available'); autoUpdater.emit('update-not-available');
} }
@@ -120,7 +125,7 @@ function shouldCheckForUpdatesOnStart(updateCheckedDate) {
function checkForUpdates(isManual = false) { function checkForUpdates(isManual = false) {
autoUpdater.isManual = isManual; autoUpdater.isManual = isManual;
autoUpdater.autoDownload = false; autoUpdater.autoDownload = false;
if (!updaterWindow) { if (!updaterModal) {
autoUpdater.checkForUpdates(); autoUpdater.checkForUpdates();
} }
} }