Skip to content

Commit 866b927

Browse files
fix(web): the prompt box follows the interface font size
Without "Separate fonts", Typography shows only the interface and monospace rows, but the composer kept its own hidden 14px prompt size and font. The prompt now follows the interface font and size until separate fonts are on, the same way the terminal follows the code font. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 5211acf commit 866b927

4 files changed

Lines changed: 100 additions & 24 deletions

File tree

‎apps/web/src/appInit.tsx‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment";
22
import type { ClientSettings } from "@t3tools/contracts/settings";
33
import { createBrowserHistory, createHashHistory } from "@tanstack/react-router";
4+
import * as Schema from "effect/Schema";
45

56
import { applyAppearanceContrast } from "./appearanceContrast";
6-
import { applyAppearanceFontVariables } from "./appearanceFonts";
7+
import { applyAppearanceFontVariables, TYPOGRAPHY_ADVANCED_STORAGE_KEY } from "./appearanceFonts";
78
import { startElectronBrowserHost } from "./browser/ElectronBrowserHost";
89
import { startBrowserProfileReporting } from "./browser/BrowserProfileReporter";
910
import { startPreviewTitleRecording } from "./browserHistoryStore";
@@ -30,6 +31,7 @@ import {
3031
} from "./composerDraftStore";
3132
import { startDefaultThemeAdoption } from "./hooks/useDefaultTheme";
3233
import { startEnvironmentThemeRepaint } from "./hooks/useEnvironmentTheme";
34+
import { readLocalStorageValue, subscribeLocalStorageKey } from "./hooks/useLocalStorage";
3335
import {
3436
getClientSettings,
3537
startModelPreferencesMigration,
@@ -58,10 +60,18 @@ import { resolveThreadRouteTarget } from "./threadRoutes";
5860
function startClientAppearance(): void {
5961
const root = document.documentElement;
6062
let applied: ClientSettings | null = null;
63+
let appliedSeparateFonts: boolean | null = null;
6164
const apply = () => {
6265
const settings = getClientSettings();
6366
const previous = applied;
6467
applied = settings;
68+
const separateFonts = readLocalStorageValue(
69+
TYPOGRAPHY_ADVANCED_STORAGE_KEY,
70+
false,
71+
Schema.Boolean,
72+
);
73+
const previousSeparateFonts = appliedSeparateFonts;
74+
appliedSeparateFonts = separateFonts;
6575
if (previous?.diffColorScheme !== settings.diffColorScheme) {
6676
root.dataset.diffColorScheme = settings.diffColorScheme;
6777
}
@@ -86,7 +96,8 @@ function startClientAppearance(): void {
8696
previous.fontSizeInterface !== settings.fontSizeInterface ||
8797
previous.fontSizePrompt !== settings.fontSizePrompt ||
8898
previous.fontSizeCode !== settings.fontSizeCode ||
89-
previous.fontSmoothing !== settings.fontSmoothing
99+
previous.fontSmoothing !== settings.fontSmoothing ||
100+
previousSeparateFonts !== separateFonts
90101
) {
91102
applyAppearanceFontVariables(root, {
92103
sans: settings.fontFamilySans,
@@ -96,10 +107,12 @@ function startClientAppearance(): void {
96107
sizePrompt: settings.fontSizePrompt,
97108
sizeCode: settings.fontSizeCode,
98109
smoothing: settings.fontSmoothing,
110+
separate: separateFonts,
99111
});
100112
}
101113
};
102114
subscribeClientSettings(apply);
115+
subscribeLocalStorageKey(TYPOGRAPHY_ADVANCED_STORAGE_KEY, apply);
103116
apply();
104117
}
105118

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import "~/index.css";
4+
import { applyAppearanceFontVariables } from "./appearanceFonts";
5+
6+
function composerFontSize(): string {
7+
const probe = document.createElement("div");
8+
// The composer editor's own size class.
9+
probe.className = "text-(length:--font-size-prompt,var(--text-sm))";
10+
document.body.append(probe);
11+
try {
12+
return getComputedStyle(probe).fontSize;
13+
} finally {
14+
probe.remove();
15+
}
16+
}
17+
18+
describe("applyAppearanceFontVariables", () => {
19+
it("sizes the composer from the interface size until fonts are set separately", () => {
20+
const root = document.documentElement;
21+
const style = root.getAttribute("style");
22+
const preferences = {
23+
sans: "",
24+
code: "",
25+
composer: "",
26+
sizeInterface: 20,
27+
sizePrompt: 14,
28+
sizeCode: 13,
29+
smoothing: true,
30+
};
31+
try {
32+
applyAppearanceFontVariables(root, { ...preferences, separate: false });
33+
expect(composerFontSize()).toBe("17.5px");
34+
35+
applyAppearanceFontVariables(root, { ...preferences, separate: true });
36+
expect(composerFontSize()).toBe("14px");
37+
} finally {
38+
if (style === null) root.removeAttribute("style");
39+
else root.setAttribute("style", style);
40+
}
41+
});
42+
});

‎apps/web/src/appearanceFonts.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,19 @@ export interface AppearanceFontPreferences {
6666
readonly sizePrompt: number;
6767
readonly sizeCode: number;
6868
readonly smoothing: boolean;
69+
/** Without separate fonts the prompt follows the interface, as the terminal follows code. */
70+
readonly separate: boolean;
6971
}
7072

71-
/** Always writes sizes: interface size sets the root; prompt and code use pixels to avoid double scaling. */
73+
/** Interface size sets the root; prompt and code use pixels to avoid double scaling. */
7274
export function applyAppearanceFontVariables(
7375
root: HTMLElement,
7476
preferences: AppearanceFontPreferences,
7577
): void {
7678
const families: ReadonlyArray<readonly [variable: string, custom: string, fallback: string]> = [
7779
["--font-sans", preferences.sans, DEFAULT_SANS_FONT_STACK],
7880
["--font-mono", preferences.code, DEFAULT_CODE_FONT_STACK],
79-
["--font-composer", preferences.composer, "var(--font-sans)"],
81+
["--font-composer", preferences.separate ? preferences.composer : "", "var(--font-sans)"],
8082
];
8183
for (const [variable, custom, fallback] of families) {
8284
const list = cssFontFamilies(custom);
@@ -88,7 +90,15 @@ export function applyAppearanceFontVariables(
8890
}
8991

9092
root.style.fontSize = `${clampInterfaceFontSize(preferences.sizeInterface)}px`;
91-
root.style.setProperty("--font-size-prompt", `${clampPromptFontSize(preferences.sizePrompt)}px`);
93+
if (preferences.separate) {
94+
root.style.setProperty(
95+
"--font-size-prompt",
96+
`${clampPromptFontSize(preferences.sizePrompt)}px`,
97+
);
98+
} else {
99+
// The composer then falls back to text-sm, which scales with the interface size.
100+
root.style.removeProperty("--font-size-prompt");
101+
}
92102
const code = clampCodeFontSize(preferences.sizeCode);
93103
root.style.setProperty("--font-size-code", `${code}px`);
94104
root.style.setProperty("--diffs-font-size", `${code}px`);

‎apps/web/src/hooks/useLocalStorage.ts‎

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,35 @@ function decodeOrInitial<T, E>(
123123
}
124124
}
125125

126+
export function readLocalStorageValue<T, E>(
127+
key: string,
128+
initialValue: T,
129+
schema: Schema.Codec<T, E>,
130+
): T {
131+
return decodeOrInitial(key, schema, readSerializedValue(key), initialValue);
132+
}
133+
134+
/** Fires on writes from this tab and from other tabs. */
135+
export function subscribeLocalStorageKey(key: string, onChange: () => void): () => void {
136+
const handleStorageChange = (event: StorageEvent) => {
137+
if (event.key === key) {
138+
onChange();
139+
}
140+
};
141+
const handleLocalChange = (event: CustomEvent<LocalStorageChangeDetail>) => {
142+
if (event.detail.key === key) {
143+
onChange();
144+
}
145+
};
146+
147+
window.addEventListener("storage", handleStorageChange);
148+
window.addEventListener(LOCAL_STORAGE_CHANGE_EVENT, handleLocalChange as EventListener);
149+
return () => {
150+
window.removeEventListener("storage", handleStorageChange);
151+
window.removeEventListener(LOCAL_STORAGE_CHANGE_EVENT, handleLocalChange as EventListener);
152+
};
153+
}
154+
126155
export function writeLocalStorageValue<T, E>(
127156
key: string,
128157
value: T | ((val: T) => T),
@@ -169,25 +198,7 @@ export function useLocalStorage<T, E>(
169198
): [T, (value: T | ((val: T) => T)) => void] {
170199
const getSnapshot = () => readSerializedValue(key);
171200

172-
const subscribe = (onStoreChange: () => void) => {
173-
const handleStorageChange = (event: StorageEvent) => {
174-
if (event.key === key) {
175-
onStoreChange();
176-
}
177-
};
178-
const handleLocalChange = (event: CustomEvent<LocalStorageChangeDetail>) => {
179-
if (event.detail.key === key) {
180-
onStoreChange();
181-
}
182-
};
183-
184-
window.addEventListener("storage", handleStorageChange);
185-
window.addEventListener(LOCAL_STORAGE_CHANGE_EVENT, handleLocalChange as EventListener);
186-
return () => {
187-
window.removeEventListener("storage", handleStorageChange);
188-
window.removeEventListener(LOCAL_STORAGE_CHANGE_EVENT, handleLocalChange as EventListener);
189-
};
190-
};
201+
const subscribe = (onStoreChange: () => void) => subscribeLocalStorageKey(key, onStoreChange);
191202

192203
const serializedValue = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
193204
const storedValue = decodeOrInitial(key, schema, serializedValue, initialValue);

0 commit comments

Comments
 (0)