import { AnimatePresence, motion, useReducedMotion } from "motion/react"; import { useEffect, useRef, useState, type CSSProperties, type ReactNode } from "react"; import { Link } from "react-router"; import { Bot, Check, ChevronDown, Clapperboard, CloudDownload, Coins, CopyPlus, Focus, FolderKanban, Gauge, Home, LayoutGrid, LoaderCircle, Menu, Pencil, Plus, Redo2, Search, Settings2, Share2, Sparkles, Trash2, Undo2, Upload } from "lucide-react"; import { Button, Dropdown } from "antd"; import { useWalletBalance } from "@/hooks/use-wallet-balance"; import { aceternityMotion } from "@/lib/aceternity-motion"; import { canvasDockStyle } from "@/lib/canvas/canvas-aceternity-style"; import type { CanvasContextSummary } from "@/lib/canvas/canvas-context-summary"; import type { CanvasShortDramaProgress } from "@/lib/canvas/canvas-short-drama"; import { canvasThemes } from "@/lib/canvas-theme"; import { useThemeStore } from "@/stores/use-theme-store"; import { useUserStore } from "@/stores/use-user-store"; import type { CanvasMediaPerformanceMode, CanvasWorkspaceMode } from "@/types/canvas"; import { CanvasShortcutsModal } from "./canvas-shortcuts-modal"; type CanvasTopBarProps = { title: string; titleDraft: string; isTitleEditing: boolean; onTitleDraftChange: (value: string) => void; onStartTitleEditing: () => void; onFinishTitleEditing: () => void; onCancelTitleEditing: () => void; canUndo: boolean; canRedo: boolean; onCreateProject: () => void; onDeleteProject: () => void; onImportImage: () => void; onImportLibTV: () => void; onImportTapNow: () => void; onUndo: () => void; onRedo: () => void; onShare: () => void; agentOpen: boolean; compactAgentStatus?: { connected: boolean; enabled: boolean; activity: string }; onToggleAgent: () => void; shortcutRequestNonce: number; mediaPerformanceMode: CanvasMediaPerformanceMode; onMediaPerformanceModeChange: (mode: CanvasMediaPerformanceMode) => void; onOpenSearch: () => void; projectContext?: CanvasContextSummary & { projectId: string; projectName: string }; onEnterFocusMode: () => void; shortDramaGuide?: { progress: CanvasShortDramaProgress; collapsed: boolean; onToggle: () => void }; }; export function CanvasTopBar({ title, titleDraft, isTitleEditing, onTitleDraftChange, onStartTitleEditing, onFinishTitleEditing, onCancelTitleEditing, canUndo, canRedo, onCreateProject, onDeleteProject, onImportImage, onImportLibTV, onImportTapNow, onUndo, onRedo, onShare, agentOpen, compactAgentStatus, onToggleAgent, shortcutRequestNonce, mediaPerformanceMode, onMediaPerformanceModeChange, onOpenSearch, projectContext, onEnterFocusMode, shortDramaGuide, }: CanvasTopBarProps) { const theme = canvasThemes[useThemeStore((state) => state.theme)]; const dockStyle = canvasDockStyle(theme, theme.node.text); const user = useUserStore((state) => state.user); const creditsEnabled = useUserStore((state) => state.features.creditsEnabled); const { availableMicrocredits, refreshing } = useWalletBalance(user?.id, creditsEnabled); const titleRef = useRef(null); const [shortcutsOpen, setShortcutsOpen] = useState(false); const handleShortDramaGuideToggle = () => { shortDramaGuide?.onToggle(); }; useEffect(() => { if (shortcutRequestNonce > 0) setShortcutsOpen(true); }, [shortcutRequestNonce]); useEffect(() => { if (!isTitleEditing) return; const close = (event: PointerEvent) => { if (!titleRef.current?.contains(event.target as Node)) onFinishTitleEditing(); }; document.addEventListener("pointerdown", close, true); return () => document.removeEventListener("pointerdown", close, true); }, [isTitleEditing, onFinishTitleEditing]); return ( <>
, label: 主页 }, { key: "projects", icon: , label: 画布 }, { type: "divider" }, { key: "new", icon: , label: "新建画布", onClick: onCreateProject }, { key: "delete", danger: true, icon: , label: "删除当前画布", onClick: onDeleteProject }, { type: "divider" }, { key: "import", icon: , label: "导入素材", onClick: onImportImage }, { key: "search", icon: , label: , onClick: onOpenSearch }, { key: "performance", icon: , label: "媒体性能", children: [ { key: "performance-auto", label: "自动性能", onClick: () => onMediaPerformanceModeChange("auto") }, { key: "performance-quality", label: "画质优先", onClick: () => onMediaPerformanceModeChange("quality") }, { key: "performance-fast", label: "性能优先", onClick: () => onMediaPerformanceModeChange("performance") }, ], }, { type: "divider" }, { key: "undo", disabled: !canUndo, icon: , label: , onClick: onUndo }, { key: "redo", disabled: !canRedo, icon: , label: , onClick: onRedo }, ], }} >
{isTitleEditing ? ( onTitleDraftChange(event.target.value)} onBlur={onFinishTitleEditing} onKeyDown={(event) => { if (event.key === "Enter") onFinishTitleEditing(); if (event.key === "Escape") onCancelTitleEditing(); }} className="h-8 w-auto min-w-12 max-w-[min(280px,42vw)] appearance-none border-0 bg-transparent p-0 text-left text-base font-semibold tracking-normal outline-none ring-0 focus:outline-none focus:ring-0 focus-visible:outline-none focus-visible:ring-0" style={{ color: theme.node.text, caretColor: theme.accent.primary, border: 0, boxShadow: "none", outline: "none" }} aria-label="画布名称" /> ) : (
)} {projectContext && !isTitleEditing ? (
{projectContext.projectName} ·
) : null}
onMediaPerformanceModeChange(key as CanvasMediaPerformanceMode), items: [ { key: "auto", label: "自动性能" }, { key: "quality", label: "画质优先" }, { key: "performance", label: "性能优先" }, ], }} > ) : null}
setShortcutsOpen(false)} /> ); } export function CanvasWorkspaceModeSwitch({ mode, onChange }: { mode: CanvasWorkspaceMode; onChange: (mode: CanvasWorkspaceMode) => void }) { const theme = canvasThemes[useThemeStore((state) => state.theme)]; const reducedMotion = useReducedMotion(); const simple = mode === "simple"; const rootRef = useRef(null); const [open, setOpen] = useState(false); useEffect(() => { if (!open) return; const closeOnOutsidePress = (event: PointerEvent) => { if (event.target instanceof Node && !rootRef.current?.contains(event.target)) setOpen(false); }; const closeOnEscape = (event: KeyboardEvent) => { if (event.key === "Escape") setOpen(false); }; document.addEventListener("pointerdown", closeOnOutsidePress); document.addEventListener("keydown", closeOnEscape); return () => { document.removeEventListener("pointerdown", closeOnOutsidePress); document.removeEventListener("keydown", closeOnEscape); }; }, [open]); const selectMode = (nextMode: CanvasWorkspaceMode) => { if (nextMode !== mode) onChange(nextMode); setOpen(false); }; return (
setOpen((value) => !value)} > {simple ? : } {simple ? "简洁模式" : "专业模式"}
{open ? ( } title="简洁模式" description="保留核心创作路径,降低参数密度" theme={theme} onClick={() => selectMode("simple")} /> } title="专业模式" description="显示完整节点、导演台与生成控制" theme={theme} onClick={() => selectMode("professional")} /> ) : null}
); } type CanvasTheme = (typeof canvasThemes)[keyof typeof canvasThemes]; function CanvasTopBarTooltip({ label, children }: { label: string; children: ReactNode }) { return ( {children} {label} ); } function ModeOption({ active, motionEnabled, icon, title, description, theme, onClick }: { active: boolean; motionEnabled: boolean; icon: ReactNode; title: string; description: string; theme: CanvasTheme; onClick: () => void }) { return ( {icon} {title}{description} ); } function MenuLabel({ text, shortcut }: { text: string; shortcut: string }) { return ( {text} {shortcut} ); } function canvasTitleInputSize(value: string) { const visualLength = Array.from(value || "画布名称").reduce((length, character) => length + (character.codePointAt(0)! > 0xff ? 2 : 1), 0); return Math.min(30, Math.max(5, visualLength)); } function CompactAgentStatus({ status, onClick }: { status: { connected: boolean; enabled: boolean; activity: string }; onClick: () => void }) { const theme = canvasThemes[useThemeStore((state) => state.theme)]; const label = status.connected ? "已连接到本地 Codex" : status.enabled ? status.activity || "连接中" : "正在连接本地 Codex"; const dotColor = status.connected ? "#22c55e" : status.enabled ? "#f59e0b" : theme.node.muted; return ; }