From a11d19a457ddf08b12474099101598bf2301c134 Mon Sep 17 00:00:00 2001 From: Anton Bracke Date: Thu, 17 Feb 2022 19:48:08 +0100 Subject: [PATCH] add ipc and test app --- desktop/package.json | 2 +- desktop/src/app.ts | 17 +++++++++++------ desktop/src/auto-updater.ts | 6 +++++- desktop/src/ipc.ts | 18 ++++++++++++++++++ desktop/src/preload/index.ts | 8 ++++++++ desktop/src/window.ts | 14 ++++++++++---- desktop/test-app/index.html | 13 +++++++++++++ desktop/test-app/web.js | 25 +++++++++++++++++++++++++ 8 files changed, 91 insertions(+), 12 deletions(-) create mode 100644 desktop/src/ipc.ts create mode 100644 desktop/src/preload/index.ts create mode 100644 desktop/test-app/index.html create mode 100644 desktop/test-app/web.js diff --git a/desktop/package.json b/desktop/package.json index 95e3e8b9..44783828 100644 --- a/desktop/package.json +++ b/desktop/package.json @@ -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", diff --git a/desktop/src/app.ts b/desktop/src/app.ts index 3c962aba..0a278f52 100644 --- a/desktop/src/app.ts +++ b/desktop/src/app.ts @@ -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. diff --git a/desktop/src/auto-updater.ts b/desktop/src/auto-updater.ts index 7a48a334..e796e808 100644 --- a/desktop/src/auto-updater.ts +++ b/desktop/src/auto-updater.ts @@ -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, +}; diff --git a/desktop/src/ipc.ts b/desktop/src/ipc.ts new file mode 100644 index 00000000..7d6dc95d --- /dev/null +++ b/desktop/src/ipc.ts @@ -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 }); + }); +}; diff --git a/desktop/src/preload/index.ts b/desktop/src/preload/index.ts new file mode 100644 index 00000000..e1381cb8 --- /dev/null +++ b/desktop/src/preload/index.ts @@ -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), +}); diff --git a/desktop/src/window.ts b/desktop/src/window.ts index 1d365dd4..a8f8babf 100644 --- a/desktop/src/window.ts +++ b/desktop/src/window.ts @@ -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 } } diff --git a/desktop/test-app/index.html b/desktop/test-app/index.html new file mode 100644 index 00000000..4ce24a29 --- /dev/null +++ b/desktop/test-app/index.html @@ -0,0 +1,13 @@ + + + + + WorkAdventure Desktop Demo + + +
Hello World!
+ + + + + \ No newline at end of file diff --git a/desktop/test-app/web.js b/desktop/test-app/web.js new file mode 100644 index 00000000..8495a393 --- /dev/null +++ b/desktop/test-app/web.js @@ -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"); +};