Skip to content

Build your personal knowledge base with Trilium Notes

feat(electron): handle lifecycle methods

Elian Dorand126fdc43b03
diff --git a/apps/client/src/components/root_command_executor.ts b/apps/client/src/components/root_command_executor.ts
index 1ae82a6923..5bb692e9bc 100644
--- a/apps/client/src/components/root_command_executor.ts
+++ b/apps/client/src/components/root_command_executor.ts
@@ -187,11 +187,7 @@ export default class RootCommandExecutor extends Component {
toggleTrayCommand() {
if (!utils.isElectron() || options.is("disableTray")) return;
- const { BrowserWindow } = utils.dynamicRequire("@electron/remote");
- const windows = BrowserWindow.getAllWindows() as Electron.BaseWindow[];
- const isVisible = windows.every((w) => w.isVisible());
- const action = isVisible ? "hide" : "show";
- for (const window of windows) window[action]();
+ window.electronApi?.window.toggleAllWindows();
}
toggleZenModeCommand() {
diff --git a/apps/client/src/services/utils.ts b/apps/client/src/services/utils.ts
index 2deec805f5..940ec5f02b 100644
--- a/apps/client/src/services/utils.ts
+++ b/apps/client/src/services/utils.ts
@@ -13,24 +13,19 @@ export function reloadFrontendApp(reason?: string) {
logInfo(`Frontend app reload: ${reason}`);
}
- if (isElectron()) {
- for (const window of dynamicRequire("@electron/remote").BrowserWindow.getAllWindows()) {
- window.reload();
- }
+ if (window.electronApi) {
+ window.electronApi.window.reloadAllWindows();
} else {
window.location.reload();
}
}
export function restartDesktopApp() {
- if (!isElectron()) {
+ if (window.electronApi) {
+ window.electronApi.window.restartApp();
+ } else {
reloadFrontendApp();
- return;
}
-
- const app = dynamicRequire("@electron/remote").app;
- app.relaunch();
- app.exit();
}
/**
diff --git a/apps/desktop/spec/preload.spec.ts b/apps/desktop/spec/preload.spec.ts
index 197351423a..31aec2f2eb 100644
--- a/apps/desktop/spec/preload.spec.ts
+++ b/apps/desktop/spec/preload.spec.ts
@@ -179,6 +179,21 @@ describe("preload script", () => {
expect(ipcRendererSent).toContainEqual({ channel: "toggle-dev-tools", args: [] });
});
+ it("reloadAllWindows sends correct IPC message", () => {
+ win().reloadAllWindows();
+ expect(ipcRendererSent).toContainEqual({ channel: "reload-all-windows", args: [] });
+ });
+
+ it("restartApp sends correct IPC message", () => {
+ win().restartApp();
+ expect(ipcRendererSent).toContainEqual({ channel: "restart-app", args: [] });
+ });
+
+ it("toggleAllWindows sends correct IPC message", () => {
+ win().toggleAllWindows();
+ expect(ipcRendererSent).toContainEqual({ channel: "toggle-all-windows", args: [] });
+ });
+
it("setBackgroundMaterial sends correct IPC message", () => {
win().setBackgroundMaterial("mica");
expect(ipcRendererSent).toContainEqual({ channel: "set-background-material", args: ["mica"] });
diff --git a/apps/desktop/src/preload.ts b/apps/desktop/src/preload.ts
index 0f0b478860..681e82ef20 100644
--- a/apps/desktop/src/preload.ts
+++ b/apps/desktop/src/preload.ts
@@ -67,6 +67,17 @@ contextBridge.exposeInMainWorld("electronApi", {
ipcRenderer.send("toggle-dev-tools");
},
+ // App lifecycle
+ reloadAllWindows() {
+ ipcRenderer.send("reload-all-windows");
+ },
+ restartApp() {
+ ipcRenderer.send("restart-app");
+ },
+ toggleAllWindows() {
+ ipcRenderer.send("toggle-all-windows");
+ },
+
// Background effects
setBackgroundMaterial(material: string) {
ipcRenderer.send("set-background-material", material);
diff --git a/apps/server/src/services/window.ts b/apps/server/src/services/window.ts
index 9c2d2066c4..3c942956ee 100644
--- a/apps/server/src/services/window.ts
+++ b/apps/server/src/services/window.ts
@@ -83,6 +83,26 @@ electron.ipcMain.on("create-extra-window", (event, arg) => {
createExtraWindow(arg.extraWindowHash);
});
+electron.ipcMain.on("reload-all-windows", () => {
+ for (const win of electron.BrowserWindow.getAllWindows()) {
+ win.reload();
+ }
+});
+
+electron.ipcMain.on("restart-app", () => {
+ electron.app.relaunch();
+ electron.app.exit();
+});
+
+electron.ipcMain.on("toggle-all-windows", () => {
+ const windows = electron.BrowserWindow.getAllWindows();
+ const isVisible = windows.every((w) => w.isVisible());
+ const action = isVisible ? "hide" : "show";
+ for (const win of windows) {
+ win[action]();
+ }
+});
+
electron.ipcMain.on("add-word-to-dictionary", (event, word: string) => {
event.sender.session.addWordToSpellCheckerDictionary(word);
customDictionary.addWord(word);
diff --git a/packages/commons/src/lib/electron_api_interface.ts b/packages/commons/src/lib/electron_api_interface.ts
index 76aeca30ec..554e75bded 100644
--- a/packages/commons/src/lib/electron_api_interface.ts
+++ b/packages/commons/src/lib/electron_api_interface.ts
@@ -48,6 +48,11 @@ export interface ElectronWindowApi {
setBackgroundMaterial(material: string): void;
setVibrancy(vibrancy: string): void;
+ // App lifecycle
+ reloadAllWindows(): void;
+ restartApp(): void;
+ toggleAllWindows(): void;
+
// Main → renderer events
onGlobalShortcut(callback: (actionName: string) => void): void;
onOpenInSameTab(callback: (noteId: string) => void): void;