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

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

@ -93,6 +93,16 @@ async function exportSave(window) {
.executeJavaScript(`${exportSaveFromIndexedDb.toString()}; exportSaveFromIndexedDb();`, true); .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) { function openExternal(url) {
shell.openExternal(url); shell.openExternal(url);
global.app_playerOpenedExternalLink = true; global.app_playerOpenedExternalLink = true;
@ -101,5 +111,5 @@ function openExternal(url) {
module.exports = { module.exports = {
reloadAndKill, showErrorBox, exportSave, reloadAndKill, showErrorBox, exportSave,
attachUnresponsiveAppHandler, detachUnresponsiveAppHandler, attachUnresponsiveAppHandler, detachUnresponsiveAppHandler,
openExternal, openExternal, writeTerminal, writeToast,
} }

@ -22,6 +22,9 @@ import { Server } from "./Server/Server";
import { Router } from "./ui/GameRoot"; import { Router } from "./ui/GameRoot";
import { Page } from "./ui/Router"; import { Page } from "./ui/Router";
import { removeLeadingSlash } from "./Terminal/DirectoryHelpers"; import { removeLeadingSlash } from "./Terminal/DirectoryHelpers";
import { Terminal } from './Terminal';
import { SnackbarEvents } from "./ui/React/Snackbar";
import { IMap } from "./types";
interface Achievement { interface Achievement {
ID: string; ID: string;
@ -417,6 +420,7 @@ export function initElectron(): void {
setAchievements([]); setAchievements([]);
initWebserver(); initWebserver();
setInterval(calculateAchievements, 5000); setInterval(calculateAchievements, 5000);
initAppNotifier();
} }
function initWebserver(): void { function initWebserver(): void {
@ -451,3 +455,27 @@ function initWebserver(): void {
return "not a script file"; return "not a script file";
}; };
} }
// Expose certain alert functions to allow the wrapper to sends message to the game
function initAppNotifier(): void {
const funcs = {
terminal: (message: string, type?: string) => {
const typesFn: IMap<(s: string) => void> = {
info: Terminal.info,
warn: Terminal.warn,
error: Terminal.error,
success: Terminal.success
};
let fn;
if (type) fn = typesFn[type];
if (!fn) fn = Terminal.print;
fn.bind(Terminal)(message);
},
toast: (message: string, type: "info" | "success" | "warning" | "error" , duration = 2000) =>
SnackbarEvents.emit(message, type, duration),
}
// Will be consumud by the electron wrapper.
// @ts-ignore
window.appNotifier = funcs;
}