Files
orca/src/main/system-fonts.ts
T
gatsby74andJinjing 98d850aeba fix(settings): keep full installed font list (#7581)
* fix(settings): keep full installed font list

* fix(settings): bound font picker rendering

* Fix redundant font queries and handle partial state in worktree purge

- Latch the loaded state even when the returned font list is empty, preventing font-less systems from reissuing font listing calls on every picker interaction.
- Add null-tolerance for `ptyIdsByTabId` when building worktree purge state to support callers that pass partial AppState.

---------

Co-authored-by: Jinjing <6427696+AmethystLiang@users.noreply.github.com>
2026-07-06 20:21:58 -07:00

171 lines
4.2 KiB
TypeScript

import { execFile } from 'node:child_process'
let cachedFonts: string[] | null = null
let fontsPromise: Promise<string[]> | null = null
const SYSTEM_FONT_LIST_TIMEOUT_MS = 15_000
// Why: large macOS font catalogs can make system_profiler exceed 15s even
// when it is healthy; keep the longer wait scoped to that slow command.
const MAC_SYSTEM_FONT_LIST_TIMEOUT_MS = 45_000
export async function listSystemFontFamilies(): Promise<string[]> {
if (cachedFonts) {
return cachedFonts
}
if (fontsPromise) {
return fontsPromise
}
fontsPromise = loadSystemFontFamilies()
.then((fonts) => {
cachedFonts = fonts.length > 0 ? fonts : fallbackFonts()
return cachedFonts
})
.catch(() => {
cachedFonts = fallbackFonts()
return cachedFonts
})
.finally(() => {
fontsPromise = null
})
return fontsPromise
}
export function warmSystemFontFamilies(): void {
void listSystemFontFamilies()
}
function loadSystemFontFamilies(): Promise<string[]> {
if (process.platform === 'darwin') {
return listMacFonts()
}
if (process.platform === 'win32') {
return listWindowsFonts()
}
return listLinuxFonts()
}
function listMacFonts(): Promise<string[]> {
return execFileText(
'system_profiler',
['SPFontsDataType', '-json'],
32 * 1024 * 1024,
MAC_SYSTEM_FONT_LIST_TIMEOUT_MS
).then((output) => {
const parsed = JSON.parse(output) as {
SPFontsDataType?: {
typefaces?: {
family?: string
}[]
}[]
}
return uniqueSorted(
(parsed.SPFontsDataType ?? []).flatMap((font) =>
(font.typefaces ?? []).map((typeface) => typeface.family)
)
)
})
}
function listLinuxFonts(): Promise<string[]> {
return execFileText('fc-list', [':', 'family'], 8 * 1024 * 1024).then((output) =>
uniqueSorted(
output
.split('\n')
.flatMap((line) => line.split(','))
.map((name) => name.trim())
.filter(Boolean)
)
)
}
function listWindowsFonts(): Promise<string[]> {
const script = `
Add-Type -AssemblyName System.Drawing
$fonts = New-Object System.Drawing.Text.InstalledFontCollection
$fonts.Families | ForEach-Object { $_.Name }
`
return execFileText(
'powershell.exe',
['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-Command', script],
8 * 1024 * 1024
).then((output) =>
uniqueSorted(
output
.split('\n')
.map((name) => name.trim())
.filter(Boolean)
)
)
}
function execFileText(
command: string,
args: string[],
maxBuffer: number,
timeoutMs = SYSTEM_FONT_LIST_TIMEOUT_MS
): Promise<string> {
return new Promise((resolve, reject) => {
let settled = false
let timer: ReturnType<typeof setTimeout> | undefined
const child = execFile(command, args, { encoding: 'utf8', maxBuffer }, (error, stdout) => {
if (settled) {
return
}
settled = true
if (timer) {
clearTimeout(timer)
}
if (error) {
reject(error)
return
}
resolve(stdout)
})
if (!settled) {
timer = setTimeout(() => {
if (settled) {
return
}
settled = true
// Why: font discovery is a startup convenience; a stuck OS font tool
// should fall back instead of keeping settings IPC pending forever.
child.kill()
reject(new Error(`Timed out listing system fonts with ${command}`))
}, timeoutMs)
if (typeof timer === 'object' && 'unref' in timer) {
timer.unref()
}
}
})
}
function uniqueSorted(values: (string | undefined)[]): string[] {
return Array.from(
new Set(
values
.map((value) => value?.trim() ?? '')
.filter((value) => value.length > 0 && !value.startsWith('.'))
)
).sort((a, b) => a.localeCompare(b))
}
function fallbackFonts(): string[] {
if (process.platform === 'darwin') {
return ['SF Mono', 'Menlo', 'Monaco', 'JetBrains Mono', 'Fira Code']
}
if (process.platform === 'win32') {
return ['Cascadia Mono', 'Consolas', 'Lucida Console', 'JetBrains Mono', 'Fira Code']
}
return [
'JetBrains Mono',
'Fira Code',
'DejaVu Sans Mono',
'Liberation Mono',
'Ubuntu Mono',
'Noto Sans Mono'
]
}