aboutsummaryrefslogtreecommitdiff
path: root/browser/VencordNativeStub.ts
diff options
context:
space:
mode:
Diffstat (limited to 'browser/VencordNativeStub.ts')
-rw-r--r--browser/VencordNativeStub.ts83
1 files changed, 51 insertions, 32 deletions
diff --git a/browser/VencordNativeStub.ts b/browser/VencordNativeStub.ts
index ef3923b..515ccc3 100644
--- a/browser/VencordNativeStub.ts
+++ b/browser/VencordNativeStub.ts
@@ -16,51 +16,70 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
+/// <reference path="../src/modules.d.ts" />
+/// <reference path="../src/globals.d.ts" />
+
+import monacoHtml from "~fileContent/../src/components/monacoWin.html";
import * as DataStore from "../src/api/DataStore";
-import IpcEvents from "../src/utils/IpcEvents";
+import { debounce } from "../src/utils";
+import { getTheme, Theme } from "../src/utils/discord";
// Discord deletes this so need to store in variable
const { localStorage } = window;
// listeners for ipc.on
-const listeners = {} as Record<string, Set<Function>>;
+const cssListeners = new Set<(css: string) => void>();
+const NOOP = () => { };
+const NOOP_ASYNC = async () => { };
-const handlers = {
- [IpcEvents.GET_REPO]: () => "https://github.com/Vendicated/Vencord", // shrug
- [IpcEvents.GET_SETTINGS_DIR]: () => "LocalStorage",
+const setCssDebounced = debounce((css: string) => VencordNative.quickCss.set(css));
- [IpcEvents.GET_QUICK_CSS]: () => DataStore.get("VencordQuickCss").then(s => s ?? ""),
- [IpcEvents.SET_QUICK_CSS]: (css: string) => {
- DataStore.set("VencordQuickCss", css);
- listeners[IpcEvents.QUICK_CSS_UPDATE]?.forEach(l => l(null, css));
+// probably should make this less cursed at some point
+window.VencordNative = {
+ native: {
+ getVersions: () => ({}),
+ openExternal: async (url) => void open(url, "_blank")
},
- [IpcEvents.GET_SETTINGS]: () => localStorage.getItem("VencordSettings") || "{}",
- [IpcEvents.SET_SETTINGS]: (s: string) => localStorage.setItem("VencordSettings", s),
-
- [IpcEvents.GET_UPDATES]: () => ({ ok: true, value: [] }),
+ updater: {
+ getRepo: async () => ({ ok: true, value: "https://github.com/Vendicated/Vencord" }),
+ getUpdates: async () => ({ ok: true, value: [] }),
+ update: async () => ({ ok: true, value: false }),
+ rebuild: async () => ({ ok: true, value: true }),
+ },
- [IpcEvents.OPEN_EXTERNAL]: (url: string) => open(url, "_blank"),
-};
+ quickCss: {
+ get: () => DataStore.get("VencordQuickCss").then(s => s ?? ""),
+ set: async (css: string) => {
+ await DataStore.set("VencordQuickCss", css);
+ cssListeners.forEach(l => l(css));
+ },
+ addChangeListener(cb) {
+ cssListeners.add(cb);
+ },
+ openFile: NOOP_ASYNC,
+ async openEditor() {
+ const features = `popup,width=${Math.min(window.innerWidth, 1000)},height=${Math.min(window.innerHeight, 1000)}`;
+ const win = open("about:blank", "VencordQuickCss", features);
+ if (!win) {
+ alert("Failed to open QuickCSS popup. Make sure to allow popups!");
+ return;
+ }
-function onEvent(event: string, ...args: any[]) {
- const handler = handlers[event];
- if (!handler) throw new Error(`Event ${event} not implemented.`);
- return handler(...args);
-}
+ win.setCss = setCssDebounced;
+ win.getCurrentCss = () => VencordNative.quickCss.get();
+ win.getTheme = () =>
+ getTheme() === Theme.Light
+ ? "vs-light"
+ : "vs-dark";
-// probably should make this less cursed at some point
-window.VencordNative = {
- getVersions: () => ({}),
- ipc: {
- send: (event: string, ...args: any[]) => void onEvent(event, ...args),
- sendSync: onEvent,
- on(event: string, listener: () => {}) {
- (listeners[event] ??= new Set()).add(listener);
- },
- off(event: string, listener: () => {}) {
- return listeners[event]?.delete(listener);
+ win.document.write(monacoHtml);
},
- invoke: (event: string, ...args: any[]) => Promise.resolve(onEvent(event, ...args))
},
+
+ settings: {
+ get: () => localStorage.getItem("VencordSettings") || "{}",
+ set: async (s: string) => localStorage.setItem("VencordSettings", s),
+ getSettingsDir: async () => "LocalStorage"
+ }
};