This commit is contained in:
Araxeus
2021-04-05 23:18:54 +03:00
parent a215035d07
commit 724c213af1
8 changed files with 89 additions and 82 deletions

View File

@ -321,13 +321,13 @@ function setProxy(item, win) {
}, },
type: 'input', type: 'input',
icon: iconPath, icon: iconPath,
customStylesheet: path.join(__dirname, "providers", "prompt", "darkPrompt.css"), customStylesheet: path.join(__dirname, "providers", "prompt", "dark-prompt.css"),
}; };
//TODO: custom bar on prompt need testing on macOS //TODO: custom bar on prompt need testing on macOS
if(!is.macOS()) { if(!is.macOS()) {
Object.assign(options, { Object.assign(options, {
frame: false, frame: false,
customScript: path.join(__dirname, "providers", "prompt", "customTitlebar.js"), customScript: path.join(__dirname, "providers", "prompt", "custom-titlebar.js"),
enableRemoteModule: true, enableRemoteModule: true,
height: 200, height: 200,
width: 450, width: 450,

View File

@ -101,6 +101,16 @@
"envs": [ "envs": [
"node", "node",
"browser" "browser"
] ],
"rules": {
"quotes": [
"error",
"double",
{
"avoidEscape": true,
"allowTemplateLiterals": true
}
]
}
} }
} }

View File

@ -9,8 +9,6 @@ const { injectCSS } = require("../utils");
//check that menu doesn't get created twice //check that menu doesn't get created twice
let calledReadyToShow = false; let calledReadyToShow = false;
//check menu state isn't changed twice
let calledFinishedLoad = false
//tracks menu visibility //tracks menu visibility
let visible = true; let visible = true;
// win hook for fixing menu // win hook for fixing menu
@ -37,6 +35,7 @@ module.exports = (winImport) => {
if (calledReadyToShow) { if (calledReadyToShow) {
return; return;
} }
calledReadyToShow = true; calledReadyToShow = true;
setApplicationMenu(win); setApplicationMenu(win);
@ -53,7 +52,7 @@ module.exports = (winImport) => {
win.webContents.on("did-finish-load", () => { win.webContents.on("did-finish-load", () => {
// fix bug with menu not applying on start when no internet connection available // fix bug with menu not applying on start when no internet connection available
setMenuVisibility(!config.get("options.hideMenu")); setMenuVisibility(!config.get("options.hideMenu"));
}) });
}; };
function switchMenuVisibility() { function switchMenuVisibility() {
@ -81,10 +80,10 @@ function updateTemplate(template) {
for (let item of template) { for (let item of template) {
// Change onClick of checkbox+radio // Change onClick of checkbox+radio
if ((item.type === "checkbox" || item.type === "radio") && !item.fixed) { if ((item.type === "checkbox" || item.type === "radio") && !item.fixed) {
let originalOnclick = item.click; const originalOnclick = item.click;
item.click = (itemClicked) => { item.click = (itemClicked) => {
originalOnclick(itemClicked); originalOnclick(itemClicked);
updateCheckboxesAndRadioButtons(itemClicked, item.type === 'radio', item.hasSubmenu); updateCheckboxesAndRadioButtons(itemClicked, item.type === "radio", item.hasSubmenu);
}; };
item.fixed = true; item.fixed = true;
} }

View File

@ -0,0 +1,14 @@
const customTitlebar = require("custom-electron-titlebar");
module.exports = () => {
const bar = new customTitlebar.Titlebar({
backgroundColor: customTitlebar.Color.fromHex("#050505"),
minimizable: false,
maximizable: false,
menu: null
});
const mainStyle = document.querySelector("#container").style;
mainStyle.width = "100%";
mainStyle.position = "fixed";
mainStyle.border = "unset";
};

View File

@ -1,14 +0,0 @@
const customTitlebar = require("custom-electron-titlebar");
module.exports = () => {
const bar = new customTitlebar.Titlebar({
backgroundColor: customTitlebar.Color.fromHex("#050505"),
minimizable: false,
maximizable: false,
menu: null
});
let mainStyle = document.querySelector('#container').style;
mainStyle.width = '100%';
mainStyle.position = 'fixed';
mainStyle.border = 'unset';
}

View File

@ -1,9 +1,9 @@
const electron = require('electron'); const electron = require("electron");
const BrowserWindow = electron.BrowserWindow || electron.remote.BrowserWindow; const BrowserWindow = electron.BrowserWindow || electron.remote.BrowserWindow;
const ipcMain = electron.ipcMain || electron.remote.ipcMain; const ipcMain = electron.ipcMain || electron.remote.ipcMain;
const url = require('url'); const url = require("url");
const path = require('path'); const path = require("path");
const DEFAULT_WIDTH = 370; const DEFAULT_WIDTH = 370;
const DEFAULT_HEIGHT = 160; const DEFAULT_HEIGHT = 160;
@ -38,7 +38,7 @@ function electronPrompt(options, parentWindow) {
options || {} options || {}
); );
if (options_.type === 'select' && (options_.selectOptions === null || typeof options_.selectOptions !== 'object')) { if (options_.type === "select" && (options_.selectOptions === null || typeof options_.selectOptions !== "object")) {
reject(new Error('"selectOptions" must be an object')); reject(new Error('"selectOptions" must be an object'));
return; return;
} }
@ -75,9 +75,9 @@ function electronPrompt(options, parentWindow) {
}; };
const cleanup = () => { const cleanup = () => {
ipcMain.removeListener('prompt-get-options:' + id, getOptionsListener); ipcMain.removeListener("prompt-get-options:" + id, getOptionsListener);
ipcMain.removeListener('prompt-post-data:' + id, postDataListener); ipcMain.removeListener("prompt-post-data:" + id, postDataListener);
ipcMain.removeListener('prompt-error:' + id, errorListener); ipcMain.removeListener("prompt-error:" + id, errorListener);
if (promptWindow) { if (promptWindow) {
promptWindow.close(); promptWindow.close();
@ -92,7 +92,7 @@ function electronPrompt(options, parentWindow) {
}; };
const unresponsiveListener = () => { const unresponsiveListener = () => {
reject(new Error('Window was unresponsive')); reject(new Error("Window was unresponsive"));
cleanup(); cleanup();
}; };
@ -102,21 +102,21 @@ function electronPrompt(options, parentWindow) {
cleanup(); cleanup();
}; };
ipcMain.on('prompt-get-options:' + id, getOptionsListener); ipcMain.on("prompt-get-options:" + id, getOptionsListener);
ipcMain.on('prompt-post-data:' + id, postDataListener); ipcMain.on("prompt-post-data:" + id, postDataListener);
ipcMain.on('prompt-error:' + id, errorListener); ipcMain.on("prompt-error:" + id, errorListener);
promptWindow.on('unresponsive', unresponsiveListener); promptWindow.on("unresponsive", unresponsiveListener);
promptWindow.on('closed', () => { promptWindow.on("closed", () => {
promptWindow = null; promptWindow = null;
cleanup(); cleanup();
resolve(null); resolve(null);
}); });
const promptUrl = url.format({ const promptUrl = url.format({
protocol: 'file', protocol: "file",
slashes: true, slashes: true,
pathname: path.join(__dirname, 'page', 'prompt.html'), pathname: path.join(__dirname, "page", "prompt.html"),
hash: id hash: id
}); });

View File

@ -1,6 +1,6 @@
const fs = require('fs'); const fs = require("fs");
const {ipcRenderer} = require('electron'); const {ipcRenderer} = require("electron");
const docReady = require('doc-ready'); const docReady = require("doc-ready");
let promptId = null; let promptId = null;
let promptOptions = null; let promptOptions = null;
@ -10,41 +10,39 @@ function promptError(error) {
error = error.message; error = error.message;
} }
ipcRenderer.sendSync('prompt-error:' + promptId, error); ipcRenderer.sendSync("prompt-error:" + promptId, error);
} }
function promptCancel() { function promptCancel() {
ipcRenderer.sendSync('prompt-post-data:' + promptId, null); ipcRenderer.sendSync("prompt-post-data:" + promptId, null);
} }
function promptSubmit() { function promptSubmit() {
const dataElement = document.querySelector('#data'); const dataElement = document.querySelector("#data");
let data = null; let data = null;
if (promptOptions.type === 'input') { if (promptOptions.type === "input") {
data = dataElement.value; data = dataElement.value;
} else if (promptOptions.type === 'select') { } else if (promptOptions.type === "select") {
if (promptOptions.selectMultiple) { data = promptOptions.selectMultiple ?
data = dataElement.querySelectorAll('option[selected]').map(o => o.getAttribute('value')); dataElement.querySelectorAll("option[selected]").map(o => o.getAttribute("value")) :
} else { dataElement.value;
data = dataElement.value;
}
} }
ipcRenderer.sendSync('prompt-post-data:' + promptId, data); ipcRenderer.sendSync("prompt-post-data:" + promptId, data);
} }
function promptCreateInput() { function promptCreateInput() {
const dataElement = document.createElement('input'); const dataElement = document.createElement("input");
dataElement.setAttribute('type', 'text'); dataElement.setAttribute("type", "text");
if (promptOptions.value) { if (promptOptions.value) {
dataElement.value = promptOptions.value; dataElement.value = promptOptions.value;
} else { } else {
dataElement.value = ''; dataElement.value = "";
} }
if (promptOptions.inputAttrs && typeof (promptOptions.inputAttrs) === 'object') { if (promptOptions.inputAttrs && typeof (promptOptions.inputAttrs) === "object") {
for (const k in promptOptions.inputAttrs) { for (const k in promptOptions.inputAttrs) {
if (!Object.prototype.hasOwnProperty.call(promptOptions.inputAttrs, k)) { if (!Object.prototype.hasOwnProperty.call(promptOptions.inputAttrs, k)) {
continue; continue;
@ -54,16 +52,16 @@ function promptCreateInput() {
} }
} }
dataElement.addEventListener('keyup', event => { dataElement.addEventListener("keyup", event => {
if (event.key === 'Escape') { if (event.key === "Escape") {
promptCancel(); promptCancel();
} }
}); });
dataElement.addEventListener('keypress', event => { dataElement.addEventListener("keypress", event => {
if (event.key === 'Enter') { if (event.key === "Enter") {
event.preventDefault(); event.preventDefault();
document.querySelector('#ok').click(); document.querySelector("#ok").click();
} }
}); });
@ -71,7 +69,7 @@ function promptCreateInput() {
} }
function promptCreateSelect() { function promptCreateSelect() {
const dataElement = document.createElement('select'); const dataElement = document.createElement("select");
let optionElement; let optionElement;
for (const k in promptOptions.selectOptions) { for (const k in promptOptions.selectOptions) {
@ -79,11 +77,11 @@ function promptCreateSelect() {
continue; continue;
} }
optionElement = document.createElement('option'); optionElement = document.createElement("option");
optionElement.setAttribute('value', k); optionElement.setAttribute("value", k);
optionElement.textContent = promptOptions.selectOptions[k]; optionElement.textContent = promptOptions.selectOptions[k];
if (k === promptOptions.value) { if (k === promptOptions.value) {
optionElement.setAttribute('selected', 'selected'); optionElement.setAttribute("selected", "selected");
} }
dataElement.append(optionElement); dataElement.append(optionElement);
@ -93,34 +91,34 @@ function promptCreateSelect() {
} }
function promptRegister() { function promptRegister() {
promptId = document.location.hash.replace('#', ''); promptId = document.location.hash.replace("#", "");
try { try {
promptOptions = JSON.parse(ipcRenderer.sendSync('prompt-get-options:' + promptId)); promptOptions = JSON.parse(ipcRenderer.sendSync("prompt-get-options:" + promptId));
} catch (error) { } catch (error) {
return promptError(error); return promptError(error);
} }
if (promptOptions.useHtmlLabel) { if (promptOptions.useHtmlLabel) {
document.querySelector('#label').innerHTML = promptOptions.label; document.querySelector("#label").innerHTML = promptOptions.label;
} else { } else {
document.querySelector('#label').textContent = promptOptions.label; document.querySelector("#label").textContent = promptOptions.label;
} }
if (promptOptions.buttonLabels && promptOptions.buttonLabels.ok) { if (promptOptions.buttonLabels && promptOptions.buttonLabels.ok) {
document.querySelector('#ok').textContent = promptOptions.buttonLabels.ok; document.querySelector("#ok").textContent = promptOptions.buttonLabels.ok;
} }
if (promptOptions.buttonLabels && promptOptions.buttonLabels.cancel) { if (promptOptions.buttonLabels && promptOptions.buttonLabels.cancel) {
document.querySelector('#cancel').textContent = promptOptions.buttonLabels.cancel; document.querySelector("#cancel").textContent = promptOptions.buttonLabels.cancel;
} }
if (promptOptions.customStylesheet) { if (promptOptions.customStylesheet) {
try { try {
const customStyleContent = fs.readFileSync(promptOptions.customStylesheet); const customStyleContent = fs.readFileSync(promptOptions.customStylesheet);
if (customStyleContent) { if (customStyleContent) {
const customStyle = document.createElement('style'); const customStyle = document.createElement("style");
customStyle.setAttribute('rel', 'stylesheet'); customStyle.setAttribute("rel", "stylesheet");
customStyle.append(document.createTextNode(customStyleContent)); customStyle.append(document.createTextNode(customStyleContent));
document.head.append(customStyle); document.head.append(customStyle);
} }
@ -129,25 +127,25 @@ function promptRegister() {
} }
} }
document.querySelector('#form').addEventListener('submit', promptSubmit); document.querySelector("#form").addEventListener("submit", promptSubmit);
document.querySelector('#cancel').addEventListener('click', promptCancel); document.querySelector("#cancel").addEventListener("click", promptCancel);
const dataContainerElement = document.querySelector('#data-container'); const dataContainerElement = document.querySelector("#data-container");
let dataElement; let dataElement;
if (promptOptions.type === 'input') { if (promptOptions.type === "input") {
dataElement = promptCreateInput(); dataElement = promptCreateInput();
} else if (promptOptions.type === 'select') { } else if (promptOptions.type === "select") {
dataElement = promptCreateSelect(); dataElement = promptCreateSelect();
} else { } else {
return promptError(`Unhandled input type '${promptOptions.type}'`); return promptError(`Unhandled input type '${promptOptions.type}'`);
} }
dataContainerElement.append(dataElement); dataContainerElement.append(dataElement);
dataElement.setAttribute('id', 'data'); dataElement.setAttribute("id", "data");
dataElement.focus(); dataElement.focus();
if (promptOptions.type === 'input') { if (promptOptions.type === "input") {
dataElement.select(); dataElement.select();
} }
@ -161,10 +159,10 @@ function promptRegister() {
} }
} }
window.addEventListener('error', error => { window.addEventListener("error", error => {
if (promptId) { if (promptId) {
promptError('An error has occured on the prompt window: \n' + error); promptError("An error has occured on the prompt window: \n" + error);
} }
}); });
docReady(promptRegister); docReady(promptRegister);