Phase 5a-3. The owner chose candidate B of each palette. - Menus are opaque (UI-043, replaces UI-013). With a background image, menus are at least 92% opaque and blur the image. Submenus show as manual popovers in the top layer, so the blur cannot clip them. - In a light palette, the page is the lightest surface, and the side panes have a slight tint (UI-044). - RuneStone uses neutral surfaces and the rune cyan of the app icon. Claude Dark uses the layout of the Claude app. Claude Light uses a white page with brand Light panes and a darker orange for text. Sakura uses Petal Veil for active rows. - All five core palettes pass every readability rule. A text role keeps the hue of its source and changes only its lightness. - The accent-text rule also checks accent-2, because inline code in Read uses it as text. - The root token defaults match RuneStone Dark. pnpm check passes. pnpm test: 447 pass, 5 todo, 0 fail. Full smoke suite: 92 passed, 2 known. editor/markdownPaste and tables/formulas failed one time and passed on retry. workspace/manageTemplates fails about half of its runs, also on the previous commit: the tree menu rebuilds when the template list reloads, and the open submenu closes.
129 lines
7.0 KiB
TypeScript
129 lines
7.0 KiB
TypeScript
import type { BackgroundFrame } from './backgroundContracts.ts';
|
|
|
|
const positions = ['center', 'top left', 'top', 'top right', 'left', 'right', 'bottom left', 'bottom', 'bottom right'];
|
|
const properties = ['--rs-background-overlay', '--rs-background-image', '--rs-background-position', '--rs-background-size', '--rs-background-layer-sizes', '--rs-background-layer-positions', '--rs-background-previous-layer-sizes', '--rs-background-previous-layer-positions', '--rs-background-previous-image', '--rs-background-previous-position', '--rs-background-previous-size', '--rs-background-transition-image', '--rs-workarea-sidebar-bg', '--rs-workarea-editor-bg', '--rs-workarea-surface-opacity', '--rs-workarea-plugin-bg', '--rs-menu-bg', '--rs-menu-active-bg', '--rs-menu-backdrop'] as const;
|
|
|
|
function validImage(frame: BackgroundFrame) {
|
|
return frame && typeof frame.url === 'string' && /^blob:[^\s"'\\()]+$/.test(frame.url)
|
|
&& Number.isFinite(frame.overlay) && frame.overlay >= -1 && frame.overlay <= 1
|
|
&& (frame.blurredUrl === undefined || (typeof frame.blurredUrl === 'string' && /^blob:[^\s"'\\()]+$/.test(frame.blurredUrl)))
|
|
&& positions.includes(frame.position) && ['cover', 'contain', 'auto'].includes(frame.size);
|
|
}
|
|
|
|
function image(frame: BackgroundFrame) {
|
|
const shade = frame.overlay < 0 ? '255,255,255' : '0,0,0';
|
|
const overlay = `linear-gradient(rgba(${shade},${Math.abs(frame.overlay)}),rgba(${shade},${Math.abs(frame.overlay)}))`;
|
|
return { overlay, image: `${overlay}, url("${frame.url}")${frame.blurredUrl ? `, url("${frame.blurredUrl}")` : ''}`,
|
|
sizes: `100% 100%, ${frame.size}${frame.blurredUrl ? ', cover' : ''}`,
|
|
positions: `center, ${frame.position}${frame.blurredUrl ? ', center' : ''}` };
|
|
}
|
|
|
|
function copy(frame: BackgroundFrame): BackgroundFrame {
|
|
return { ...frame, ...(frame.previous ? { previous: { ...frame.previous } } : {}) };
|
|
}
|
|
|
|
function checkTarget(target: string) {
|
|
if (target !== 'app' && target !== 'pm-all' && !/^pm-board:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(target)) {
|
|
throw new Error('Invalid background target.');
|
|
}
|
|
}
|
|
|
|
/** Each window retains temporary frames without changing saved appearance settings. */
|
|
export class BackgroundHub {
|
|
private frames = new Map<string, Map<symbol, BackgroundFrame>>();
|
|
private listeners = new Set<(target: string) => void>();
|
|
private restore?: () => void;
|
|
private body: () => HTMLElement | undefined;
|
|
|
|
constructor(body: () => HTMLElement | undefined = () => typeof document === 'undefined' ? undefined : document.body) {
|
|
this.body = body;
|
|
}
|
|
|
|
get(target: string): BackgroundFrame | null {
|
|
checkTarget(target);
|
|
const frame = [...(this.frames.get(target)?.values() ?? [])].at(-1);
|
|
return frame ? copy(frame) : null;
|
|
}
|
|
|
|
set(owner: symbol, target: string, frame: BackgroundFrame | null) {
|
|
checkTarget(target);
|
|
if (frame !== null && (!validImage(frame)
|
|
|| (frame.previous !== undefined && (!validImage(frame.previous) || 'previous' in frame.previous || 'transitionMs' in frame.previous))
|
|
|| (frame.transitionMs !== undefined && (!Number.isFinite(frame.transitionMs) || frame.transitionMs < 0 || frame.transitionMs > 5000)))) {
|
|
throw new Error('Invalid background frame. Use a blob URL and an overlay from -1 to 1.');
|
|
}
|
|
const owners = this.frames.get(target) ?? new Map<symbol, BackgroundFrame>();
|
|
const removed = owners.delete(owner);
|
|
if (frame) owners.set(owner, copy(frame));
|
|
if (owners.size) this.frames.set(target, owners);
|
|
else this.frames.delete(target);
|
|
if (frame || removed) this.changed(target);
|
|
}
|
|
|
|
removeOwner(owner: symbol) {
|
|
for (const [target, owners] of this.frames) {
|
|
if (!owners.delete(owner)) continue;
|
|
if (!owners.size) this.frames.delete(target);
|
|
this.changed(target);
|
|
}
|
|
}
|
|
|
|
subscribe(listener: (target: string) => void): () => void {
|
|
this.listeners.add(listener);
|
|
return () => { this.listeners.delete(listener); };
|
|
}
|
|
|
|
private changed(target: string) {
|
|
if (target === 'app') this.applyApp();
|
|
for (const listener of [...this.listeners]) {
|
|
try { listener(target); } catch { /* A subscriber must not prevent cleanup or other subscribers. */ }
|
|
}
|
|
}
|
|
|
|
private applyApp() {
|
|
const frame = this.get('app');
|
|
if (!frame) { this.restore?.(); this.restore = undefined; return; }
|
|
const body = this.body();
|
|
if (!body) return;
|
|
if (!this.restore) {
|
|
const previous = properties.map(key => [key, body.style.getPropertyValue(key), body.style.getPropertyPriority(key)]);
|
|
this.restore = () => {
|
|
for (const [key, value, priority] of previous) {
|
|
if (value) body.style.setProperty(key, value, priority);
|
|
else body.style.removeProperty(key);
|
|
}
|
|
};
|
|
}
|
|
const { overlay, image: currentImage, sizes, positions: layerPositions } = image(frame);
|
|
body.style.setProperty('--rs-background-overlay', overlay);
|
|
body.style.setProperty('--rs-background-image', currentImage);
|
|
body.style.setProperty('--rs-background-position', frame.position);
|
|
body.style.setProperty('--rs-background-size', frame.size);
|
|
body.style.setProperty('--rs-background-layer-sizes', sizes);
|
|
body.style.setProperty('--rs-background-layer-positions', layerPositions);
|
|
body.style.setProperty('--rs-background-previous-layer-sizes', frame.previous ? image(frame.previous).sizes : sizes);
|
|
body.style.setProperty('--rs-background-previous-layer-positions', frame.previous ? image(frame.previous).positions : layerPositions);
|
|
body.style.setProperty('--rs-workarea-sidebar-bg', 'var(--rs-image-sidebar-bg)');
|
|
body.style.setProperty('--rs-workarea-editor-bg', 'var(--rs-image-editor-bg)');
|
|
body.style.setProperty('--rs-workarea-surface-opacity', 'var(--rs-surface-opacity, 82%)');
|
|
body.style.setProperty('--rs-workarea-plugin-bg', 'var(--rs-transparent-plugin-bg, transparent)');
|
|
body.style.setProperty('--rs-menu-bg', 'var(--rs-image-menu-bg)');
|
|
body.style.setProperty('--rs-menu-active-bg', 'var(--rs-image-menu-active-bg)');
|
|
body.style.setProperty('--rs-menu-backdrop', 'var(--rs-image-menu-backdrop)');
|
|
body.style.setProperty('--rs-background-transition-image', frame.previous ? currentImage : 'none');
|
|
body.style.setProperty('--rs-background-previous-image', frame.previous ? image(frame.previous).image : 'none');
|
|
body.style.setProperty('--rs-background-previous-position', frame.previous?.position ?? frame.position);
|
|
body.style.setProperty('--rs-background-previous-size', frame.previous?.size ?? frame.size);
|
|
if (typeof document !== 'undefined') {
|
|
for (const element of body.querySelectorAll('.workarea-background')) {
|
|
for (const animation of element.getAnimations()) animation.cancel();
|
|
if (frame.previous && frame.transitionMs && !window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
|
|
element.animate([{ opacity: 0 }, { opacity: 1 }], { duration: frame.transitionMs, easing: 'ease-in-out', pseudoElement: '::after' });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
export const backgroundHub = new BackgroundHub();
|