import { useState } from "react"; import { create } from "zustand"; import { useTx } from "@/platform/i18n"; import { Popup } from "@/ui"; import { projectColorIndex, projectColorSlotVars } from "./projectColor"; import { setTabColorOverride, tabColorOverride } from "./tabColors"; import styles from "./TabColorPicker.module.css"; export interface TabColorTarget { root: string; name: string; x: number; y: number; } export const useTabColorPicker = create<{ target: TabColorTarget | null }>(() => ({ target: null })); export function openTabColorPicker(target: TabColorTarget): void { useTabColorPicker.setState({ target }); } /** Where the pointer last went down: a menu item's picker opens where the item was. */ let lastPointer: { x: number; y: number } | null = null; if (typeof window !== "undefined") window.addEventListener("pointerdown", (e) => (lastPointer = { x: e.clientX, y: e.clientY }), { capture: true }); /** Change Project Color… from a menu: the picker opens at the chosen item (the window centre from the keyboard). */ export function openProjectColorPicker(root: string, name: string): void { const at = lastPointer ?? { x: window.innerWidth / 2 - 110, y: window.innerHeight / 3 }; openTabColorPicker({ root, name, ...at }); } const PRESETS = [ { slot: "1", label: "Orange" }, { slot: "2", label: "Yellow" }, { slot: "3", label: "Olive" }, { slot: "4", label: "Cyan" }, { slot: "5", label: "Blue" }, { slot: "6", label: "Rose" }, { slot: "7", label: "Violet" }, { slot: "8", label: "Teal" }, { slot: "9", label: "Green" }, ]; const HEX_COLOR = /^#[0-9a-f]{6}$/; export function TabColorPicker() { const target = useTabColorPicker((s) => s.target); if (!target) return null; return ; } function Panel({ target }: { target: TabColorTarget }) { const tx = useTx(); const [override, setOverride] = useState(() => tabColorOverride(target.root)); const [hex, setHex] = useState(override?.startsWith("#") ? override : ""); const close = () => useTabColorPicker.setState({ target: null }); const apply = (value: string | null) => { setTabColorOverride(target.root, value); setOverride(value); }; const custom = override !== null && override.startsWith("#"); const current = override ?? String(projectColorIndex(target.name)); return ( {PRESETS.map((p, i) => { const on = current === p.slot; return ( { apply(p.slot); close(); }} > ); })} apply(e.target.value)} /> { setHex(e.target.value); if (HEX_COLOR.test(e.target.value)) apply(e.target.value.toLowerCase()); }} /> { apply(null); close(); }} > {tx("Reset to Default")} ); }