add main to renderer command API
This commit is contained in:
40
src/renderer/arrpc.ts
Normal file
40
src/renderer/arrpc.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: GPL-3.0
|
||||
* Vesktop, a desktop app aiming to give you a snappier Discord Experience
|
||||
* Copyright (c) 2023 Vendicated and Vencord contributors
|
||||
*/
|
||||
|
||||
import { onceReady } from "@vencord/types/webpack";
|
||||
import { FluxDispatcher, InviteActions } from "@vencord/types/webpack/common";
|
||||
import { IpcCommands } from "shared/IpcEvents";
|
||||
|
||||
import { onIpcCommand } from "./ipcCommands";
|
||||
import { Settings } from "./settings";
|
||||
|
||||
const arRPC = Vencord.Plugins.plugins["WebRichPresence (arRPC)"] as any as {
|
||||
handleEvent(e: MessageEvent): void;
|
||||
};
|
||||
|
||||
onIpcCommand(IpcCommands.RPC_ACTIVITY, async data => {
|
||||
if (!Settings.store.arRPC) return;
|
||||
|
||||
await onceReady;
|
||||
|
||||
arRPC.handleEvent(new MessageEvent("message", { data }));
|
||||
});
|
||||
|
||||
onIpcCommand(IpcCommands.RPC_INVITE, async code => {
|
||||
const { invite } = await InviteActions.resolveInvite(code, "Desktop Modal");
|
||||
if (!invite) return false;
|
||||
|
||||
VesktopNative.win.focus();
|
||||
|
||||
FluxDispatcher.dispatch({
|
||||
type: "INVITE_MODAL_OPEN",
|
||||
invite,
|
||||
code,
|
||||
context: "APP"
|
||||
});
|
||||
|
||||
return true;
|
||||
});
|
||||
@@ -8,12 +8,14 @@ import "./fixes";
|
||||
import "./appBadge";
|
||||
import "./patches";
|
||||
import "./themedSplash";
|
||||
import "./ipcCommands";
|
||||
import "./arrpc";
|
||||
|
||||
console.log("read if cute :3");
|
||||
|
||||
export * as Components from "./components";
|
||||
import { findByPropsLazy, onceReady } from "@vencord/types/webpack";
|
||||
import { Alerts, FluxDispatcher } from "@vencord/types/webpack/common";
|
||||
import { Alerts } from "@vencord/types/webpack/common";
|
||||
|
||||
import SettingsUi from "./components/settings/Settings";
|
||||
import { Settings } from "./settings";
|
||||
@@ -21,22 +23,6 @@ export { Settings };
|
||||
|
||||
const InviteActions = findByPropsLazy("resolveInvite");
|
||||
|
||||
export async function openInviteModal(code: string) {
|
||||
const { invite } = await InviteActions.resolveInvite(code, "Desktop Modal");
|
||||
if (!invite) return false;
|
||||
|
||||
VesktopNative.win.focus();
|
||||
|
||||
FluxDispatcher.dispatch({
|
||||
type: "INVITE_MODAL_OPEN",
|
||||
invite,
|
||||
code,
|
||||
context: "APP"
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const customSettingsSections = (
|
||||
Vencord.Plugins.plugins.Settings as any as { customSections: ((ID: Record<string, unknown>) => any)[] }
|
||||
).customSections;
|
||||
@@ -48,18 +34,6 @@ customSettingsSections.push(() => ({
|
||||
className: "vc-vesktop-settings"
|
||||
}));
|
||||
|
||||
const arRPC = Vencord.Plugins.plugins["WebRichPresence (arRPC)"] as any as {
|
||||
handleEvent(e: MessageEvent): void;
|
||||
};
|
||||
|
||||
VesktopNative.arrpc.onActivity(async data => {
|
||||
if (!Settings.store.arRPC) return;
|
||||
|
||||
await onceReady;
|
||||
|
||||
arRPC.handleEvent(new MessageEvent("message", { data }));
|
||||
});
|
||||
|
||||
// TODO: remove soon
|
||||
const vencordDir = "vencordDir" as keyof typeof Settings.store;
|
||||
if (Settings.store[vencordDir]) {
|
||||
|
||||
49
src/renderer/ipcCommands.ts
Normal file
49
src/renderer/ipcCommands.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: GPL-3.0
|
||||
* Vesktop, a desktop app aiming to give you a snappier Discord Experience
|
||||
* Copyright (c) 2023 Vendicated and Vencord contributors
|
||||
*/
|
||||
|
||||
import { SettingsRouter } from "@vencord/types/webpack/common";
|
||||
import { IpcCommands } from "shared/IpcEvents";
|
||||
|
||||
type IpcCommandHandler = (data: any) => any;
|
||||
|
||||
const handlers = new Map<string, IpcCommandHandler>();
|
||||
|
||||
function respond(nonce: string, ok: boolean, data: any) {
|
||||
VesktopNative.commands.respond({ nonce, ok, data });
|
||||
}
|
||||
|
||||
VesktopNative.commands.onCommand(async ({ message, nonce, data }) => {
|
||||
const handler = handlers.get(message);
|
||||
if (!handler) {
|
||||
return respond(nonce, false, `No handler for message: ${message}`);
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await handler(data);
|
||||
respond(nonce, true, result);
|
||||
} catch (err) {
|
||||
respond(nonce, false, String(err));
|
||||
}
|
||||
});
|
||||
|
||||
export function onIpcCommand(channel: string, handler: IpcCommandHandler) {
|
||||
if (handlers.has(channel)) {
|
||||
throw new Error(`Handler for message ${channel} already exists`);
|
||||
}
|
||||
|
||||
handlers.set(channel, handler);
|
||||
}
|
||||
|
||||
export function offIpcCommand(channel: string) {
|
||||
handlers.delete(channel);
|
||||
}
|
||||
|
||||
/* Generic Handlers */
|
||||
|
||||
onIpcCommand(IpcCommands.NAVIGATE_SETTINGS, () => {
|
||||
SettingsRouter.open("My Account");
|
||||
});
|
||||
onIpcCommand(IpcCommands.GET_LANGUAGES, () => navigator.languages);
|
||||
Reference in New Issue
Block a user