Skip to content

Build your personal knowledge base with Trilium Notes

fix(desktop): scale native window button offsets with the zoom factor

Elian Doran68f68cb10803
diff --git a/apps/client/src/components/zoom.ts b/apps/client/src/components/zoom.ts
index 633d703bb9..7a76d22ba8 100644
--- a/apps/client/src/components/zoom.ts
+++ b/apps/client/src/components/zoom.ts
@@ -28,6 +28,10 @@ class ZoomComponent extends Component {
setZoomFactor(zoomFactor: string | number) {
const parsedZoomFactor = typeof zoomFactor !== "number" ? parseFloat(zoomFactor) : zoomFactor;
window.electronApi?.window.setZoomFactor(parsedZoomFactor);
+
+ // The native window buttons are laid out in device-independent pixels, so they stay put
+ // while the rest of the chrome scales; re-push the theme's geometry at the new zoom.
+ void import("../services/native_window.js").then(({ syncNativeWindowWithTheme }) => syncNativeWindowWithTheme());
}
async setZoomFactorAndSave(zoomFactor: number) {
diff --git a/apps/client/src/services/native_window.spec.ts b/apps/client/src/services/native_window.spec.ts
index 4a867774a9..c5a41a8567 100644
--- a/apps/client/src/services/native_window.spec.ts
+++ b/apps/client/src/services/native_window.spec.ts
@@ -13,6 +13,7 @@ vi.mock("./theme.js", () => ({ getThemeStyle: themeCtrl.getThemeStyle }));
/** The subset of the Electron window API this module drives, all spied. */
const win = {
+ getZoomFactor: vi.fn(() => 1),
setNativeThemeSource: vi.fn(),
setBackgroundMaterial: vi.fn(),
setVibrancy: vi.fn(),
@@ -40,6 +41,7 @@ beforeEach(() => {
vi.clearAllMocks();
optionsCtrl.get.mockReturnValue("false");
themeCtrl.getThemeStyle.mockReturnValue("auto");
+ win.getZoomFactor.mockReturnValue(1);
setPlatform("linux");
window.electronApi = { window: win } as unknown as ElectronApi;
stubThemeVariables({});
@@ -120,6 +122,32 @@ describe("syncNativeWindowWithTheme", () => {
expect(win.setTitleBarOverlay).not.toHaveBeenCalled();
});
+ it("scales the native button geometry by the zoom factor, since it is given in device-independent pixels", () => {
+ // The traffic lights are drawn by the system and do not follow the page zoom, so the
+ // theme's offsets — expressed in the same CSS pixels as the tab bar — have to be converted.
+ setPlatform("darwin");
+ win.getZoomFactor.mockReturnValue(0.8);
+ stubThemeVariables({
+ "--native-titlebar-darwin-x-offset": "12",
+ "--native-titlebar-darwin-y-offset": "14"
+ });
+ syncNativeWindowWithTheme();
+ expect(win.setWindowButtonPosition).toHaveBeenCalledWith({ x: 10, y: 11 });
+
+ // The Windows/Linux caption buttons are centred within the overlay, so its height is the
+ // same kind of measurement and scales the same way.
+ vi.clearAllMocks();
+ setPlatform("linux");
+ win.getZoomFactor.mockReturnValue(1.5);
+ stubThemeVariables({
+ "--native-titlebar-background": "#111",
+ "--native-titlebar-foreground": "#eee",
+ "--native-titlebar-height": "36"
+ });
+ syncNativeWindowWithTheme();
+ expect(win.setTitleBarOverlay).toHaveBeenCalledWith({ color: "#111", symbolColor: "#eee", height: 54 });
+ });
+
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.ts
index 82a2615b54..8e71c135df 100644
--- a/apps/client/src/services/native_window.ts
+++ b/apps/client/src/services/native_window.ts
@@ -9,8 +9,9 @@ import { getThemeStyle } from "./theme.js";
* window background material and the native title bar colors and button position.
*
* These all come from the active theme's CSS variables, which Electron cannot observe itself, so
- * this runs at startup, after every live theme change (see `applyTheme`) and when the OS color
- * scheme changes. No-op outside Electron.
+ * this runs at startup, after every live theme change (see `applyTheme`), when the OS color scheme
+ * changes and when the zoom factor changes (see `zoom.ts`, since the button geometry is expressed
+ * in the theme's own CSS pixels). No-op outside Electron.
*/
export function syncNativeWindowWithTheme() {
const win = window.electronApi?.window;
@@ -65,6 +66,13 @@ function applyTransparencyEffects(win: ElectronWindowApi, style: CSSStyleDeclara
}
function applyTitleBarButtons(win: ElectronWindowApi, style: CSSStyleDeclaration) {
+ // The themes express these offsets in the same CSS pixels as the tab bar they have to line up
+ // with, but Electron takes them in device-independent pixels, which the page zoom does not
+ // touch. Converting through the zoom factor keeps the native buttons where the (scaled) tab bar
+ // expects them instead of leaving them stranded at their 100% position.
+ const zoomFactor = win.getZoomFactor();
+ const toDeviceIndependentPixels = (value: number) => Math.round(value * zoomFactor);
+
// Window Controls Overlay is supported on Windows and Linux. The height is opt-in: themes only
// set --native-titlebar-height where the buttons need repositioning (currently Linux), and
// leaving it unset keeps Chromium's system caption height.
@@ -73,13 +81,20 @@ function applyTitleBarButtons(win: ElectronWindowApi, style: CSSStyleDeclaration
const symbolColor = style.getPropertyValue("--native-titlebar-foreground");
const height = parseInt(style.getPropertyValue("--native-titlebar-height"), 10);
if (color && symbolColor) {
- win.setTitleBarOverlay({ color, symbolColor, ...(Number.isFinite(height) && { height }) });
+ win.setTitleBarOverlay({
+ color,
+ symbolColor,
+ ...(Number.isFinite(height) && { height: toDeviceIndependentPixels(height) })
+ });
}
}
if (window.glob.platform === "darwin") {
const xOffset = parseInt(style.getPropertyValue("--native-titlebar-darwin-x-offset"), 10);
const yOffset = parseInt(style.getPropertyValue("--native-titlebar-darwin-y-offset"), 10);
- win.setWindowButtonPosition({ x: xOffset, y: yOffset });
+ win.setWindowButtonPosition({
+ x: toDeviceIndependentPixels(xOffset),
+ y: toDeviceIndependentPixels(yOffset)
+ });
}
}
diff --git a/packages/commons/src/lib/electron_api_interface.ts b/packages/commons/src/lib/electron_api_interface.ts
index 2fddb4a22f..8ae8864f7d 100644
--- a/packages/commons/src/lib/electron_api_interface.ts
+++ b/packages/commons/src/lib/electron_api_interface.ts
@@ -78,13 +78,16 @@ export interface ElectronWindowApi {
/**
* 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.
+ * since the buttons are centred within the overlay. It is given in device-independent
+ * pixels, which the page zoom does not scale — see {@link setWindowButtonPosition}.
*/
setTitleBarOverlay(options: { color: string; symbolColor: string; height?: number }): void;
/**
- * Repositions the macOS traffic-light window buttons.
- * Coordinates are in CSS pixels relative to the window's top-left corner.
+ * Repositions the macOS traffic-light window buttons. Coordinates are relative to the
+ * window's top-left corner and given in device-independent pixels: the buttons are drawn by
+ * the system rather than by the page, so unlike the surrounding chrome they are unaffected by
+ * the page zoom factor and callers have to scale the offsets themselves.
*/
setWindowButtonPosition(position: { x: number; y: number }): void;