From 71544450d50ec11850a9850eec76b4d6e4f0cddd Mon Sep 17 00:00:00 2001 From: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Date: Mon, 20 Jul 2026 18:54:45 -0700 Subject: [PATCH] fix(terminal): gate xterm contrast correction by terminal background luminance (#7934) (#9599) * fix(terminal): gate xterm contrast correction by theme mode (#7934) minimumContrastRatio: 4.5 (added in #5986 to rescue invisible white/bright-white ANSI text on light backgrounds) was applied globally, over-correcting vibrant ANSI colors on dark themes. Keep 4.5 on light themes; disable it (1) on dark themes. applyTerminalAppearance re-gates it in its per-pane loop, so live auto/system dark<->light flips are handled for free. buildDefaultTerminalOptions keeps 4.5 as the initial value so the first paint on a light theme never flashes low-contrast text. Supersedes #7939. Co-authored-by: Orca * fix(terminal): gate contrast correction by terminal background luminance Contrast correction rescues white text on light backgrounds but over-corrects on dark. The previous mode-based gate failed when the terminal theme didn't match the app mode (e.g., terminalUseSeparateLightTheme=false). Now gates by the theme's background luminance via isTerminalBackgroundLight() instead. * refactor(terminal): extract contrast correction gating into shared helpe Centralizes the minimumContrastRatio gating logic (#7934) into a dedicated module so both the settings preview and live terminal appearance apply the same contrast-by-luminance rule, preventing drift. Adds value-gating to skip redundant writes that would clear xterm's contrast cache on re-applies. --------- Co-authored-by: Orca --- .../settings/TerminalSettingsPreview.tsx | 8 +- .../terminal-pane/terminal-appearance.test.ts | 74 +++++++++++++++++++ .../terminal-pane/terminal-appearance.ts | 11 +++ .../lib/pane-manager/pane-lifecycle.test.ts | 2 +- .../lib/pane-manager/pane-terminal-options.ts | 6 +- .../src/lib/terminal-contrast-correction.ts | 18 +++++ 6 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 src/renderer/src/lib/terminal-contrast-correction.ts diff --git a/src/renderer/src/components/settings/TerminalSettingsPreview.tsx b/src/renderer/src/components/settings/TerminalSettingsPreview.tsx index f21b8b939..d9dee715d 100644 --- a/src/renderer/src/components/settings/TerminalSettingsPreview.tsx +++ b/src/renderer/src/components/settings/TerminalSettingsPreview.tsx @@ -8,6 +8,7 @@ import { buildDefaultTerminalOptions } from '@/lib/pane-manager/pane-terminal-op import { buildFontFamily } from '@/components/terminal-pane/layout-serialization' import { composeActiveTerminalTheme } from '@/components/terminal-pane/terminal-appearance' import { clampNumber, resolveEffectiveTerminalAppearance } from '@/lib/terminal-theme' +import { resolveTerminalMinimumContrastRatio } from '@/lib/terminal-contrast-correction' import { resolveTerminalFontWeights } from '../../../../shared/terminal-fonts' import { resolveTerminalLigaturesEnabled } from '../../../../shared/terminal-ligatures' import { normalizeTerminalLineHeight } from '../../../../shared/terminal-line-height-settings' @@ -195,6 +196,11 @@ export function TerminalSettingsPreview({ return } terminal.options.theme = composedTheme + // Why: share applyTerminalAppearance's gating helper (#7934) so the preview can't drift from live panes. + terminal.options.minimumContrastRatio = resolveTerminalMinimumContrastRatio( + composedTheme.background, + effectiveMode + ) // Why: xterm renders an alpha-channel background opaque unless allowTransparency is set (matches applyTerminalAppearance). terminal.options.allowTransparency = settings.terminalBackgroundOpacity !== undefined && settings.terminalBackgroundOpacity < 1 @@ -205,7 +211,7 @@ export function TerminalSettingsPreview({ // Why reset() not clear(): buffer ends mid-line on the prompt, so clear()+write would duplicate the trailing fragment. terminal.reset() terminal.write(PREVIEW_BUFFER) - }, [composedTheme, settings.terminalBackgroundOpacity]) + }, [composedTheme, effectiveMode, settings.terminalBackgroundOpacity]) useEffect(() => { const terminal = terminalRef.current diff --git a/src/renderer/src/components/terminal-pane/terminal-appearance.test.ts b/src/renderer/src/components/terminal-pane/terminal-appearance.test.ts index 68f20a382..fdf3da8e2 100644 --- a/src/renderer/src/components/terminal-pane/terminal-appearance.test.ts +++ b/src/renderer/src/components/terminal-pane/terminal-appearance.test.ts @@ -405,6 +405,80 @@ describe('applyTerminalAppearance theme assignment', () => { expect(pane.terminal.options.theme).not.toBe(firstTheme) expect(pane.terminal.options.theme?.background).toBe('#102030') }) + + // #7934: contrast correction rescues invisible white text on light backgrounds but over-corrects on dark; + // gate by the composed theme's background luminance (either theme slot can hold either kind of theme). + it('keeps xterm contrast correction on light themes', () => { + const pane = makePane(1) + const settings = getDefaultSettings('/tmp') + + apply(pane, { ...settings, theme: 'light' }) + + expect(pane.terminal.options.minimumContrastRatio).toBe(4.5) + }) + + it('disables xterm contrast correction on dark themes', () => { + const pane = makePane(1) + const settings = getDefaultSettings('/tmp') + + apply(pane, { ...settings, theme: 'dark' }) + + expect(pane.terminal.options.minimumContrastRatio).toBe(1) + }) + + it('re-gates contrast correction when the theme flips live', () => { + const pane = makePane(1) + const settings = getDefaultSettings('/tmp') + + apply(pane, { ...settings, theme: 'light' }) + expect(pane.terminal.options.minimumContrastRatio).toBe(4.5) + + apply(pane, { ...settings, theme: 'dark' }) + expect(pane.terminal.options.minimumContrastRatio).toBe(1) + }) + + it('disables contrast correction in light mode when the terminal matches dark mode', () => { + // terminalUseSeparateLightTheme=false keeps the dark terminal theme in light app mode; the gate must follow the background. + const pane = makePane(1) + const settings = getDefaultSettings('/tmp') + + apply(pane, { ...settings, theme: 'light', terminalUseSeparateLightTheme: false }) + + expect(pane.terminal.options.minimumContrastRatio).toBe(1) + }) + + it('keeps contrast correction in dark mode when a light theme fills the dark slot', () => { + const pane = makePane(1) + const settings = getDefaultSettings('/tmp') + + apply(pane, { ...settings, theme: 'dark', terminalThemeDark: 'Builtin Tango Light' }) + + expect(pane.terminal.options.minimumContrastRatio).toBe(4.5) + }) + + it('skips the minimumContrastRatio write on a no-op re-apply (preserves xterm contrast cache)', () => { + const pane = makePane(1) + let writes = 0 + let stored: number | undefined + Object.defineProperty(pane.terminal.options, 'minimumContrastRatio', { + configurable: true, + enumerable: true, + get: () => stored, + set: (value: number) => { + stored = value + writes += 1 + } + }) + const settings = getDefaultSettings('/tmp') + + apply(pane, { ...settings, theme: 'dark' }) + const writesAfterFirst = writes + + apply(pane, { ...settings, theme: 'dark' }) + + // The value-gate must not rewrite an unchanged ratio — each write clears xterm's contrast cache. + expect(writes).toBe(writesAfterFirst) + }) }) describe('publishTerminalViewAttributesAtAppStart', () => { diff --git a/src/renderer/src/components/terminal-pane/terminal-appearance.ts b/src/renderer/src/components/terminal-pane/terminal-appearance.ts index 40564024d..fa9355238 100644 --- a/src/renderer/src/components/terminal-pane/terminal-appearance.ts +++ b/src/renderer/src/components/terminal-pane/terminal-appearance.ts @@ -24,6 +24,7 @@ import type { TerminalViewAttributes } from '../../../../shared/terminal-view-at import { publishTerminalViewAttributes } from './terminal-view-attributes-publisher' import { normalizeTerminalLineHeight } from '../../../../shared/terminal-line-height-settings' import { maybePushMode2031Flip } from './terminal-mode-2031-replies' +import { resolveTerminalMinimumContrastRatio } from '@/lib/terminal-contrast-correction' // Why Pick over a hand-rolled type: stays tied to xterm's canonical signature so upstream tightening surfaces here. type Mode2031Parser = Pick @@ -207,6 +208,16 @@ export function applyTerminalAppearance( if (theme && !composedTerminalThemesEqual(pane.terminal.options.theme, theme)) { pane.terminal.options.theme = theme } + // Gate off the configured theme background; the live OSC-11 background is deliberately preserved by the + // theme write above, so a TUI that repaints its background at runtime won't re-gate (known limitation). + // Why value-gated: writing minimumContrastRatio clears xterm's contrast cache, so skip on no-op re-applies. + const minimumContrastRatio = resolveTerminalMinimumContrastRatio( + theme?.background, + appearance.mode + ) + if (pane.terminal.options.minimumContrastRatio !== minimumContrastRatio) { + pane.terminal.options.minimumContrastRatio = minimumContrastRatio + } // Why clear explicitly: allowTransparency has rendering cost and a stale `true` could bleed in from a prior opacity. pane.terminal.options.allowTransparency = settings.terminalBackgroundOpacity !== undefined && settings.terminalBackgroundOpacity < 1 diff --git a/src/renderer/src/lib/pane-manager/pane-lifecycle.test.ts b/src/renderer/src/lib/pane-manager/pane-lifecycle.test.ts index c69600e8a..607ad1cdb 100644 --- a/src/renderer/src/lib/pane-manager/pane-lifecycle.test.ts +++ b/src/renderer/src/lib/pane-manager/pane-lifecycle.test.ts @@ -114,7 +114,7 @@ describe('buildDefaultTerminalOptions', () => { expect(normalizeTerminalFastScrollSensitivity(25)).toBe(20) }) - it('enables xterm contrast correction for low-contrast CLI colors', () => { + it('defaults minimumContrastRatio to the light-background value (applyTerminalAppearance re-gates it)', () => { expect(buildDefaultTerminalOptions().minimumContrastRatio).toBe(4.5) }) diff --git a/src/renderer/src/lib/pane-manager/pane-terminal-options.ts b/src/renderer/src/lib/pane-manager/pane-terminal-options.ts index 911d3fa75..1fc475134 100644 --- a/src/renderer/src/lib/pane-manager/pane-terminal-options.ts +++ b/src/renderer/src/lib/pane-manager/pane-terminal-options.ts @@ -1,5 +1,6 @@ import type { ITerminalOptions } from '@xterm/xterm' import { DESKTOP_TERMINAL_SCROLLBACK_ROWS_DEFAULT } from '../../../../shared/terminal-scrollback-policy' +import { LIGHT_BG_MIN_CONTRAST } from '@/lib/terminal-contrast-correction' type TerminalCursorStyle = NonNullable type TerminalCursorInactiveStyle = NonNullable @@ -47,9 +48,8 @@ export function buildDefaultTerminalOptions(): ITerminalOptions { scrollSensitivity: DEFAULT_TERMINAL_SCROLL_SENSITIVITY, fastScrollSensitivity: DEFAULT_TERMINAL_FAST_SCROLL_SENSITIVITY, allowTransparency: false, - // Why: agent CLIs sometimes render body text with ANSI white/bright-white - // on light themes; xterm can keep those cells readable across renderers. - minimumContrastRatio: 4.5, + // Initial value only; applyTerminalAppearance re-gates by background luminance (#7934) before any content paints. + minimumContrastRatio: LIGHT_BG_MIN_CONTRAST, // Why: on macOS, non-US layouts rely on Option to compose characters like @ and €. macOptionIsMeta: false, macOptionClickForcesSelection: true, diff --git a/src/renderer/src/lib/terminal-contrast-correction.ts b/src/renderer/src/lib/terminal-contrast-correction.ts new file mode 100644 index 000000000..4cd147af8 --- /dev/null +++ b/src/renderer/src/lib/terminal-contrast-correction.ts @@ -0,0 +1,18 @@ +import { isTerminalBackgroundLight } from '@/lib/terminal-title-contrast' + +// xterm minimumContrastRatio tuning (#7934). Light backgrounds keep WCAG-AA correction so invisible +// white/bright-white ANSI body text stays readable; dark backgrounds disable it (ratio 1) because +// correction over-brightens vibrant ANSI colors. +export const LIGHT_BG_MIN_CONTRAST = 4.5 +export const DARK_BG_MIN_CONTRAST = 1 + +// Why gate by background luminance, not app mode (#7934): either theme slot can hold either kind of +// theme (match-dark-mode, or a light theme in the dark slot), so follow the composed background. +export function resolveTerminalMinimumContrastRatio( + background: string | undefined, + appSurface: 'dark' | 'light' +): number { + return isTerminalBackgroundLight(background, { appSurface }) + ? LIGHT_BG_MIN_CONTRAST + : DARK_BG_MIN_CONTRAST +}