Skip to content

Build your personal knowledge base with Trilium Notes

feat(electron): handle more window-related commands (full screen, devtools)

Elian Doranca55bc27b6a8
diff --git a/apps/client/src/components/entrypoints.ts b/apps/client/src/components/entrypoints.ts
index 781790e156..ac464e3c4a 100644
--- a/apps/client/src/components/entrypoints.ts
+++ b/apps/client/src/components/entrypoints.ts
@@ -20,9 +20,7 @@ export default class Entrypoints extends Component {
}
openDevToolsCommand() {
- if (utils.isElectron()) {
- utils.dynamicRequire("@electron/remote").getCurrentWindow().webContents.toggleDevTools();
- }
+ window.electronApi?.toggleDevTools();
}
async createNoteIntoInboxCommand() {
@@ -87,12 +85,9 @@ export default class Entrypoints extends Component {
}
toggleFullscreenCommand() {
- if (utils.isElectron()) {
- const win = utils.dynamicRequire("@electron/remote").getCurrentWindow();
-
- if (win.isFullScreenable()) {
- win.setFullScreen(!win.isFullScreen());
- }
+ const api = window.electronApi;
+ if (api) {
+ api.setFullScreen(!api.isFullScreen());
} else {
document.documentElement.requestFullscreen();
}
@@ -108,24 +103,20 @@ export default class Entrypoints extends Component {
}
backInNoteHistoryCommand() {
- if (utils.isElectron()) {
- // standard JS version does not work completely correctly in electron
- const webContents = utils.dynamicRequire("@electron/remote").getCurrentWebContents();
- const activeIndex = webContents.navigationHistory.getActiveIndex();
-
- webContents.goToIndex(activeIndex - 1);
+ const api = window.electronApi;
+ if (api) {
+ const activeIndex = api.navigationGetActiveIndex();
+ api.navigationGoToIndex(activeIndex - 1);
} else {
window.history.back();
}
}
forwardInNoteHistoryCommand() {
- if (utils.isElectron()) {
- // standard JS version does not work completely correctly in electron
- const webContents = utils.dynamicRequire("@electron/remote").getCurrentWebContents();
- const activeIndex = webContents.navigationHistory.getActiveIndex();
-
- webContents.goToIndex(activeIndex + 1);
+ const api = window.electronApi;
+ if (api) {
+ const activeIndex = api.navigationGetActiveIndex();
+ api.navigationGoToIndex(activeIndex + 1);
} else {
window.history.forward();
}
@@ -146,10 +137,8 @@ export default class Entrypoints extends Component {
async openInWindowCommand({ notePath, hoistedNoteId, viewScope }: NoteCommandData) {
const extraWindowHash = linkService.calculateHash({ notePath, hoistedNoteId, viewScope });
- if (utils.isElectron()) {
- const { ipcRenderer } = utils.dynamicRequire("electron");
-
- ipcRenderer.send("create-extra-window", { extraWindowHash });
+ if (window.electronApi) {
+ window.electronApi.createExtraWindow(extraWindowHash);
} else {
const url = `${window.location.protocol}//${window.location.host}${window.location.pathname}?extraWindow=1${extraWindowHash}`;
diff --git a/apps/client/src/types.d.ts b/apps/client/src/types.d.ts
index fb4358d988..00e191573e 100644
--- a/apps/client/src/types.d.ts
+++ b/apps/client/src/types.d.ts
@@ -69,8 +69,12 @@ declare global {
setWindowButtonPosition(position: { x: number; y: number }): void;
onEnterFullScreen(callback: () => void): void;
onLeaveFullScreen(callback: () => void): void;
+ toggleDevTools(): void;
+ isFullScreen(): boolean;
+ setFullScreen(enabled: boolean): void;
isAlwaysOnTop(): boolean;
setAlwaysOnTop(enabled: boolean): void;
+ createExtraWindow(extraWindowHash: string): void;
setBackgroundMaterial(material: string): void;
setVibrancy(vibrancy: string): void;
clearNavigationHistory(): void;
diff --git a/apps/desktop/spec/preload.spec.ts b/apps/desktop/spec/preload.spec.ts
index 99b8a58f49..a4e0a96e2b 100644
--- a/apps/desktop/spec/preload.spec.ts
+++ b/apps/desktop/spec/preload.spec.ts
@@ -204,6 +204,35 @@ describe("preload script", () => {
});
});
+ it("toggleDevTools sends correct IPC message", () => {
+ getApi().toggleDevTools();
+ expect(ipcRendererSent).toContainEqual({
+ channel: "toggle-dev-tools",
+ args: []
+ });
+ });
+
+ it("isFullScreen uses sendSync", () => {
+ ipcRendererSyncResults.set("is-full-screen:undefined", true);
+ expect(getApi().isFullScreen()).toBe(true);
+ });
+
+ it("setFullScreen sends correct IPC message", () => {
+ getApi().setFullScreen(true);
+ expect(ipcRendererSent).toContainEqual({
+ channel: "set-full-screen",
+ args: [true]
+ });
+ });
+
+ it("createExtraWindow sends correct IPC message", () => {
+ getApi().createExtraWindow("#root/abc123");
+ expect(ipcRendererSent).toContainEqual({
+ channel: "create-extra-window",
+ args: [{ extraWindowHash: "#root/abc123" }]
+ });
+ });
+
it("isAlwaysOnTop uses sendSync", () => {
ipcRendererSyncResults.set("is-always-on-top:undefined", true);
expect(getApi().isAlwaysOnTop()).toBe(true);
diff --git a/apps/desktop/src/preload.ts b/apps/desktop/src/preload.ts
index e430fe65b0..dc51890b57 100644
--- a/apps/desktop/src/preload.ts
+++ b/apps/desktop/src/preload.ts
@@ -71,6 +71,18 @@ contextBridge.exposeInMainWorld("electronApi", {
setAlwaysOnTop(enabled: boolean) {
ipcRenderer.send("set-always-on-top", enabled);
},
+ toggleDevTools() {
+ ipcRenderer.send("toggle-dev-tools");
+ },
+ isFullScreen(): boolean {
+ return ipcRenderer.sendSync("is-full-screen");
+ },
+ setFullScreen(enabled: boolean) {
+ ipcRenderer.send("set-full-screen", enabled);
+ },
+ createExtraWindow(extraWindowHash: string) {
+ ipcRenderer.send("create-extra-window", { extraWindowHash });
+ },
// Tray
reloadTray() {
diff --git a/apps/server/src/services/window.ts b/apps/server/src/services/window.ts
index 6cf522cf61..c5053dc8c2 100644
--- a/apps/server/src/services/window.ts
+++ b/apps/server/src/services/window.ts
@@ -115,6 +115,18 @@ electron.ipcMain.on("set-native-theme-source", (_event, source: "system" | "ligh
electron.nativeTheme.themeSource = source;
});
+electron.ipcMain.on("toggle-dev-tools", (event) => {
+ event.sender.toggleDevTools();
+});
+
+electron.ipcMain.on("is-full-screen", (event) => {
+ event.returnValue = electron.BrowserWindow.fromWebContents(event.sender)?.isFullScreen() ?? false;
+});
+
+electron.ipcMain.on("set-full-screen", (event, enabled: boolean) => {
+ electron.BrowserWindow.fromWebContents(event.sender)?.setFullScreen(enabled);
+});
+
electron.ipcMain.on("is-always-on-top", (event) => {
event.returnValue = electron.BrowserWindow.fromWebContents(event.sender)?.isAlwaysOnTop() ?? false;
});