theme logic fix for setting fallbacks in jetbrains

This commit is contained in:
Dallin Romney 2025-05-21 11:49:00 -07:00
parent 887bd1d028
commit f66f0980f2
3 changed files with 90 additions and 95 deletions

View file

@ -102,8 +102,8 @@ class GetTheme {
?: EditorColorsManager.getInstance().globalScheme.getAttributes(CodeInsightColors.WARNINGS_ATTRIBUTES)?.effectColor
?: namedColor("Component.warningForeground")
?: namedColor("Label.warningForeground")
val errorColor = namedColor("ValidationError.errorColor") ?: namedColor("Component.errorForeground") ?: namedColor("Label.errorForeground")
val accentColor = namedColor("Focus.defaultButtonBorderColor") ?: namedColor("Button.default.focusedBorderColor") ?: namedColor("Button.focusedBorderColor")
val editorBackground = editorScheme.defaultBackground

View file

@ -62,36 +62,39 @@ function ThemePage() {
return isJetBrains();
}, []);
const refreshJetbrainsColors = () => {
ideMessenger.request("jetbrains/getColors", undefined).then((result) => {
console.log(result);
if (result.status === "success") {
setDocumentStylesFromTheme(result.content);
}
});
const [missingVars, setMissingVars] = useState<string[]>([]);
const refreshColors = () => {
if (jetbrains) {
// Jetbrains: get colors and check which ones are missing from theme
void ideMessenger
.request("jetbrains/getColors", undefined)
.then((result) => {
console.log(result);
if (result.status === "success") {
const missingColors = setDocumentStylesFromTheme(result.content);
setMissingVars(missingColors);
}
});
} else {
// VS Code: find actual missing CSS variables
const notFound: string[] = [];
Object.entries(THEME_COLORS).forEach(([colorName, themeVals], idx) => {
let found = false;
themeVals.vars.forEach((cssVar) => {
const value = getComputedStyle(
document.documentElement,
).getPropertyValue(cssVar);
if (!value) {
notFound.push(`cssVar (choice #${idx + 1} for color ${colorName}`);
}
});
});
setMissingVars(notFound);
}
};
const [missingVars, setMissingVars] = useState<string[]>([]);
const checkMissingClasses = () => {
const missingVars: string[] = [];
Object.entries(THEME_COLORS).forEach(([colorName, themeVals]) => {
let found = false;
themeVals.vars.forEach((cssVar) => {
const value = getComputedStyle(
document.documentElement,
).getPropertyValue(cssVar);
if (value) {
found = true;
}
});
if (!found) {
missingVars.push(colorName);
}
});
setMissingVars(missingVars);
};
useEffect(() => {
checkMissingClasses();
refreshColors();
}, []);
return (
@ -185,37 +188,22 @@ function ThemePage() {
<div className="bg-accent h-3 w-3" />
</div>
</div>
<div className="flex flex-col gap-2">
<h2 className="mb-2 mt-6 text-xl font-semibold">
Missing Theme Variables
</h2>
{missingVars.length > 0 ? (
<div className="flex flex-col gap-1">
{missingVars.map((varName) => (
<div key={varName} className="text-error">
{varName}
</div>
))}
</div>
) : (
<div className="text-success">No missing variables</div>
)}
<Button onClick={checkMissingClasses}>
Re-check Missing Theme Vars
</Button>
</div>
<h2 className="mb-2 mt-6 text-xl font-semibold">Manage Theme</h2>
<h2 className="mb-2 text-lg font-semibold">Missing Theme Colors</h2>
{missingVars.length > 0 ? (
<div className="flex flex-col gap-1">
{missingVars.map((varName) => (
<div key={varName} className="">
{varName}
</div>
))}
</div>
) : (
<div className="">No missing variables</div>
)}
<Button onClick={refreshColors}>Refresh Missing Colors</Button>
{jetbrains ? (
<>
<h2 className="mb-2 mt-6 text-xl font-semibold">
Manage Jetbrains Theme
</h2>
<div className="flex flex-col flex-wrap gap-2">
<Button onClick={refreshJetbrainsColors}>
Refresh Jetbrains Theme
</Button>
<Button onClick={clearThemeLocalCache}>Clear Theme Cache</Button>
</div>
</>
<Button onClick={clearThemeLocalCache}>Clear Theme Cache</Button>
) : null}
<h2 className="mb-2 mt-6 text-xl font-semibold">All Theme Colors</h2>
<div className="grid grid-cols-3">

View file

@ -117,15 +117,23 @@ export const THEME_COLORS = {
default: "#ffffff", // white
},
success: {
vars: ["--vscode-notebookStatusSuccessIcon-foreground"], // "var(--vscode-testing-iconPassed, #1bbe84)" // --vscode-charts-green
vars: [
"--vscode-notebookStatusSuccessIcon-foreground",
"--vscode-testing-iconPassed",
"--vscode-gitDecoration-addedResourceForeground",
"--vscode-charts-green",
],
default: "#4caf50", // green
},
warning: {
vars: ["--vscode-editorWarning-foreground"], // --vscode-list-warningForeground
vars: [
"--vscode-editorWarning-foreground",
"--vscode-list-warningForeground",
],
default: "#ffb74d", // amber/yellow
},
error: {
vars: ["--vscode-editorError-foreground"], // --vscode-list-errorForeground
vars: ["--vscode-editorError-foreground", "--vscode-list-errorForeground"],
default: "#f44336", // red
},
link: {
@ -187,7 +195,7 @@ export const THEME_DEFAULTS = Object.entries(THEME_COLORS).reduce(
// Generates recursive CSS variable fallback for a given color name
// e.g. var(--vscode-button-background, var(--vscode-button-foreground, #ffffff))
export const getRecursiveVar = (vars: string[], defaultColor: string) => {
return vars.reverse().reduce((curr, varName) => {
return [...vars].reverse().reduce((curr, varName) => {
return `var(${varName}, ${curr})`;
}, defaultColor);
};
@ -203,44 +211,43 @@ export const varWithFallback = (colorName: keyof typeof THEME_COLORS) => {
export const setDocumentStylesFromTheme = (
theme: Record<string, string | undefined | null>,
) => {
// Check for missing theme items
const themeKeys = Object.keys(THEME_COLORS);
const missingKeys = themeKeys.filter(
(key) => !Object.keys(theme).includes(key),
);
if (missingKeys.length > 0) {
console.warn(
`Missing theme keys: ${missingKeys.join(", ")}. Please check theme`,
);
}
// Write each theme color to the document
// Check for extraneous theme items
Object.entries(theme).forEach(([colorName, value]) => {
if (value) {
const themeVals = THEME_COLORS[colorName as keyof typeof THEME_COLORS];
if (!themeVals) {
console.warn(
`Receieved theme color ${colorName} which is not used by the theme`,
);
return;
}
// Remove alpha channel from all hex colors (seems to cause bad colors)
let colorVal = value;
if (colorVal.startsWith("#") && value.length > 7) {
colorVal = colorVal.slice(0, 7);
}
localStorage.setItem(colorName, colorVal);
for (const cssVar of themeVals.vars) {
document.body.style.setProperty(cssVar, colorVal);
document.documentElement.style.setProperty(cssVar, colorVal);
}
} else {
const themeVals = THEME_COLORS[colorName as keyof typeof THEME_COLORS];
if (!themeVals) {
console.warn(
`Theme color ${colorName} is undefined or null. Please check theme. Falling back to defaults.`,
`Receieved theme color ${colorName} which is not used by the theme`,
);
return;
}
});
// Write theme values to document
const missingColors: string[] = [];
Object.entries(THEME_COLORS).forEach(([colorName, settings]) => {
let colorVal = settings.default;
const newColor = theme[colorName];
if (newColor) {
colorVal = newColor;
// Remove alpha channel from all hex colors (seems to cause bad colors)
if (newColor.startsWith("#") && newColor.length > 7) {
colorVal = colorVal.slice(0, 7);
}
} else {
missingColors.push(colorName);
// console.warn(
// `Missing theme color: ${colorName}. Falling back to default ${colorVal}`,
// );
}
localStorage.setItem(colorName, colorVal);
for (const cssVar of settings.vars) {
document.body.style.setProperty(cssVar, colorVal);
document.documentElement.style.setProperty(cssVar, colorVal);
}
});
return missingColors;
};
export const setDocumentStylesFromLocalStorage = (checkCache: boolean) => {