add ipc and test app

This commit is contained in:
Anton Bracke 2022-02-17 19:48:08 +01:00
parent 31a0d7d452
commit a11d19a457
No known key found for this signature in database
GPG Key ID: B1222603899C6B25
8 changed files with 91 additions and 12 deletions

View File

@ -6,7 +6,7 @@
"license": "SEE LICENSE IN LICENSE.txt",
"scripts": {
"tsc": "tsup-node ./src/main.ts",
"dev": "tsup-node ./src/main.ts --watch --onSuccess 'yarn electron dist/main.js'",
"dev": "tsup-node ./src/main.ts ./src/preload/index.ts --watch --onSuccess 'yarn electron dist/main.js'",
"prod": "tsc && node --max-old-space-size=4096 ./dist/server.js",
"runprod": "node --max-old-space-size=4096 ./dist/server.js",
"profile": "tsc && node --prof ./dist/server.js",

View File

@ -1,9 +1,10 @@
import { app, BrowserWindow } from "electron";
import { app, BrowserWindow, globalShortcut } from "electron";
import { createWindow, getWindow } from "./window";
import { createTray } from "./tray";
// import * as autoUpdater from "./auto-updater";
import autoUpdater from "./auto-updater";
import updateAutoLaunch from "./update-auto-launch";
import ipc, { emitMutedKeyPress } from "./ipc";
function init() {
const appLock = app.requestSingleInstanceLock();
@ -32,14 +33,11 @@ function init() {
// This method will be called when Electron has finished loading
app.on("ready", () => {
// autoUpdater.init();
autoUpdater.init();
// enable auto launch
updateAutoLaunch();
// load ipc handler
// ipc();
// Don't show the app in the doc
// if (app.dock) {
// app.dock.hide();
@ -47,6 +45,13 @@ function init() {
createWindow();
createTray();
// load ipc handler
ipc();
globalShortcut.register("Alt+CommandOrControl+M", () => {
emitMutedKeyPress();
});
});
// Quit when all windows are closed.

View File

@ -38,7 +38,7 @@ export async function manualRequestUpdateCheck() {
isManualRequestedUpdate = false;
}
export function init() {
function init() {
autoUpdater.logger = log;
autoUpdater.on("update-downloaded", ({ releaseNotes, releaseName }) => {
@ -92,3 +92,7 @@ export function init() {
// run update check every hour again
setInterval(() => checkForUpdates, 1000 * 60 * 1);
}
export default {
init,
};

18
desktop/src/ipc.ts Normal file
View File

@ -0,0 +1,18 @@
import { ipcMain } from "electron";
import { createAndShowNotification } from "./notification";
import { getWindow } from "./window";
export function emitMutedKeyPress() {
const mainWindow = getWindow();
if (!mainWindow) {
throw new Error("Main window not found");
}
mainWindow.webContents.send("on-muted-key-press");
}
export default () => {
ipcMain.on("notify", (event, txt) => {
createAndShowNotification({ body: txt });
});
};

View File

@ -0,0 +1,8 @@
import { contextBridge, ipcRenderer, IpcRendererEvent } from "electron";
contextBridge.exposeInMainWorld("WorkAdventureDesktopApi", {
desktop: true,
notify: (txt: string) => ipcRenderer.send("notify", txt),
onMutedKeyPress: (callback: (event: IpcRendererEvent) => void) =>
ipcRenderer.on("on-muted-key-press", callback),
});

View File

@ -1,10 +1,12 @@
import { BrowserWindow } from "electron";
import electronIsDev from "electron-is-dev";
import windowStateKeeper from "electron-window-state";
import { getTray } from "./tray";
import path from "path";
let mainWindow: BrowserWindow | undefined;
const url = process.env.PLAY_URL; // TODO
const url = process.env.PLAY_URL;
// "https://play.staging.workadventu.re/@/tcm/workadventure/wa-village"; // TODO
export function getWindow() {
return mainWindow;
@ -32,6 +34,7 @@ export function createWindow() {
show: false,
title: "WorkAdventure",
webPreferences: {
preload: path.join(__dirname, "../dist/preload/index.js"),
// allowRunningInsecureContent: false,
// contextIsolation: true, // TODO: remove in electron 12
// nodeIntegration: false,
@ -73,9 +76,12 @@ export function createWindow() {
// app.confirmedExitPrompt = false;
// }
// });
// and load the index.html of the app.
if (url) {
if (!url || electronIsDev) {
// TODO
mainWindow.loadFile("../test-app/index.html");
mainWindow.webContents.openDevTools();
} else {
mainWindow.loadURL(url); // TODO: load app on demand
}
}

View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WorkAdventure Desktop Demo</title>
</head>
<body>
<div id="demo">Hello World!</div>
<button id="btn-reload">Refresh</button>
<button id="btn-api">Call api</button>
<script src="./web.js" lang="javascript"></script>
</body>
</html>

25
desktop/test-app/web.js Normal file
View File

@ -0,0 +1,25 @@
let muted = false;
if (window?.WorkAdventureDesktopApi?.desktop) {
document.getElementById("demo").innerHTML =
"Hello Desktop app! Press ctrl-alt-m to mute.";
window?.WorkAdventureDesktopApi?.onMutedKeyPress((event) => {
if (muted) {
document.getElementById("demo").innerHTML =
"Ready to speak! Press ctrl-alt-m to mute.";
} else {
document.getElementById("demo").innerHTML =
"Muted! Press ctrl-alt-m to unmute again.";
}
muted = !muted;
});
}
document.getElementById("btn-reload").onclick = () => {
location.reload();
};
document.getElementById("btn-api").onclick = () => {
window.WorkAdventureDesktopApi.notify("Hello from website");
};