fix(frontend): make general-settings font picker actually work
The font picker in 常规设置 silently failed in three distinct ways: 1. Font family changes were invisible on macOS because 6 of the 7 options (Inter / Roboto / Segoe UI / Cascadia Code / JetBrains Mono / Fira Code) aren't installed by default and fell back to system SF. Swap the picker to a platform-aware list — Mac / Windows / Linux each see only the fonts their OS actually ships. The "third sans slot" now always serves a serif face (Georgia / Times New Roman / DejaVu Serif) so the difference is unambiguously visible instead of being three near-identical sans families. 2. Font size flipped --app-font-scale on <html>, but that variable was only consumed by a handful of TDesign tokens and the body reset, so the ~1000+ hard-coded font-size: NNpx rules across the frontend stayed put. Apply the multiplier via `zoom` on <html> so the whole document scales, then fix the 15 `100vh` rules that would overflow under zoom (html / body / #app are already height: 100%, so the descendants can use 100% too). 3. The monospace picker had no visible effect even on a working stack because `<t-input style="font-family: ...">` is forwarded to the inner <input>, but TDesign applies `font: var(--td-font-body-medium)` — a shorthand that resets font-family. Reach the real <input> / <textarea> via `:deep()` instead of inline style. Additional polish: - Add a live preview row under each picker so users can see the effect without hunting for a code block or API Info screen - Rename "等宽字体 / Monospace Font" → "代码字体 / Code Font" to match VSCode / GitHub / Notion convention; the old label was jargon - Rewrite descriptions to explain the actual purpose and scope
This commit is contained in:
@@ -5,22 +5,55 @@ import {
|
||||
migratePreferencesIntoUser,
|
||||
} from './preferenceStorage'
|
||||
|
||||
// Font options are filtered by host platform. A Windows user selecting
|
||||
// "PingFang SC" in a cross-platform list would see Microsoft YaHei anyway
|
||||
// (fallback via the stack), so the labels would lie. Instead each OS
|
||||
// surfaces the fonts it actually ships.
|
||||
// Mac: system / pingfang / georgia / sans-serif
|
||||
// system / menlo / monaco / monospace
|
||||
// Windows: system / yahei / times / sans-serif
|
||||
// system / consolas / cascadia / monospace
|
||||
// Linux: system / noto-cjk / dejavu-serif / sans-serif
|
||||
// system / dejavu-mono / liberation-mono / monospace
|
||||
//
|
||||
// The "third" sans slot on each platform is a serif face (Georgia /
|
||||
// Times New Roman / DejaVu Serif). This is intentional: on macOS the
|
||||
// system sans, PingFang, and Helvetica are all visually close enough
|
||||
// that users reported "the font doesn't change". A serif option produces
|
||||
// a genuinely distinct look, so the setting feels responsive.
|
||||
//
|
||||
// All keys live in a single union so the persisted value is stable across
|
||||
// devices; the runtime picker just hides the non-matching subset. A user
|
||||
// who moves a localStorage value (e.g. "pingfang") to a Windows machine
|
||||
// gets it validated against SANS_STACKS (which still contains the key) and
|
||||
// the font stack still works — the browser just falls back to yahei on
|
||||
// its own. Only truly unknown keys fall through to DEFAULT_SANS.
|
||||
export type FontKey =
|
||||
| 'system'
|
||||
// Mac
|
||||
| 'pingfang'
|
||||
| 'inter'
|
||||
| 'helvetica'
|
||||
| 'segoe'
|
||||
| 'roboto'
|
||||
| 'georgia'
|
||||
// Windows
|
||||
| 'yahei'
|
||||
| 'times'
|
||||
// Linux
|
||||
| 'noto-cjk'
|
||||
| 'dejavu-serif'
|
||||
// cross-platform
|
||||
| 'sans-serif'
|
||||
|
||||
export type MonoFontKey =
|
||||
| 'system'
|
||||
| 'cascadia'
|
||||
| 'jetbrains'
|
||||
| 'fira'
|
||||
// Mac
|
||||
| 'menlo'
|
||||
| 'monaco'
|
||||
// Windows
|
||||
| 'consolas'
|
||||
| 'cascadia'
|
||||
// Linux
|
||||
| 'dejavu-mono'
|
||||
| 'liberation-mono'
|
||||
// cross-platform
|
||||
| 'monospace'
|
||||
|
||||
export type FontSizeKey = 'small' | 'normal' | 'large'
|
||||
@@ -32,29 +65,41 @@ const SIZE_KEY = 'font_size'
|
||||
export const SANS_STACKS: Record<FontKey, string> = {
|
||||
system:
|
||||
'-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif',
|
||||
// Mac
|
||||
pingfang:
|
||||
'"PingFang SC", "Microsoft YaHei", "Hiragino Sans GB", -apple-system, BlinkMacSystemFont, sans-serif',
|
||||
inter:
|
||||
'Inter, "Segoe UI", -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif',
|
||||
helvetica:
|
||||
'"Helvetica Neue", Helvetica, Arial, "PingFang SC", "Microsoft YaHei", sans-serif',
|
||||
segoe:
|
||||
'"Segoe UI", Tahoma, Verdana, Arial, "PingFang SC", "Microsoft YaHei", sans-serif',
|
||||
roboto:
|
||||
'Roboto, "Segoe UI", -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif',
|
||||
georgia:
|
||||
'Georgia, "Times New Roman", "Songti SC", "SimSun", serif',
|
||||
// Windows
|
||||
yahei:
|
||||
'"Microsoft YaHei", "PingFang SC", "Hiragino Sans GB", Tahoma, Arial, sans-serif',
|
||||
times:
|
||||
'"Times New Roman", Times, Georgia, "SimSun", "Songti SC", serif',
|
||||
// Linux
|
||||
'noto-cjk':
|
||||
'"Noto Sans CJK SC", "Noto Sans SC", "Source Han Sans SC", "WenQuanYi Micro Hei", "Microsoft YaHei", sans-serif',
|
||||
'dejavu-serif':
|
||||
'"DejaVu Serif", "Liberation Serif", "Noto Serif", Georgia, "Times New Roman", serif',
|
||||
// cross-platform
|
||||
'sans-serif': 'sans-serif',
|
||||
}
|
||||
|
||||
export const MONO_STACKS: Record<MonoFontKey, string> = {
|
||||
system:
|
||||
'ui-monospace, SFMono-Regular, "SF Mono", Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace',
|
||||
cascadia:
|
||||
'"Cascadia Code", "Cascadia Mono", ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace',
|
||||
jetbrains:
|
||||
'"JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace',
|
||||
fira: '"Fira Code", ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace',
|
||||
// Mac
|
||||
menlo: 'Menlo, Monaco, Consolas, "Courier New", monospace',
|
||||
monaco: 'Monaco, Menlo, Consolas, "Courier New", monospace',
|
||||
// Windows
|
||||
consolas: 'Consolas, "Courier New", Menlo, Monaco, monospace',
|
||||
cascadia:
|
||||
'"Cascadia Code", "Cascadia Mono", Consolas, "Courier New", monospace',
|
||||
// Linux
|
||||
'dejavu-mono':
|
||||
'"DejaVu Sans Mono", "Liberation Mono", Menlo, Consolas, monospace',
|
||||
'liberation-mono':
|
||||
'"Liberation Mono", "DejaVu Sans Mono", Menlo, Consolas, monospace',
|
||||
// cross-platform
|
||||
monospace: 'monospace',
|
||||
}
|
||||
|
||||
@@ -64,6 +109,65 @@ export const FONT_SCALES: Record<FontSizeKey, number> = {
|
||||
large: 1.125,
|
||||
}
|
||||
|
||||
export type Platform = 'mac' | 'windows' | 'linux'
|
||||
|
||||
/**
|
||||
* Detect the host OS so the font picker can offer options the user will
|
||||
* actually see. Prefers navigator.userAgentData (modern Chromium, Edge,
|
||||
* current Wails WebView) and falls back to the UA string — older Safari
|
||||
* and some Firefox builds still don't expose userAgentData.
|
||||
*
|
||||
* Unknown platforms fall back to Mac, which is the most common development
|
||||
* environment for this project.
|
||||
*/
|
||||
export function detectPlatform(): Platform {
|
||||
if (typeof navigator === 'undefined') return 'mac'
|
||||
|
||||
// Modern: client hints (Chromium ≥ 90, Edge, Wails WebView on Win/Mac).
|
||||
const uaData = (navigator as unknown as {
|
||||
userAgentData?: { platform?: string }
|
||||
}).userAgentData
|
||||
const hintPlatform = uaData?.platform
|
||||
if (typeof hintPlatform === 'string' && hintPlatform.length > 0) {
|
||||
const p = hintPlatform.toLowerCase()
|
||||
if (p.includes('mac')) return 'mac'
|
||||
if (p.includes('win')) return 'windows'
|
||||
if (p.includes('linux') || p.includes('android') || p.includes('chrome os')) return 'linux'
|
||||
}
|
||||
|
||||
// Fallback: parse the UA string. Matches Safari, older Firefox, WebView.
|
||||
const ua = navigator.userAgent.toLowerCase()
|
||||
if (ua.includes('mac os x') || ua.includes('macintosh') || ua.includes('iphone') || ua.includes('ipad')) {
|
||||
return 'mac'
|
||||
}
|
||||
if (ua.includes('windows')) return 'windows'
|
||||
if (ua.includes('linux') || ua.includes('android') || ua.includes('cros')) return 'linux'
|
||||
|
||||
return 'mac'
|
||||
}
|
||||
|
||||
const SANS_KEYS_BY_PLATFORM: Record<Platform, FontKey[]> = {
|
||||
mac: ['system', 'pingfang', 'georgia', 'sans-serif'],
|
||||
windows: ['system', 'yahei', 'times', 'sans-serif'],
|
||||
linux: ['system', 'noto-cjk', 'dejavu-serif', 'sans-serif'],
|
||||
}
|
||||
|
||||
const MONO_KEYS_BY_PLATFORM: Record<Platform, MonoFontKey[]> = {
|
||||
mac: ['system', 'menlo', 'monaco', 'monospace'],
|
||||
windows: ['system', 'consolas', 'cascadia', 'monospace'],
|
||||
linux: ['system', 'dejavu-mono', 'liberation-mono', 'monospace'],
|
||||
}
|
||||
|
||||
/** Keys visible in the sans-serif picker on the current platform. */
|
||||
export function visibleSansKeys(platform: Platform = detectPlatform()): FontKey[] {
|
||||
return SANS_KEYS_BY_PLATFORM[platform]
|
||||
}
|
||||
|
||||
/** Keys visible in the monospace picker on the current platform. */
|
||||
export function visibleMonoKeys(platform: Platform = detectPlatform()): MonoFontKey[] {
|
||||
return MONO_KEYS_BY_PLATFORM[platform]
|
||||
}
|
||||
|
||||
const DEFAULT_SANS: FontKey = 'system'
|
||||
const DEFAULT_MONO: MonoFontKey = 'system'
|
||||
const DEFAULT_SIZE: FontSizeKey = 'normal'
|
||||
@@ -120,7 +224,18 @@ function applyFont() {
|
||||
lastApplied.mono = monoStack
|
||||
}
|
||||
if (lastApplied.scale !== scale) {
|
||||
root.style.setProperty('--app-font-scale', scale)
|
||||
// Apply size via CSS zoom on <html> so the multiplier reaches every
|
||||
// element — including the ~1000+ hard-coded `font-size: NNpx` rules
|
||||
// scattered across the frontend. The previous approach set
|
||||
// `--app-font-scale` and relied on `calc(NNpx * var(--app-font-scale))`,
|
||||
// but calc() only runs where the variable is consumed (a few TDesign
|
||||
// tokens plus the body reset), so users saw only parts of the UI
|
||||
// resize. Zoom composites the whole document at the requested factor
|
||||
// and is supported in all Chromium, WebKit, and modern Firefox (126+).
|
||||
// https://developer.mozilla.org/en-US/docs/Web/CSS/zoom
|
||||
// setProperty is used instead of root.style.zoom because `zoom` is not
|
||||
// in the standard CSSStyleDeclaration type.
|
||||
root.style.setProperty('zoom', scale)
|
||||
lastApplied.scale = scale
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user