mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
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>
58 lines
1.9 KiB
TypeScript
58 lines
1.9 KiB
TypeScript
/**
|
||
* 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));
|
||
}
|