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): centre the macOS traffic lights when zoomed
Elian Dorana5d09701475d
diff --git a/apps/client/src/services/native_window.spec.ts b/apps/client/src/services/native_window.spec.tsindex c5a41a8567..61a2be198e 100644--- a/apps/client/src/services/native_window.spec.ts+++ b/apps/client/src/services/native_window.spec.ts@@ -132,7 +132,7 @@ describe("syncNativeWindowWithTheme", () => {"--native-titlebar-darwin-y-offset": "14"});syncNativeWindowWithTheme();- expect(win.setWindowButtonPosition).toHaveBeenCalledWith({ x: 10, y: 11 });+ expect(win.setWindowButtonPosition).toHaveBeenCalledWith({ x: 10, y: 10 });// The Windows/Linux caption buttons are centred within the overlay, so its height is the// same kind of measurement and scales the same way.@@ -148,6 +148,30 @@ describe("syncNativeWindowWithTheme", () => {expect(win.setTitleBarOverlay).toHaveBeenCalledWith({ color: "#111", symbolColor: "#eee", height: 54 });});+ it("keeps the traffic lights on the centre line the theme asked for, at any zoom", () => {+ setPlatform("darwin");++ // The buttons are a fixed 14 device-independent pixels tall, so the band Electron reserves+ // for them — `14 + 2 * y`, with the buttons centred in it — has to come out at a constant+ // height in CSS pixels for them to stay centred on the (scaling) tab bar. A y that merely+ // tracked the zoom would give 14 + 2 * 26 = 66 at 200%, dragging the buttons 7 px high.+ const bandInCssPixels = (zoomFactor: number) => {+ vi.clearAllMocks();+ win.getZoomFactor.mockReturnValue(zoomFactor);+ stubThemeVariables({+ "--native-titlebar-darwin-x-offset": "12",+ "--native-titlebar-darwin-y-offset": "13"+ });+ syncNativeWindowWithTheme();+ const [{ y }] = win.setWindowButtonPosition.mock.calls[0];+ return (14 + 2 * y) / zoomFactor;+ };++ for (const zoomFactor of [0.5, 0.8, 1, 1.5, 2]) {+ expect(bandInCssPixels(zoomFactor)).toBe(40);+ }+ });+it("leaves the title bar alone when the native one is visible, since setting the overlay then throws", () => {optionsCtrl.get.mockReturnValue("true");stubThemeVariables({diff --git a/apps/client/src/services/native_window.ts b/apps/client/src/services/native_window.tsindex 8e71c135df..d40e80a4f5 100644--- a/apps/client/src/services/native_window.ts+++ b/apps/client/src/services/native_window.ts@@ -3,6 +3,17 @@ import type { ElectronWindowApi } from "@triliumnext/commons";import options from "./options.js";import { getThemeStyle } from "./theme.js";+/**+ * Height of the macOS traffic-light buttons, in device-independent pixels. AppKit draws them at a+ * fixed physical size regardless of the page zoom, so their height has to take part in the+ * conversion in {@link applyTitleBarButtons}.+ *+ * Electron reports the band it reserves for them — `TRAFFIC_LIGHT_HEIGHT + 2 * yOffset` — as+ * `env(titlebar-area-height)`, which is how this was measured rather than guessed. Should a future+ * macOS resize the buttons, the symptom is a constant vertical drift that grows with the zoom.+ */+const TRAFFIC_LIGHT_HEIGHT = 14;+/*** Pushes the theme-derived part of the native window configuration to Electron: the preferred* light/dark mode (which drives the tint of background effects such as Mica on Windows), the@@ -94,7 +105,13 @@ function applyTitleBarButtons(win: ElectronWindowApi, style: CSSStyleDeclarationconst yOffset = parseInt(style.getPropertyValue("--native-titlebar-darwin-y-offset"), 10);win.setWindowButtonPosition({x: toDeviceIndependentPixels(xOffset),- y: toDeviceIndependentPixels(yOffset)+ // The vertical offset is the gap *above* the traffic lights, not their centre line:+ // Electron reserves a band of `TRAFFIC_LIGHT_HEIGHT + 2 * offset` at the top of the+ // window and centres the buttons in it. The buttons keep their size whatever the zoom,+ // so scaling the gap alone would leave them off-centre by half their height for every+ // 100% of zoom — visibly high when zoomed in. Scale the centre line the theme asked for+ // and derive the gap that puts the (unscaled) buttons on it.+ y: Math.max(0, Math.round((yOffset + TRAFFIC_LIGHT_HEIGHT / 2) * zoomFactor - TRAFFIC_LIGHT_HEIGHT / 2))});}}