Add simple settings window with update checking

This commit is contained in:
Alice Gaudon 2020-05-19 10:54:00 +02:00
parent 645a485daa
commit 5562841a19
6 changed files with 163 additions and 4 deletions

6
dev-app-update.yml Normal file
View File

@ -0,0 +1,6 @@
owner: ArisuOngaku
repo: tabs
provider: github
updaterCacheDirName: tabs-updater
publisherName:
- Alice Gaudon

View File

@ -17,12 +17,12 @@ const icons = [];
let services = [];
let selectedService = null;
let statusButton, homeButton, forwardButton, backButton, reloadButton;
let addButton;
let addButton, settingsButton;
let emptyPage;
let urlPreview;
let lastDragPosition;
let oldActiveService; // For service reordering
// Service reordering
let lastDragPosition, oldActiveService;
// Service context menu
function openServiceContextMenu(event, serviceId) {
@ -380,6 +380,9 @@ document.addEventListener('DOMContentLoaded', () => {
addButton = document.getElementById('add-button');
addButton.addEventListener('click', () => ipcRenderer.send('openServiceSettings', null));
settingsButton = document.getElementById('settings-button');
settingsButton.addEventListener('click', () => ipcRenderer.send('openSettings', null));
});
function setActiveService(serviceId) {

29
resources/js/settings.js Normal file
View File

@ -0,0 +1,29 @@
const {ipcRenderer, remote} = require('electron');
let currentVersion;
let updateStatus;
ipcRenderer.on('current-version', (e, version) => {
currentVersion.innerText = `Version: ${version.version}`;
});
ipcRenderer.on('updateStatus', (e, available, version) => {
if (available) {
updateStatus.innerHTML = `A new update is available! <a href="https://github.com/ArisuOngaku/tabs/releases/v${version}">Click here to download manually</a>.`;
} else {
updateStatus.innerText = 'Tabs is up to date.';
}
});
function save() {
const formData = new FormData(document.querySelector('form'));
remote.getCurrentWindow().close();
}
document.addEventListener('DOMContentLoaded', () => {
currentVersion = document.getElementById('current-version');
updateStatus = document.getElementById('update-status');
ipcRenderer.send('syncSettings');
ipcRenderer.send('checkForUpdates');
});

40
resources/settings.html Normal file
View File

@ -0,0 +1,40 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Service settings</title>
<meta http-equiv="Content-Security-Policy"
content="style-src 'self' 'unsafe-inline' https://use.fontawesome.com; font-src 'self' https://use.fontawesome.com; script-src 'self' 'unsafe-inline'">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css"
integrity="sha384-oS3vJWv+0UjzBfQzYUhtDYW+Pj2yciDJxpsK1OYPAYjqT085Qq/1cq5FLXAZQ7Ay" crossorigin="anonymous">
<link rel="stylesheet" href="style/layout.css">
<link rel="stylesheet" href="style/service-settings.css">
<script src="js/settings.js" defer></script>
</head>
<body>
<form action="javascript: save();">
<div class="form-header">
<h1>Settings</h1>
</div>
<div class="form-body">
<div class="updates">
<p id="current-version"></p>
<p id="update-status">Loading...</p>
</div>
</div>
<div class="form-footer">
<div class="form-group" id="buttons">
<button id="cancel-button">Cancel</button>
<button type="submit">Save</button>
</div>
</div>
</form>
</body>
</html>

36
src/Updater.js Normal file
View File

@ -0,0 +1,36 @@
import {autoUpdater} from "electron-updater";
export default class Updater {
#callback = (available, data) => {
console.log('Update:', available, data);
};
constructor() {
autoUpdater.autoDownload = false;
autoUpdater.on('error', err => {
this.#callback(false, err);
});
autoUpdater.on('update-available', v => {
this.#callback(true, v);
});
autoUpdater.on('update-not-available', () => {
this.#callback(false);
});
}
/**
* @param {Function} callback
*/
checkForUpdates(callback) {
this.#callback = callback;
autoUpdater.checkForUpdates().then(r => {
this.#callback(false, r.updateInfo);
}).catch(err => {
this.#callback(false, err);
});
}
getCurrentVersion() {
return autoUpdater.currentVersion;
}
}

View File

@ -6,11 +6,13 @@ import Meta from "./Meta";
import Config from "./Config";
import Service from "./Service";
import {autoUpdater} from "electron-updater";
import Updater from "./Updater";
const resourcesDir = path.resolve(__dirname, '../resources');
const iconPath = path.resolve(resourcesDir, 'logo.png');
const config = new Config();
const updater = new Updater();
const devMode = Meta.isDevMode();
@ -22,7 +24,7 @@ let selectedService = 0;
let tray;
let window;
let serviceSettingsWindow;
let serviceSettingsWindow, settingsWindow;
function toggleMainWindow() {
if (window != null) {
@ -201,6 +203,49 @@ async function createWindow() {
}
});
// Open add service window
ipcMain.on('openSettings', (e) => {
if (!settingsWindow) {
console.log('Opening settings');
settingsWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true,
webviewTag: true,
},
parent: window,
modal: true,
autoHideMenuBar: true,
height: 850,
});
settingsWindow.on('close', () => {
settingsWindow = null;
});
if (devMode) {
settingsWindow.webContents.openDevTools({
mode: 'right'
});
}
let syncListener;
ipcMain.on('syncSettings', syncListener = () => {
settingsWindow.webContents.send('current-version', updater.getCurrentVersion());
});
let checkForUpdatesListener;
ipcMain.on('checkForUpdates', checkForUpdatesListener = (e) => {
updater.checkForUpdates((available, version) => {
ipcMain.emit('updateStatus', available, version);
});
});
settingsWindow.on('close', () => {
ipcMain.removeListener('sync-settings', syncListener);
ipcMain.removeListener('checkForUpdates', checkForUpdatesListener);
});
settingsWindow.loadFile(path.resolve(resourcesDir, 'settings.html'))
.catch(console.error);
}
});
console.log('> App started');
}