Skip to content

Commit 97b1fee

Browse files
morgmartclaude
andauthored
fix(desktop): restore true zoom by scaling the root rem (#6514)
## Summary Follow-up to #5644. Cmd +/- had become a text-only zoom: type scaled while rem-based padding, gaps, widths, avatars, and controls stayed frozen, which produced cramped layouts (see [#buzz-frontend thread](buzz://message?channel=a410ffde-c61f-416a-96e0-c296b5f5ecc9&id=1a758115cf07b00c097f6e988553908c045165325a57637519cfa7ed9c9accec)). Root cause: #5644 introduced a virtual typography rem so the **Font size** preference could change text without moving layout — a good decoupling — but it also routed **Cmd +/- zoom** through that same px-valued token and pinned the real root at 16px. One decision ("freeze layout") was applied to two dials that shouldn't share it. This PR gives each dial one owner and lets CSS compose them: | Control | Changes | How | |---|---|---| | **Cmd +/- zoom** | Everything — true zoom | Scales the real `<html>` font-size again (`useWebviewZoomShortcuts`) | | **Font size preference** | Text only | Sets `data-font-size`; `typography.css` maps it to a unitless `--buzz-type-scale`, mirroring how density already works | `--buzz-type-rem` becomes `calc(1rem * var(--buzz-type-scale))` — rem-relative, so it rides on zoom automatically. Resulting text px = `16 × zoom × scale × token-ratio`. The 13 / 14 / 15px conversation contract is unchanged at default zoom. Density and the type ramp from #5644 are untouched. The preference module no longer does px math or knows about zoom; the zoom hook no longer imports the preference module. Net deletion in production code. ## Validation - `pnpm test` — 5,308 desktop unit tests - `pnpm check:px-text`, `tsc --noEmit`, biome - Playwright: `top-chrome-zoom-clearance.spec.ts` (native-chrome clearance stays fixed under root zoom), `inbox-refactor-screenshots.spec.ts` (zoomed row padding now asserts `4.4px` instead of the frozen `4px`), and both `profile.spec.ts` zoom tests (composed zoom × preference, cross-window storage reset) - Before/after screenshots at 140% zoom in the comment below --------- Signed-off-by: morgmart <[email protected]> Co-authored-by: Claude <[email protected]>
1 parent eb48438 commit 97b1fee

9 files changed

Lines changed: 218 additions & 184 deletions

‎desktop/src/app/useWebviewZoomShortcuts.ts‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import * as React from "react";
22
import { getCurrentWebview } from "@tauri-apps/api/webview";
33

4-
import { applyTextZoomFactor } from "@/shared/lib/fontSizePreference";
54
import { hasPrimaryShortcutModifier } from "@/shared/lib/platform";
65

6+
/**
7+
* Cmd +/- scales the real root font-size, so every rem in the app — text,
8+
* spacing, widths, radii — zooms together. The Font size preference is a
9+
* separate, text-only dial layered on top (see `styles/globals/typography.css`).
10+
*/
11+
const BASE_FONT_SIZE_PX = 16;
712
const DEFAULT_ZOOM_FACTOR = 1;
813
const MIN_ZOOM_FACTOR = 0.75;
914
const MAX_ZOOM_FACTOR = 1.5;
@@ -75,8 +80,15 @@ function readStoredZoomFactor() {
7580
return Math.min(Math.max(parsed, MIN_ZOOM_FACTOR), MAX_ZOOM_FACTOR);
7681
}
7782

83+
function applyRootZoom(zoomFactor: number) {
84+
document.documentElement.style.fontSize =
85+
zoomFactor === DEFAULT_ZOOM_FACTOR
86+
? ""
87+
: `${BASE_FONT_SIZE_PX * zoomFactor}px`;
88+
}
89+
7890
function applyTextScale(zoomFactor: number) {
79-
applyTextZoomFactor(zoomFactor);
91+
applyRootZoom(zoomFactor);
8092
if (zoomFactor === DEFAULT_ZOOM_FACTOR) {
8193
window.localStorage.removeItem(TEXT_SCALE_STORAGE_KEY);
8294
return;
@@ -95,7 +107,8 @@ export function useWebviewZoomShortcuts() {
95107
zoomFactorRef.current = storedZoomFactor;
96108
applyTextScale(storedZoomFactor);
97109

98-
// Keep the webview coordinate system stable; only text should scale.
110+
// Pin the native webview zoom so the rem root is the only zoom dial and
111+
// window/coordinate math stays stable.
99112
void webview.setZoom(DEFAULT_ZOOM_FACTOR).catch((error) => {
100113
console.error("Failed to reset webview zoom", error);
101114
});
@@ -126,7 +139,7 @@ export function useWebviewZoomShortcuts() {
126139

127140
const storedZoomFactor = readStoredZoomFactor();
128141
zoomFactorRef.current = storedZoomFactor;
129-
applyTextZoomFactor(storedZoomFactor);
142+
applyRootZoom(storedZoomFactor);
130143
}
131144

132145
window.addEventListener("keydown", handleKeyDown);

‎desktop/src/shared/lib/fontSizePreference.test.mjs‎

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import assert from "node:assert/strict";
22
import test from "node:test";
33

4+
import { readFileSync } from "node:fs";
5+
46
import config from "../../../tailwind.config.js";
57

8+
const typographyCss = readFileSync(
9+
new URL("../styles/globals/typography.css", import.meta.url),
10+
"utf8",
11+
);
12+
613
const values = new Map();
714
const attributes = new Map();
8-
const styleValues = new Map();
915
const windowListeners = new Map();
10-
const style = {
11-
setProperty: (name, value) => styleValues.set(name, value),
12-
};
1316

1417
globalThis.window = {
1518
addEventListener: (type, listener) => windowListeners.set(type, listener),
@@ -21,7 +24,6 @@ globalThis.localStorage = {
2124
globalThis.document = {
2225
documentElement: {
2326
setAttribute: (name, value) => attributes.set(name, value),
24-
style,
2527
},
2628
};
2729

@@ -39,6 +41,28 @@ test("scales fixed line-height utilities with the typography rem", () => {
3941
});
4042
});
4143

44+
test("derives the typography rem from the real root so zoom scales layout too", () => {
45+
// Cmd +/- scales the root font-size; the type rem must stay rem-relative
46+
// (never an absolute px) or text would zoom while containers froze.
47+
assert.match(
48+
typographyCss,
49+
/--buzz-type-rem:\s*calc\(1rem \* var\(--buzz-type-scale\)\);/,
50+
);
51+
assert.doesNotMatch(typographyCss, /--buzz-type-rem:\s*[\d.]+px/);
52+
});
53+
54+
test("maps the font size attribute to the 13 / 14 / 15px type contract", () => {
55+
assert.match(typographyCss, /:root\s*\{[^}]*--buzz-type-scale:\s*1;/s);
56+
assert.match(
57+
typographyCss,
58+
/:root\[data-font-size="smaller"\]\s*\{\s*--buzz-type-scale:\s*calc\(13 \/ 14\);/,
59+
);
60+
assert.match(
61+
typographyCss,
62+
/:root\[data-font-size="larger"\]\s*\{\s*--buzz-type-scale:\s*calc\(15 \/ 14\);/,
63+
);
64+
});
65+
4266
test("defaults invalid and missing font sizes to default", () => {
4367
assert.equal(preference.parseFontSize(null), "default");
4468
assert.equal(preference.parseFontSize("medium"), "default");
@@ -48,43 +72,35 @@ test("defaults invalid and missing font sizes to default", () => {
4872
});
4973

5074
test("persists and applies the selected font size across the app", () => {
51-
preference.applyTextZoomFactor(1);
5275
preference.setFontSize("smaller");
5376
assert.equal(preference.getFontSize(), "smaller");
5477
assert.equal(values.get(preference.FONT_SIZE_STORAGE_KEY), "smaller");
5578
assert.equal(attributes.get("data-font-size"), "smaller");
56-
assert.equal(styleValues.get("--buzz-type-rem"), "14.857143px");
5779
});
5880

5981
test("previews a font size without changing the saved preference", () => {
60-
preference.applyTextZoomFactor(1.1);
6182
preference.setFontSize("smaller");
6283
preference.previewFontSize("larger");
6384
assert.equal(preference.getFontSize(), "smaller");
6485
assert.equal(values.get(preference.FONT_SIZE_STORAGE_KEY), "smaller");
6586
assert.equal(attributes.get("data-font-size"), "larger");
66-
assert.equal(styleValues.get("--buzz-type-rem"), "18.857143px");
6787

6888
preference.previewFontSize(null);
6989
assert.equal(attributes.get("data-font-size"), "smaller");
70-
assert.equal(styleValues.get("--buzz-type-rem"), "16.342857px");
7190
});
7291

7392
test("initializes from the stored font size", () => {
74-
preference.applyTextZoomFactor(1);
7593
values.set(preference.FONT_SIZE_STORAGE_KEY, "larger");
7694
preference.initializeFontSizePreference();
7795
assert.equal(preference.getFontSize(), "larger");
7896
assert.equal(attributes.get("data-font-size"), "larger");
79-
assert.equal(styleValues.get("--buzz-type-rem"), "17.142857px");
8097
});
8198

8299
test("applies font size changes from another window", () => {
83100
values.set(preference.FONT_SIZE_STORAGE_KEY, "smaller");
84101
windowListeners.get("storage")({ key: preference.FONT_SIZE_STORAGE_KEY });
85102
assert.equal(preference.getFontSize(), "smaller");
86103
assert.equal(attributes.get("data-font-size"), "smaller");
87-
assert.equal(styleValues.get("--buzz-type-rem"), "14.857143px");
88104
});
89105

90106
test("returns to the default when another window clears storage", () => {
@@ -93,5 +109,4 @@ test("returns to the default when another window clears storage", () => {
93109
windowListeners.get("storage")({ key: null });
94110
assert.equal(preference.getFontSize(), "default");
95111
assert.equal(attributes.get("data-font-size"), "default");
96-
assert.equal(styleValues.get("--buzz-type-rem"), "16px");
97112
});

‎desktop/src/shared/lib/fontSizePreference.ts‎

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,15 @@ export const FONT_SIZE_STORAGE_KEY = "buzz.appearance.fontSize";
77
export const DEFAULT_FONT_SIZE: FontSize = "default";
88

99
/**
10-
* Virtual rem sizes used by typography tokens. Keeping the real root at 16px
11-
* prevents a text preference from also resizing rem-based layout geometry.
10+
* Root attribute that selects the type scale. The 13 / 14 / 15px contract and
11+
* the virtual typography rem it drives live in `styles/globals/typography.css`;
12+
* this module only records the user's choice. Cmd +/- zoom is a separate dial
13+
* (`useWebviewZoomShortcuts`) that scales the real root font-size.
1214
*/
13-
const TYPE_REM_SIZE_PX: Record<FontSize, number> = {
14-
smaller: 13 / 0.875,
15-
default: 14 / 0.875,
16-
larger: 15 / 0.875,
17-
};
18-
19-
const TYPE_REM_PROPERTY = "--buzz-type-rem";
15+
const FONT_SIZE_ATTRIBUTE = "data-font-size";
2016

2117
const listeners = new Set<() => void>();
2218
let fontSize: FontSize = DEFAULT_FONT_SIZE;
23-
let textZoomFactor = 1;
2419
let listeningForStorageChanges = false;
2520

2621
export function parseFontSize(value: string | null | undefined): FontSize {
@@ -39,16 +34,8 @@ function readStoredFontSize(): FontSize {
3934
}
4035
}
4136

42-
function typeRemSizePx(size: FontSize): number {
43-
return (
44-
Math.round(TYPE_REM_SIZE_PX[size] * textZoomFactor * 1_000_000) / 1_000_000
45-
);
46-
}
47-
4837
function applyFontSize(size: FontSize): void {
49-
const root = globalThis.document?.documentElement;
50-
root?.setAttribute("data-font-size", size);
51-
root?.style.setProperty(TYPE_REM_PROPERTY, `${typeRemSizePx(size)}px`);
38+
globalThis.document?.documentElement?.setAttribute(FONT_SIZE_ATTRIBUTE, size);
5239
}
5340

5441
function notifyListeners(): void {
@@ -80,13 +67,6 @@ export function initializeFontSizePreference(): void {
8067
listenForStorageChanges();
8168
}
8269

83-
/** Combine Cmd +/- zoom with the selected app-wide type scale. */
84-
export function applyTextZoomFactor(zoomFactor: number): void {
85-
if (!Number.isFinite(zoomFactor) || zoomFactor <= 0) return;
86-
textZoomFactor = zoomFactor;
87-
applyFontSize(fontSize);
88-
}
89-
9070
function subscribe(listener: () => void): () => void {
9171
listeners.add(listener);
9272
return () => listeners.delete(listener);

‎desktop/src/shared/styles/globals/typography.css‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
@layer base {
22
:root {
33
/*
4-
* A virtual typography rem. Font preferences and Cmd +/- change this
5-
* token while the browser root remains 16px, so text scales without also
6-
* resizing rem-based widths, gaps, radii, and controls.
4+
* Two independent dials compose here:
5+
*
6+
* - Cmd +/- zoom scales the real root font-size (see
7+
* useWebviewZoomShortcuts), so every rem in the app — text, gaps,
8+
* widths, radii — grows and shrinks together. True zoom.
9+
* - The Font size preference sets `data-font-size` on the root; it nudges
10+
* only the type scale below, so text changes without moving layout.
11+
*
12+
* Every text token derives from this virtual typography rem, which is
13+
* rem-relative so it rides on top of zoom automatically. At the default
14+
* preference and zoom it equals 16px, making `text-sm` 14px.
715
*/
8-
--buzz-type-rem: 1rem;
16+
--buzz-type-scale: 1;
17+
--buzz-type-rem: calc(1rem * var(--buzz-type-scale));
918
--text-xs: calc(var(--buzz-type-rem) * 0.75);
1019
--text-sm: calc(var(--buzz-type-rem) * 0.875);
1120
--text-base: var(--buzz-type-rem);
@@ -33,6 +42,15 @@
3342
--conversation-timestamp-line-height: var(--buzz-type-rem);
3443
}
3544

45+
/* Conversation text contract: 13 / 14 / 15px before keyboard zoom. */
46+
:root[data-font-size="smaller"] {
47+
--buzz-type-scale: calc(13 / 14);
48+
}
49+
50+
:root[data-font-size="larger"] {
51+
--buzz-type-scale: calc(15 / 14);
52+
}
53+
3654
:root[data-conversation-density="compact"] {
3755
--conversation-body-gap: 0rem;
3856
--conversation-row-padding-block: 0.25rem;

‎desktop/tailwind.config.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ export default {
33
theme: {
44
extend: {
55
// Sub-`text-xs` ramp for meta text (timestamps, count badges, tracking
6-
// labels) and tiny glyphs. These follow the virtual typography rem so
7-
// preferences and Cmd +/- scale text without changing layout geometry.
8-
// Do NOT reintroduce arbitrary `text-[…rem]` / `text-[…px]` literals;
6+
// labels) and tiny glyphs. These follow the virtual typography rem
7+
// (`--buzz-type-rem` in styles/globals/typography.css), which is
8+
// rem-relative: Cmd +/- zooms it with the rest of the layout, and the
9+
// Font size preference nudges it alone. Do NOT reintroduce arbitrary `text-[…rem]` / `text-[…px]` literals;
910
// the px-text guard rejects them. Stock scale picks up from xs.
1011
fontSize: {
1112
"2xs": "calc(var(--buzz-type-rem) * 0.6875)", // 11px at 16px type rem

0 commit comments

Comments
 (0)