The best way to control Claude Code, Codex and more from any computer or phone
refactor(web): the typography settings section lives in its own file (#22667)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 21, 2026 at 01:39 UTC
568cbb42e389f4532693457c1956904e078413e1
2 files changed
+525
-506
apps/web/src/components/settings/SettingsPanels.tsx
+3
-506
@@ -4,8 +4,8 @@ import { NotificationSettings } from "./NotificationSettings";
4
import { PRIVACY_POLICY_URL } from "../../legalLinks";
5
import { CheckIcon, ChevronRightIcon, SettingsIcon } from "lucide-react";
6
import { Link, useNavigate } from "@tanstack/react-router";
7
-import type { CSSProperties, ReactNode } from "react";
8
-import { useEffect, useEffectEvent, useRef, useState } from "react";
7
+import type { CSSProperties } from "react";
8
+import { useEffect, useRef, useState } from "react";
9
import {
10
type BackgroundActivityProfile,
11
type DesktopUpdateChannel,
@@ -20,22 +20,14 @@ import {
20
type DiffLayout,
21
type EnvironmentIdentificationMode,
22
MAX_APPEARANCE_CONTRAST,
23
- MAX_CODE_FONT_SIZE,
23
MAX_GLASS_OPACITY,
25
- MAX_INTERFACE_FONT_SIZE,
24
MAX_PANEL_ANIMATION_DURATION_MS,
27
- MAX_PROMPT_FONT_SIZE,
25
MAX_SIDEBAR_AUTO_SETTLE_AFTER_DAYS,
29
- MAX_TERMINAL_FONT_SIZE,
30
- MIN_CODE_FONT_SIZE,
26
MIN_APPEARANCE_CONTRAST,
27
MIN_GLASS_OPACITY,
33
- MIN_INTERFACE_FONT_SIZE,
28
MIN_PANEL_ANIMATION_DURATION_MS,
35
- MIN_PROMPT_FONT_SIZE,
29
MIN_SIDEBAR_AUTO_SETTLE_AFTER_DAYS,
30
type ResponseStreamingMode,
38
- MIN_TERMINAL_FONT_SIZE,
31
type QuitConfirmationMode,
32
SidebarProjectSortOrder,
33
} from "@t3tools/contracts/settings";
@@ -70,7 +62,6 @@ import {
62
readThemePreference,
63
useTheme,
64
} from "../../hooks/useTheme";
73
-import { useLocalStorage } from "../../hooks/useLocalStorage";
65
import {
66
useScopedSettings,
67
useScopedSettingsMixed,
@@ -107,18 +98,6 @@ import {
98
} from "../ui/dialog";
99
import { DraftInput } from "../ui/draft-input";
100
import { Input } from "../ui/input";
110
-import {
111
- DEFAULT_CODE_FONT_STACK,
112
- DEFAULT_SANS_FONT_STACK,
113
- isFontFamilyAvailable,
114
- isMonospaceFamily,
115
- resolveDefaultFamilyLabel,
116
- resolveTerminalFontPreference,
117
- resolveTerminalFontSizePreference,
118
- TYPOGRAPHY_ADVANCED_STORAGE_KEY,
119
-} from "../../appearanceFonts";
120
-import { CodeFontPreview, PromptFontPreview, TerminalFontPreview } from "./SettingsFontPreviews";
121
-import { discoverInstalledFonts, FontFamilyPicker, useFontEnumeration } from "./FontFamilyPicker";
101
import {
102
NumberField,
103
NumberFieldDecrement,
@@ -159,6 +138,7 @@ import {
138
} from "./settingsLayout";
139
import { searchableSetting } from "./settingsSearch";
140
import { PanelAnimationsPreview } from "./PanelAnimationsPreview";
141
+import { TypographySection } from "./TypographySettings";
142
143
const ENVIRONMENT_IDENTIFICATION_LABELS: Record<EnvironmentIdentificationMode, string> = {
144
artwork: "Artwork",
@@ -1450,489 +1430,6 @@ export function AppearanceSettingsPanel() {
1430
);
1431
}
1432
1453
-function useFontDefaultFamilies() {
1454
- const settings = useScopedSettings();
1455
- const [defaults] = useState(() => ({
1456
- sans: resolveDefaultFamilyLabel(DEFAULT_SANS_FONT_STACK) ?? "System default",
1457
- code: resolveDefaultFamilyLabel(DEFAULT_CODE_FONT_STACK) ?? "System monospace",
1458
- }));
1459
- return {
1460
- sans: defaults.sans,
1461
- code: defaults.code,
1462
- interfaceFamily: settings.fontFamilySans.trim() || defaults.sans,
1463
- };
1464
-}
1465
-
1466
-function InterfaceFontRow({ preview }: { preview?: ReactNode }) {
1467
- const settings = useScopedSettings();
1468
- const updateSettings = useUpdateScopedSettings();
1469
- const defaults = useFontDefaultFamilies();
1470
- return (
1471
- <FontFamilySettingsRow
1472
- {...searchableSetting("interface-font")}
1473
- description="Everything outside code blocks and the terminal."
1474
- defaultFamily={defaults.sans}
1475
- defaultValue={DEFAULT_UNIFIED_SETTINGS.fontFamilySans}
1476
- value={settings.fontFamilySans}
1477
- onValueChange={(fontFamilySans) => updateSettings({ fontFamilySans })}
1478
- onReset={() =>
1479
- updateSettings({
1480
- fontFamilySans: DEFAULT_UNIFIED_SETTINGS.fontFamilySans,
1481
- fontSizeInterface: DEFAULT_UNIFIED_SETTINGS.fontSizeInterface,
1482
- })
1483
- }
1484
- size={{
1485
- label: "Interface font size",
1486
- min: MIN_INTERFACE_FONT_SIZE,
1487
- max: MAX_INTERFACE_FONT_SIZE,
1488
- value: settings.fontSizeInterface,
1489
- defaultValue: DEFAULT_UNIFIED_SETTINGS.fontSizeInterface,
1490
- onChange: (fontSizeInterface) => updateSettings({ fontSizeInterface }),
1491
- }}
1492
- {...(preview !== undefined ? { preview } : {})}
1493
- />
1494
- );
1495
-}
1496
-
1497
-function PromptFontRow() {
1498
- const settings = useScopedSettings();
1499
- const updateSettings = useUpdateScopedSettings();
1500
- const defaults = useFontDefaultFamilies();
1501
- return (
1502
- <FontFamilySettingsRow
1503
- {...searchableSetting("prompt-font")}
1504
- description="Only the box you write prompts in. Mono works well here."
1505
- defaultFamily={defaults.interfaceFamily}
1506
- defaultValue={DEFAULT_UNIFIED_SETTINGS.fontFamilyComposer}
1507
- value={settings.fontFamilyComposer}
1508
- onValueChange={(fontFamilyComposer) => updateSettings({ fontFamilyComposer })}
1509
- onReset={() =>
1510
- updateSettings({
1511
- fontFamilyComposer: DEFAULT_UNIFIED_SETTINGS.fontFamilyComposer,
1512
- fontSizePrompt: DEFAULT_UNIFIED_SETTINGS.fontSizePrompt,
1513
- })
1514
- }
1515
- size={{
1516
- label: "Prompt font size",
1517
- min: MIN_PROMPT_FONT_SIZE,
1518
- max: MAX_PROMPT_FONT_SIZE,
1519
- value: settings.fontSizePrompt,
1520
- defaultValue: DEFAULT_UNIFIED_SETTINGS.fontSizePrompt,
1521
- onChange: (fontSizePrompt) => updateSettings({ fontSizePrompt }),
1522
- }}
1523
- preview={<PromptFontPreview />}
1524
- />
1525
- );
1526
-}
1527
-
1528
-function CodeFontRow({
1529
- title,
1530
- description = "Code blocks, diffs, and file previews.",
1531
- preview,
1532
-}: {
1533
- title?: string;
1534
- description?: string;
1535
- preview?: ReactNode;
1536
-}) {
1537
- const settings = useScopedSettings();
1538
- const updateSettings = useUpdateScopedSettings();
1539
- const defaults = useFontDefaultFamilies();
1540
- return (
1541
- <FontFamilySettingsRow
1542
- {...searchableSetting("code-font")}
1543
- {...(title !== undefined ? { title } : {})}
1544
- description={description}
1545
- defaultFamily={defaults.code}
1546
- defaultValue={DEFAULT_UNIFIED_SETTINGS.fontFamilyCode}
1547
- value={settings.fontFamilyCode}
1548
- onValueChange={(fontFamilyCode) => updateSettings({ fontFamilyCode })}
1549
- onReset={() =>
1550
- updateSettings({
1551
- fontFamilyCode: DEFAULT_UNIFIED_SETTINGS.fontFamilyCode,
1552
- fontSizeCode: DEFAULT_UNIFIED_SETTINGS.fontSizeCode,
1553
- })
1554
- }
1555
- requireMonospace
1556
- size={{
1557
- label: "Code font size",
1558
- min: MIN_CODE_FONT_SIZE,
1559
- max: MAX_CODE_FONT_SIZE,
1560
- value: settings.fontSizeCode,
1561
- defaultValue: DEFAULT_UNIFIED_SETTINGS.fontSizeCode,
1562
- onChange: (fontSizeCode) => updateSettings({ fontSizeCode }),
1563
- }}
1564
- preview={preview ?? <CodeFontPreview />}
1565
- />
1566
- );
1567
-}
1568
-
1569
-function TerminalFontRow() {
1570
- const settings = useScopedSettings();
1571
- const updateSettings = useUpdateScopedSettings();
1572
- const defaults = useFontDefaultFamilies();
1573
- return (
1574
- <FontFamilySettingsRow
1575
- {...searchableSetting("terminal-font")}
1576
- description="Terminal output, independent from code blocks and diffs."
1577
- defaultFamily={defaults.code}
1578
- defaultValue={DEFAULT_UNIFIED_SETTINGS.fontFamilyTerminal}
1579
- value={settings.fontFamilyTerminal}
1580
- onValueChange={(fontFamilyTerminal) => updateSettings({ fontFamilyTerminal })}
1581
- onReset={() =>
1582
- updateSettings({
1583
- fontFamilyTerminal: DEFAULT_UNIFIED_SETTINGS.fontFamilyTerminal,
1584
- fontSizeTerminal: DEFAULT_UNIFIED_SETTINGS.fontSizeTerminal,
1585
- })
1586
- }
1587
- requireMonospace
1588
- size={{
1589
- label: "Terminal font size",
1590
- min: MIN_TERMINAL_FONT_SIZE,
1591
- max: MAX_TERMINAL_FONT_SIZE,
1592
- value: settings.fontSizeTerminal,
1593
- defaultValue: DEFAULT_UNIFIED_SETTINGS.fontSizeTerminal,
1594
- onChange: (fontSizeTerminal) => updateSettings({ fontSizeTerminal }),
1595
- }}
1596
- preview={
1597
- <TerminalFontPreview
1598
- family={resolveTerminalFontPreference({
1599
- advanced: true,
1600
- code: settings.fontFamilyCode,
1601
- terminal: settings.fontFamilyTerminal,
1602
- })}
1603
- size={settings.fontSizeTerminal}
1604
- />
1605
- }
1606
- />
1607
- );
1608
-}
1609
-
1610
-function FontSmoothingRow() {
1611
- const settings = useScopedSettings();
1612
- const updateSettings = useUpdateScopedSettings();
1613
- if (!isMacPlatform(navigator.platform)) return null;
1614
- return (
1615
- <SettingsRow
1616
- {...searchableSetting("font-smoothing")}
1617
- description="Use thinner grayscale text smoothing instead of the macOS default."
1618
- resetAction={
1619
- settings.fontSmoothing !== DEFAULT_UNIFIED_SETTINGS.fontSmoothing ? (
1620
- <SettingResetButton
1621
- label="font smoothing"
1622
- onClick={() =>
1623
- updateSettings({ fontSmoothing: DEFAULT_UNIFIED_SETTINGS.fontSmoothing })
1624
- }
1625
- />
1626
- ) : null
1627
- }
1628
- control={
1629
- <Switch
1630
- checked={settings.fontSmoothing}
1631
- onCheckedChange={(checked) => updateSettings({ fontSmoothing: Boolean(checked) })}
1632
- aria-label="Font smoothing"
1633
- />
1634
- }
1635
- />
1636
- );
1637
-}
1638
-
1639
-function WordWrapRow() {
1640
- const settings = useScopedSettings();
1641
- const updateSettings = useUpdateScopedSettings();
1642
- return (
1643
- <SettingsRow
1644
- {...searchableSetting("word-wrap")}
1645
- description="Wrap long lines in code blocks, tables, diffs, and file previews by default."
1646
- resetAction={
1647
- settings.wordWrap !== DEFAULT_UNIFIED_SETTINGS.wordWrap ? (
1648
- <SettingResetButton
1649
- label="word wrapping"
1650
- onClick={() => updateSettings({ wordWrap: DEFAULT_UNIFIED_SETTINGS.wordWrap })}
1651
- />
1652
- ) : null
1653
- }
1654
- control={
1655
- <Switch
1656
- checked={settings.wordWrap}
1657
- onCheckedChange={(checked) => updateSettings({ wordWrap: Boolean(checked) })}
1658
- aria-label="Wrap code, tables, diffs, and file previews by default"
1659
- />
1660
- }
1661
- />
1662
- );
1663
-}
1664
-
1665
-function FontSettingsGroup() {
1666
- return (
1667
- <>
1668
- <InterfaceFontRow />
1669
- <PromptFontRow />
1670
- <CodeFontRow />
1671
- <TerminalFontRow />
1672
- <FontSmoothingRow />
1673
- </>
1674
- );
1675
-}
1676
-
1677
-function SimpleFontRows() {
1678
- const settings = useScopedSettings();
1679
- return (
1680
- <>
1681
- <InterfaceFontRow preview={<PromptFontPreview />} />
1682
- <CodeFontRow
1683
- title="Monospace font"
1684
- description="Code blocks, diffs, file previews, and the terminal."
1685
- preview={
1686
- <>
1687
- <CodeFontPreview />
1688
- <TerminalFontPreview
1689
- family={resolveTerminalFontPreference({
1690
- advanced: false,
1691
- code: settings.fontFamilyCode,
1692
- terminal: settings.fontFamilyTerminal,
1693
- })}
1694
- size={resolveTerminalFontSizePreference({
1695
- advanced: false,
1696
- code: settings.fontSizeCode,
1697
- terminal: settings.fontSizeTerminal,
1698
- })}
1699
- />
1700
- </>
1701
- }
1702
- />
1703
- </>
1704
- );
1705
-}
1706
-
1707
-// Font smoothing only renders on macOS, so a search jump to it elsewhere
1708
-// must not flip the section - the target would never mount to be scrolled to.
1709
-const ADVANCED_TYPOGRAPHY_TARGET_IDS: ReadonlySet<string> = new Set([
1710
- "prompt-font",
1711
- "terminal-font",
1712
- ...(typeof navigator !== "undefined" && isMacPlatform(navigator.platform)
1713
- ? ["font-smoothing"]
1714
- : []),
1715
-]);
1716
-
1717
-function TypographySection() {
1718
- const [advanced, setAdvanced] = useLocalStorage(
1719
- TYPOGRAPHY_ADVANCED_STORAGE_KEY,
1720
- false,
1721
- Schema.Boolean,
1722
- );
1723
- const searchTargetId = useSettingsSearchTargetId();
1724
- const lastExpandedTargetRef = useRef<string | null>(null);
1725
- useEffect(() => {
1726
- if (searchTargetId === null || !ADVANCED_TYPOGRAPHY_TARGET_IDS.has(searchTargetId)) return;
1727
- if (lastExpandedTargetRef.current === searchTargetId) return;
1728
- lastExpandedTargetRef.current = searchTargetId;
1729
- setAdvanced(true);
1730
- }, [searchTargetId, setAdvanced]);
1731
- return (
1732
- <SettingsSection
1733
- id="typography"
1734
- title="Typography"
1735
- headerAction={
1736
- <label className="flex cursor-pointer items-center gap-2 text-xs font-medium text-muted-foreground">
1737
- Advanced
1738
- <Switch
1739
- checked={advanced}
1740
- onCheckedChange={(checked) => setAdvanced(Boolean(checked))}
1741
- aria-label="Show advanced typography settings"
1742
- />
1743
- </label>
1744
- }
1745
- >
1746
- {advanced ? <FontSettingsGroup /> : <SimpleFontRows />}
1747
- <WordWrapRow />
1748
- </SettingsSection>
1749
- );
1750
-}
1751
-
1752
-function FontFamilySettingsRow({
1753
- id,
1754
- title,
1755
- description,
1756
- defaultFamily,
1757
- defaultValue,
1758
- preview,
1759
- value,
1760
- onValueChange,
1761
- onReset,
1762
- requireMonospace = false,
1763
- size,
1764
-}: {
1765
- id?: string;
1766
- title: string;
1767
- description: string;
1768
- defaultFamily: string;
1769
- defaultValue: string;
1770
- preview?: ReactNode;
1771
- value: string;
1772
- onValueChange: (value: string) => void;
1773
- onReset: () => void;
1774
- requireMonospace?: boolean;
1775
- size: {
1776
- label: string;
1777
- min: number;
1778
- max: number;
1779
- value: number;
1780
- defaultValue: number;
1781
- onChange: (v: number) => void;
1782
- };
1783
-}) {
1784
- const trimmed = value.trim();
1785
- const [draft, setDraft] = useState(value);
1786
- const [draftSettled, setDraftSettled] = useState(true);
1787
- // Each pending commit remembers the value it was typed against, so one that outlives
1788
- // an external change (hydration, reset, picker selection) drops its stale draft.
1789
- const commitTimerRef = useRef<{ readonly id: number; readonly value: string } | null>(null);
1790
- const cancelPendingCommit = () => {
1791
- if (commitTimerRef.current !== null) window.clearTimeout(commitTimerRef.current.id);
1792
- commitTimerRef.current = null;
1793
- };
1794
- // Bumped on every external value change, so a pending commit typed before one never lands,
1795
- // even if the value has since changed back to what it was typed against.
1796
- const [adopted, setAdopted] = useState({ value, generation: 0 });
1797
- if (adopted.value !== value) {
1798
- setAdopted({ value, generation: adopted.generation + 1 });
1799
- setDraft(value);
1800
- setDraftSettled(true);
1801
- }
1802
- const readAdoptedGeneration = useEffectEvent(() => adopted.generation);
1803
- useEffect(
1804
- () => () => {
1805
- if (commitTimerRef.current !== null) window.clearTimeout(commitTimerRef.current.id);
1806
- },
1807
- [],
1808
- );
1809
- const acceptsFamily = (candidate: string) =>
1810
- isFontFamilyAvailable(candidate) && (!requireMonospace || isMonospaceFamily(candidate));
1811
- const commitDraft = (next: string) => {
1812
- setDraftSettled(true);
1813
- if (next.trim().length === 0 || acceptsFamily(next)) {
1814
- onValueChange(next);
1815
- }
1816
- };
1817
- const flushDraft = () => {
1818
- const pending = commitTimerRef.current;
1819
- cancelPendingCommit();
1820
- if (pending !== null && pending.value === value) commitDraft(draft);
1821
- };
1822
- const draftTrimmed = draft.trim();
1823
- const draftPending = draftSettled && draftTrimmed.length > 0 && draftTrimmed !== trimmed;
1824
- const resetToDefault = () => {
1825
- cancelPendingCommit();
1826
- setDraft(defaultValue);
1827
- setDraftSettled(true);
1828
- onReset();
1829
- };
1830
- const resetAction =
1831
- value !== defaultValue || size.value !== size.defaultValue ? (
1832
- <SettingResetButton label={title.toLowerCase()} onClick={resetToDefault} />
1833
- ) : null;
1834
- const fontEnumeration = useFontEnumeration();
1835
- const [inputFocused, setInputFocused] = useState(false);
1836
- const familyControl =
1837
- fontEnumeration.status === "granted" ? (
1838
- <FontFamilyPicker
1839
- ariaLabel={`${title} family`}
1840
- defaultFamily={defaultFamily}
1841
- selectedFamily={trimmed}
1842
- requireMonospace={requireMonospace}
1843
- initialOpen={inputFocused}
1844
- onSelect={onValueChange}
1845
- />
1846
- ) : (
1847
- <Input
1848
- size="sm"
1849
- aria-label={`${title} family`}
1850
- aria-invalid={draftPending || undefined}
1851
- autoCapitalize="off"
1852
- autoComplete="off"
1853
- className="min-w-0 flex-1"
1854
- maxLength={200}
1855
- onFocus={() => {
1856
- setInputFocused(true);
1857
- discoverInstalledFonts();
1858
- }}
1859
- onBlur={() => {
1860
- setInputFocused(false);
1861
- flushDraft();
1862
- }}
1863
- onChange={(event) => {
1864
- const next = event.currentTarget.value;
1865
- setDraft(next);
1866
- setDraftSettled(false);
1867
- cancelPendingCommit();
1868
- const typedAgainst = value;
1869
- const typedInGeneration = adopted.generation;
1870
- commitTimerRef.current = {
1871
- id: window.setTimeout(() => {
1872
- commitTimerRef.current = null;
1873
- if (readAdoptedGeneration() === typedInGeneration) commitDraft(next);
1874
- }, 400),
1875
- value: typedAgainst,
1876
- };
1877
- }}
1878
- onKeyDown={(event) => {
1879
- if (event.key === "Enter") flushDraft();
1880
- if (event.key === "Escape") {
1881
- // Discard uncommitted typing without closing the settings page,
1882
- // which is what an unhandled Escape does.
1883
- event.preventDefault();
1884
- event.stopPropagation();
1885
- cancelPendingCommit();
1886
- setDraft(value);
1887
- setDraftSettled(true);
1888
- }
1889
- }}
1890
- placeholder={defaultFamily}
1891
- spellCheck={false}
1892
- value={draft}
1893
- />
1894
- );
1895
- const control = (
1896
- <div className="flex w-full items-center gap-2 sm:w-auto">
1897
- <div className="min-w-0 flex-1 sm:w-44 sm:flex-none">{familyControl}</div>
1898
- <Select
1899
- value={String(size.value)}
1900
- onValueChange={(next) => {
1901
- if (typeof next !== "string") return;
1902
- const parsed = Number(next);
1903
- if (Number.isInteger(parsed) && parsed >= size.min && parsed <= size.max) {
1904
- size.onChange(parsed);
1905
- }
1906
- }}
1907
- >
1908
- <SelectTrigger size="sm" className="w-22 shrink-0" aria-label={size.label}>
1909
- <SelectValue>{size.value} px</SelectValue>
1910
- </SelectTrigger>
1911
- <SelectPopup align="end" alignItemWithTrigger={false}>
1912
- {Array.from({ length: size.max - size.min + 1 }, (_, index) => size.min + index).map(
1913
- (px) => (
1914
- <SelectItem hideIndicator key={px} value={String(px)}>
1915
- {px} px
1916
- </SelectItem>
1917
- ),
1918
- )}
1919
- </SelectPopup>
1920
- </Select>
1921
- </div>
1922
- );
1923
- return (
1924
- <SettingsRow
1925
- {...(id !== undefined ? { id } : {})}
1926
- title={title}
1927
- description={description}
1928
- resetAction={resetAction}
1929
- control={control}
1930
- >
1931
- {preview}
1932
- </SettingsRow>
1933
- );
1934
-}
1935
-
1433
const AUTO_SETTLE_DEFAULT_DAYS = DEFAULT_UNIFIED_SETTINGS.sidebarAutoSettleAfterDays ?? 3;
1434
1435
function AutoSettleDaysInput({
apps/web/src/components/settings/TypographySettings.tsx
new
+522
@@ -0,0 +1,522 @@
1
+import type { ReactNode } from "react";
2
+import { useEffect, useEffectEvent, useRef, useState } from "react";
3
+import {
4
+ DEFAULT_UNIFIED_SETTINGS,
5
+ MAX_CODE_FONT_SIZE,
6
+ MAX_INTERFACE_FONT_SIZE,
7
+ MAX_PROMPT_FONT_SIZE,
8
+ MAX_TERMINAL_FONT_SIZE,
9
+ MIN_CODE_FONT_SIZE,
10
+ MIN_INTERFACE_FONT_SIZE,
11
+ MIN_PROMPT_FONT_SIZE,
12
+ MIN_TERMINAL_FONT_SIZE,
13
+} from "@t3tools/contracts/settings";
14
+import * as Schema from "effect/Schema";
15
+import { useLocalStorage } from "../../hooks/useLocalStorage";
16
+import { useScopedSettings, useUpdateScopedSettings } from "./useScopedSettings";
17
+import { isMacPlatform } from "../../lib/utils";
18
+import { Input } from "../ui/input";
19
+import {
20
+ DEFAULT_CODE_FONT_STACK,
21
+ DEFAULT_SANS_FONT_STACK,
22
+ isFontFamilyAvailable,
23
+ isMonospaceFamily,
24
+ resolveDefaultFamilyLabel,
25
+ resolveTerminalFontPreference,
26
+ resolveTerminalFontSizePreference,
27
+ TYPOGRAPHY_ADVANCED_STORAGE_KEY,
28
+} from "../../appearanceFonts";
29
+import { CodeFontPreview, PromptFontPreview, TerminalFontPreview } from "./SettingsFontPreviews";
30
+import { discoverInstalledFonts, FontFamilyPicker, useFontEnumeration } from "./FontFamilyPicker";
31
+import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
32
+import { Switch } from "../ui/switch";
33
+import {
34
+ SettingResetButton,
35
+ SettingsRow,
36
+ SettingsSection,
37
+ useSettingsSearchTargetId,
38
+} from "./settingsLayout";
39
+import { searchableSetting } from "./settingsSearch";
40
+
41
+function useFontDefaultFamilies() {
42
+ const settings = useScopedSettings();
43
+ const [defaults] = useState(() => ({
44
+ sans: resolveDefaultFamilyLabel(DEFAULT_SANS_FONT_STACK) ?? "System default",
45
+ code: resolveDefaultFamilyLabel(DEFAULT_CODE_FONT_STACK) ?? "System monospace",
46
+ }));
47
+ return {
48
+ sans: defaults.sans,
49
+ code: defaults.code,
50
+ interfaceFamily: settings.fontFamilySans.trim() || defaults.sans,
51
+ };
52
+}
53
+
54
+function InterfaceFontRow({ preview }: { preview?: ReactNode }) {
55
+ const settings = useScopedSettings();
56
+ const updateSettings = useUpdateScopedSettings();
57
+ const defaults = useFontDefaultFamilies();
58
+ return (
59
+ <FontFamilySettingsRow
60
+ {...searchableSetting("interface-font")}
61
+ description="Everything outside code blocks and the terminal."
62
+ defaultFamily={defaults.sans}
63
+ defaultValue={DEFAULT_UNIFIED_SETTINGS.fontFamilySans}
64
+ value={settings.fontFamilySans}
65
+ onValueChange={(fontFamilySans) => updateSettings({ fontFamilySans })}
66
+ onReset={() =>
67
+ updateSettings({
68
+ fontFamilySans: DEFAULT_UNIFIED_SETTINGS.fontFamilySans,
69
+ fontSizeInterface: DEFAULT_UNIFIED_SETTINGS.fontSizeInterface,
70
+ })
71
+ }
72
+ size={{
73
+ label: "Interface font size",
74
+ min: MIN_INTERFACE_FONT_SIZE,
75
+ max: MAX_INTERFACE_FONT_SIZE,
76
+ value: settings.fontSizeInterface,
77
+ defaultValue: DEFAULT_UNIFIED_SETTINGS.fontSizeInterface,
78
+ onChange: (fontSizeInterface) => updateSettings({ fontSizeInterface }),
79
+ }}
80
+ {...(preview !== undefined ? { preview } : {})}
81
+ />
82
+ );
83
+}
84
+
85
+function PromptFontRow() {
86
+ const settings = useScopedSettings();
87
+ const updateSettings = useUpdateScopedSettings();
88
+ const defaults = useFontDefaultFamilies();
89
+ return (
90
+ <FontFamilySettingsRow
91
+ {...searchableSetting("prompt-font")}
92
+ description="Only the box you write prompts in. Mono works well here."
93
+ defaultFamily={defaults.interfaceFamily}
94
+ defaultValue={DEFAULT_UNIFIED_SETTINGS.fontFamilyComposer}
95
+ value={settings.fontFamilyComposer}
96
+ onValueChange={(fontFamilyComposer) => updateSettings({ fontFamilyComposer })}
97
+ onReset={() =>
98
+ updateSettings({
99
+ fontFamilyComposer: DEFAULT_UNIFIED_SETTINGS.fontFamilyComposer,
100
+ fontSizePrompt: DEFAULT_UNIFIED_SETTINGS.fontSizePrompt,
101
+ })
102
+ }
103
+ size={{
104
+ label: "Prompt font size",
105
+ min: MIN_PROMPT_FONT_SIZE,
106
+ max: MAX_PROMPT_FONT_SIZE,
107
+ value: settings.fontSizePrompt,
108
+ defaultValue: DEFAULT_UNIFIED_SETTINGS.fontSizePrompt,
109
+ onChange: (fontSizePrompt) => updateSettings({ fontSizePrompt }),
110
+ }}
111
+ preview={<PromptFontPreview />}
112
+ />
113
+ );
114
+}
115
+
116
+function CodeFontRow({
117
+ title,
118
+ description = "Code blocks, diffs, and file previews.",
119
+ preview,
120
+}: {
121
+ title?: string;
122
+ description?: string;
123
+ preview?: ReactNode;
124
+}) {
125
+ const settings = useScopedSettings();
126
+ const updateSettings = useUpdateScopedSettings();
127
+ const defaults = useFontDefaultFamilies();
128
+ return (
129
+ <FontFamilySettingsRow
130
+ {...searchableSetting("code-font")}
131
+ {...(title !== undefined ? { title } : {})}
132
+ description={description}
133
+ defaultFamily={defaults.code}
134
+ defaultValue={DEFAULT_UNIFIED_SETTINGS.fontFamilyCode}
135
+ value={settings.fontFamilyCode}
136
+ onValueChange={(fontFamilyCode) => updateSettings({ fontFamilyCode })}
137
+ onReset={() =>
138
+ updateSettings({
139
+ fontFamilyCode: DEFAULT_UNIFIED_SETTINGS.fontFamilyCode,
140
+ fontSizeCode: DEFAULT_UNIFIED_SETTINGS.fontSizeCode,
141
+ })
142
+ }
143
+ requireMonospace
144
+ size={{
145
+ label: "Code font size",
146
+ min: MIN_CODE_FONT_SIZE,
147
+ max: MAX_CODE_FONT_SIZE,
148
+ value: settings.fontSizeCode,
149
+ defaultValue: DEFAULT_UNIFIED_SETTINGS.fontSizeCode,
150
+ onChange: (fontSizeCode) => updateSettings({ fontSizeCode }),
151
+ }}
152
+ preview={preview ?? <CodeFontPreview />}
153
+ />
154
+ );
155
+}
156
+
157
+function TerminalFontRow() {
158
+ const settings = useScopedSettings();
159
+ const updateSettings = useUpdateScopedSettings();
160
+ const defaults = useFontDefaultFamilies();
161
+ return (
162
+ <FontFamilySettingsRow
163
+ {...searchableSetting("terminal-font")}
164
+ description="Terminal output, independent from code blocks and diffs."
165
+ defaultFamily={defaults.code}
166
+ defaultValue={DEFAULT_UNIFIED_SETTINGS.fontFamilyTerminal}
167
+ value={settings.fontFamilyTerminal}
168
+ onValueChange={(fontFamilyTerminal) => updateSettings({ fontFamilyTerminal })}
169
+ onReset={() =>
170
+ updateSettings({
171
+ fontFamilyTerminal: DEFAULT_UNIFIED_SETTINGS.fontFamilyTerminal,
172
+ fontSizeTerminal: DEFAULT_UNIFIED_SETTINGS.fontSizeTerminal,
173
+ })
174
+ }
175
+ requireMonospace
176
+ size={{
177
+ label: "Terminal font size",
178
+ min: MIN_TERMINAL_FONT_SIZE,
179
+ max: MAX_TERMINAL_FONT_SIZE,
180
+ value: settings.fontSizeTerminal,
181
+ defaultValue: DEFAULT_UNIFIED_SETTINGS.fontSizeTerminal,
182
+ onChange: (fontSizeTerminal) => updateSettings({ fontSizeTerminal }),
183
+ }}
184
+ preview={
185
+ <TerminalFontPreview
186
+ family={resolveTerminalFontPreference({
187
+ advanced: true,
188
+ code: settings.fontFamilyCode,
189
+ terminal: settings.fontFamilyTerminal,
190
+ })}
191
+ size={settings.fontSizeTerminal}
192
+ />
193
+ }
194
+ />
195
+ );
196
+}
197
+
198
+function FontSmoothingRow() {
199
+ const settings = useScopedSettings();
200
+ const updateSettings = useUpdateScopedSettings();
201
+ if (!isMacPlatform(navigator.platform)) return null;
202
+ return (
203
+ <SettingsRow
204
+ {...searchableSetting("font-smoothing")}
205
+ description="Use thinner grayscale text smoothing instead of the macOS default."
206
+ resetAction={
207
+ settings.fontSmoothing !== DEFAULT_UNIFIED_SETTINGS.fontSmoothing ? (
208
+ <SettingResetButton
209
+ label="font smoothing"
210
+ onClick={() =>
211
+ updateSettings({ fontSmoothing: DEFAULT_UNIFIED_SETTINGS.fontSmoothing })
212
+ }
213
+ />
214
+ ) : null
215
+ }
216
+ control={
217
+ <Switch
218
+ checked={settings.fontSmoothing}
219
+ onCheckedChange={(checked) => updateSettings({ fontSmoothing: Boolean(checked) })}
220
+ aria-label="Font smoothing"
221
+ />
222
+ }
223
+ />
224
+ );
225
+}
226
+
227
+function WordWrapRow() {
228
+ const settings = useScopedSettings();
229
+ const updateSettings = useUpdateScopedSettings();
230
+ return (
231
+ <SettingsRow
232
+ {...searchableSetting("word-wrap")}
233
+ description="Wrap long lines in code blocks, tables, diffs, and file previews by default."
234
+ resetAction={
235
+ settings.wordWrap !== DEFAULT_UNIFIED_SETTINGS.wordWrap ? (
236
+ <SettingResetButton
237
+ label="word wrapping"
238
+ onClick={() => updateSettings({ wordWrap: DEFAULT_UNIFIED_SETTINGS.wordWrap })}
239
+ />
240
+ ) : null
241
+ }
242
+ control={
243
+ <Switch
244
+ checked={settings.wordWrap}
245
+ onCheckedChange={(checked) => updateSettings({ wordWrap: Boolean(checked) })}
246
+ aria-label="Wrap code, tables, diffs, and file previews by default"
247
+ />
248
+ }
249
+ />
250
+ );
251
+}
252
+
253
+function FontSettingsGroup() {
254
+ return (
255
+ <>
256
+ <InterfaceFontRow />
257
+ <PromptFontRow />
258
+ <CodeFontRow />
259
+ <TerminalFontRow />
260
+ <FontSmoothingRow />
261
+ </>
262
+ );
263
+}
264
+
265
+function SimpleFontRows() {
266
+ const settings = useScopedSettings();
267
+ return (
268
+ <>
269
+ <InterfaceFontRow preview={<PromptFontPreview />} />
270
+ <CodeFontRow
271
+ title="Monospace font"
272
+ description="Code blocks, diffs, file previews, and the terminal."
273
+ preview={
274
+ <>
275
+ <CodeFontPreview />
276
+ <TerminalFontPreview
277
+ family={resolveTerminalFontPreference({
278
+ advanced: false,
279
+ code: settings.fontFamilyCode,
280
+ terminal: settings.fontFamilyTerminal,
281
+ })}
282
+ size={resolveTerminalFontSizePreference({
283
+ advanced: false,
284
+ code: settings.fontSizeCode,
285
+ terminal: settings.fontSizeTerminal,
286
+ })}
287
+ />
288
+ </>
289
+ }
290
+ />
291
+ </>
292
+ );
293
+}
294
+
295
+// Font smoothing only renders on macOS, so a search jump to it elsewhere
296
+// must not flip the section - the target would never mount to be scrolled to.
297
+const ADVANCED_TYPOGRAPHY_TARGET_IDS: ReadonlySet<string> = new Set([
298
+ "prompt-font",
299
+ "terminal-font",
300
+ ...(typeof navigator !== "undefined" && isMacPlatform(navigator.platform)
301
+ ? ["font-smoothing"]
302
+ : []),
303
+]);
304
+
305
+export function TypographySection() {
306
+ const [advanced, setAdvanced] = useLocalStorage(
307
+ TYPOGRAPHY_ADVANCED_STORAGE_KEY,
308
+ false,
309
+ Schema.Boolean,
310
+ );
311
+ const searchTargetId = useSettingsSearchTargetId();
312
+ const lastExpandedTargetRef = useRef<string | null>(null);
313
+ useEffect(() => {
314
+ if (searchTargetId === null || !ADVANCED_TYPOGRAPHY_TARGET_IDS.has(searchTargetId)) return;
315
+ if (lastExpandedTargetRef.current === searchTargetId) return;
316
+ lastExpandedTargetRef.current = searchTargetId;
317
+ setAdvanced(true);
318
+ }, [searchTargetId, setAdvanced]);
319
+ return (
320
+ <SettingsSection
321
+ id="typography"
322
+ title="Typography"
323
+ headerAction={
324
+ <label className="flex cursor-pointer items-center gap-2 text-xs font-medium text-muted-foreground">
325
+ Advanced
326
+ <Switch
327
+ checked={advanced}
328
+ onCheckedChange={(checked) => setAdvanced(Boolean(checked))}
329
+ aria-label="Show advanced typography settings"
330
+ />
331
+ </label>
332
+ }
333
+ >
334
+ {advanced ? <FontSettingsGroup /> : <SimpleFontRows />}
335
+ <WordWrapRow />
336
+ </SettingsSection>
337
+ );
338
+}
339
+
340
+function FontFamilySettingsRow({
341
+ id,
342
+ title,
343
+ description,
344
+ defaultFamily,
345
+ defaultValue,
346
+ preview,
347
+ value,
348
+ onValueChange,
349
+ onReset,
350
+ requireMonospace = false,
351
+ size,
352
+}: {
353
+ id?: string;
354
+ title: string;
355
+ description: string;
356
+ defaultFamily: string;
357
+ defaultValue: string;
358
+ preview?: ReactNode;
359
+ value: string;
360
+ onValueChange: (value: string) => void;
361
+ onReset: () => void;
362
+ requireMonospace?: boolean;
363
+ size: {
364
+ label: string;
365
+ min: number;
366
+ max: number;
367
+ value: number;
368
+ defaultValue: number;
369
+ onChange: (v: number) => void;
370
+ };
371
+}) {
372
+ const trimmed = value.trim();
373
+ const [draft, setDraft] = useState(value);
374
+ const [draftSettled, setDraftSettled] = useState(true);
375
+ // Each pending commit remembers the value it was typed against, so one that outlives
376
+ // an external change (hydration, reset, picker selection) drops its stale draft.
377
+ const commitTimerRef = useRef<{ readonly id: number; readonly value: string } | null>(null);
378
+ const cancelPendingCommit = () => {
379
+ if (commitTimerRef.current !== null) window.clearTimeout(commitTimerRef.current.id);
380
+ commitTimerRef.current = null;
381
+ };
382
+ // Bumped on every external value change, so a pending commit typed before one never lands,
383
+ // even if the value has since changed back to what it was typed against.
384
+ const [adopted, setAdopted] = useState({ value, generation: 0 });
385
+ if (adopted.value !== value) {
386
+ setAdopted({ value, generation: adopted.generation + 1 });
387
+ setDraft(value);
388
+ setDraftSettled(true);
389
+ }
390
+ const readAdoptedGeneration = useEffectEvent(() => adopted.generation);
391
+ useEffect(
392
+ () => () => {
393
+ if (commitTimerRef.current !== null) window.clearTimeout(commitTimerRef.current.id);
394
+ },
395
+ [],
396
+ );
397
+ const acceptsFamily = (candidate: string) =>
398
+ isFontFamilyAvailable(candidate) && (!requireMonospace || isMonospaceFamily(candidate));
399
+ const commitDraft = (next: string) => {
400
+ setDraftSettled(true);
401
+ if (next.trim().length === 0 || acceptsFamily(next)) {
402
+ onValueChange(next);
403
+ }
404
+ };
405
+ const flushDraft = () => {
406
+ const pending = commitTimerRef.current;
407
+ cancelPendingCommit();
408
+ if (pending !== null && pending.value === value) commitDraft(draft);
409
+ };
410
+ const draftTrimmed = draft.trim();
411
+ const draftPending = draftSettled && draftTrimmed.length > 0 && draftTrimmed !== trimmed;
412
+ const resetToDefault = () => {
413
+ cancelPendingCommit();
414
+ setDraft(defaultValue);
415
+ setDraftSettled(true);
416
+ onReset();
417
+ };
418
+ const resetAction =
419
+ value !== defaultValue || size.value !== size.defaultValue ? (
420
+ <SettingResetButton label={title.toLowerCase()} onClick={resetToDefault} />
421
+ ) : null;
422
+ const fontEnumeration = useFontEnumeration();
423
+ const [inputFocused, setInputFocused] = useState(false);
424
+ const familyControl =
425
+ fontEnumeration.status === "granted" ? (
426
+ <FontFamilyPicker
427
+ ariaLabel={`${title} family`}
428
+ defaultFamily={defaultFamily}
429
+ selectedFamily={trimmed}
430
+ requireMonospace={requireMonospace}
431
+ initialOpen={inputFocused}
432
+ onSelect={onValueChange}
433
+ />
434
+ ) : (
435
+ <Input
436
+ size="sm"
437
+ aria-label={`${title} family`}
438
+ aria-invalid={draftPending || undefined}
439
+ autoCapitalize="off"
440
+ autoComplete="off"
441
+ className="min-w-0 flex-1"
442
+ maxLength={200}
443
+ onFocus={() => {
444
+ setInputFocused(true);
445
+ discoverInstalledFonts();
446
+ }}
447
+ onBlur={() => {
448
+ setInputFocused(false);
449
+ flushDraft();
450
+ }}
451
+ onChange={(event) => {
452
+ const next = event.currentTarget.value;
453
+ setDraft(next);
454
+ setDraftSettled(false);
455
+ cancelPendingCommit();
456
+ const typedAgainst = value;
457
+ const typedInGeneration = adopted.generation;
458
+ commitTimerRef.current = {
459
+ id: window.setTimeout(() => {
460
+ commitTimerRef.current = null;
461
+ if (readAdoptedGeneration() === typedInGeneration) commitDraft(next);
462
+ }, 400),
463
+ value: typedAgainst,
464
+ };
465
+ }}
466
+ onKeyDown={(event) => {
467
+ if (event.key === "Enter") flushDraft();
468
+ if (event.key === "Escape") {
469
+ // Discard uncommitted typing without closing the settings page,
470
+ // which is what an unhandled Escape does.
471
+ event.preventDefault();
472
+ event.stopPropagation();
473
+ cancelPendingCommit();
474
+ setDraft(value);
475
+ setDraftSettled(true);
476
+ }
477
+ }}
478
+ placeholder={defaultFamily}
479
+ spellCheck={false}
480
+ value={draft}
481
+ />
482
+ );
483
+ const control = (
484
+ <div className="flex w-full items-center gap-2 sm:w-auto">
485
+ <div className="min-w-0 flex-1 sm:w-44 sm:flex-none">{familyControl}</div>
486
+ <Select
487
+ value={String(size.value)}
488
+ onValueChange={(next) => {
489
+ if (typeof next !== "string") return;
490
+ const parsed = Number(next);
491
+ if (Number.isInteger(parsed) && parsed >= size.min && parsed <= size.max) {
492
+ size.onChange(parsed);
493
+ }
494
+ }}
495
+ >
496
+ <SelectTrigger size="sm" className="w-22 shrink-0" aria-label={size.label}>
497
+ <SelectValue>{size.value} px</SelectValue>
498
+ </SelectTrigger>
499
+ <SelectPopup align="end" alignItemWithTrigger={false}>
500
+ {Array.from({ length: size.max - size.min + 1 }, (_, index) => size.min + index).map(
501
+ (px) => (
502
+ <SelectItem hideIndicator key={px} value={String(px)}>
503
+ {px} px
504
+ </SelectItem>
505
+ ),
506
+ )}
507
+ </SelectPopup>
508
+ </Select>
509
+ </div>
510
+ );
511
+ return (
512
+ <SettingsRow
513
+ {...(id !== undefined ? { id } : {})}
514
+ title={title}
515
+ description={description}
516
+ resetAction={resetAction}
517
+ control={control}
518
+ >
519
+ {preview}
520
+ </SettingsRow>
521
+ );
522
+}