Add toasts & terminal funcs to electron app

Attach functions to the window object so that the wrapper can executeJs
to display messages to the player
This commit is contained in:
Martin Fournier
2022-01-03 10:32:01 -05:00
parent bdef14b029
commit 6d8df6744e
3 changed files with 54 additions and 1 deletions

View File

@@ -54,12 +54,21 @@ function getMenu(window) {
{
label: api.isListening() ? 'Disable Server' : 'Enable Server',
click: (async () => {
let success = false;
try {
await api.toggleServer();
success = true;
} catch (error) {
log.error(error);
utils.showErrorBox('Error Toggling Server', error);
}
if (success && api.isListening()) {
utils.writeToast(window, "Started API Server", "success");
} else if (success && !api.isListening()) {
utils.writeToast(window, "Stopped API Server", "success");
} else {
utils.writeToast(window, 'Error Toggling Server', "error");
}
refreshMenu(window);
})
},
@@ -67,6 +76,11 @@ function getMenu(window) {
label: api.isAutostart() ? 'Disable Autostart' : 'Enable Autostart',
click: (async () => {
api.toggleAutostart();
if (api.isAutostart()) {
utils.writeToast(window, "Enabled API Server Autostart", "success");
} else {
utils.writeToast(window, "Disabled API Server Autostart", "success");
}
refreshMenu(window);
})
},
@@ -76,6 +90,7 @@ function getMenu(window) {
const token = api.getAuthenticationToken();
log.log('Wrote authentication token to clipboard');
clipboard.writeText(token);
utils.writeToast(window, "Copied Authentication Token to Clipboard", "info");
})
},
{

View File

@@ -93,6 +93,16 @@ async function exportSave(window) {
.executeJavaScript(`${exportSaveFromIndexedDb.toString()}; exportSaveFromIndexedDb();`, true);
}
async function writeTerminal(window, message, type = null) {
await window.webContents
.executeJavaScript(`window.appNotifier.terminal("${message}", "${type}");`, true)
}
async function writeToast(window, message, type = "info", duration = 2000) {
await window.webContents
.executeJavaScript(`window.appNotifier.toast("${message}", "${type}", ${duration});`, true)
}
function openExternal(url) {
shell.openExternal(url);
global.app_playerOpenedExternalLink = true;
@@ -101,5 +111,5 @@ function openExternal(url) {
module.exports = {
reloadAndKill, showErrorBox, exportSave,
attachUnresponsiveAppHandler, detachUnresponsiveAppHandler,
openExternal,
openExternal, writeTerminal, writeToast,
}