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(desktop): use native window controls on Linux
Elian Doran7017e1cfab31
diff --git a/apps/client/src/layouts/desktop_layout.tsx b/apps/client/src/layouts/desktop_layout.tsxindex a7aeb65aba..1a35894cd2 100644--- a/apps/client/src/layouts/desktop_layout.tsx+++ b/apps/client/src/layouts/desktop_layout.tsx@@ -75,7 +75,9 @@ export default class DesktopLayout {* On macOS we need to force the full-width tab bar on Electron in order to allow the semaphore (window controls) enough space.*/const fullWidthTabBar = launcherPaneIsHorizontal || (isElectron && !hasNativeTitleBar && isMac);- const customTitleBarButtons = !hasNativeTitleBar && utils.isLinux();+ // No platform draws its own title bar buttons any more: macOS has the traffic lights and+ // Windows/Linux get the Chromium-drawn Window Controls Overlay.+ const customTitleBarButtons = false;const isNewLayout = isExperimentalFeatureEnabled("new-layout");const rootContainer = new RootContainer(true)diff --git a/apps/client/src/services/native_window.ts b/apps/client/src/services/native_window.tsindex d3cb1f0d7b..59eaee9b43 100644--- a/apps/client/src/services/native_window.ts+++ b/apps/client/src/services/native_window.ts@@ -65,11 +65,13 @@ function applyTransparencyEffects(win: ElectronWindowApi, style: CSSStyleDeclara}function applyTitleBarButtons(win: ElectronWindowApi, style: CSSStyleDeclaration) {- if (window.glob.platform === "win32") {+ // Window Controls Overlay is supported on Windows and Linux.+ if (window.glob.platform === "win32" || window.glob.platform === "linux") {const color = style.getPropertyValue("--native-titlebar-background");const symbolColor = style.getPropertyValue("--native-titlebar-foreground");+ const height = parseInt(style.getPropertyValue("--native-titlebar-height"), 10);if (color && symbolColor) {- win.setTitleBarOverlay({ color, symbolColor });+ win.setTitleBarOverlay({ color, symbolColor, ...(Number.isFinite(height) && { height }) });}}diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.cssindex 16ea9790a6..029f49d932 100644--- a/apps/client/src/stylesheets/style.css+++ b/apps/client/src/stylesheets/style.css@@ -87,6 +87,14 @@ body {font-size: var(--main-font-size);--native-titlebar-foreground: var(--main-text-color);+ /*+ * Height of the Window Controls Overlay on Windows and Linux. Chromium centres the caption+ * buttons within it, so this is the only way to shift them vertically -- there is no x/y+ * equivalent to macOS' setWindowButtonPosition(). Set it to twice the centre line of the tab+ * bar's own buttons so the two line up; themes that place those buttons differently (see+ * theme-next) need their own value.+ */+ --native-titlebar-height: 40;--native-titlebar-darwin-x-offset: 10;--native-titlebar-darwin-y-offset: 12;--launcher-pane-height: 53px;@@ -2188,8 +2196,14 @@ body.electron.platform-darwin:not(.native-titlebar):not(.full-screen) #tab-row-lwidth: 80px;}+/*+ * Keep the tab bar clear of the Window Controls Overlay. On Windows the controls are always on+ * the right, but on Linux their side follows the desktop's decoration layout (GNOME's+ * `button-layout`), so reserve space on whichever side(s) Chromium actually took.+ */body.electron:not(.platform-darwin) .tab-row-container {- padding-inline-end: calc(100vw - env(titlebar-area-width, 100vw));+ padding-inline-start: env(titlebar-area-x, 0px);+ padding-inline-end: calc(100vw - env(titlebar-area-x, 0px) - env(titlebar-area-width, 100vw));}.tab-row-container .toggle-button {diff --git a/apps/client/src/stylesheets/theme-next/shell.css b/apps/client/src/stylesheets/theme-next/shell.cssindex 6c7404c83d..b4d02402e8 100644--- a/apps/client/src/stylesheets/theme-next/shell.css+++ b/apps/client/src/stylesheets/theme-next/shell.css@@ -21,6 +21,13 @@ body.mobile #root-widget {}body {+ /*+ * The Windows / Linux caption buttons are centred within the overlay, so its height has to be+ * twice the centre line of the tab bar's own buttons for the two to line up. Those buttons are+ * top-aligned (`margin: 3px ... auto ...` below), so the centre line follows the tab bar+ * height: 3 + 30/2 = 18 here, and 26 in the taller horizontal layout.+ */+ --native-titlebar-height: 36 !important;--native-titlebar-darwin-x-offset: 10;--native-titlebar-darwin-y-offset: 12 !important;}@@ -30,6 +37,7 @@ body.layout-horizontal {--active-tab-hover-background-color: var(--active-tab-background-color);--new-tab-button-background: transparent;--tab-bar-height: 44px;+ --native-titlebar-height: 52 !important;--native-titlebar-darwin-x-offset: 12;--native-titlebar-darwin-y-offset: 14 !important;}diff --git a/apps/desktop/src/preload.ts b/apps/desktop/src/preload.tsindex bad2ba8e3b..d82a0d7ef7 100644--- a/apps/desktop/src/preload.ts+++ b/apps/desktop/src/preload.ts@@ -17,7 +17,7 @@ contextBridge.exposeInMainWorld("electronApi", {},// Title bar- setTitleBarOverlay(options: { color: string; symbolColor: string }) {+ setTitleBarOverlay(options: { color: string; symbolColor: string; height?: number }) {ipcRenderer.send("set-title-bar-overlay", options);},setWindowButtonPosition(position: { x: number; y: number }) {diff --git a/apps/desktop/src/services/window.spec.ts b/apps/desktop/src/services/window.spec.tsindex 7489862418..4d570c5fd7 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,+ isLinux: false,supportsBackgroundMaterial: false,appVersion: "1.0.0",options: {} as Record<string, string>,@@ -222,7 +223,8 @@ vi.mock("@triliumnext/core", async (importOriginal) => {...actual.utils,isDev: () => state.isDev,isMac: () => state.isMac,- isWindows: () => state.isWindows+ isWindows: () => state.isWindows,+ isLinux: () => state.isLinux},options: {...actual.options,@@ -281,6 +283,7 @@ beforeEach(() => {state.isDev = true;state.isMac = false;state.isWindows = false;+ state.isLinux = false;state.supportsBackgroundMaterial = false;state.appVersion = "1.0.0";state.options = { spellCheckLanguageCode: "en-US, de , " };@@ -352,14 +355,22 @@ describe("window service", () => {expect(opts.backgroundMaterial).toBeUndefined();});- it("does not apply Linux transparent effect when background effects enabled", async () => {+ it("applies the Linux window controls overlay without a transparent effect", async () => {+ state.isLinux = true;state.optionBools = { backgroundEffects: true };await windowService.createMainWindow();const opts = state.windows[state.windows.length - 1].opts as Record<string, unknown>;- expect(opts.frame).toBe(false);+ expect(opts.titleBarStyle).toBe("hidden");+ expect(opts.titleBarOverlay).toBe(true);expect(opts.transparent).toBe(undefined);});+ it("falls back to a frameless window on platforms without a controls overlay", async () => {+ await windowService.createMainWindow();+ const opts = state.windows[state.windows.length - 1].opts as Record<string, unknown>;+ expect(opts.frame).toBe(false);+ });+it("keeps native title bar when option enabled", async () => {state.optionBools = { nativeTitleBarVisible: true };await windowService.createMainWindow();diff --git a/apps/desktop/src/services/window.ts b/apps/desktop/src/services/window.tsindex 1d65b62b5a..e5a42636f8 100644--- a/apps/desktop/src/services/window.ts+++ b/apps/desktop/src/services/window.ts@@ -179,11 +179,13 @@ function getWindowExtraOpts() {if (coreUtils.isMac()) {extraOpts.titleBarStyle = "hiddenInset";extraOpts.titleBarOverlay = true;- } else if (coreUtils.isWindows()) {+ } else if (coreUtils.isWindows() || coreUtils.isLinux()) {+ // Window Controls Overlay: Chromium draws the caption buttons itself, following the+ // platform's own conventions (button glyphs, order and which side they sit on).+ // Supported on Windows and, since Electron 27, on Linux too.extraOpts.titleBarStyle = "hidden";extraOpts.titleBarOverlay = true;} else {- // Linux or other platforms.extraOpts.frame = false;}@@ -535,7 +537,7 @@ export function setupWindowing() {});// Window management IPC handlers (replacing @electron/remote for renderer access)- electron.ipcMain.on("set-title-bar-overlay", (event, options: { color: string; symbolColor: string }) => {+ electron.ipcMain.on("set-title-bar-overlay", (event, options: { color: string; symbolColor: string; height?: number }) => {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 e52b7ea325..770af504f6 100644--- a/packages/commons/src/lib/electron_api_interface.ts+++ b/packages/commons/src/lib/electron_api_interface.ts@@ -76,10 +76,11 @@ export interface ElectronWindowApi {// #region Title bar/**- * Customizes the colors of the Windows native title bar overlay- * (the area containing the minimize / maximize / close buttons).+ * Customizes the Windows and Linux native title bar overlay (the area containing the+ * minimize / maximize / close buttons). `height` also controls their vertical placement,+ * since the buttons are centred within the overlay.*/- setTitleBarOverlay(options: { color: string; symbolColor: string }): void;+ setTitleBarOverlay(options: { color: string; symbolColor: string; height?: number }): void;/*** Repositions the macOS traffic-light window buttons.