fix: solve #5662 — keep diff view comments readable with imported dark themes

Draft
GI
4mo ago
+175-4

Root cause

Since #5404 the diff view uses the active theme's syntax colors. For imported themes without an explicit editor.syntax.comment, the comment color fell back to ANSI terminal.brightBlack:

comment: terminal?.brightBlack ?? theme.ui.mutedForeground,

brightBlack is the "dim" ANSI slot by design. In popular dark themes it sits at 1.3–3.4:1 against the background, and the diff view then composites its green/red line tints and word-level highlights on top, costing another ~⅓ of the contrast. With Tokyo Night (brightBlack #414868 on #1a1b26) a comment measures ~1.9:1 plain and bottoms out at ~1.0:1 inside a deleted-word highlight — the text disappears. Every other syntax token derives from a readable slot, so comments were the only casualty, and only for imported themes (built-ins pin their own comment color).

The fix

getEditorTheme now derives the comment color via a contrast guard (apps/desktop/src/shared/themes/editor-theme.ts): keep brightBlack only when it clears 4.5:1 (WCAG AA for normal text) against the editor background; otherwise fall back to ui.mutedForeground, the theme's designed-to-be-readable secondary text color. A small contrastRatio helper (wrapping culori's wcagContrast) was added to utils.ts.

Built-in Monokai's brightBlack (#75715e, ~3.0:1) is its iconic comment color, which the generic guard would otherwise swap. It's now pinned explicitly via editor.syntax.comment the way ember already pins its comment — so Monokai keeps its signature look. Themes that deliberately want dimmer comments can still set editor.syntax.comment.

Tests

apps/desktop/src/shared/themes/editor.test.ts:

  • Reproduction — a stock Tokyo Night fixture (no editor.syntax) asserts its raw brightBlack is below 4.5:1, then asserts the derived comment is not brightBlack and does clear 4.5:1 against the background. This test fails on the old code and passes on the fix.
  • A theme whose brightBlack already clears the floor keeps it as the comment color (guard doesn't over-correct).
  • Monokai keeps its pinned #75715e comment.

All theme tests pass (bun test src/shared/themes/), lint and typecheck clean.

Closes #5662


Open in Stage

Summary by cubic

Keep diff view comments readable in imported dark themes by rejecting dim terminal.brightBlack when contrast is low and falling back to ui.mutedForeground. Closes #5662.

  • Bug Fixes
    • Apply a 4.5:1 contrast guard for comment color; use terminal.brightBlack only if it meets the threshold, else use ui.mutedForeground.
    • Add contrastRatio helper wrapping culori’s wcagContrast.
    • Pin Monokai’s comment color (#75715e) via editor.syntax.comment to preserve its look.
    • Tests cover Tokyo Night regression, readable brightBlack preservation, and Monokai pin.