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
fix(desktop): transparency issues when dev tools is docked
Elian Doran612749075001
diff --git a/apps/client/src/desktop.ts b/apps/client/src/desktop.tsindex 12eba31ec8..129ea53556 100644--- a/apps/client/src/desktop.ts+++ b/apps/client/src/desktop.ts@@ -10,6 +10,7 @@ import { t } from "./services/i18n.js";import { syncNativeWindowWithTheme } from "./services/native_window.js";import noteAutocompleteService from "./services/note_autocomplete.js";import noteTooltipService from "./services/note_tooltip.js";+import { setBackgroundEffectsSuspended } from "./services/theme.js";import toastService from "./services/toast.js";import utils from "./services/utils.js";@@ -57,6 +58,7 @@ function initOnElectron() {syncNativeWindowWithTheme();initFullScreenDetection(win);+ initDevToolsDockDetection(win);// With an "auto" theme the effective colors of background effects and the native title bar// follow the OS color scheme.@@ -70,3 +72,13 @@ function initFullScreenDetection(win: ElectronWindowApi) {win.onEnterFullScreen(() => document.body.classList.add("full-screen"));win.onLeaveFullScreen(() => document.body.classList.remove("full-screen"));}++/**+ * Chromium disables the native window material (Mica / vibrancy) while DevTools is docked into+ * the window, which would leave the transparent background effects floating over a solid void —+ * suspend them for the duration. DevTools in a separate window does not affect the material.+ */+function initDevToolsDockDetection(win: ElectronWindowApi) {+ setBackgroundEffectsSuspended(win.isDevToolsDocked());+ win.onDevToolsDockChanged(setBackgroundEffectsSuspended);+}diff --git a/apps/client/src/services/theme.ts b/apps/client/src/services/theme.tsindex aa7d28ab7c..9e803dc527 100644--- a/apps/client/src/services/theme.ts+++ b/apps/client/src/services/theme.ts@@ -116,12 +116,27 @@ export function updateColorSchemeClasses() {document.body.classList.toggle("dark-theme", colorScheme === "dark");}+let backgroundEffectsSuspended = false;++/**+ * Temporarily suppresses background effects without touching the user's option, e.g. while+ * DevTools is docked into the window — Chromium disables the native window material then, which+ * would leave the transparent UI floating over a solid void.+ */+export function setBackgroundEffectsSuspended(suspended: boolean) {+ if (backgroundEffectsSuspended === suspended) {+ return;+ }+ backgroundEffectsSuspended = suspended;+ updateThemeCapabilities();+}+/** Toggles the `theme-supports-background-effects` body class to match the active theme's `--allow-background-effects`. */export function updateThemeCapabilities() {const useBgfx = readCssVar(document.documentElement, "allow-background-effects").asBoolean(false);- document.body.classList.toggle("theme-supports-background-effects", useBgfx);+ document.body.classList.toggle("theme-supports-background-effects", useBgfx && !backgroundEffectsSuspended);}/** Reads the current `theme` option and applies it live, resolving custom-theme metadata from the server if needed. */diff --git a/apps/desktop/src/preload.spec.ts b/apps/desktop/src/preload.spec.tsindex 70be4972ba..e5e7dbdc31 100644--- a/apps/desktop/src/preload.spec.ts+++ b/apps/desktop/src/preload.spec.ts@@ -189,6 +189,20 @@ describe("preload script", () => {expect(ipcRendererSent).toContainEqual({ channel: "toggle-dev-tools", args: [] });});+ it("isDevToolsDocked uses sendSync", () => {+ ipcRendererSyncResults.set("is-dev-tools-docked:undefined", true);+ expect(win().isDevToolsDocked()).toBe(true);+ });++ it("onDevToolsDockChanged registers and forwards the dock state", () => {+ const callback = vi.fn();+ win().onDevToolsDockChanged(callback);+ const listeners = ipcRendererListeners.get("dev-tools-dock-changed") ?? [];+ expect(listeners).toHaveLength(1);+ listeners[0]({}, true);+ expect(callback).toHaveBeenCalledWith(true);+ });+it("reloadAllWindows sends correct IPC message", () => {win().reloadAllWindows();expect(ipcRendererSent).toContainEqual({ channel: "reload-all-windows", args: [] });diff --git a/apps/desktop/src/preload.ts b/apps/desktop/src/preload.tsindex a6afbc17b7..1cdaef96cb 100644--- a/apps/desktop/src/preload.ts+++ b/apps/desktop/src/preload.ts@@ -66,6 +66,9 @@ contextBridge.exposeInMainWorld("electronApi", {toggleDevTools() {ipcRenderer.send("toggle-dev-tools");},+ isDevToolsDocked(): boolean {+ return ipcRenderer.sendSync("is-dev-tools-docked");+ },// App lifecyclereloadAllWindows() {@@ -98,6 +101,9 @@ contextBridge.exposeInMainWorld("electronApi", {},onOpenInSameTab(callback: (noteId: string) => void) {ipcRenderer.on("openInSameTab", (_event, noteId) => callback(noteId));+ },+ onDevToolsDockChanged(callback: (docked: boolean) => void) {+ ipcRenderer.on("dev-tools-dock-changed", (_event, docked: boolean) => callback(docked));}},diff --git a/apps/desktop/src/services/window.spec.ts b/apps/desktop/src/services/window.spec.tsindex 7d9b09da6f..1f75967472 100644--- a/apps/desktop/src/services/window.spec.ts+++ b/apps/desktop/src/services/window.spec.ts@@ -48,6 +48,8 @@ class FakeWebContents {});public listeners = new Map<string, Handler[]>();public toggleDevTools = vi.fn();+ public isDevToolsOpened = vi.fn(() => false);+ public devToolsWebContents: FakeWebContents | null = null;public cut = vi.fn();public copy = vi.fn();public paste = vi.fn();@@ -413,6 +415,30 @@ describe("window service", () => {expect(wc.send).toHaveBeenCalledWith("context-menu", expect.objectContaining({ x: 1 }));});+ it("forwards the DevTools dock state to the renderer", () => {+ const win = state.windows[state.windows.length - 1];+ const wc = win.webContents;++ // Docked: the DevTools contents resolves to the same BrowserWindow as the page.+ wc.isDevToolsOpened.mockReturnValue(true);+ wc.devToolsWebContents = new FakeWebContents();+ wc.fire("devtools-opened");+ expect(wc.send).toHaveBeenCalledWith("dev-tools-dock-changed", true);++ // Detached: the DevTools contents has no owning BrowserWindow.+ wc.send.mockClear();+ state.fromWebContentsResult = "null";+ wc.fire("devtools-focused");+ expect(wc.send).toHaveBeenCalledWith("dev-tools-dock-changed", false);++ // Closed.+ wc.send.mockClear();+ state.fromWebContentsResult = undefined;+ wc.isDevToolsOpened.mockReturnValue(false);+ wc.fire("devtools-closed");+ expect(wc.send).toHaveBeenCalledWith("dev-tools-dock-changed", false);+ });+it("skips full-screen wiring when no window resolves", async () => {state.fromWebContentsResult = "null";// configureWebContents is invoked during createExtraWindow@@ -708,6 +734,18 @@ describe("window service", () => {expect(ev.sender.toggleDevTools).toHaveBeenCalled();});+ it("is-dev-tools-docked reports whether DevTools shares the sender's window", () => {+ const closed = makeEvent();+ fireOn("is-dev-tools-docked", closed);+ expect(closed.returnValue).toBe(false);++ const docked = makeEvent();+ docked.sender.isDevToolsOpened.mockReturnValue(true);+ docked.sender.devToolsWebContents = new FakeWebContents();+ fireOn("is-dev-tools-docked", docked);+ expect(docked.returnValue).toBe(true);+ });+it("window state queries and mutations", () => {const win = state.windows[state.windows.length - 1];const fs1 = makeEvent();diff --git a/apps/desktop/src/services/window.ts b/apps/desktop/src/services/window.tsindex 79854a43f1..a6ce58db9e 100644--- a/apps/desktop/src/services/window.ts+++ b/apps/desktop/src/services/window.ts@@ -187,6 +187,15 @@ async function configureWebContents(webContents: WebContents, spellcheckEnabled:webContents.on("did-navigate", () => webContents.send("did-navigate"));webContents.on("did-navigate-in-page", () => webContents.send("did-navigate-in-page"));+ // Keep the renderer informed about whether DevTools is docked into this window, where+ // Chromium disables the native window material (Mica / vibrancy) and the renderer should+ // suspend its transparent background effects. Re-checked on focus because re-docking an+ // already open DevTools emits no dedicated event.+ const sendDevToolsDockState = () => webContents.send("dev-tools-dock-changed", isDevToolsDocked(webContents));+ webContents.on("devtools-opened", sendDevToolsDockState);+ webContents.on("devtools-focused", sendDevToolsDockState);+ webContents.on("devtools-closed", sendDevToolsDockState);+// Forward context-menu event to the renderer with only the fields we need.webContents.on("context-menu", (_event, params) => {webContents.send("context-menu", {@@ -208,6 +217,20 @@ async function configureWebContents(webContents: WebContents, spellcheckEnabled:});}+/** Whether DevTools for the given contents is open and docked into the page's own window (as opposed to a separate window). */+function isDevToolsDocked(webContents: WebContents) {+ const devToolsWebContents = webContents.devToolsWebContents;+ if (!webContents.isDevToolsOpened() || !devToolsWebContents) {+ return false;+ }++ // A docked DevTools view is hosted by the same BrowserWindow as the page itself; when+ // detached it lives in its own native window, which is not a BrowserWindow and for which+ // fromWebContents() returns null.+ const devToolsWindow = electron.BrowserWindow.fromWebContents(devToolsWebContents);+ return devToolsWindow !== null && devToolsWindow === electron.BrowserWindow.fromWebContents(webContents);+}+function setupSpellcheckForSession(session: Session) {if (!loadedSpellcheckSessions.has(session)) {loadedSpellcheckSessions.add(session);@@ -428,6 +451,10 @@ export function setupWindowing() {event.sender.toggleDevTools();});+ electron.ipcMain.on("is-dev-tools-docked", (event) => {+ event.returnValue = isDevToolsDocked(event.sender);+ });+electron.ipcMain.on("is-full-screen", (event) => {event.returnValue = electron.BrowserWindow.fromWebContents(event.sender)?.isFullScreen() ?? false;});diff --git a/packages/commons/src/lib/electron_api_interface.ts b/packages/commons/src/lib/electron_api_interface.tsindex 4c31052341..82ac40dac2 100644--- a/packages/commons/src/lib/electron_api_interface.ts+++ b/packages/commons/src/lib/electron_api_interface.ts@@ -127,6 +127,12 @@ export interface ElectronWindowApi {/** Opens or closes Chromium DevTools for the current window. */toggleDevTools(): void;+ /**+ * Synchronously returns whether DevTools is currently open and docked into this window+ * (as opposed to detached into a separate window).+ */+ isDevToolsDocked(): boolean;+// #endregion// #region Background effects@@ -181,6 +187,13 @@ export interface ElectronWindowApi {*/onOpenInSameTab(callback: (noteId: string) => void): void;+ /**+ * Subscribes to changes of the DevTools docking state. `docked` is `true` only while+ * DevTools is attached to this window — where Chromium disables the native window+ * material (Mica / vibrancy) — and `false` when it is closed or in a separate window.+ */+ onDevToolsDockChanged(callback: (docked: boolean) => void): void;+// #endregion}