From d40c803ed95fe0f9de347822010bdb86398e0be5 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Sat, 18 Jul 2026 23:38:43 +0800 Subject: [PATCH] feat(webui): add adaptive pointer interactions Introduce a CSS-driven adaptive cursor that morphs for controls, text editing, dragging, resizing, busy states, and other native pointer semantics on desktop devices. Add a themed context menu for editing, selections, links, images, and page navigation while preserving explicit native-menu escape paths. Store a default-on interaction preference through the normalized appearance model and cover its migration and opt-out behavior. --- packages/core/src/webui/ui-config.ts | 4 + packages/core/tests/webui-ui-config.test.ts | Bin 10197 -> 10549 bytes packages/webui/src/App.tsx | 4 + .../src/components/ui/adaptive-pointer.tsx | 304 +++++++++++ .../src/components/ui/global-context-menu.tsx | 510 +++++++++++++++++ .../webui/src/components/ui/scroll-area.tsx | 2 +- packages/webui/src/contexts/ThemeContext.tsx | 1 + packages/webui/src/index.css | 513 ++++++++++++++++++ packages/webui/src/types.ts | 2 + 9 files changed, 1339 insertions(+), 1 deletion(-) create mode 100644 packages/webui/src/components/ui/adaptive-pointer.tsx create mode 100644 packages/webui/src/components/ui/global-context-menu.tsx diff --git a/packages/core/src/webui/ui-config.ts b/packages/core/src/webui/ui-config.ts index b8830ac2..62e5264a 100644 --- a/packages/core/src/webui/ui-config.ts +++ b/packages/core/src/webui/ui-config.ts @@ -100,6 +100,8 @@ export interface UiAppearance { reduceMotion: boolean; /** Hard-disable all UI animations (stronger than reduceMotion). */ disableMotion: boolean; + /** Use SnowLuma's adaptive cursor and custom context menu. */ + customPointerSystem: boolean; highContrast: boolean; sidebarPinned: boolean; timeFormat: TimeFormat; @@ -226,6 +228,7 @@ const DEFAULT_APPEARANCE: UiAppearance = { density: 'cozy', reduceMotion: false, disableMotion: false, + customPointerSystem: true, highContrast: false, sidebarPinned: false, timeFormat: '24h', @@ -520,6 +523,7 @@ export function normalizeAppearance(value: unknown, imageState: ServerImageState density: oneOf(v.density, ['cozy', 'compact'], DEFAULT_APPEARANCE.density), reduceMotion: boolOr(v.reduceMotion, DEFAULT_APPEARANCE.reduceMotion), disableMotion: boolOr(v.disableMotion, DEFAULT_APPEARANCE.disableMotion), + customPointerSystem: boolOr(v.customPointerSystem, DEFAULT_APPEARANCE.customPointerSystem), highContrast: boolOr(v.highContrast, DEFAULT_APPEARANCE.highContrast), sidebarPinned: boolOr(v.sidebarPinned, DEFAULT_APPEARANCE.sidebarPinned), timeFormat: oneOf(v.timeFormat, ['12h', '24h'], DEFAULT_APPEARANCE.timeFormat), diff --git a/packages/core/tests/webui-ui-config.test.ts b/packages/core/tests/webui-ui-config.test.ts index eb614578840b0f2c03ac5dd2124fb29b8cb6424a..66b547362c135e675ac7939c32e4bcca5f0c0143 100644 GIT binary patch delta 286 zcmccWzcpxsHfso%fKd?Nn%2Oes0ETB3;<>_Wm*6L delta 33 mcmdlQbk%=@HtS?BE{)A`Y?X?WpK|k0=F<4YQLAaq1;PN)@(S|+ diff --git a/packages/webui/src/App.tsx b/packages/webui/src/App.tsx index 588ba12e..f7adae88 100644 --- a/packages/webui/src/App.tsx +++ b/packages/webui/src/App.tsx @@ -9,12 +9,16 @@ import { ConsentPage } from '@/components/pages/consent-page'; import { ApiProvider, createApiClient, useApi, type ApiClient } from '@/lib/api'; import { DebugTaskProvider } from '@/contexts/DebugTaskContext'; import { TaskBadge } from '@/components/debug/task-badge'; +import { AdaptivePointer } from '@/components/ui/adaptive-pointer'; +import { GlobalContextMenu } from '@/components/ui/global-context-menu'; import type { AgreementsPayload } from '@/lib/api/types'; import { appRouter } from '@/router'; export default function App() { return ( + + ); diff --git a/packages/webui/src/components/ui/adaptive-pointer.tsx b/packages/webui/src/components/ui/adaptive-pointer.tsx new file mode 100644 index 00000000..9cbbb1c7 --- /dev/null +++ b/packages/webui/src/components/ui/adaptive-pointer.tsx @@ -0,0 +1,304 @@ +import * as React from 'react'; +import { + motion, + useMotionValue, + useReducedMotion, + useSpring, +} from 'motion/react'; +import { useTheme } from '@/contexts/ThemeContext'; + +const POINTER_MEDIA = '(hover: hover) and (pointer: fine)'; +const MODE_PROPERTY = '--snowluma-cursor-mode'; +const RESIZE_MODE_PROPERTY = '--snowluma-cursor-resize-mode'; +const POINTER_SPRING = { stiffness: 1000, damping: 50 } as const; + +const CURSOR_MODES = [ + 'default', + 'target', + 'text', + 'grab', + 'grabbing', + 'move', + 'ew-resize', + 'ns-resize', + 'nwse-resize', + 'nesw-resize', + 'not-allowed', + 'wait', + 'crosshair', + 'copy', + 'zoom-in', + 'zoom-out', + 'help', + 'hidden', +] as const; + +type CursorMode = (typeof CURSOR_MODES)[number]; + +interface CursorContext { + mode: CursorMode; + anchor: Element | null; + tracksPointerRegion: boolean; +} + +function useMediaQuery(query: string): boolean { + const [matches, setMatches] = React.useState(false); + + React.useEffect(() => { + const media = window.matchMedia(query); + const update = () => setMatches(media.matches); + update(); + media.addEventListener('change', update); + return () => media.removeEventListener('change', update); + }, [query]); + + return matches; +} + +function readCursorMode(element: Element): CursorMode { + const value = getComputedStyle(element).getPropertyValue(MODE_PROPERTY).trim(); + return (CURSOR_MODES as readonly string[]).includes(value) + ? value as CursorMode + : 'default'; +} + +/** + * CSS owns the semantic mapping. JavaScript only reads the resolved custom + * property and finds the outermost element that still belongs to that mode. + * This keeps nested SVGs/spans inside a button attached to the whole button. + */ +function resolveCursorContext( + target: EventTarget | null, + clientX: number, + clientY: number, +): CursorContext { + if (!(target instanceof Element)) { + return { mode: 'default', anchor: null, tracksPointerRegion: false }; + } + + const styles = getComputedStyle(target); + const resizeModeValue = styles.getPropertyValue(RESIZE_MODE_PROPERTY).trim(); + const resizeMode = (CURSOR_MODES as readonly string[]).includes(resizeModeValue) + ? resizeModeValue as CursorMode + : null; + + if (resizeMode) { + const rect = target.getBoundingClientRect(); + const inResizeCorner = clientX >= rect.right - 18 + && clientX <= rect.right + && clientY >= rect.bottom - 18 + && clientY <= rect.bottom; + if (inResizeCorner) { + return { mode: resizeMode, anchor: null, tracksPointerRegion: true }; + } + } + + const modeValue = styles.getPropertyValue(MODE_PROPERTY).trim(); + const mode = (CURSOR_MODES as readonly string[]).includes(modeValue) + ? modeValue as CursorMode + : 'default'; + if (mode !== 'target') { + return { mode, anchor: null, tracksPointerRegion: Boolean(resizeMode) }; + } + + let anchor = target; + let parent = target.parentElement; + while (parent && readCursorMode(parent) === mode) { + anchor = parent; + parent = parent.parentElement; + } + return { mode, anchor, tracksPointerRegion: Boolean(resizeMode) }; +} + +function clamp(value: number, min: number, max: number): number { + return Math.min(max, Math.max(min, value)); +} + +/** + * Global adaptive cursor for fine-pointer desktop environments. + * + * All cursor semantics and visual variants live in index.css. The component + * only tracks the pointer, reads the CSS-selected mode, and supplies target + * geometry for Mikk Martin-style target morphing. + */ +export function AdaptivePointer() { + const { appearance } = useTheme(); + const systemReducedMotion = useReducedMotion(); + const finePointer = useMediaQuery(POINTER_MEDIA); + const enabled = appearance.customPointerSystem + && finePointer + && !appearance.reduceMotion + && !appearance.disableMotion + && !systemReducedMotion; + + const cursorRef = React.useRef(null); + const rawX = useMotionValue(-100); + const rawY = useMotionValue(-100); + const x = useSpring(rawX, POINTER_SPRING); + const y = useSpring(rawY, POINTER_SPRING); + + React.useEffect(() => { + const cursor = cursorRef.current; + if (!enabled || !cursor) return; + + const root = document.documentElement; + const lastPoint = { x: -100, y: -100 }; + let context: CursorContext = { mode: 'default', anchor: null, tracksPointerRegion: false }; + let lastEventTarget: EventTarget | null = null; + let targetObserver: ResizeObserver | null = null; + + root.dataset.cursorSystem = 'custom'; + cursor.dataset.visible = 'false'; + + const stopObservingTarget = () => { + targetObserver?.disconnect(); + targetObserver = null; + }; + + const setMode = (mode: CursorMode) => { + cursor.dataset.mode = mode; + if (mode !== 'target') { + cursor.style.removeProperty('--cursor-target-width'); + cursor.style.removeProperty('--cursor-target-height'); + cursor.style.removeProperty('--cursor-target-radius'); + } + }; + + const positionTarget = () => { + const anchor = context.anchor; + if (context.mode !== 'target' || !anchor?.isConnected) return false; + + const rect = anchor.getBoundingClientRect(); + if (rect.width <= 0 || rect.height <= 0) return false; + + const centerX = rect.left + rect.width / 2; + const centerY = rect.top + rect.height / 2; + const magneticX = clamp((lastPoint.x - centerX) / Math.max(rect.width / 2, 1), -1, 1) * 10; + const magneticY = clamp((lastPoint.y - centerY) / Math.max(rect.height / 2, 1), -1, 1) * 10; + const radius = getComputedStyle(anchor).borderRadius || '0.75rem'; + + cursor.style.setProperty('--cursor-target-width', `${rect.width}px`); + cursor.style.setProperty('--cursor-target-height', `${rect.height}px`); + cursor.style.setProperty('--cursor-target-radius', radius); + rawX.set(centerX + magneticX); + rawY.set(centerY + magneticY); + return true; + }; + + const observeTarget = (anchor: Element | null) => { + stopObservingTarget(); + if (!anchor) return; + targetObserver = new ResizeObserver(positionTarget); + targetObserver.observe(anchor); + }; + + const applyContext = (next: CursorContext) => { + const targetChanged = next.anchor !== context.anchor; + context = next; + setMode(next.mode); + if (targetChanged) observeTarget(next.mode === 'target' ? next.anchor : null); + }; + + const positionAtPointer = () => { + if (context.mode === 'target' && positionTarget()) return; + rawX.set(lastPoint.x); + rawY.set(lastPoint.y); + }; + + const refreshContext = (target: EventTarget | null) => { + lastEventTarget = target; + applyContext(resolveCursorContext(target, lastPoint.x, lastPoint.y)); + positionAtPointer(); + }; + + const onPointerMove = (event: PointerEvent) => { + if (event.pointerType === 'touch') return; + lastPoint.x = event.clientX; + lastPoint.y = event.clientY; + cursor.dataset.visible = 'true'; + if (event.target !== lastEventTarget || context.tracksPointerRegion) refreshContext(event.target); + else positionAtPointer(); + }; + + const onPointerOver = (event: PointerEvent) => { + if (event.pointerType === 'touch') return; + lastPoint.x = event.clientX; + lastPoint.y = event.clientY; + cursor.dataset.visible = 'true'; + refreshContext(event.target); + }; + + const onPointerOut = (event: PointerEvent) => { + if (event.relatedTarget === null) cursor.dataset.visible = 'false'; + }; + + const onPointerDown = (event: PointerEvent) => { + if (event.pointerType === 'touch') return; + cursor.dataset.pressed = 'true'; + refreshContext(event.target); + if (context.mode === 'grab') setMode('grabbing'); + }; + + const restorePointerContext = () => { + cursor.dataset.pressed = 'false'; + const target = document.elementFromPoint(lastPoint.x, lastPoint.y); + refreshContext(target); + }; + + const onDragStart = () => { + cursor.dataset.pressed = 'true'; + setMode('grabbing'); + }; + + const onViewportChange = () => positionTarget(); + const hide = () => { cursor.dataset.visible = 'false'; }; + const onVisibilityChange = () => { + if (document.visibilityState !== 'visible') hide(); + }; + + window.addEventListener('pointermove', onPointerMove, { passive: true }); + window.addEventListener('pointerover', onPointerOver, { passive: true }); + window.addEventListener('pointerout', onPointerOut, { passive: true }); + window.addEventListener('pointerdown', onPointerDown, { capture: true, passive: true }); + window.addEventListener('pointerup', restorePointerContext, { capture: true, passive: true }); + window.addEventListener('pointercancel', restorePointerContext, { capture: true, passive: true }); + window.addEventListener('dragstart', onDragStart, { capture: true }); + window.addEventListener('dragend', restorePointerContext, { capture: true }); + window.addEventListener('scroll', onViewportChange, { capture: true, passive: true }); + window.addEventListener('resize', onViewportChange, { passive: true }); + window.addEventListener('blur', hide); + document.addEventListener('visibilitychange', onVisibilityChange); + + return () => { + stopObservingTarget(); + delete root.dataset.cursorSystem; + cursor.dataset.visible = 'false'; + window.removeEventListener('pointermove', onPointerMove); + window.removeEventListener('pointerover', onPointerOver); + window.removeEventListener('pointerout', onPointerOut); + window.removeEventListener('pointerdown', onPointerDown, { capture: true }); + window.removeEventListener('pointerup', restorePointerContext, { capture: true }); + window.removeEventListener('pointercancel', restorePointerContext, { capture: true }); + window.removeEventListener('dragstart', onDragStart, { capture: true }); + window.removeEventListener('dragend', restorePointerContext, { capture: true }); + window.removeEventListener('scroll', onViewportChange, { capture: true }); + window.removeEventListener('resize', onViewportChange); + window.removeEventListener('blur', hide); + document.removeEventListener('visibilitychange', onVisibilityChange); + }; + }, [enabled, rawX, rawY]); + + return ( + + ); +} diff --git a/packages/webui/src/components/ui/global-context-menu.tsx b/packages/webui/src/components/ui/global-context-menu.tsx new file mode 100644 index 00000000..61703a9c --- /dev/null +++ b/packages/webui/src/components/ui/global-context-menu.tsx @@ -0,0 +1,510 @@ +import * as React from 'react'; +import { createPortal } from 'react-dom'; +import { AnimatePresence, motion } from 'motion/react'; +import { + ArrowLeft, + ClipboardPaste, + Copy, + ExternalLink, + Image as ImageIcon, + Link2, + RefreshCw, + Scissors, + TextSelect, + type LucideIcon, +} from 'lucide-react'; +import { useTheme } from '@/contexts/ThemeContext'; + +type TextEditor = HTMLInputElement | HTMLTextAreaElement | HTMLElement; + +interface SelectionSnapshot { + editor: TextEditor | null; + editorStart: number | null; + editorEnd: number | null; + range: Range | null; + selectedText: string; +} + +interface ContextSnapshot extends SelectionSnapshot { + target: Element; + link: HTMLAnchorElement | null; + image: HTMLImageElement | null; +} + +interface MenuState { + id: number; + x: number; + y: number; + keyboard: boolean; + snapshot: ContextSnapshot; +} + +interface MenuAction { + id: string; + label: string; + icon: LucideIcon; + shortcut?: string; + disabled?: boolean; + disabledReason?: string; + run: () => void | Promise; +} + +const TEXT_INPUT_SELECTOR = [ + 'input:not([type])', + 'input[type="text"]', + 'input[type="search"]', + 'input[type="url"]', + 'input[type="tel"]', + 'input[type="password"]', + 'input[type="email"]', + 'textarea', + '[contenteditable]:not([contenteditable="false"])', +].join(','); + +function clamp(value: number, min: number, max: number): number { + return Math.min(max, Math.max(min, value)); +} + +function findEditor(target: Element): TextEditor | null { + const candidate = target.closest(TEXT_INPUT_SELECTOR); + if ( + candidate instanceof HTMLInputElement + || candidate instanceof HTMLTextAreaElement + || (candidate instanceof HTMLElement && candidate.isContentEditable) + ) return candidate; + return null; +} + +function captureSelection(target: Element): SelectionSnapshot { + const editor = findEditor(target); + if (editor instanceof HTMLInputElement || editor instanceof HTMLTextAreaElement) { + const start = editor.selectionStart; + const end = editor.selectionEnd; + return { + editor, + editorStart: start, + editorEnd: end, + range: null, + selectedText: start !== null && end !== null ? editor.value.slice(start, end) : '', + }; + } + + const selection = window.getSelection(); + const range = selection?.rangeCount ? selection.getRangeAt(0).cloneRange() : null; + const selectionInsideEditor = editor instanceof HTMLElement + && range !== null + && editor.contains(range.commonAncestorContainer); + + return { + editor, + editorStart: null, + editorEnd: null, + range: selectionInsideEditor ? range : null, + selectedText: editor instanceof HTMLElement && !selectionInsideEditor + ? '' + : selection?.toString() ?? '', + }; +} + +function restoreRange(snapshot: SelectionSnapshot): Range { + if (!(snapshot.editor instanceof HTMLElement) || !snapshot.range) { + throw new Error('无法恢复文本选区'); + } + snapshot.editor.focus({ preventScroll: true }); + const range = snapshot.range.cloneRange(); + const selection = window.getSelection(); + if (!selection) throw new Error('浏览器未提供文本选区'); + selection.removeAllRanges(); + selection.addRange(range); + return range; +} + +function emitInput(target: TextEditor, inputType: string, data: string | null) { + target.dispatchEvent(new InputEvent('input', { + bubbles: true, + composed: true, + inputType, + data, + })); +} + +function replaceSelection(snapshot: SelectionSnapshot, value: string, inputType: string) { + const { editor } = snapshot; + if (editor instanceof HTMLInputElement || editor instanceof HTMLTextAreaElement) { + if (snapshot.editorStart === null || snapshot.editorEnd === null) { + throw new Error('当前输入框不支持选区替换'); + } + editor.focus({ preventScroll: true }); + editor.setRangeText(value, snapshot.editorStart, snapshot.editorEnd, 'end'); + emitInput(editor, inputType, value || null); + return; + } + + if (!(editor instanceof HTMLElement)) throw new Error('缺少可编辑文本目标'); + const range = restoreRange(snapshot); + range.deleteContents(); + if (value) { + const text = document.createTextNode(value); + range.insertNode(text); + range.setStartAfter(text); + } + range.collapse(true); + const selection = window.getSelection(); + selection?.removeAllRanges(); + selection?.addRange(range); + emitInput(editor, inputType, value || null); +} + +function selectEditorContents(editor: TextEditor) { + editor.focus({ preventScroll: true }); + if (editor instanceof HTMLInputElement || editor instanceof HTMLTextAreaElement) { + editor.select(); + return; + } + const range = document.createRange(); + range.selectNodeContents(editor); + const selection = window.getSelection(); + if (!selection) throw new Error('浏览器未提供文本选区'); + selection.removeAllRanges(); + selection.addRange(range); +} + +function legacyCopy(value: string): boolean { + const active = document.activeElement instanceof HTMLElement ? document.activeElement : null; + const selection = window.getSelection(); + const ranges = selection + ? Array.from({ length: selection.rangeCount }, (_, index) => selection.getRangeAt(index).cloneRange()) + : []; + const textarea = document.createElement('textarea'); + textarea.value = value; + textarea.setAttribute('readonly', ''); + textarea.style.position = 'fixed'; + textarea.style.left = '-9999px'; + textarea.style.opacity = '0'; + document.body.appendChild(textarea); + textarea.select(); + + try { + const exec = (document as unknown as { execCommand(command: string): boolean }).execCommand; + return exec.call(document, 'copy'); + } finally { + textarea.remove(); + active?.focus({ preventScroll: true }); + if (selection) { + selection.removeAllRanges(); + ranges.forEach((range) => selection.addRange(range)); + } + } +} + +async function writeClipboard(value: string) { + let clipboardError: unknown; + try { + if (!navigator.clipboard?.writeText) throw new Error('Clipboard API unavailable'); + await navigator.clipboard.writeText(value); + return; + } catch (error) { + clipboardError = error; + } + + if (legacyCopy(value)) return; + throw clipboardError instanceof Error + ? clipboardError + : new Error('浏览器拒绝写入剪贴板'); +} + +async function readClipboard(): Promise { + if (!navigator.clipboard?.readText) { + throw new Error('当前页面没有读取剪贴板的权限'); + } + return navigator.clipboard.readText(); +} + +function openInNewTab(url: string) { + window.open(url, '_blank', 'noopener,noreferrer'); +} + +function platformShortcut(key: string): string { + return /Mac|iPhone|iPad|iPod/i.test(navigator.platform) ? `⌘${key}` : `Ctrl+${key}`; +} + +function buildActionGroups(snapshot: ContextSnapshot): MenuAction[][] { + const groups: MenuAction[][] = []; + const contextual: MenuAction[] = []; + + if (snapshot.link) { + const href = snapshot.link.href; + contextual.push( + { id: 'open-link', label: '在新标签页打开链接', icon: ExternalLink, run: () => openInNewTab(href) }, + { id: 'copy-link', label: '复制链接地址', icon: Link2, run: () => writeClipboard(href) }, + ); + } + + if (snapshot.image) { + const src = snapshot.image.currentSrc || snapshot.image.src; + contextual.push( + { id: 'open-image', label: '在新标签页打开图片', icon: ImageIcon, run: () => openInNewTab(src) }, + { id: 'copy-image-url', label: '复制图片地址', icon: Copy, run: () => writeClipboard(src) }, + ); + } + if (contextual.length) groups.push(contextual); + + if (snapshot.editor) { + const hasSelection = snapshot.selectedText.length > 0; + groups.push([ + { + id: 'cut', + label: '剪切', + icon: Scissors, + shortcut: platformShortcut('X'), + disabled: !hasSelection, + run: async () => { + await writeClipboard(snapshot.selectedText); + replaceSelection(snapshot, '', 'deleteByCut'); + }, + }, + { + id: 'copy', + label: '复制', + icon: Copy, + shortcut: platformShortcut('C'), + disabled: !hasSelection, + run: () => writeClipboard(snapshot.selectedText), + }, + { + id: 'paste', + label: '粘贴', + icon: ClipboardPaste, + shortcut: platformShortcut('V'), + disabled: !navigator.clipboard?.readText, + disabledReason: '当前页面没有读取剪贴板的权限', + run: async () => replaceSelection(snapshot, await readClipboard(), 'insertFromPaste'), + }, + { + id: 'select-all', + label: '全选', + icon: TextSelect, + shortcut: platformShortcut('A'), + run: () => selectEditorContents(snapshot.editor!), + }, + ]); + } else if (snapshot.selectedText) { + groups.push([{ + id: 'copy-selection', + label: '复制选中文本', + icon: Copy, + shortcut: platformShortcut('C'), + run: () => writeClipboard(snapshot.selectedText), + }]); + } + + groups.push([ + { + id: 'back', + label: '返回上一页', + icon: ArrowLeft, + disabled: window.history.length <= 1, + run: () => window.history.back(), + }, + { id: 'reload', label: '刷新页面', icon: RefreshCw, shortcut: platformShortcut('R'), run: () => window.location.reload() }, + { id: 'copy-page-url', label: '复制当前页面地址', icon: Link2, run: () => writeClipboard(window.location.href) }, + ]); + + return groups; +} + +export function GlobalContextMenu() { + const { appearance } = useTheme(); + const enabled = appearance.customPointerSystem; + const [menu, setMenu] = React.useState(null); + const [position, setPosition] = React.useState({ x: -9999, y: -9999, ready: false }); + const [busyAction, setBusyAction] = React.useState(null); + const [error, setError] = React.useState(null); + const menuRef = React.useRef(null); + const sequence = React.useRef(0); + + React.useEffect(() => { + if (!enabled) return; + + const onContextMenu = (event: MouseEvent) => { + if (event.shiftKey || !window.matchMedia('(hover: hover) and (pointer: fine)').matches) return; + if (!(event.target instanceof Element)) return; + if (event.target.closest('[data-context-menu="native"]')) return; + if (event.target.closest('.global-context-menu')) { + event.preventDefault(); + return; + } + + event.preventDefault(); + const target = event.target; + const keyboard = event.clientX === 0 && event.clientY === 0; + const rect = target.getBoundingClientRect(); + const x = keyboard ? rect.left + Math.min(rect.width / 2, 24) : event.clientX; + const y = keyboard ? rect.top + Math.min(rect.height, 24) : event.clientY; + + setError(null); + setBusyAction(null); + setPosition({ x, y, ready: false }); + setMenu({ + id: ++sequence.current, + x, + y, + keyboard, + snapshot: { + target, + link: target.closest('a[href]'), + image: target.closest('img'), + ...captureSelection(target), + }, + }); + }; + + window.addEventListener('contextmenu', onContextMenu, { capture: true }); + return () => window.removeEventListener('contextmenu', onContextMenu, { capture: true }); + }, [enabled]); + + React.useEffect(() => { + if (enabled) return; + setMenu(null); + setError(null); + setBusyAction(null); + }, [enabled]); + + React.useLayoutEffect(() => { + const node = menuRef.current; + if (!menu || !node) return; + const rect = node.getBoundingClientRect(); + const edge = 8; + setPosition({ + x: clamp(menu.x, edge, Math.max(edge, window.innerWidth - rect.width - edge)), + y: clamp(menu.y, edge, Math.max(edge, window.innerHeight - rect.height - edge)), + ready: true, + }); + }, [error, menu]); + + React.useEffect(() => { + if (!menu) return; + const close = () => setMenu(null); + const onPointerDown = (event: PointerEvent) => { + if (menuRef.current?.contains(event.target as Node)) return; + close(); + }; + const onKeyDown = (event: KeyboardEvent) => { + if (event.key === 'Escape') { + event.preventDefault(); + close(); + return; + } + if (!['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key)) return; + const items = Array.from(menuRef.current?.querySelectorAll('[role="menuitem"]:not(:disabled)') ?? []); + if (!items.length) return; + event.preventDefault(); + const current = items.indexOf(document.activeElement as HTMLButtonElement); + let next = 0; + if (event.key === 'End') next = items.length - 1; + else if (event.key === 'ArrowUp') next = current <= 0 ? items.length - 1 : current - 1; + else if (event.key === 'ArrowDown') next = current < 0 || current === items.length - 1 ? 0 : current + 1; + items[next]?.focus({ preventScroll: true }); + }; + + window.addEventListener('pointerdown', onPointerDown, { capture: true }); + window.addEventListener('keydown', onKeyDown, { capture: true }); + window.addEventListener('scroll', close, { capture: true, passive: true }); + window.addEventListener('resize', close, { passive: true }); + window.addEventListener('blur', close); + return () => { + window.removeEventListener('pointerdown', onPointerDown, { capture: true }); + window.removeEventListener('keydown', onKeyDown, { capture: true }); + window.removeEventListener('scroll', close, { capture: true }); + window.removeEventListener('resize', close); + window.removeEventListener('blur', close); + }; + }, [menu]); + + React.useEffect(() => { + if (menu?.keyboard && position.ready) { + menuRef.current?.querySelector('[role="menuitem"]:not(:disabled)')?.focus({ preventScroll: true }); + } + }, [menu, position.ready]); + + const groups = enabled && menu ? buildActionGroups(menu.snapshot) : []; + + const runAction = async (action: MenuAction) => { + if (action.disabled || busyAction) return; + setBusyAction(action.id); + setError(null); + try { + await action.run(); + if (menu?.keyboard && menu.snapshot.target instanceof HTMLElement) { + menu.snapshot.target.focus({ preventScroll: true }); + } + setMenu(null); + } catch (cause) { + const message = cause instanceof Error ? cause.message : String(cause); + console.error(`[context-menu] ${action.id} failed`, cause); + setError(message || '操作失败'); + } finally { + setBusyAction(null); + } + }; + + return createPortal( + + {enabled && menu && ( + window.innerWidth / 2 ? 'right' : 'left'} ${menu.y > window.innerHeight / 2 ? 'bottom' : 'top'}`, + }} + initial={{ opacity: 0, scale: 0.96, y: -4 }} + animate={{ opacity: 1, scale: 1, y: 0 }} + exit={{ opacity: 0, scale: 0.98, y: -2, transition: { duration: 0.12, ease: 'easeIn' } }} + transition={{ type: 'spring', duration: 0.3, bounce: 0 }} + > + {groups.map((group, groupIndex) => ( + action.id).join(':')}> + {groupIndex > 0 &&
} +
+ {group.map((action) => { + const Icon = action.icon; + const busy = busyAction === action.id; + return ( + + ); + })} +
+ + ))} + {error && ( +
+ {error} +
+ )} + + )} + , + document.body, + ); +} diff --git a/packages/webui/src/components/ui/scroll-area.tsx b/packages/webui/src/components/ui/scroll-area.tsx index d53090bb..8cd0b7c3 100644 --- a/packages/webui/src/components/ui/scroll-area.tsx +++ b/packages/webui/src/components/ui/scroll-area.tsx @@ -38,7 +38,7 @@ const ScrollBar = React.forwardRef< ref={ref} orientation={orientation} className={cn( - 'flex touch-none select-none p-0.5 transition-colors', + 'flex touch-none select-none p-0.5 transition-colors cursor-grab active:cursor-grabbing', orientation === 'vertical' && 'h-full w-2 border-l border-l-transparent', orientation === 'horizontal' && 'h-2 w-full flex-col border-t border-t-transparent', className diff --git a/packages/webui/src/contexts/ThemeContext.tsx b/packages/webui/src/contexts/ThemeContext.tsx index 0a8d2d75..3682606b 100644 --- a/packages/webui/src/contexts/ThemeContext.tsx +++ b/packages/webui/src/contexts/ThemeContext.tsx @@ -110,6 +110,7 @@ export const DEFAULT_APPEARANCE: UiAppearance = { density: 'cozy', reduceMotion: false, disableMotion: false, + customPointerSystem: true, highContrast: false, sidebarPinned: false, timeFormat: '24h', diff --git a/packages/webui/src/index.css b/packages/webui/src/index.css index ec8c7e65..840c5cb4 100644 --- a/packages/webui/src/index.css +++ b/packages/webui/src/index.css @@ -232,6 +232,519 @@ } } +/* ────────────────────────────────────────────── + Adaptive cursor system + + CSS is the single source of truth for cursor semantics. The React layer + only reads --snowluma-cursor-mode, follows the pointer, and supplies the + hovered target's bounds. Coarse pointers and reduced-motion environments + never receive data-cursor-system="custom", so native cursors remain intact. + ────────────────────────────────────────────── */ +@media (hover: hover) and (pointer: fine) { + html { + --snowluma-cursor-mode: default; + } + + /* Native textarea resize handles are pseudo-elements, so the tracker reads + this CSS-owned regional mode only inside the bottom-right 18px hot zone. */ + .resize-y { --snowluma-cursor-resize-mode: ns-resize; } + .resize-x { --snowluma-cursor-resize-mode: ew-resize; } + .resize { --snowluma-cursor-resize-mode: nwse-resize; } + .resize-none { --snowluma-cursor-resize-mode: initial; } + + /* Clickable targets morph the dot into the complete hit surface. */ + :where( + button, + a[href], + summary, + select, + label[for], + input[type="button"], + input[type="submit"], + input[type="reset"], + input[type="checkbox"], + input[type="radio"], + input[type="file"], + input[type="image"], + [role="button"], + [role="link"], + [role="tab"], + [role="checkbox"], + [role="radio"], + [role="switch"], + [role="menuitem"], + [role="option"], + [data-cursor-target], + .cursor-pointer + ) { + --snowluma-cursor-mode: target; + } + + /* Text entry and selectable/editor surfaces use a narrow I-beam capsule. */ + :where( + input:not([type="button"]):not([type="submit"]):not([type="reset"]):not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="color"]):not([type="file"]):not([type="image"]):not([type="hidden"]), + textarea, + [contenteditable]:not([contenteditable="false"]), + pre, + code, + .font-mono, + .select-text, + .cursor-text + ) { + --snowluma-cursor-mode: text; + } + + :where(input[type="color"], .cursor-crosshair, .cursor-cell) { + --snowluma-cursor-mode: crosshair; + } + + :where(input[type="range"], [draggable="true"], .slider-root, .cursor-grab) { + --snowluma-cursor-mode: grab; + } + :where(input[type="range"], [draggable="true"], .slider-root, .cursor-grab):active, + :where([aria-grabbed="true"], .cursor-grabbing, .ui-draggable-dragging) { + --snowluma-cursor-mode: grabbing; + } + + :where(.cursor-move, .cursor-all-scroll) { + --snowluma-cursor-mode: move; + } + :where(.cursor-ew-resize, .cursor-col-resize, .ui-resizable-e, .ui-resizable-w) { + --snowluma-cursor-mode: ew-resize; + } + :where(.cursor-ns-resize, .cursor-row-resize, .ui-resizable-n, .ui-resizable-s) { + --snowluma-cursor-mode: ns-resize; + } + :where(.cursor-nwse-resize, .cursor-nw-resize, .cursor-se-resize, .ui-resizable-nw, .ui-resizable-se) { + --snowluma-cursor-mode: nwse-resize; + } + :where(.cursor-nesw-resize, .cursor-ne-resize, .cursor-sw-resize, .ui-resizable-ne, .ui-resizable-sw) { + --snowluma-cursor-mode: nesw-resize; + } + + :where([aria-busy="true"], .cursor-wait, .cursor-progress) { + --snowluma-cursor-mode: wait; + } + :where(.cursor-copy) { --snowluma-cursor-mode: copy; } + :where(.cursor-zoom-in) { --snowluma-cursor-mode: zoom-in; } + :where(.cursor-zoom-out) { --snowluma-cursor-mode: zoom-out; } + :where(.cursor-help) { --snowluma-cursor-mode: help; } + :where(:disabled, [aria-disabled="true"], .cursor-not-allowed, .cursor-no-drop) { + --snowluma-cursor-mode: not-allowed; + } + + /* Explicit data attributes are the escape hatch for third-party widgets. */ + [data-cursor="default"] { --snowluma-cursor-mode: default; } + [data-cursor="target"] { --snowluma-cursor-mode: target; } + [data-cursor="text"] { --snowluma-cursor-mode: text; } + [data-cursor="grab"] { --snowluma-cursor-mode: grab; } + [data-cursor="grabbing"] { --snowluma-cursor-mode: grabbing; } + [data-cursor="move"] { --snowluma-cursor-mode: move; } + [data-cursor="ew-resize"] { --snowluma-cursor-mode: ew-resize; } + [data-cursor="ns-resize"] { --snowluma-cursor-mode: ns-resize; } + [data-cursor="nwse-resize"] { --snowluma-cursor-mode: nwse-resize; } + [data-cursor="nesw-resize"] { --snowluma-cursor-mode: nesw-resize; } + [data-cursor="not-allowed"] { --snowluma-cursor-mode: not-allowed; } + [data-cursor="wait"] { --snowluma-cursor-mode: wait; } + [data-cursor="crosshair"] { --snowluma-cursor-mode: crosshair; } + [data-cursor="copy"] { --snowluma-cursor-mode: copy; } + [data-cursor="zoom-in"] { --snowluma-cursor-mode: zoom-in; } + [data-cursor="zoom-out"] { --snowluma-cursor-mode: zoom-out; } + [data-cursor="help"] { --snowluma-cursor-mode: help; } + [data-cursor="hidden"] { --snowluma-cursor-mode: hidden; } + + /* Applied only after the tracker mounts, so a JavaScript failure can never + strand the application without a usable native pointer. */ + html[data-cursor-system="custom"], + html[data-cursor-system="custom"] body, + html[data-cursor-system="custom"] *, + html[data-cursor-system="custom"] *::before, + html[data-cursor-system="custom"] *::after { + cursor: none !important; + } +} + +.adaptive-cursor { + position: fixed; + top: 0; + left: 0; + z-index: 2147483647; + width: 0; + height: 0; + opacity: 0; + pointer-events: none; + will-change: transform, opacity; + transition-property: opacity; + transition-duration: 120ms; + transition-timing-function: ease-out; +} + +html[data-cursor-system="custom"] .adaptive-cursor[data-visible="true"] { + opacity: 1; +} + +.adaptive-cursor-shape { + --cursor-width: 1.125rem; + --cursor-height: 1.125rem; + --cursor-radius: 9999px; + --cursor-scale: 1; + --cursor-fill: color-mix(in oklab, var(--primary) 82%, var(--foreground)); + --cursor-stroke: color-mix(in oklab, var(--primary) 34%, transparent); + + position: absolute; + top: 0; + left: 0; + display: block; + width: var(--cursor-width); + height: var(--cursor-height); + border: 1px solid var(--cursor-stroke); + border-radius: var(--cursor-radius); + background: var(--cursor-fill); + box-shadow: + 0 1px 3px oklch(0% 0 0 / 0.16), + 0 0 0 3px color-mix(in oklab, var(--background) 34%, transparent); + opacity: 1; + translate: -50% -50%; + scale: var(--cursor-scale); + transition-property: width, height, border-radius, background-color, border-color, box-shadow, opacity, scale; + transition-duration: 180ms; + transition-timing-function: cubic-bezier(0.2, 0, 0, 1); +} + +.adaptive-cursor-shape::before, +.adaptive-cursor-shape::after { + content: ''; + position: absolute; + top: 50%; + left: 50%; + opacity: 0; + pointer-events: none; + transition-property: opacity, transform; + transition-duration: 140ms; + transition-timing-function: ease-out; +} + +.adaptive-cursor[data-pressed="true"] .adaptive-cursor-shape { + --cursor-scale: 0.8; +} + +/* Mikk-style target morph: bounds and radius come from the hovered element. */ +.adaptive-cursor[data-mode="target"] .adaptive-cursor-shape { + --cursor-width: var(--cursor-target-width, 1.125rem); + --cursor-height: var(--cursor-target-height, 1.125rem); + --cursor-radius: var(--cursor-target-radius, var(--radius)); + --cursor-fill: color-mix(in oklab, var(--primary) 14%, transparent); + --cursor-stroke: color-mix(in oklab, var(--primary) 28%, transparent); + box-shadow: + 0 4px 18px -10px color-mix(in oklab, var(--primary) 56%, transparent), + inset 0 1px color-mix(in oklab, var(--primary-foreground) 16%, transparent); +} +.adaptive-cursor[data-mode="target"][data-pressed="true"] .adaptive-cursor-shape { + --cursor-scale: 0.96; + --cursor-fill: color-mix(in oklab, var(--primary) 23%, transparent); +} + +/* Narrow but visibly substantial text caret capsule. */ +.adaptive-cursor[data-mode="text"] .adaptive-cursor-shape { + --cursor-width: 0.25rem; + --cursor-height: 1.75rem; + --cursor-fill: color-mix(in oklab, var(--primary) 84%, var(--foreground)); + --cursor-stroke: color-mix(in oklab, var(--background) 48%, transparent); + box-shadow: + 0 0 0 1px color-mix(in oklab, var(--background) 52%, transparent), + 0 2px 8px color-mix(in oklab, var(--primary) 46%, transparent); +} + +/* Grab / grabbing: a compact four-dot grip compresses under load. */ +.adaptive-cursor[data-mode="grab"] .adaptive-cursor-shape, +.adaptive-cursor[data-mode="grabbing"] .adaptive-cursor-shape { + --cursor-width: 1.5rem; + --cursor-height: 1.5rem; + --cursor-fill: color-mix(in oklab, var(--primary) 16%, var(--background)); + --cursor-stroke: color-mix(in oklab, var(--primary) 52%, transparent); +} +.adaptive-cursor[data-mode="grabbing"] .adaptive-cursor-shape { + --cursor-width: 1.2rem; + --cursor-height: 1.2rem; + --cursor-fill: color-mix(in oklab, var(--primary) 28%, var(--background)); +} +.adaptive-cursor[data-mode="grab"] .adaptive-cursor-shape::before, +.adaptive-cursor[data-mode="grabbing"] .adaptive-cursor-shape::before { + width: 0.1875rem; + height: 0.1875rem; + border-radius: 9999px; + background: var(--primary); + box-shadow: 0.375rem 0 var(--primary), 0 0.375rem var(--primary), 0.375rem 0.375rem var(--primary); + opacity: 0.88; + transform: translate(-0.28125rem, -0.28125rem); +} +.adaptive-cursor[data-mode="grabbing"] .adaptive-cursor-shape::before { + box-shadow: 0.3rem 0 var(--primary), 0 0.3rem var(--primary), 0.3rem 0.3rem var(--primary); + transform: translate(-0.24375rem, -0.24375rem); +} + +/* Move and all resize directions share one CSS arrow glyph. */ +.adaptive-cursor[data-mode="move"] .adaptive-cursor-shape, +.adaptive-cursor[data-mode$="resize"] .adaptive-cursor-shape { + --cursor-width: 1.625rem; + --cursor-height: 0.875rem; + --cursor-fill: color-mix(in oklab, var(--primary) 15%, var(--background)); + --cursor-stroke: color-mix(in oklab, var(--primary) 48%, transparent); +} +.adaptive-cursor[data-mode="move"] .adaptive-cursor-shape { + --cursor-width: 1.625rem; + --cursor-height: 1.625rem; +} +.adaptive-cursor[data-mode="move"] .adaptive-cursor-shape::before, +.adaptive-cursor[data-mode$="resize"] .adaptive-cursor-shape::before { + width: 1rem; + height: 1rem; + background: var(--primary); + opacity: 0.9; + transform: translate(-50%, -50%); + -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M2 8h12M5 5 2 8l3 3m6-6 3 3-3 3' fill='none' stroke='black' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / contain no-repeat; + mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M2 8h12M5 5 2 8l3 3m6-6 3 3-3 3' fill='none' stroke='black' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / contain no-repeat; +} +.adaptive-cursor[data-mode="move"] .adaptive-cursor-shape::before { + -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M8 1v14M1 8h14M5 4 8 1l3 3m-6 8 3 3 3-3M4 5 1 8l3 3m8-6 3 3-3 3' fill='none' stroke='black' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E"); + mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M8 1v14M1 8h14M5 4 8 1l3 3m-6 8 3 3 3-3M4 5 1 8l3 3m8-6 3 3-3 3' fill='none' stroke='black' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E"); +} +.adaptive-cursor[data-mode="ns-resize"] .adaptive-cursor-shape::before { transform: translate(-50%, -50%) rotate(90deg); } +.adaptive-cursor[data-mode="nwse-resize"] .adaptive-cursor-shape::before { transform: translate(-50%, -50%) rotate(45deg); } +.adaptive-cursor[data-mode="nesw-resize"] .adaptive-cursor-shape::before { transform: translate(-50%, -50%) rotate(-45deg); } + +/* Disabled / forbidden action. */ +.adaptive-cursor[data-mode="not-allowed"] .adaptive-cursor-shape { + --cursor-width: 1.45rem; + --cursor-height: 1.45rem; + --cursor-fill: color-mix(in oklab, var(--destructive) 8%, var(--background)); + --cursor-stroke: color-mix(in oklab, var(--destructive) 76%, transparent); + border-width: 2px; + box-shadow: 0 2px 8px color-mix(in oklab, var(--destructive) 22%, transparent); +} +.adaptive-cursor[data-mode="not-allowed"] .adaptive-cursor-shape::after { + width: 1rem; + height: 2px; + border-radius: 9999px; + background: var(--destructive); + opacity: 0.9; + transform: translate(-50%, -50%) rotate(-45deg); +} + +/* Busy state: a CSS-only spinner, used only while motion is allowed. */ +.adaptive-cursor[data-mode="wait"] .adaptive-cursor-shape { + --cursor-width: 1.4rem; + --cursor-height: 1.4rem; + --cursor-fill: color-mix(in oklab, var(--background) 84%, transparent); + --cursor-stroke: color-mix(in oklab, var(--primary) 22%, transparent); + border-width: 2px; +} +.adaptive-cursor[data-mode="wait"] .adaptive-cursor-shape::before { + inset: -2px; + border: 2px solid transparent; + border-top-color: var(--primary); + border-right-color: color-mix(in oklab, var(--primary) 55%, transparent); + border-radius: 9999px; + opacity: 1; + transform: none; + animation: adaptive-cursor-spin 700ms linear infinite; +} +@keyframes adaptive-cursor-spin { to { rotate: 1turn; } } + +/* Precision selection. */ +.adaptive-cursor[data-mode="crosshair"] .adaptive-cursor-shape { + --cursor-width: 1.25rem; + --cursor-height: 1.25rem; + --cursor-fill: transparent; + --cursor-stroke: color-mix(in oklab, var(--primary) 45%, transparent); + box-shadow: none; +} +.adaptive-cursor[data-mode="crosshair"] .adaptive-cursor-shape::before, +.adaptive-cursor[data-mode="crosshair"] .adaptive-cursor-shape::after { + border-radius: 9999px; + background: var(--primary); + opacity: 0.9; + transform: translate(-50%, -50%); +} +.adaptive-cursor[data-mode="crosshair"] .adaptive-cursor-shape::before { width: 1.6rem; height: 2px; } +.adaptive-cursor[data-mode="crosshair"] .adaptive-cursor-shape::after { width: 2px; height: 1.6rem; } + +/* Copy uses two offset sheets. */ +.adaptive-cursor[data-mode="copy"] .adaptive-cursor-shape { + --cursor-width: 1.45rem; + --cursor-height: 1.45rem; + --cursor-fill: color-mix(in oklab, var(--primary) 13%, var(--background)); + --cursor-stroke: color-mix(in oklab, var(--primary) 45%, transparent); +} +.adaptive-cursor[data-mode="copy"] .adaptive-cursor-shape::before, +.adaptive-cursor[data-mode="copy"] .adaptive-cursor-shape::after { + width: 0.55rem; + height: 0.65rem; + border: 1.5px solid var(--primary); + border-radius: 0.15rem; + background: var(--background); + opacity: 0.95; +} +.adaptive-cursor[data-mode="copy"] .adaptive-cursor-shape::before { transform: translate(-65%, -65%); } +.adaptive-cursor[data-mode="copy"] .adaptive-cursor-shape::after { transform: translate(-30%, -30%); } + +/* Zoom modes share a lens; the second line appears only for zoom-in. */ +.adaptive-cursor[data-mode="zoom-in"] .adaptive-cursor-shape, +.adaptive-cursor[data-mode="zoom-out"] .adaptive-cursor-shape { + --cursor-width: 1.3rem; + --cursor-height: 1.3rem; + --cursor-fill: color-mix(in oklab, var(--primary) 10%, var(--background)); + --cursor-stroke: color-mix(in oklab, var(--primary) 62%, transparent); + border-width: 2px; + box-shadow: 0 2px 8px color-mix(in oklab, var(--primary) 18%, transparent); +} +.adaptive-cursor[data-mode="zoom-in"] .adaptive-cursor-shape::before, +.adaptive-cursor[data-mode="zoom-out"] .adaptive-cursor-shape::before { + width: 0.55rem; + height: 2px; + border-radius: 9999px; + background: var(--primary); + opacity: 0.9; + transform: translate(-50%, -50%); +} +.adaptive-cursor[data-mode="zoom-in"] .adaptive-cursor-shape::after { + width: 2px; + height: 0.55rem; + border-radius: 9999px; + background: var(--primary); + opacity: 0.9; + transform: translate(-50%, -50%); +} + +.adaptive-cursor[data-mode="help"] .adaptive-cursor-shape { + --cursor-width: 1.35rem; + --cursor-height: 1.35rem; + --cursor-fill: color-mix(in oklab, var(--primary) 14%, var(--background)); + --cursor-stroke: color-mix(in oklab, var(--primary) 55%, transparent); +} +.adaptive-cursor[data-mode="help"] .adaptive-cursor-shape::before { + content: '?'; + color: var(--primary); + font-size: 0.75rem; + font-weight: 700; + line-height: 1; + opacity: 0.95; + transform: translate(-50%, -52%); +} + +.adaptive-cursor[data-mode="hidden"] .adaptive-cursor-shape { + opacity: 0; +} + +/* ────────────────────────────────────────────── + Global context menu + ────────────────────────────────────────────── */ +.global-context-menu { + position: fixed; + z-index: 2147483646; + width: min(15.5rem, calc(100vw - 1rem)); + padding: 0.375rem; + border-radius: calc(var(--radius) + 0.25rem); + background: color-mix(in oklab, var(--popover) 92%, transparent); + color: var(--popover-foreground); + box-shadow: + 0 0 0 1px color-mix(in oklab, var(--foreground) 9%, transparent), + 0 2px 5px -2px oklch(0% 0 0 / 0.18), + 0 18px 48px -20px oklch(0% 0 0 / 0.42); + outline: none; + overflow: hidden; + backdrop-filter: blur(20px) saturate(1.15); +} + +.global-context-menu-group { + display: grid; + gap: 0.125rem; +} + +.global-context-menu-separator { + height: 1px; + margin: 0.375rem 0.5rem; + background: color-mix(in oklab, var(--border) 74%, transparent); +} + +.global-context-menu-item { + display: grid; + grid-template-columns: 1.125rem minmax(0, 1fr) auto; + align-items: center; + gap: 0.625rem; + width: 100%; + min-height: 2.5rem; + padding: 0 0.625rem; + border: 0; + border-radius: calc(var(--radius) - 0.125rem); + background: transparent; + color: var(--popover-foreground); + font: inherit; + text-align: left; + scale: 1; + outline: none; + transition-property: color, background-color, box-shadow, scale, opacity; + transition-duration: 140ms; + transition-timing-function: ease-out; +} +.global-context-menu-item:hover, +.global-context-menu-item:focus-visible { + background: color-mix(in oklab, var(--primary) 12%, var(--accent)); + color: var(--foreground); +} +.global-context-menu-item:focus-visible { + box-shadow: inset 0 0 0 1px color-mix(in oklab, var(--ring) 45%, transparent); +} +.global-context-menu-item:active:not(:disabled) { + scale: 0.96; +} +.global-context-menu-item:disabled { + color: color-mix(in oklab, var(--muted-foreground) 62%, transparent); + opacity: 0.62; +} + +.global-context-menu-icon { + width: 1rem; + height: 1rem; + justify-self: center; + color: var(--muted-foreground); + stroke-width: 1.8; +} +.global-context-menu-item:hover .global-context-menu-icon, +.global-context-menu-item:focus-visible .global-context-menu-icon { + color: var(--primary); +} + +.global-context-menu-label { + min-width: 0; + overflow: hidden; + font-size: 0.8125rem; + font-weight: 500; + line-height: 1.25rem; + text-overflow: ellipsis; + white-space: nowrap; +} + +.global-context-menu-shortcut { + color: var(--muted-foreground); + font-family: var(--font-sans); + font-size: 0.6875rem; + font-weight: 500; + letter-spacing: 0.01em; + line-height: 1rem; +} + +.global-context-menu-error { + margin: 0.375rem 0.25rem 0.125rem; + padding: 0.5rem 0.625rem; + border-radius: calc(var(--radius) - 0.25rem); + background: color-mix(in oklab, var(--destructive) 10%, transparent); + color: var(--destructive); + font-size: 0.75rem; + line-height: 1.125rem; + overflow-wrap: anywhere; +} + /* ────────────────────────────────────────────── Customization layers (driven by ThemeContext via data-* attributes + CSS vars on ) diff --git a/packages/webui/src/types.ts b/packages/webui/src/types.ts index abf27b58..37a64974 100644 --- a/packages/webui/src/types.ts +++ b/packages/webui/src/types.ts @@ -363,6 +363,8 @@ export interface UiAppearance { density: Density; reduceMotion: boolean; disableMotion: boolean; + /** Master switch for the adaptive cursor and custom context menu. */ + customPointerSystem: boolean; highContrast: boolean; /** Pin the sidebar permanently expanded, opting out of the hover-expand rail. */ sidebarPinned: boolean;