From 7f54858b274f9a884305d5ced96f368535bef8e2 Mon Sep 17 00:00:00 2001 From: V Date: Sun, 25 Jun 2023 16:48:26 +0200 Subject: [PATCH] Add SpellCheck toggle in textarea context menu --- src/main/mainWindow.ts | 32 ++++++++++++++++++-- src/renderer/patches/spellCheck.tsx | 45 ++++++++++++++++++++--------- 2 files changed, 60 insertions(+), 17 deletions(-) diff --git a/src/main/mainWindow.ts b/src/main/mainWindow.ts index 139ae5a..7f89436 100644 --- a/src/main/mainWindow.ts +++ b/src/main/mainWindow.ts @@ -8,6 +8,7 @@ import { app, BrowserWindow, BrowserWindowConstructorOptions, Menu, Tray } from import { join } from "path"; import { IpcEvents } from "shared/IpcEvents"; import { once } from "shared/utils/once"; +import type { SettingsStore } from "shared/utils/SettingsStore"; import { ICON_PATH } from "../shared/paths"; import { createAboutWindow } from "./about"; @@ -27,6 +28,27 @@ app.on("before-quit", () => { export let mainWin: BrowserWindow; +function makeSettingsListenerHelpers(o: SettingsStore) { + const listeners = new Map<(data: any) => void, PropertyKey>(); + + const addListener: typeof o.addChangeListener = (path, cb) => { + listeners.set(cb, path); + o.addChangeListener(path, cb); + }; + const removeListener = () => { + for (const [listener, path] of listeners) { + o.removeChangeListener(path as any, listener); + } + + listeners.clear(); + }; + + return [addListener, removeListener] as const; +} + +const [addSettingsListener, removeSettingsListeners] = makeSettingsListenerHelpers(Settings); +const [addVencordSettingsListener, removeVencordSettingsListeners] = makeSettingsListenerHelpers(VencordSettings); + function initTray(win: BrowserWindow) { const trayMenu = Menu.buildFromTemplate([ { @@ -187,11 +209,11 @@ function initWindowBoundsListeners(win: BrowserWindow) { } function initSettingsListeners(win: BrowserWindow) { - Settings.addChangeListener("tray", enable => { + addSettingsListener("tray", enable => { if (enable) initTray(win); else tray?.destroy(); }); - Settings.addChangeListener("disableMinSize", disable => { + addSettingsListener("disableMinSize", disable => { if (disable) { // 0 no work win.setMinimumSize(1, 1); @@ -206,7 +228,7 @@ function initSettingsListeners(win: BrowserWindow) { } }); - VencordSettings.addChangeListener("macosTranslucency", enabled => { + addVencordSettingsListener("macosTranslucency", enabled => { if (enabled) { win.setVibrancy("sidebar"); win.setBackgroundColor("#ffffff00"); @@ -224,6 +246,10 @@ function initSpellCheck(win: BrowserWindow) { } function createMainWindow() { + // Clear up previous settings listeners + removeSettingsListeners(); + removeVencordSettingsListeners(); + const win = (mainWin = new BrowserWindow({ show: false, autoHideMenuBar: true, diff --git a/src/renderer/patches/spellCheck.tsx b/src/renderer/patches/spellCheck.tsx index 9654bc2..d6c0f51 100644 --- a/src/renderer/patches/spellCheck.tsx +++ b/src/renderer/patches/spellCheck.tsx @@ -5,13 +5,16 @@ */ import { addContextMenuPatch } from "@vencord/types/api/ContextMenu"; -import { Menu } from "@vencord/types/webpack/common"; +import { findStoreLazy } from "@vencord/types/webpack"; +import { ContextMenu, FluxDispatcher, Menu } from "@vencord/types/webpack/common"; import { addPatch } from "./shared"; let word: string; let corrections: string[]; +const SpellCheckStore = findStoreLazy("SpellcheckStore"); + // Make spellcheck suggestions work addPatch({ patches: [ @@ -38,22 +41,36 @@ addPatch({ }); addContextMenuPatch("textarea-context", children => () => { - if (!word || !corrections?.length) return; + const hasCorrections = Boolean(word && corrections?.length); children.push( - {corrections.map(c => ( - VencordDesktopNative.spellcheck.replaceMisspelling(c)} - /> - ))} - - VencordDesktopNative.spellcheck.addToDictionary(word)} + {hasCorrections && ( + <> + {corrections.map(c => ( + VencordDesktopNative.spellcheck.replaceMisspelling(c)} + /> + ))} + + VencordDesktopNative.spellcheck.addToDictionary(word)} + /> + + )} + { + FluxDispatcher.dispatch({ type: "SPELLCHECK_TOGGLE" }); + // Haven't found a good way to update state, so just close for now 🤷‍♀️ + ContextMenu.close(); + }} /> );