Files
openidea/src/workbench/tabColors.ts
T
wangfeiandhapput 15fcf4f2c1 feat(workbench): custom colours for project tabs (#4481)
Right-clicking a project tab in a window group offers Change Project Color…: nine preset colours from the project badge palette, a custom colour and hex field, and Reset to Default. The colour is stored per project root, syncs across windows, and tints the tab, the title bar project badge and the project's badge in Recent Projects and the project popup. Without a choice the tab keeps its plain surface.

Co-authored-by: happut <2594794+happut@users.noreply.github.com>
2026-11-19 19:20:38 +08:00

58 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Per-project tab colour overrides, persisted per project root in localStorage
* (like the tool window layout). A value is a preset slot "1"–"9" or "#rrggbb";
* without one the tab keeps the name-hashed project colour.
*/
import { PROJECT_COLOR_COUNT, projectColorSlotVars, projectColorVars } from "./projectColor";
const KEY_PREFIX = "openidea.tabColor.v1:";
const HEX_COLOR = /^#[0-9a-f]{6}$/;
function valid(value: string | null): string | null {
if (value === null) return null;
if (HEX_COLOR.test(value)) return value;
const slot = Number(value);
return Number.isInteger(slot) && slot >= 1 && slot <= PROJECT_COLOR_COUNT ? value : null;
}
export function tabColorOverride(root: string): string | null {
return valid(localStorage.getItem(KEY_PREFIX + root));
}
export function setTabColorOverride(root: string, value: string | null): void {
if (value === null) localStorage.removeItem(KEY_PREFIX + root);
else localStorage.setItem(KEY_PREFIX + root, value.toLowerCase());
emit();
}
const listeners = new Set<() => void>();
let version = 0;
function emit() {
version++;
for (const listener of listeners) listener();
}
/** Bumped on every local or cross-window change; a useSyncExternalStore snapshot. */
export function tabColorsVersion(): number {
return version;
}
export function subscribeTabColors(listener: () => void): () => void {
listeners.add(listener);
const onStorage = (e: StorageEvent) => {
if (e.key === null || e.key.startsWith(KEY_PREFIX)) emit();
};
window.addEventListener("storage", onStorage);
return () => {
listeners.delete(listener);
window.removeEventListener("storage", onStorage);
};
}
export function tabColorVars(name: string, override: string | null): Record<string, string> {
if (override === null) return projectColorVars(name);
if (override.startsWith("#")) return { "--project-color": override, "--project-color-end": override };
return projectColorSlotVars(Number(override));
}