Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(spellcheck): apply language changes live without restart
Elian Doranba66aa4cb3d8
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 3e4527cc13..0e351bd4a2 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -1745,7 +1745,7 @@"description": "These options apply only for desktop builds, browsers will use their own native spell check.","enable": "Check spelling","language_code_label": "Spell Check Languages",- "restart-required": "Changes to the spell check options will take effect after application restart.",+ "restart-required": "Enabling or disabling spell check takes effect after the application is restarted. Language changes apply immediately.","custom_dictionary_title": "Custom Dictionary","custom_dictionary_description": "Words added to the dictionary are synced across all your devices.","custom_dictionary_edit": "Custom words",diff --git a/apps/client/src/widgets/type_widgets/options/spellcheck.tsx b/apps/client/src/widgets/type_widgets/options/spellcheck.tsxindex 7e1daae3b4..fa32acdb2e 100644--- a/apps/client/src/widgets/type_widgets/options/spellcheck.tsx+++ b/apps/client/src/widgets/type_widgets/options/spellcheck.tsx@@ -67,6 +67,9 @@ function SpellcheckLanguages() {const setSelectedCodes = useCallback((codes: string[]) => {setSpellCheckLanguageCode(codes.join(", "));+ // Apply immediately to the live Electron sessions so the change takes+ // effect without restarting the app.+ window.electronApi?.spellcheck.setSpellCheckerLanguages(codes);}, [setSpellCheckLanguageCode]);const availableLanguages = useMemo<SpellcheckLanguage[]>(() => {diff --git a/apps/desktop/src/preload.spec.ts b/apps/desktop/src/preload.spec.tsindex d6f17c197b..6bda87e507 100644--- a/apps/desktop/src/preload.spec.ts+++ b/apps/desktop/src/preload.spec.ts@@ -369,6 +369,14 @@ describe("preload script", () => {ipcRendererSyncResults.set("get-available-spellchecker-languages:undefined", ["en-US", "de-DE"]);expect(spell().getAvailableSpellCheckerLanguages()).toEqual(["en-US", "de-DE"]);});++ it("setSpellCheckerLanguages sends correct IPC message", () => {+ spell().setSpellCheckerLanguages(["en-US", "fr"]);+ expect(ipcRendererSent).toContainEqual({+ channel: "set-spellchecker-languages",+ args: [["en-US", "fr"]]+ });+ });});describe("systemIntegration", () => {diff --git a/apps/desktop/src/preload.ts b/apps/desktop/src/preload.tsindex aee615ef5a..c4a3c4e3e3 100644--- a/apps/desktop/src/preload.ts+++ b/apps/desktop/src/preload.ts@@ -152,6 +152,9 @@ contextBridge.exposeInMainWorld("electronApi", {},getAvailableSpellCheckerLanguages(): string[] {return ipcRenderer.sendSync("get-available-spellchecker-languages");+ },+ setSpellCheckerLanguages(languageCodes: string[]) {+ ipcRenderer.send("set-spellchecker-languages", languageCodes);}},diff --git a/apps/desktop/src/services/window.spec.ts b/apps/desktop/src/services/window.spec.tsindex a83ca5dbb6..0d8de1bfa5 100644--- a/apps/desktop/src/services/window.spec.ts+++ b/apps/desktop/src/services/window.spec.ts@@ -732,6 +732,14 @@ describe("window service", () => {expect(ev.returnValue).toEqual(["en-US", "de"]);});+ it("set-spellchecker-languages applies the codes to every open window's session", () => {+ new FakeBrowserWindow();+ fireOn("set-spellchecker-languages", makeEvent(), ["en-US", "fr"]);+ for (const win of state.windows) {+ expect(win.webContents.session.setSpellCheckerLanguages).toHaveBeenCalledWith(["en-US", "fr"]);+ }+ });+it("title bar / material / vibrancy / button position setters", () => {const win = state.windows[state.windows.length - 1];fireOn("set-title-bar-overlay", makeEvent(), { color: "#fff", symbolColor: "#000" });diff --git a/apps/desktop/src/services/window.ts b/apps/desktop/src/services/window.tsindex c6a8b817cf..83a794512a 100644--- a/apps/desktop/src/services/window.ts+++ b/apps/desktop/src/services/window.ts@@ -259,13 +259,29 @@ function isDevToolsDocked(webContents: WebContents) {function setupSpellcheckForSession(session: Session) {if (!loadedSpellcheckSessions.has(session)) {loadedSpellcheckSessions.add(session);+ session.setSpellCheckerLanguages(getConfiguredSpellcheckLanguages());+ }+}- const languageCodes = optionService- .getOption("spellCheckLanguageCode")- .split(",")- .map((code) => code.trim())- .filter(Boolean);+function getConfiguredSpellcheckLanguages(): string[] {+ return optionService+ .getOption("spellCheckLanguageCode")+ .split(",")+ .map((code) => code.trim())+ .filter(Boolean);+}+/**+ * Re-applies the spell-check language list to every open window's session so a+ * language change in the settings takes effect without restarting the app.+ * Sessions are deduplicated because all windows share the default session.+ */+function applySpellcheckLanguages(languageCodes: string[]) {+ const sessions = new Set<Session>();+ for (const win of electron.BrowserWindow.getAllWindows()) {+ sessions.add(win.webContents.session);+ }+ for (const session of sessions) {session.setSpellCheckerLanguages(languageCodes);}}@@ -451,6 +467,10 @@ export function setupWindowing() {event.returnValue = event.sender.session.availableSpellCheckerLanguages;});+ electron.ipcMain.on("set-spellchecker-languages", (_event, languageCodes: string[]) => {+ applySpellcheckLanguages(languageCodes);+ });+// Window management IPC handlers (replacing @electron/remote for renderer access)electron.ipcMain.on("set-title-bar-overlay", (event, options: { color: string; symbolColor: string }) => {electron.BrowserWindow.fromWebContents(event.sender)?.setTitleBarOverlay(options);diff --git a/packages/commons/src/lib/electron_api_interface.ts b/packages/commons/src/lib/electron_api_interface.tsindex 876dc3303e..cb10e324bc 100644--- a/packages/commons/src/lib/electron_api_interface.ts+++ b/packages/commons/src/lib/electron_api_interface.ts@@ -340,6 +340,13 @@ export interface ElectronSpellcheckApi {/** Returns the BCP-47 language tags Chromium can spell-check on this platform. */getAvailableSpellCheckerLanguages(): string[];++ /**+ * Applies the spell-check language list to every open window's session+ * immediately, without an application restart. No-op on macOS, where the OS+ * spell checker auto-detects the language.+ */+ setSpellCheckerLanguages(languageCodes: string[]): void;}/** OS integration controls — system tray and autostart / launch-on-login. */