partey_workadventure/desktop/electron/src/ipc.ts

107 lines
3.4 KiB
TypeScript
Raw Normal View History

import { ipcMain, app, desktopCapturer } from "electron";
2022-02-20 20:30:53 +01:00
import electronIsDev from "electron-is-dev";
2022-02-17 19:48:08 +01:00
import { createAndShowNotification } from "./notification";
2022-02-20 20:30:53 +01:00
import { Server } from "./preload-local-app/types";
2022-02-22 12:02:56 +01:00
import settings, { SettingsData } from "./settings";
2022-02-22 16:57:56 +01:00
import { loadShortcuts, setShortcutsEnabled } from "./shortcuts";
import { getAppView, hideAppView, showAppView } from "./window";
2022-02-22 16:57:56 +01:00
// import fetch from "node-fetch";
2022-02-17 19:48:08 +01:00
2022-02-22 10:05:21 +01:00
export function emitMuteToggle() {
2022-02-22 16:57:56 +01:00
const appView = getAppView();
if (!appView) {
2022-02-18 23:03:05 +01:00
throw new Error("Main window not found");
}
2022-02-17 19:48:08 +01:00
2022-02-22 18:10:36 +01:00
appView.webContents.send("app:on-mute-toggle");
2022-02-22 10:05:21 +01:00
}
export function emitCameraToggle() {
2022-02-22 16:57:56 +01:00
const appView = getAppView();
if (!appView) {
2022-02-22 10:05:21 +01:00
throw new Error("Main window not found");
}
2022-02-22 18:10:36 +01:00
appView.webContents.send("app:on-camera-toggle");
2022-02-17 19:48:08 +01:00
}
export default () => {
2022-02-20 20:30:53 +01:00
ipcMain.handle("is-development", () => electronIsDev);
2022-02-22 10:05:21 +01:00
ipcMain.handle("get-version", () => (electronIsDev ? "dev" : app.getVersion()));
2022-02-20 20:30:53 +01:00
// app ipc
ipcMain.on("app:notify", (event, txt: string) => {
2022-02-18 23:03:05 +01:00
createAndShowNotification({ body: txt });
});
2022-02-18 01:51:35 +01:00
ipcMain.handle("app:getDesktopCapturerSources", async (event, options: Electron.SourcesOptions) => {
return (await desktopCapturer.getSources(options)).map((source) => ({
id: source.id,
name: source.name,
thumbnailURL: source.thumbnail.toDataURL(),
}));
});
2022-02-20 20:30:53 +01:00
// local-app ipc
ipcMain.handle("local-app:showLocalApp", () => {
hideAppView();
});
2022-02-19 01:08:33 +01:00
ipcMain.handle("local-app:getServers", () => {
return settings.get("servers");
2022-02-18 23:03:05 +01:00
});
2022-02-18 01:51:35 +01:00
ipcMain.handle("local-app:selectServer", async (event, serverId: string) => {
2022-02-19 01:48:56 +01:00
const servers = settings.get("servers") || [];
2022-02-18 23:03:05 +01:00
const selectedServer = servers.find((s) => s._id === serverId);
2022-02-18 01:51:35 +01:00
2022-02-18 23:03:05 +01:00
if (!selectedServer) {
return new Error("Server not found");
}
2022-02-18 01:51:35 +01:00
await showAppView(selectedServer.url);
2022-02-18 23:03:05 +01:00
return true;
});
2022-02-18 01:51:35 +01:00
2022-02-22 18:15:36 +01:00
ipcMain.handle("local-app:addServer", (event, server: Omit<Server, "_id">) => {
2022-02-19 01:48:56 +01:00
const servers = settings.get("servers") || [];
2022-02-22 10:05:21 +01:00
2022-02-22 20:24:09 +01:00
// TODO: add proper test to see if server url is valid and points to a real WA server
// try {
//
// await fetch(`${server.url}/iframe_api.js`);
// } catch (e) {
// console.error(e);
// return new Error("Invalid server url");
// }
2022-02-22 10:05:21 +01:00
2022-02-20 20:30:53 +01:00
const newServer = {
...server,
2022-02-22 16:57:56 +01:00
_id: `${Date.now()}-${servers.length + 1}`,
2022-02-20 20:30:53 +01:00
};
servers.push(newServer);
2022-02-18 23:03:05 +01:00
settings.set("servers", servers);
2022-02-20 20:30:53 +01:00
return newServer;
});
ipcMain.handle("local-app:removeServer", (event, server: Server) => {
const servers = settings.get("servers") || [];
settings.set(
"servers",
servers.filter((s) => s._id !== server._id)
);
2022-02-18 23:03:05 +01:00
return true;
});
2022-02-22 10:05:21 +01:00
ipcMain.handle("local-app:reloadShortcuts", (event) => loadShortcuts());
2022-02-22 10:05:21 +01:00
2022-02-22 12:02:56 +01:00
ipcMain.handle("local-app:getSettings", (event) => settings.get() || {});
ipcMain.handle(
"local-app:saveSetting",
<T extends keyof SettingsData>(event: Electron.IpcMainInvokeEvent, key: T, value: SettingsData[T]) =>
settings.set(key, value)
2022-02-22 12:02:56 +01:00
);
ipcMain.handle("local-app:setShortcutsEnabled", (event, enabled: boolean) => setShortcutsEnabled(enabled));
2022-02-17 19:48:08 +01:00
};