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): don't apply Mica on Windows builds without backdrop support, closes #10590
Elian Doran240566626da4
diff --git a/apps/client/src/setup.tsx b/apps/client/src/setup.tsxindex 405afae32c..4ca3760913 100644--- a/apps/client/src/setup.tsx+++ b/apps/client/src/setup.tsx@@ -26,7 +26,13 @@ async function main() {bodyWrapper.classList.add("setup-outer-wrapper");document.body.classList.add("setup", window.glob.device || "desktop");if (isElectron()) {- document.body.classList.add("electron", `platform-${window.glob.platform}`, "background-effects");+ document.body.classList.add("electron", `platform-${window.glob.platform}`);+ // Going transparent is only safe where the window actually has a backdrop material+ // (Windows 11 22H2+ Mica / macOS vibrancy) — elsewhere the window composites to+ // black, making light-theme text unreadable (#10590).+ if (window.glob.hasBackgroundEffects) {+ document.body.classList.add("background-effects");+ }}render(<App />, bodyWrapper);document.body.replaceChildren(bodyWrapper);diff --git a/apps/desktop/src/services/window.spec.ts b/apps/desktop/src/services/window.spec.tsindex bb34e77dbf..7489862418 100644--- a/apps/desktop/src/services/window.spec.ts+++ b/apps/desktop/src/services/window.spec.ts@@ -8,6 +8,7 @@ const state = vi.hoisted(() => ({isDev: true,isMac: false,isWindows: false,+ supportsBackgroundMaterial: false,appVersion: "1.0.0",options: {} as Record<string, string>,optionBools: {} as Record<string, boolean>,@@ -201,6 +202,16 @@ vi.mock("./web_contents_security", () => ({ setupWebContentsSecurity: vi.fn() })// verify that window creation marks the right milestones.vi.mock("./startup_metrics", () => ({ markStartupMetric: vi.fn() }));+// The real value is an import-time constant derived from the OS running the tests;+// route it through mutable state so Windows 10 and 11 scenarios are both testable.+vi.mock("@triliumnext/server/src/services/utils.js", async (importOriginal) => {+ const actual = await importOriginal<typeof import("@triliumnext/server/src/services/utils.js")>();+ return {+ ...actual,+ get supportsBackgroundMaterial() { return state.supportsBackgroundMaterial; }+ };+});+vi.mock("@triliumnext/core", async (importOriginal) => {const actual = await importOriginal<typeof import("@triliumnext/core")>();return {@@ -270,6 +281,7 @@ beforeEach(() => {state.isDev = true;state.isMac = false;state.isWindows = false;+ state.supportsBackgroundMaterial = false;state.appVersion = "1.0.0";state.options = { spellCheckLanguageCode: "en-US, de , " };state.optionBools = {};@@ -322,6 +334,7 @@ describe("window service", () => {it("applies Windows hidden title bar + mica material", async () => {state.isWindows = true;+ state.supportsBackgroundMaterial = true;state.optionBools = { backgroundEffects: true };await windowService.createMainWindow();const opts = state.windows[state.windows.length - 1].opts as Record<string, unknown>;@@ -329,6 +342,16 @@ describe("window service", () => {expect(opts.backgroundMaterial).toBe("auto");});+ it("omits the window material on Windows builds without backdrop support (Win10 / Win11 21H2)", async () => {+ state.isWindows = true;+ state.supportsBackgroundMaterial = false;+ state.optionBools = { backgroundEffects: true };+ await windowService.createMainWindow();+ const opts = state.windows[state.windows.length - 1].opts as Record<string, unknown>;+ expect(opts.titleBarStyle).toBe("hidden");+ expect(opts.backgroundMaterial).toBeUndefined();+ });+it("does not apply Linux transparent effect when background effects enabled", async () => {state.optionBools = { backgroundEffects: true };await windowService.createMainWindow();@@ -388,11 +411,18 @@ describe("window service", () => {windowService.closeSetupWindow();});- it("applies Windows mica background to the setup window", async () => {+ it("applies Windows mica background to the setup window only where supported", async () => {state.isWindows = true;+ state.supportsBackgroundMaterial = true;await windowService.createSetupWindow();- const opts = state.windows[state.windows.length - 1].opts as Record<string, unknown>;+ let opts = state.windows[state.windows.length - 1].opts as Record<string, unknown>;expect(opts.backgroundMaterial).toBe("mica");++ // Windows 10: no DWM backdrop — a material would leave the window black (#10590).+ state.supportsBackgroundMaterial = false;+ await windowService.createSetupWindow();+ opts = state.windows[state.windows.length - 1].opts as Record<string, unknown>;+ expect(opts.backgroundMaterial).toBeUndefined();});it("applies macOS vibrancy to the setup window", async () => {diff --git a/apps/desktop/src/services/window.ts b/apps/desktop/src/services/window.tsindex 6046e90c5b..1d65b62b5a 100644--- a/apps/desktop/src/services/window.ts+++ b/apps/desktop/src/services/window.ts@@ -1,5 +1,6 @@import { app_info, cls, events, getLog, keyboard_actions as keyboardActionsService, options as optionService, sql_init, utils as coreUtils } from "@triliumnext/core";import { RESOURCE_DIR } from "@triliumnext/server/src/services/resource_dir.js";+import { supportsBackgroundMaterial } from "@triliumnext/server/src/services/utils.js";import { type BrowserWindow, type BrowserWindowConstructorOptions, default as electron, type Session, type WebContents } from "electron";import path from "path";@@ -192,7 +193,7 @@ function getWindowExtraOpts() {if (coreUtils.isMac()) {extraOpts.transparent = true;extraOpts.visualEffectState = "active";- } else if (coreUtils.isWindows()) {+ } else if (coreUtils.isWindows() && supportsBackgroundMaterial) {extraOpts.backgroundMaterial = "auto";}}@@ -366,8 +367,8 @@ async function createSetupWindow() {autoHideMenuBar: true,title: "Trilium Notes Setup",icon: getIcon(),- // Background effects (Mica on Windows, vibrancy on macOS)- ...(coreUtils.isWindows() && { backgroundMaterial: "mica" as const }),+ // Background effects (Mica on Windows 11 22H2+, vibrancy on macOS)+ ...(coreUtils.isWindows() && supportsBackgroundMaterial && { backgroundMaterial: "mica" as const }),...(coreUtils.isMac() && { transparent: true, visualEffectState: "active" as const, vibrancy: "under-window" as const, titleBarStyle: "hiddenInset" as const }),webPreferences: {nodeIntegration: false,diff --git a/apps/server/src/routes/index.ts b/apps/server/src/routes/index.tsindex edfd4a3c38..afc65221ef 100644--- a/apps/server/src/routes/index.ts+++ b/apps/server/src/routes/index.ts@@ -9,7 +9,7 @@ import config from "../services/config.js";import { getLog } from "@triliumnext/core";import port from "../services/port.js";import openID from "../services/open_id.js";-import { isDev, isElectron, isMac, isWindows11 } from "../services/utils.js";+import { isDev, isElectron, isMac, supportsBackgroundMaterial } from "../services/utils.js";import totp from "../services/totp.js";import { generateCsrfToken } from "./csrf_protection.js";@@ -55,7 +55,7 @@ export function bootstrap(req: Request, res: Response) {res.send({...commonItems,hasNativeTitleBar: false,- hasBackgroundEffects: isElectron && (isWindows11 || isMac),+ hasBackgroundEffects: isElectron && (supportsBackgroundMaterial || isMac),isMainWindow: true,appCssNoteIds: []} satisfies BootstrapDefinition);@@ -153,7 +153,7 @@ export function bootstrap(req: Request, res: Response) {hasNativeTitleBar: isElectron && nativeTitleBarVisible,hasBackgroundEffects: options.backgroundEffects === "true"&& isElectron- && (isWindows11 || isMac)+ && (supportsBackgroundMaterial || isMac)&& !nativeTitleBarVisible,isMainWindow: view === "mobile" ? true : !req.query.extraWindow,iconPackCss: [diff --git a/apps/server/src/services/utils.spec.ts b/apps/server/src/services/utils.spec.tsindex 484bb4a824..cd939734f2 100644--- a/apps/server/src/services/utils.spec.ts+++ b/apps/server/src/services/utils.spec.ts@@ -30,7 +30,7 @@ describe("platform flags & isDev", () => {});});-describe("isWindows11 detection", () => {+describe("supportsBackgroundMaterial detection", () => {const ORIGINAL_PLATFORM = Object.getOwnPropertyDescriptor(process, "platform");afterEach(() => {@@ -41,28 +41,30 @@ describe("isWindows11 detection", () => {vi.doUnmock("os");});- // isWindows11 is an import-time constant computed from process.platform and- // os.release(), so each scenario needs a fresh module load with both faked.- async function loadFreshIsWindows11(platform: string, release: string) {+ // supportsBackgroundMaterial is an import-time constant computed from process.platform+ // and os.release(), so each scenario needs a fresh module load with both faked.+ async function loadFreshSupportsBackgroundMaterial(platform: string, release: string) {vi.resetModules();Object.defineProperty(process, "platform", { value: platform, configurable: true });vi.doMock("os", async (importOriginal) => ({...(await importOriginal<typeof import("os")>()),release: () => release}));- return (await import("./utils.js")).isWindows11;+ return (await import("./utils.js")).supportsBackgroundMaterial;}- it("is true on Windows with a build >= 22000", async () => {- expect(await loadFreshIsWindows11("win32", "10.0.22631")).toBe(true);+ it("is true on Windows 11 22H2 and later (build >= 22621)", async () => {+ expect(await loadFreshSupportsBackgroundMaterial("win32", "10.0.22621")).toBe(true);+ expect(await loadFreshSupportsBackgroundMaterial("win32", "10.0.26100")).toBe(true);});- it("is false on Windows with an older build (< 22000)", async () => {- expect(await loadFreshIsWindows11("win32", "10.0.19045")).toBe(false);+ it("is false on Windows 11 21H2 and Windows 10, which lack the DWM backdrop API", async () => {+ expect(await loadFreshSupportsBackgroundMaterial("win32", "10.0.22000")).toBe(false);+ expect(await loadFreshSupportsBackgroundMaterial("win32", "10.0.19045")).toBe(false);});it("is false on non-Windows platforms", async () => {- expect(await loadFreshIsWindows11("linux", "10.0.22631")).toBe(false);+ expect(await loadFreshSupportsBackgroundMaterial("linux", "10.0.22631")).toBe(false);});});diff --git a/apps/server/src/services/utils.ts b/apps/server/src/services/utils.tsindex 6d5211e545..6cf8cfe11b 100644--- a/apps/server/src/services/utils.ts+++ b/apps/server/src/services/utils.ts@@ -9,7 +9,12 @@ export const isMac = process.platform === "darwin";export const isWindows = process.platform === "win32";-export const isWindows11 = isWindows && osVersion[0] === 10 && osVersion[2] >= 22000;+/**+ * Whether the OS supports Electron's `backgroundMaterial` (Mica). The underlying DWM+ * system-backdrop API only exists on Windows 11 22H2 (build 22621) and later; on older builds+ * Electron still stops painting the window background, compositing it to black (#10590).+ */+export const supportsBackgroundMaterial = isWindows && osVersion[0] === 10 && osVersion[2] >= 22621;export const isElectron = !!process.versions["electron"];