tabs/frontend/ts/settings.ts

101 lines
3.8 KiB
TypeScript
Raw Normal View History

import {ipcRenderer, shell} from "electron";
2020-09-29 20:43:41 +02:00
import Config from "../../src/Config";
import {SemVer} from "semver";
import {UpdateInfo} from "electron-updater";
let currentVersion: HTMLElement | null;
let updateStatus: HTMLElement | null;
2020-09-29 20:43:41 +02:00
let updateInfo: UpdateInfo;
let updateButton: HTMLElement | null;
2020-09-29 20:43:41 +02:00
let config: Config;
let startMinimizedField: HTMLInputElement | null;
let bigNavBarField: HTMLInputElement | null;
let securityButtonField: HTMLInputElement | null,
homeButtonField: HTMLInputElement | null,
backButtonField: HTMLInputElement | null,
forwardButtonField: HTMLInputElement | null,
refreshButtonField: HTMLInputElement | null;
2020-09-29 20:43:41 +02:00
ipcRenderer.on('current-version', (e, version: SemVer) => {
if (currentVersion) currentVersion.innerText = `Version: ${version.version}`;
});
2020-09-29 20:43:41 +02:00
ipcRenderer.on('config', (e, c: Config) => {
config = c;
if (startMinimizedField) startMinimizedField.checked = config.startMinimized;
if (bigNavBarField) bigNavBarField.checked = config.bigNavBar;
if (securityButtonField) securityButtonField.checked = config.securityButton;
if (homeButtonField) homeButtonField.checked = config.homeButton;
if (backButtonField) backButtonField.checked = config.backButton;
if (forwardButtonField) forwardButtonField.checked = config.forwardButton;
if (refreshButtonField) refreshButtonField.checked = config.refreshButton;
});
2020-09-29 20:43:41 +02:00
ipcRenderer.on('updateStatus', (e, available: boolean, version: UpdateInfo) => {
console.log(available, version);
updateInfo = version;
if (available) {
if (updateStatus) updateStatus.innerHTML = 'A new update is available!';
if (updateButton) updateButton.classList.remove('hidden');
} else {
if (updateStatus) updateStatus.innerText = 'Tabs is up to date.';
}
});
2020-05-26 10:54:47 +02:00
function save() {
2020-09-29 20:43:41 +02:00
const form = document.querySelector('form');
if (!form) return;
const formData = new FormData(form);
config.startMinimized = formData.get('start-minimized') === 'on';
config.bigNavBar = formData.get('big-nav-bar') === 'on';
config.securityButton = formData.get('security-button') === 'on';
config.homeButton = formData.get('home-button') === 'on';
config.backButton = formData.get('back-button') === 'on';
config.forwardButton = formData.get('forward-button') === 'on';
config.refreshButton = formData.get('refresh-button') === 'on';
ipcRenderer.send('save-config', config);
ipcRenderer.send('close-window', 'SettingsWindow');
}
document.addEventListener('DOMContentLoaded', () => {
currentVersion = document.getElementById('current-version');
updateStatus = document.getElementById('update-status');
updateButton = document.getElementById('download-button');
updateButton?.addEventListener('click', () => {
2020-11-27 14:31:42 +01:00
shell.openExternal(`https://update.eternae.ink/arisu/tabs/${updateInfo.path}`)
.catch(console.error);
});
startMinimizedField = <HTMLInputElement>document.getElementById('start-minimized');
bigNavBarField = <HTMLInputElement>document.getElementById('big-nav-bar');
securityButtonField = <HTMLInputElement>document.getElementById('security-button');
homeButtonField = <HTMLInputElement>document.getElementById('home-button');
backButtonField = <HTMLInputElement>document.getElementById('back-button');
forwardButtonField = <HTMLInputElement>document.getElementById('forward-button');
refreshButtonField = <HTMLInputElement>document.getElementById('refresh-button');
document.getElementById('cancel-button')?.addEventListener('click', e => {
e.preventDefault();
ipcRenderer.send('close-window', 'SettingsWindow');
});
2020-05-26 10:54:47 +02:00
document.querySelector('form')?.addEventListener('submit', e => {
e.preventDefault();
save();
});
ipcRenderer.send('syncSettings');
ipcRenderer.send('checkForUpdates');
2020-09-29 20:43:41 +02:00
});