diff --git a/packages/core/src/webui/ui-config.ts b/packages/core/src/webui/ui-config.ts index 303d1484..5fce253b 100644 --- a/packages/core/src/webui/ui-config.ts +++ b/packages/core/src/webui/ui-config.ts @@ -100,8 +100,10 @@ export interface UiAppearance { reduceMotion: boolean; /** Hard-disable all UI animations (stronger than reduceMotion). */ disableMotion: boolean; - /** Use SnowLuma's adaptive cursor and custom context menu. */ + /** Use SnowLuma's adaptive cursor. */ customPointerSystem: boolean; + /** Replace the browser context menu with SnowLuma's context menu. */ + customContextMenu: boolean; highContrast: boolean; sidebarPinned: boolean; timeFormat: TimeFormat; @@ -229,6 +231,7 @@ const DEFAULT_APPEARANCE: UiAppearance = { reduceMotion: false, disableMotion: false, customPointerSystem: false, + customContextMenu: true, highContrast: false, sidebarPinned: false, timeFormat: '24h', @@ -524,6 +527,7 @@ export function normalizeAppearance(value: unknown, imageState: ServerImageState reduceMotion: boolOr(v.reduceMotion, DEFAULT_APPEARANCE.reduceMotion), disableMotion: boolOr(v.disableMotion, DEFAULT_APPEARANCE.disableMotion), customPointerSystem: boolOr(v.customPointerSystem, DEFAULT_APPEARANCE.customPointerSystem), + customContextMenu: boolOr(v.customContextMenu, DEFAULT_APPEARANCE.customContextMenu), 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 efc8f370..d73d6c20 100644 Binary files a/packages/core/tests/webui-ui-config.test.ts and b/packages/core/tests/webui-ui-config.test.ts differ diff --git a/packages/webui/src/components/debug/message-composer.tsx b/packages/webui/src/components/debug/message-composer.tsx index cfff59cc..0e1d8632 100644 --- a/packages/webui/src/components/debug/message-composer.tsx +++ b/packages/webui/src/components/debug/message-composer.tsx @@ -5,7 +5,7 @@ import { useEffect, useMemo, useState } from 'react'; import { motion } from 'motion/react'; import { Loader2, MessageSquarePlus, Send } from 'lucide-react'; -import { Select } from '@/components/ui/select'; +import { Dropdown } from '@/components/interior/dropdown'; import { Picker } from '@/components/ui/picker'; import { JsonTree } from '@/components/ui/json-tree'; import { Segmented } from '@/components/debug/segmented'; @@ -97,10 +97,17 @@ export function MessageComposer({ accounts }: { accounts: QQInfo[] }) {
账号
- + ({ + value: account.uin, + label: account.nickname || account.uin, + }))} + className="w-full sm:w-44" + />
diff --git a/packages/webui/src/components/debug/param-field.tsx b/packages/webui/src/components/debug/param-field.tsx index f3bfb203..7a12c928 100644 --- a/packages/webui/src/components/debug/param-field.tsx +++ b/packages/webui/src/components/debug/param-field.tsx @@ -5,7 +5,7 @@ // group_id, an image a FileSource, a bool a switch, an enum a select. import { useState } from 'react'; import { Input } from '@/components/ui/input'; -import { Select } from '@/components/ui/select'; +import { Dropdown } from '@/components/interior/dropdown'; import { ToggleSwitch } from '@/components/ui/toggle-switch'; import { Picker } from '@/components/ui/picker'; import { FaceGrid } from '@/components/ui/face-grid'; @@ -84,13 +84,20 @@ export function ParamField({ param, value, onChange, uin, groupContext }: { // ── type-driven widgets ── if (param.values && param.values.length > 0) { return ( - // Always render the empty option so a controlled onChange(e.target.value)}> - - {param.values.map((v) => )} - + ({ + value: String(nextValue), + label: String(nextValue), + })), + ]} + className="w-full" + /> ); } if (param.type === 'bool' || param.type === 'boolean') { diff --git a/packages/webui/src/components/interior/context-menu.tsx b/packages/webui/src/components/interior/context-menu.tsx new file mode 100644 index 00000000..e5414eab --- /dev/null +++ b/packages/webui/src/components/interior/context-menu.tsx @@ -0,0 +1,74 @@ +"use client"; + +import { type ReactNode, type RefObject } from "react"; +import { createPortal } from "react-dom"; +import { AnimatePresence, motion, useReducedMotion } from "motion/react"; + +const EASE = [0.23, 1, 0.32, 1] as const; +const EXIT = [0.4, 0, 1, 1] as const; + +export type ContextMenuPlacement = { + left: number; + top: number; + width: number; + maxHeight: number; + transformOrigin: string; + visible?: boolean; +}; + +export type ContextMenuSurfaceProps = { + openKey: number | string | null; + menuRef: RefObject; + placement: ContextMenuPlacement; + label: string; + children: ReactNode; +}; + +/** + * Interior's context-menu surface, kept separate from action discovery so the + * WebUI can build native browser-style actions from the element under pointer. + */ +export function ContextMenuSurface({ + openKey, + menuRef, + placement, + label, + children, +}: ContextMenuSurfaceProps) { + const reduced = useReducedMotion(); + + return createPortal( + + {openKey !== null ? ( + + {children} + + ) : null} + , + document.body, + ); +} diff --git a/packages/webui/src/components/interior/dropdown.tsx b/packages/webui/src/components/interior/dropdown.tsx new file mode 100644 index 00000000..96372dc3 --- /dev/null +++ b/packages/webui/src/components/interior/dropdown.tsx @@ -0,0 +1,422 @@ +"use client"; + +import { useCallback, useEffect, useId, useRef, useState } from "react"; +import { AnimatePresence, motion, useReducedMotion } from "motion/react"; + +const EASE = [0.23, 1, 0.32, 1] as const; +const EXIT = [0.4, 0, 1, 1] as const; +const CELL = { type: "spring", stiffness: 520, damping: 34, mass: 0.45 } as const; +const NUDGE = { type: "spring", stiffness: 700, damping: 46, mass: 0.5 } as const; +const NONE = { duration: 0 } as const; +const SLIDE = { type: "spring", stiffness: 700, damping: 46, mass: 0.5 } as const; +const ROW_H = 32; +const OPEN = { type: "spring", stiffness: 620, damping: 38, mass: 0.6 } as const; + +export type DropdownItem = { + value: string; + label: string; + hint?: string; + disabled?: boolean; +}; + +export type UseDropdownOptions = { + items: DropdownItem[]; + value?: string; + defaultValue?: string; + onChange?: (value: string) => void; + disabled?: boolean; + typeaheadDelay?: number; +}; + +export function useDropdown({ + items, + value, + defaultValue, + onChange, + disabled = false, + typeaheadDelay = 600, +}: UseDropdownOptions) { + const uid = useId(); + const listId = `${uid}-list`; + const itemId = useCallback((i: number) => `${uid}-opt-${i}`, [uid]); + + const [uncontrolled, setUncontrolled] = useState(defaultValue ?? null); + const selectedValue = value !== undefined ? value : uncontrolled; + const selectedIndex = items.findIndex((it) => it.value === selectedValue); + + const [open, setOpen] = useState(false); + const [activeIndex, setActiveIndex] = useState(-1); + + const rootRef = useRef(null); + const triggerRef = useRef(null); + const listRef = useRef(null); + const itemRefs = useRef<(HTMLLIElement | null)[]>([]); + const viaKey = useRef(false); + const buffer = useRef(""); + const bufferTimer = useRef | null>(null); + const emit = useRef(onChange); + + useEffect(() => { + emit.current = onChange; + }, [onChange]); + + const step = useCallback( + (from: number, dir: 1 | -1) => { + const n = items.length; + if (n === 0) return -1; + let i = from; + for (let k = 0; k < n; k += 1) { + i = (i + dir + n) % n; + if (!items[i].disabled) return i; + } + return from; + }, + [items], + ); + + const edge = useCallback( + (dir: 1 | -1) => step(dir === 1 ? -1 : items.length, dir), + [step, items.length], + ); + + const openMenu = useCallback( + (index?: number) => { + if (disabled || items.length === 0) return; + const usable = selectedIndex >= 0 && !items[selectedIndex].disabled; + viaKey.current = true; + setActiveIndex(index ?? (usable ? selectedIndex : edge(1))); + setOpen(true); + }, + [disabled, items, selectedIndex, edge], + ); + + const close = useCallback((restoreFocus = true) => { + buffer.current = ""; + setOpen(false); + setActiveIndex(-1); + if (restoreFocus) triggerRef.current?.focus(); + }, []); + + const select = useCallback( + (index: number) => { + const item = items[index]; + if (!item || item.disabled) return; + if (value === undefined) setUncontrolled(item.value); + emit.current?.(item.value); + close(); + }, + [items, value, close], + ); + + const typeahead = useCallback( + (char: string) => { + if (bufferTimer.current) clearTimeout(bufferTimer.current); + buffer.current += char.toLowerCase(); + bufferTimer.current = setTimeout(() => { + buffer.current = ""; + }, typeaheadDelay); + + const q = buffer.current; + const n = items.length; + const from = activeIndex < 0 ? 0 : activeIndex; + const start = q.length > 1 ? from : from + 1; + for (let k = 0; k < n; k += 1) { + const i = (start + k) % n; + const it = items[i]; + if (!it.disabled && it.label.toLowerCase().startsWith(q)) { + viaKey.current = true; + setActiveIndex(i); + return; + } + } + }, + [items, activeIndex, typeaheadDelay], + ); + + useEffect(() => { + if (open) listRef.current?.focus(); + }, [open]); + + useEffect(() => { + if (!open) return; + const onDown = (event: PointerEvent) => { + if (!rootRef.current?.contains(event.target as Node)) close(false); + }; + const onWindowBlur = () => close(false); + document.addEventListener("pointerdown", onDown, true); + window.addEventListener("blur", onWindowBlur); + return () => { + document.removeEventListener("pointerdown", onDown, true); + window.removeEventListener("blur", onWindowBlur); + }; + }, [open, close]); + + useEffect(() => { + if (!open || activeIndex < 0 || !viaKey.current) return; + viaKey.current = false; + itemRefs.current[activeIndex]?.scrollIntoView({ block: "nearest" }); + }, [open, activeIndex]); + + useEffect( + () => () => { + if (bufferTimer.current) clearTimeout(bufferTimer.current); + }, + [], + ); + + const triggerProps = { + ref: triggerRef, + type: "button" as const, + disabled, + "aria-haspopup": "listbox" as const, + "aria-expanded": open, + "aria-controls": open ? listId : undefined, + onClick: () => (open ? close() : openMenu()), + onKeyDown: (event: React.KeyboardEvent) => { + if (event.key === "ArrowDown" || event.key === "Enter" || event.key === " ") { + event.preventDefault(); + openMenu(); + } else if (event.key === "ArrowUp") { + event.preventDefault(); + openMenu(edge(-1)); + } + }, + }; + + const listProps = { + ref: listRef, + id: listId, + role: "listbox" as const, + tabIndex: -1, + "aria-activedescendant": activeIndex >= 0 ? itemId(activeIndex) : undefined, + onKeyDown: (event: React.KeyboardEvent) => { + if (event.key === "ArrowDown" || event.key === "ArrowUp") { + event.preventDefault(); + const dir = event.key === "ArrowDown" ? 1 : -1; + viaKey.current = true; + setActiveIndex((i) => step(i, dir)); + } else if (event.key === "Home" || event.key === "End") { + event.preventDefault(); + viaKey.current = true; + setActiveIndex(edge(event.key === "Home" ? 1 : -1)); + } else if (event.key === "Enter" || event.key === " ") { + event.preventDefault(); + select(activeIndex); + } else if (event.key === "Escape") { + event.preventDefault(); + event.stopPropagation(); + close(); + } else if (event.key === "Tab") { + close(false); + } else if ( + event.key.length === 1 + && !event.metaKey + && !event.ctrlKey + && !event.altKey + ) { + event.preventDefault(); + typeahead(event.key); + } + }, + }; + + const getItemProps = useCallback( + (index: number) => ({ + id: itemId(index), + role: "option" as const, + "aria-selected": index === selectedIndex, + "aria-disabled": items[index]?.disabled ? (true as const) : undefined, + ref: (element: HTMLLIElement | null) => { + itemRefs.current[index] = element; + }, + onPointerMove: () => { + if (items[index]?.disabled) return; + viaKey.current = false; + setActiveIndex(index); + }, + onClick: () => select(index), + }), + [itemId, items, selectedIndex, select], + ); + + return { + open, + activeIndex, + selectedIndex, + selectedItem: selectedIndex >= 0 ? items[selectedIndex] : null, + rootRef, + triggerProps, + listProps, + getItemProps, + }; +} + +export type DropdownProps = { + items: DropdownItem[]; + value?: string; + defaultValue?: string; + onChange?: (value: string) => void; + label?: string; + placeholder?: string; + disabled?: boolean; + emptyLabel?: string; + className?: string; +}; + +export function Dropdown({ + items, + value, + defaultValue, + onChange, + label = "Options", + placeholder = "Select an option", + disabled = false, + emptyLabel = "Nothing to choose", + className = "", +}: DropdownProps) { + const reduced = useReducedMotion(); + const { + open, + activeIndex, + selectedIndex, + selectedItem, + rootRef, + triggerProps, + listProps, + getItemProps, + } = useDropdown({ items, value, defaultValue, onChange, disabled }); + + const cell = reduced ? NONE : CELL; + + return ( +
+ + + {open ? ( + +
    + + {items.map((item, index) => { + const active = index === activeIndex && !item.disabled; + const picked = index === selectedIndex; + return ( +
  • + + {item.label} + {item.hint ? ( + + {item.hint} + + ) : null} + + + + + + +
  • + ); + })} + + {items.length === 0 ? ( +
  • + {emptyLabel} +
  • + ) : null} +
+
+ ) : null} +
+
+ ); +} diff --git a/packages/webui/src/components/interior/segmented-control.tsx b/packages/webui/src/components/interior/segmented-control.tsx new file mode 100644 index 00000000..b3504a6b --- /dev/null +++ b/packages/webui/src/components/interior/segmented-control.tsx @@ -0,0 +1,196 @@ +"use client"; + +import { useCallback, useEffect, useRef, useState, type ReactNode } from "react"; +import { + animate, + motion, + useMotionValue, + useReducedMotion, + useTransform, +} from "motion/react"; + +const CELL = { type: "spring", stiffness: 520, damping: 34, mass: 0.45 } as const; +const SEG = + "px-3 py-[7px] text-center text-[13px] font-medium leading-[18px] tracking-[-0.01em] whitespace-nowrap"; + +export type SegmentedOption = { + value: string; + label: string; + content?: ReactNode; + disabled?: boolean; +}; + +export type SegmentedControlProps = { + options: SegmentedOption[]; + label: string; + value?: string; + defaultValue?: string; + onValueChange?: (value: string) => void; + className?: string; +}; + +export function SegmentedControl({ + options, + label, + value, + defaultValue, + onValueChange, + className = "", +}: SegmentedControlProps) { + const count = Math.max(1, options.length); + const template = `repeat(${count}, minmax(0, 1fr))`; + + const [internal, setInternal] = useState( + () => defaultValue ?? options[0]?.value ?? "", + ); + const [hovered, setHovered] = useState(-1); + + const controlled = value !== undefined; + const current = controlled ? value : internal; + const found = options.findIndex((option) => option.value === current); + const index = found < 0 ? 0 : found; + + const buttons = useRef<(HTMLButtonElement | null)[]>([]); + const emit = useRef(onValueChange); + useEffect(() => { + emit.current = onValueChange; + }, [onValueChange]); + + const reduced = useReducedMotion(); + const pos = useMotionValue(index); + const thumbX = useTransform(pos, (next) => `${next * 100}%`); + const maskX = useTransform(pos, (next) => `${next * -100}%`); + + useEffect(() => { + if (reduced) { + pos.set(index); + return; + } + const controls = animate(pos, index, CELL); + return () => controls.stop(); + }, [index, reduced, pos]); + + const select = useCallback( + (next: string) => { + if (!controlled) setInternal(next); + if (next !== current) emit.current?.(next); + }, + [controlled, current], + ); + + const seek = useCallback( + (from: number, direction: number) => { + let next = from; + for (let offset = 0; offset < count; offset += 1) { + next = (next + direction + count) % count; + if (!options[next]?.disabled) return next; + } + return from; + }, + [count, options], + ); + + const go = useCallback( + (next: number) => { + const option = options[next]; + if (!option || option.disabled) return; + buttons.current[next]?.focus(); + select(option.value); + }, + [options, select], + ); + + const onKeyDown = (event: React.KeyboardEvent, optionIndex: number) => { + if (event.key === "ArrowRight" || event.key === "ArrowDown") { + event.preventDefault(); + go(seek(optionIndex, 1)); + } else if (event.key === "ArrowLeft" || event.key === "ArrowUp") { + event.preventDefault(); + go(seek(optionIndex, -1)); + } else if (event.key === "Home") { + event.preventDefault(); + go(seek(count - 1, 1)); + } else if (event.key === "End") { + event.preventDefault(); + go(seek(0, -1)); + } + }; + + return ( +
+
+ {options.map((option, optionIndex) => ( + + {option.content ?? option.label} + + ))} + + + +
+ {options.map((option) => ( + + {option.content ?? option.label} + + ))} +
+
+
+
setHovered(-1)} + > + {options.map((option, optionIndex) => ( + + ))} +
+
+
+ ); +} diff --git a/packages/webui/src/components/pages/settings-page.tsx b/packages/webui/src/components/pages/settings-page.tsx index f3dda514..b6a3e7a5 100644 --- a/packages/webui/src/components/pages/settings-page.tsx +++ b/packages/webui/src/components/pages/settings-page.tsx @@ -2,7 +2,7 @@ import { createContext, useContext, useId, useRef, useState, type ComponentProps import { motion } from 'motion/react'; import { Accessibility, AlertTriangle, Bell, Bug, Check, Clock, Code2, Download, ExternalLink, Github, Image as ImageIcon, - Info, KeyRound, Loader2, Monitor, Moon, Palette, PanelTop, Plus, RefreshCw, RotateCcw, Server, ShieldCheck, + Info, KeyRound, Loader2, Monitor, Moon, MousePointer2, Palette, PanelTop, Plus, RefreshCw, RotateCcw, Server, ShieldCheck, SlidersHorizontal, Sparkles, Star, Sun, Tag, Upload, Trash2, HardDrive, } from 'lucide-react'; import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card'; @@ -28,6 +28,7 @@ import { type DarkIntensity, type Density, type FontSpec, + type Palette as PaletteId, type SidebarStyle, type ThemeMode, type TimeFormat, @@ -39,6 +40,7 @@ import { TOPBAR_CATALOGUE } from '@/components/layout/top-bar'; import { ChangePasswordDialog } from '@/components/change-password-dialog'; import { ConfirmDialog } from '@/components/confirm-dialog'; import { SkeletonSwap } from '@/components/interior/skeleton-swap'; +import { SegmentedControl } from '@/components/interior/segmented-control'; import { useApi } from '@/lib/api'; import { useAppState } from '@/contexts/AppStateContext'; import { cn } from '@/lib/utils'; @@ -181,42 +183,29 @@ interface Opt { value: T; label: string; icon?: typeof Sun } // Associates a SettingRow's visible label with the control inside it (so the // segmented radiogroup gets an accessible name without prop-threading). -const RowLabelContext = createContext(undefined); +const RowLabelContext = createContext<{ id: string; label: string } | undefined>(undefined); // iOS/macOS segmented control: a recessed track with a raised pill on the // selected segment. Single-select → exposed as a radiogroup. Wraps gracefully. function Segmented({ value, options, onChange, disabled, }: { value: T; options: Opt[]; onChange: (v: T) => void; disabled?: boolean }) { - const labelledBy = useContext(RowLabelContext); + const rowLabel = useContext(RowLabelContext); return ( -
- {options.map((opt) => { - const active = value === opt.value; - const Icon = opt.icon; - return ( - - ); - })} +
+ ({ + value: String(option.value), + label: option.label, + disabled, + }))} + onValueChange={(next) => { + const option = options.find((candidate) => String(candidate.value) === next); + if (option) onChange(option.value); + }} + />
); } @@ -237,7 +226,7 @@ function SettingRow({ {hint &&

{hint}

}
); - const control = {children}; + const control = {children}; if (layout === 'stack') { return
{head}{control}
; } @@ -407,33 +396,29 @@ function AppearancePanel() {
-
- {PALETTE_OPTIONS.map((p) => { - const active = a.palette === p.id; - return ( - - ); - })} + ), + }))} + />
@@ -543,12 +528,22 @@ function AppearancePanel() { - - + + + setAppearance({ customContextMenu })} + ariaLabel="自定义右键菜单" + /> + + + + + setAppearance({ customPointerSystem })} - ariaLabel="自定义光标与右键菜单" + ariaLabel="自定义光标" /> diff --git a/packages/webui/src/components/settings/system-panel.tsx b/packages/webui/src/components/settings/system-panel.tsx index c93adbbc..9d07a612 100644 --- a/packages/webui/src/components/settings/system-panel.tsx +++ b/packages/webui/src/components/settings/system-panel.tsx @@ -10,10 +10,10 @@ import { AlertTriangle, Database, Download, Loader2, Lock, Pencil, Save, ShieldC import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { SkeletonSwap } from '@/components/interior/skeleton-swap'; +import { Dropdown } from '@/components/interior/dropdown'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; -import { Select } from '@/components/ui/select'; import { Textarea } from '@/components/ui/textarea'; import { ToggleSwitch } from '@/components/ui/toggle-switch'; import { ConfirmDialog } from '@/components/confirm-dialog'; @@ -281,10 +281,16 @@ export function SystemPanel() {
- +
diff --git a/packages/webui/src/components/ui/dropdown-select.tsx b/packages/webui/src/components/ui/dropdown-select.tsx index 164d4ada..d17e6fde 100644 --- a/packages/webui/src/components/ui/dropdown-select.tsx +++ b/packages/webui/src/components/ui/dropdown-select.tsx @@ -1,7 +1,4 @@ -import { useEffect, useRef, useState } from 'react'; -import { AnimatePresence, motion } from 'motion/react'; -import { Check, ChevronDown } from 'lucide-react'; -import { cn } from '@/lib/utils'; +import { Dropdown } from '@/components/interior/dropdown'; export interface DropdownOption { value: T; @@ -31,113 +28,13 @@ export function DropdownSelect({ ariaLabel, className, }: DropdownSelectProps) { - const [open, setOpen] = useState(false); - const [active, setActive] = useState(0); - const rootRef = useRef(null); - - const selected = options.find((o) => o.value === value); - - useEffect(() => { - if (!open) return; - // Seed the keyboard highlight on the current selection each time we open. - setActive(Math.max(0, options.findIndex((o) => o.value === value))); - const onPointer = (e: PointerEvent) => { - if (rootRef.current && !rootRef.current.contains(e.target as Node)) setOpen(false); - }; - document.addEventListener('pointerdown', onPointer); - return () => document.removeEventListener('pointerdown', onPointer); - }, [open]); // eslint-disable-line react-hooks/exhaustive-deps - - const choose = (v: T) => { - onChange(v); - setOpen(false); - }; - - const onKeyDown = (e: React.KeyboardEvent) => { - if (e.key === 'Escape' && open) { - // Swallow so the surrounding Dialog doesn't also close on the same key. - e.preventDefault(); - e.stopPropagation(); - setOpen(false); - return; - } - if (e.key === 'ArrowDown' || e.key === 'ArrowUp') { - e.preventDefault(); - if (!open) { - setOpen(true); - return; - } - setActive((i) => { - const n = options.length; - return e.key === 'ArrowDown' ? (i + 1) % n : (i - 1 + n) % n; - }); - return; - } - if (e.key === 'Enter' || e.key === ' ') { - e.preventDefault(); - if (!open) setOpen(true); - else if (options[active]) choose(options[active].value); - } - }; - return ( -
- - - - {open && ( - - {options.map((o, i) => { - const isSel = o.value === value; - const isActive = i === active; - return ( -
  • - -
  • - ); - })} -
    - )} -
    -
    + ({ ...option }))} + onChange={(next) => onChange(next as T)} + label={ariaLabel} + className={className} + /> ); } diff --git a/packages/webui/src/components/ui/global-context-menu.tsx b/packages/webui/src/components/ui/global-context-menu.tsx index 61703a9c..e2e00626 100644 --- a/packages/webui/src/components/ui/global-context-menu.tsx +++ b/packages/webui/src/components/ui/global-context-menu.tsx @@ -1,6 +1,4 @@ import * as React from 'react'; -import { createPortal } from 'react-dom'; -import { AnimatePresence, motion } from 'motion/react'; import { ArrowLeft, ClipboardPaste, @@ -13,6 +11,7 @@ import { TextSelect, type LucideIcon, } from 'lucide-react'; +import { ContextMenuSurface } from '@/components/interior/context-menu'; import { useTheme } from '@/contexts/ThemeContext'; type TextEditor = HTMLInputElement | HTMLTextAreaElement | HTMLElement; @@ -315,7 +314,7 @@ function buildActionGroups(snapshot: ContextSnapshot): MenuAction[][] { export function GlobalContextMenu() { const { appearance } = useTheme(); - const enabled = appearance.customPointerSystem; + const enabled = appearance.customContextMenu; const [menu, setMenu] = React.useState(null); const [position, setPosition] = React.useState({ x: -9999, y: -9999, ready: false }); const [busyAction, setBusyAction] = React.useState(null); @@ -448,26 +447,24 @@ export function GlobalContextMenu() { } }; - 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 }} - > + return ( + + {enabled && menu ? ( + <> {groups.map((group, groupIndex) => ( action.id).join(':')}> {groupIndex > 0 &&
    } @@ -502,9 +499,8 @@ export function GlobalContextMenu() { {error}
    )} -
    - )} -
    , - document.body, + + ) : null} + ); } diff --git a/packages/webui/src/components/ui/picker.tsx b/packages/webui/src/components/ui/picker.tsx index c9c9ae7b..c3c40433 100644 --- a/packages/webui/src/components/ui/picker.tsx +++ b/packages/webui/src/components/ui/picker.tsx @@ -6,8 +6,8 @@ // can still commit it as a raw value. Loading / error / empty states degrade to // the raw-value input so the picker never traps you. import { useEffect, useMemo, useRef, useState } from 'react'; -import { AnimatePresence, motion } from 'motion/react'; -import { ChevronDown, RefreshCw, Search } from 'lucide-react'; +import { AnimatePresence, motion, useReducedMotion } from 'motion/react'; +import { RefreshCw, Search } from 'lucide-react'; import { SkeletonSwap } from '@/components/interior/skeleton-swap'; import { cn } from '@/lib/utils'; @@ -39,11 +39,18 @@ interface PickerProps { const ROW_H = 44; // px — fixed row height drives the windowing math const OVERSCAN = 6; const PANEL_MAX = 264; // px of scroll viewport +const EASE = [0.23, 1, 0.32, 1] as const; +const EXIT = [0.4, 0, 1, 1] as const; +const NUDGE = { type: 'spring', stiffness: 700, damping: 46, mass: 0.5 } as const; +const SLIDE = { type: 'spring', stiffness: 700, damping: 46, mass: 0.5 } as const; +const OPEN = { type: 'spring', stiffness: 620, damping: 38, mass: 0.6 } as const; +const NONE = { duration: 0 } as const; export function Picker({ value, onChange, options, loading, error, onRefresh, placeholder = '选择…', validateRaw, ariaLabel, className, disabled, }: PickerProps) { + const reduced = useReducedMotion(); const [open, setOpen] = useState(false); const [query, setQuery] = useState(''); const [scrollTop, setScrollTop] = useState(0); @@ -145,17 +152,39 @@ export function Picker({ {selected ? selected.label : value || placeholder} {selected?.sub && {selected.sub}} - + + + {open && (
    @@ -204,6 +233,17 @@ export function Picker({ style={{ height: viewH }} >
    + 0 ? 1 : 0, + }} + transition={reduced ? NONE : { ...SLIDE, opacity: { duration: 0.1, ease: EASE } }} + /> {rows.map(({ idx, opt }) => (
    diff --git a/packages/webui/src/components/ui/select.tsx b/packages/webui/src/components/ui/select.tsx deleted file mode 100644 index 7f7387b7..00000000 --- a/packages/webui/src/components/ui/select.tsx +++ /dev/null @@ -1,30 +0,0 @@ -import * as React from 'react'; -import { ChevronDown } from 'lucide-react'; -import { cn } from '@/lib/utils'; - -// A styled native - {children} - - -
    - ) -); -Select.displayName = 'Select'; - -export { Select }; diff --git a/packages/webui/src/contexts/ThemeContext.tsx b/packages/webui/src/contexts/ThemeContext.tsx index cf8391a6..bcafbb69 100644 --- a/packages/webui/src/contexts/ThemeContext.tsx +++ b/packages/webui/src/contexts/ThemeContext.tsx @@ -112,6 +112,7 @@ export const DEFAULT_APPEARANCE: UiAppearance = { reduceMotion: false, disableMotion: false, customPointerSystem: false, + customContextMenu: true, highContrast: false, sidebarPinned: false, timeFormat: '24h', @@ -484,6 +485,17 @@ function authToken(): string | null { try { return localStorage.getItem(TOKEN_KEY); } catch { return null; } } +function withAppearanceDefaults(value: Partial): UiAppearance { + return { + ...DEFAULT_APPEARANCE, + ...value, + background: { + ...DEFAULT_APPEARANCE.background, + ...value.background, + }, + }; +} + async function fetchAppearance(): Promise { const token = authToken(); try { @@ -492,15 +504,15 @@ async function fetchAppearance(): Promise { if (token) { const res = await fetch('/api/ui', { headers: { Authorization: `Bearer ${token}` } }); if (res.ok) { - const data = (await res.json()) as { config?: { appearance?: UiAppearance } }; - if (data.config?.appearance) return data.config.appearance; + const data = (await res.json()) as { config?: { appearance?: Partial } }; + if (data.config?.appearance) return withAppearanceDefaults(data.config.appearance); } // 401/expired → fall through to the public subset. } const res = await fetch('/api/ui/public'); if (!res.ok) return null; - const data = (await res.json()) as { appearance?: UiAppearance }; - return data.appearance ?? null; + const data = (await res.json()) as { appearance?: Partial }; + return data.appearance ? withAppearanceDefaults(data.appearance) : null; } catch { return null; } @@ -593,7 +605,7 @@ function readCache(): UiAppearance { const parsed = JSON.parse(raw) as Partial; // Shallow merge over defaults so a cache written by an older build still // yields a complete object (the server is the real validator). - return { ...DEFAULT_APPEARANCE, ...parsed, background: { ...DEFAULT_APPEARANCE.background, ...parsed.background } }; + return withAppearanceDefaults(parsed); } catch { return DEFAULT_APPEARANCE; } diff --git a/packages/webui/src/index.css b/packages/webui/src/index.css index 6f8b2bf9..10fffa17 100644 --- a/packages/webui/src/index.css +++ b/packages/webui/src/index.css @@ -653,52 +653,45 @@ html[data-cursor-system="custom"] .adaptive-cursor[data-visible="true"] { 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); + padding: 5px; + border: 1px solid color-mix(in oklab, var(--border) 74%, transparent); + border-radius: 14px; + background: var(--popover); 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); + 0 1px 2px color-mix(in oklab, var(--foreground) 6%, transparent), + 0 16px 36px -18px color-mix(in oklab, var(--foreground) 50%, transparent); outline: none; - overflow: hidden; - backdrop-filter: blur(20px) saturate(1.15); + overflow-y: auto; + overscroll-behavior: contain; } .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); + margin: 4px; + background: var(--border); } .global-context-menu-item { display: grid; - grid-template-columns: 1.125rem minmax(0, 1fr) auto; + grid-template-columns: 1rem minmax(0, 1fr) auto; align-items: center; - gap: 0.625rem; + gap: 0.5rem; width: 100%; - min-height: 2.5rem; + height: 32px; padding: 0 0.625rem; border: 0; - border-radius: calc(var(--radius) - 0.125rem); + border-radius: 7px; 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 { @@ -709,7 +702,7 @@ html[data-cursor-system="custom"] .adaptive-cursor[data-visible="true"] { 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; + background: color-mix(in oklab, var(--primary) 16%, var(--accent)); } .global-context-menu-item:disabled { color: color-mix(in oklab, var(--muted-foreground) 62%, transparent); diff --git a/packages/webui/src/types.ts b/packages/webui/src/types.ts index 33f21fc8..4a3939ab 100644 --- a/packages/webui/src/types.ts +++ b/packages/webui/src/types.ts @@ -512,8 +512,10 @@ export interface UiAppearance { density: Density; reduceMotion: boolean; disableMotion: boolean; - /** Master switch for the adaptive cursor and custom context menu. */ + /** Use SnowLuma's adaptive cursor. */ customPointerSystem: boolean; + /** Replace the browser context menu with SnowLuma's context menu. */ + customContextMenu: boolean; highContrast: boolean; /** Pin the sidebar permanently expanded, opting out of the hover-expand rail. */ sidebarPinned: boolean;