Skip to content

Commit 357da8e

Browse files
committed
fix(web): imported themes keep switches and focus rings visible
1 parent 6b286ae commit 357da8e

2 files changed

Lines changed: 239 additions & 33 deletions

File tree

‎apps/web/src/vscodeThemeImport.test.ts‎

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,157 @@ describe("VS Code theme import", () => {
112112
);
113113
});
114114

115+
it("prefers a visible input background over a transparent border", () => {
116+
const catppuccin = parseVsCodeThemeFile({
117+
name: "Catppuccin Mocha",
118+
type: "dark",
119+
colors: {
120+
"editor.background": "#1e1e2e",
121+
"input.border": "#00000000",
122+
"input.background": "#313244",
123+
},
124+
});
125+
expect(asHex(catppuccin.colors.input)).toBe("#313244");
126+
});
127+
128+
it("keeps unchecked inputs distinct from the checked action color", () => {
129+
const gruvbox = parseVsCodeThemeFile({
130+
name: "Gruvbox Light Soft",
131+
type: "light",
132+
colors: {
133+
"editor.background": "#f2e5bc",
134+
"button.background": "#45858880",
135+
"input.background": "#45858880",
136+
"input.border": "#928374",
137+
},
138+
});
139+
expect(
140+
contrastRatio(gruvbox.colors.input, gruvbox.colors.messageAction),
141+
).toBeGreaterThanOrEqual(1.1);
142+
});
143+
144+
it("keeps the derived input distinct when a button reuses it", () => {
145+
const theme = parseVsCodeThemeFile({
146+
name: "Derived input collision",
147+
type: "dark",
148+
colors: {
149+
"editor.background": "#1e1e2e",
150+
focusBorder: "#89b4fa",
151+
"button.background": "#525661",
152+
},
153+
});
154+
155+
expect(asHex(theme.colors.messageAction)).toBe("#525661");
156+
expect(asHex(theme.colors.input)).not.toBe("#525661");
157+
expect(contrastRatio(theme.colors.input, theme.colors.canvas)).toBeGreaterThanOrEqual(1.1);
158+
expect(contrastRatio(theme.colors.input, theme.colors.messageAction)).toBeGreaterThanOrEqual(
159+
1.1,
160+
);
161+
});
162+
163+
it("skips a transparent focus border for a visible accent key", () => {
164+
const vitesse = parseVsCodeThemeFile({
165+
name: "Vitesse Dark",
166+
type: "dark",
167+
colors: {
168+
"editor.background": "#121212",
169+
focusBorder: "#00000000",
170+
"button.background": "#4d9375",
171+
},
172+
});
173+
expect(asHex(vitesse.colors.accent)).toBe("#4d9375");
174+
expect(asHex(vitesse.colors.focus)).toBe("#4d9375");
175+
expect(contrastRatio(vitesse.colors.focus, vitesse.colors.canvas)).toBeGreaterThanOrEqual(1.1);
176+
});
177+
178+
it("keeps focus visible against an explicit raised surface", () => {
179+
const theme = parseVsCodeThemeFile({
180+
name: "Raised focus",
181+
type: "dark",
182+
colors: {
183+
"editor.background": "#000000",
184+
focusBorder: "#111111",
185+
"editorWidget.background": "#111111",
186+
"button.background": "#4d9375",
187+
},
188+
});
189+
expect(contrastRatio(theme.colors.focus, theme.colors.surfaceRaised)).toBeGreaterThanOrEqual(
190+
1.1,
191+
);
192+
expect(asHex(theme.colors.focus)).toBe("#4d9375");
193+
});
194+
195+
it("keeps the fallback focus visible against an explicit raised surface", () => {
196+
const theme = parseVsCodeThemeFile({
197+
name: "Raised fallback focus",
198+
type: "dark",
199+
colors: {
200+
"editor.background": "#121212",
201+
"editorWidget.background": "#346bf1",
202+
},
203+
});
204+
expect(asHex(theme.colors.focus)).toBe("#ffffff");
205+
expect(contrastRatio(theme.colors.focus, theme.colors.canvas)).toBeGreaterThanOrEqual(1.1);
206+
expect(contrastRatio(theme.colors.focus, theme.colors.surfaceRaised)).toBeGreaterThanOrEqual(
207+
1.1,
208+
);
209+
});
210+
211+
it("skips a transparent button background for the action color", () => {
212+
const theme = parseVsCodeThemeFile({
213+
name: "Transparent button",
214+
type: "dark",
215+
colors: {
216+
"editor.background": "#121212",
217+
focusBorder: "#4d9375",
218+
"button.background": "#00000000",
219+
},
220+
});
221+
expect(asHex(theme.colors.messageAction)).toBe("#4d9375");
222+
});
223+
224+
it("skips a button background that matches the raised surface", () => {
225+
const theme = parseVsCodeThemeFile({
226+
name: "Raised button",
227+
type: "dark",
228+
colors: {
229+
"editor.background": "#121212",
230+
focusBorder: "#4d9375",
231+
"editorWidget.background": "#2a2d3a",
232+
"button.background": "#2a2d3a",
233+
},
234+
});
235+
expect(asHex(theme.colors.messageAction)).toBe("#4d9375");
236+
});
237+
238+
it("uses a visible default accent when the file has no usable accent key", () => {
239+
const theme = parseVsCodeThemeFile({
240+
name: "No accent",
241+
type: "dark",
242+
colors: { "editor.background": "#121212", focusBorder: "#121212" },
243+
});
244+
expect(contrastRatio(theme.colors.focus, theme.colors.canvas)).toBeGreaterThanOrEqual(1.1);
245+
});
246+
247+
it("validates placeholders against the resolved raised surface", () => {
248+
const lightPlus = parseVsCodeThemeFile({
249+
name: "Light Plus Shape",
250+
type: "light",
251+
colors: {
252+
"editor.background": "#eaeff3",
253+
"editor.foreground": "#1f1f1f",
254+
"editorWidget.background": "#ffffff",
255+
"input.placeholderForeground": "#767676",
256+
},
257+
});
258+
expect(
259+
contrastRatio(lightPlus.colors.placeholder, lightPlus.colors.surfaceRaised),
260+
).toBeGreaterThanOrEqual(4.5);
261+
expect(
262+
contrastRatio(lightPlus.colors.placeholder, lightPlus.colors.surfaceRaised),
263+
).toBeLessThan(contrastRatio(lightPlus.colors.text, lightPlus.colors.surfaceRaised));
264+
});
265+
115266
it("fills every role the file omits with a readable derived value", () => {
116267
const theme = parseVsCodeThemeFile(VSCODE_DARK);
117268
const colors = getThemeColorsForMode(theme, "dark")!;

‎apps/web/src/vscodeThemeImport.ts‎

Lines changed: 88 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
createVividThemeColors,
3+
getStandardThemeColors,
34
getThemeModes,
45
parseThemeFile,
56
themeColorToHex,
@@ -14,10 +15,10 @@ import {
1415
*
1516
* VS Code themes describe editor chrome, not an app palette: they carry a few
1617
* hundred workbench keys, leave most of them unset, and freely use 8-digit
17-
* hex with alpha for overlays. So the conversion derives a complete, contrast
18-
* -solved palette from the theme's editor background and accent, then layers
19-
* the workbench colors it did specify on top. Anything the file omits keeps
20-
* the derived value instead of falling back to an unrelated palette.
18+
* hex with alpha for overlays. The conversion derives a complete palette from
19+
* a visible accent and the editor background, then layers usable workbench
20+
* colors on top. Foregrounds must remain readable, while authored focus and
21+
* control candidates must remain distinct from adjacent surfaces and states.
2122
*/
2223

2324
type VsCodeRgba = { r: number; g: number; b: number; a: number };
@@ -210,26 +211,61 @@ export function parseVsCodeThemeFile(value: unknown): ThemeDefinition {
210211
const canvas = { r: canvasColor.r, g: canvasColor.g, b: canvasColor.b };
211212
const appearance = resolveAppearance(value, canvas);
212213

213-
const accentColor = pick(
214+
const canvasHex = toHex(canvas);
215+
const raisedSurfaceCandidateHex = solidOver(
216+
canvas,
217+
"editorWidget.background",
218+
"dropdown.background",
219+
);
220+
221+
/** Accent and control candidates must clear this small separation floor
222+
* from every adjacent surface or state checked by the importer. */
223+
const standsApart = (first: string, second: string) =>
224+
contrastRatio(hexToRgb(first), hexToRgb(second)) >= 1.1;
225+
226+
let accentColor: VsCodeRgba | null = null;
227+
let accentHex: string | null = null;
228+
for (const key of [
214229
"focusBorder",
215230
"button.background",
216231
"textLink.foreground",
217232
"activityBarBadge.background",
218233
"progressBar.background",
219234
"badge.background",
220-
);
221-
const canvasHex = toHex(canvas);
222-
const accentHex = accentColor ? flattenOver(accentColor, canvas) : null;
235+
]) {
236+
const candidate = parseVsCodeColor(colors[key]);
237+
if (!candidate) continue;
238+
const candidateHex = flattenOver(candidate, canvas);
239+
if (
240+
!standsApart(candidateHex, canvasHex) ||
241+
(raisedSurfaceCandidateHex !== null && !standsApart(candidateHex, raisedSurfaceCandidateHex))
242+
)
243+
continue;
244+
accentColor = candidate;
245+
accentHex = candidateHex;
246+
break;
247+
}
248+
if (!accentColor || !accentHex) {
249+
const standardAccentHex = themeColorToHex(getStandardThemeColors(appearance).accent)!;
250+
accentHex =
251+
[standardAccentHex, "#ffffff", "#000000"].find(
252+
(candidate) =>
253+
standsApart(candidate, canvasHex) &&
254+
(raisedSurfaceCandidateHex === null || standsApart(candidate, raisedSurfaceCandidateHex)),
255+
) ?? standardAccentHex;
256+
accentColor = parseVsCodeColor(accentHex)!;
257+
}
223258

224259
// The derived palette is the floor: every role starts contrast-solved, then
225260
// the theme's own workbench colors replace what it actually specified. The
226261
// floor derives from a muted accent -- the vivid engine carries the accent
227262
// hue into every surface, which washes an imported neutral palette (a gray
228263
// theme with a blue focusBorder would get blue code and text surfaces).
229-
const mutedAccentHex = accentColor
230-
? flattenOver({ r: accentColor.r, g: accentColor.g, b: accentColor.b, a: 0.2 }, canvas)
231-
: null;
232-
const derived = createVividThemeColors(appearance, canvasHex, mutedAccentHex ?? canvasHex);
264+
const mutedAccentHex = flattenOver(
265+
{ r: accentColor.r, g: accentColor.g, b: accentColor.b, a: 0.2 },
266+
canvas,
267+
);
268+
const derived = createVividThemeColors(appearance, canvasHex, mutedAccentHex);
233269
const sidebarHex =
234270
solidOver(canvas, "sideBar.background", "activityBar.background") ?? derived.sidebar;
235271
const sidebar = hexToRgb(sidebarHex);
@@ -256,6 +292,34 @@ export function parseVsCodeThemeFile(value: unknown): ThemeDefinition {
256292
return relativeLuminance(surfaceRgb) < 0.179 ? "#ffffff" : "#000000";
257293
};
258294

295+
const surfaceRaisedHex = raisedSurfaceCandidateHex ?? derived.surfaceRaised;
296+
// The checked switch track maps to messageAction, so resolve it before
297+
// choosing the input role used by the unchecked track.
298+
const buttonHex = solidOver(canvas, "button.background");
299+
const actionHex =
300+
buttonHex && standsApart(buttonHex, canvasHex) && standsApart(buttonHex, surfaceRaisedHex)
301+
? buttonHex
302+
: accentHex;
303+
const inputCandidates = [
304+
derived.input,
305+
derived.surfaceRaised,
306+
getStandardThemeColors(appearance).input,
307+
"#000000",
308+
"#ffffff",
309+
"#808080",
310+
];
311+
let inputHex =
312+
inputCandidates.find(
313+
(candidate) => standsApart(candidate, canvasHex) && standsApart(candidate, actionHex),
314+
) ?? "#808080";
315+
for (const key of ["input.background", "input.border"]) {
316+
const candidate = solidOver(canvas, key);
317+
if (candidate && standsApart(candidate, canvasHex) && standsApart(candidate, actionHex)) {
318+
inputHex = candidate;
319+
break;
320+
}
321+
}
322+
259323
const overrides: Partial<Record<ThemeColorRole, string>> = {
260324
canvas: canvasHex,
261325
text: readableOn(canvasHex, derived.text, "editor.foreground", "foreground"),
@@ -266,15 +330,14 @@ export function parseVsCodeThemeFile(value: unknown): ThemeDefinition {
266330
"disabledForeground",
267331
),
268332
surface: solidOver(canvas, "editorWidget.background") ?? derived.surface,
269-
surfaceRaised:
270-
solidOver(canvas, "editorWidget.background", "dropdown.background") ?? derived.surfaceRaised,
333+
surfaceRaised: surfaceRaisedHex,
271334
surfaceOverlay:
272335
solidOver(canvas, "menu.background", "quickInput.background", "dropdown.background") ??
273336
derived.surfaceOverlay,
274337
border:
275338
solidOver(canvas, "panel.border", "editorGroup.border", "contrastBorder") ?? derived.border,
276-
input: solidOver(canvas, "input.border", "dropdown.border") ?? derived.input,
277-
placeholder: readableOn(canvasHex, derived.placeholder, "input.placeholderForeground"),
339+
input: inputHex,
340+
placeholder: readableOn(surfaceRaisedHex, derived.placeholder, "input.placeholderForeground"),
278341
error: readableOn(canvasHex, derived.error, "editorError.foreground", "errorForeground"),
279342
warning: readableOn(canvasHex, derived.warning, "editorWarning.foreground"),
280343
accentSurface:
@@ -301,23 +364,15 @@ export function parseVsCodeThemeFile(value: unknown): ThemeDefinition {
301364
terminalScrollbar:
302365
solidOver(terminal, "scrollbarSlider.background") ?? derived.terminalScrollbar,
303366
};
304-
if (accentHex) {
305-
overrides.accent = accentHex;
306-
overrides.focus = accentHex;
307-
// The button pair is the closest thing VS Code has to our action color.
308-
const actionHex = solidOver(canvas, "button.background") ?? accentHex;
309-
overrides.messageAction = actionHex;
310-
overrides.messageActionForeground = readableOn(
311-
actionHex,
312-
derived.messageActionForeground,
313-
"button.foreground",
314-
);
315-
overrides.accentForeground = readableOn(
316-
accentHex,
317-
derived.accentForeground,
318-
"button.foreground",
319-
);
320-
}
367+
overrides.accent = accentHex;
368+
overrides.focus = accentHex;
369+
overrides.messageAction = actionHex;
370+
overrides.messageActionForeground = readableOn(
371+
actionHex,
372+
derived.messageActionForeground,
373+
"button.foreground",
374+
);
375+
overrides.accentForeground = readableOn(accentHex, derived.accentForeground, "button.foreground");
321376

322377
// Reuse the theme-file parser so ids, names, and color values go through the
323378
// same validation as a hand-written file.

0 commit comments

Comments
 (0)