Files
aigc-home/calliope-web/src/lib/components/shell/AppShell.svelte
T

657 lines
19 KiB
Svelte
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script lang="ts">
import { resolve } from "$app/paths";
import { onMount } from "svelte";
import { createQuery } from "@tanstack/svelte-query";
import { toStore } from "svelte/store";
import { page } from "$app/state";
import LanguageSwitcher from "$lib/components/LanguageSwitcher.svelte";
import Icon from "$lib/components/ui/Icon.svelte";
import MotionChoices from "./MotionChoices.svelte";
import { settings, jobsApi, comfy } from "$lib/api";
import { t } from "$lib/i18n.svelte";
import { reduceMotion } from "$lib/reduceMotion.svelte";
import CommandPalette from "./CommandPalette.svelte";
import ProjectScope from "./ProjectScope.svelte";
import QueueTray from "./QueueTray.svelte";
import { NAV_MAIN, NAV_MANAGE, type NavId } from "./nav";
import { storageGet, storageSet } from "$lib/storage";
import Modal from "$lib/components/ui/Modal.svelte";
import ThemeChoices from "./ThemeChoices.svelte";
import { themeStorageUnavailable } from "$lib/theme";
/** Desktop studio shell: six named sections, project scope, search,
* real queue and connection status. Individual tools keep their routes. */
interface Props {
workspace?: boolean;
/** The workspace supplies its own full-height header (creative desk). */
hideHeader?: boolean;
pageHeader?: import("svelte").Snippet<[{ openSearch: () => void }]>;
active: NavId;
title: string;
subtitle?: string;
/** Page-level actions rendered in the top bar (chips / buttons). */
actions?: import("svelte").Snippet;
children?: import("svelte").Snippet;
}
let {
title,
actions,
children,
pageHeader,
workspace = false,
hideHeader = false,
}: Props = $props();
let paletteOpen = $state(false);
// Real ComfyUI connections from settings — shown honestly without a
// fake "connected" claim; tested state lives on /connections (a6).
const settingsQuery = createQuery({
queryKey: ["settings"],
queryFn: settings.get,
staleTime: 60_000,
});
// Queue capsule / tray: the same image + video jobs the task page lists.
// Polled lightly; SSE-driven pages keep their own faster cadence.
const activeJobsQuery = createQuery({
queryKey: ["jobs", "active-count"],
queryFn: () => jobsApi.list(),
refetchInterval: 8_000,
staleTime: 5_000,
});
const genJobs = $derived(
($activeJobsQuery.data ?? []).filter(
(j) => j.kind === "image" || j.kind === "video",
),
);
// The ACTIVE ComfyUI connection (HEL-611). Its status dot comes from a
// real server-side probe (GET /system_stats from the workbench server,
// §3) — cyan only when that probe passes.
const activeEndpoint = $derived.by(() => {
const s = $settingsQuery.data;
if (!s) return null;
return (
s.comfyui_endpoints?.find((ep) => ep.id === s.comfyui_active_id) ?? null
);
});
const comfyUrl = $derived.by(() => {
const raw = (
activeEndpoint?.base_url ??
$settingsQuery.data?.comfyui_base_url ??
""
).trim();
if (!raw) return "";
try {
const u = new URL(raw.includes("://") ? raw : `http://${raw}`);
return u.host;
} catch {
return raw;
}
});
const activeId = $derived(activeEndpoint?.id ?? null);
const probeQuery = createQuery(
toStore(() => ({
queryKey: ["comfy-probe", activeId],
queryFn: () =>
comfy.testEndpoint({
endpoint_id: activeId ?? undefined,
timeout_sec: 5,
}),
enabled: activeId != null,
staleTime: 60_000,
refetchInterval: 60_000,
retry: false,
})),
);
const probeState = $derived.by<"unset" | "testing" | "ok" | "fail">(() => {
if (!activeEndpoint) return "unset";
if ($probeQuery.isPending) return "testing";
if ($probeQuery.isError) return "fail";
return $probeQuery.data?.ok ? "ok" : "fail";
});
const endpointName = $derived(activeEndpoint?.name?.trim() || "ComfyUI");
const connDetail = $derived.by(() => {
if (probeState === "unset") return t("shell.comfyUnset");
const state = t(`shell.comfy_${probeState}`, { name: endpointName });
if (probeState === "fail" && $probeQuery.data?.message)
return `${state}:${$probeQuery.data.message}`;
return comfyUrl ? `${state} · ${comfyUrl}` : state;
});
const connDot = $derived(
probeState === "ok" || probeState === "testing"
? "run"
: probeState === "fail"
? "fail"
: "idle",
);
// 范围胶囊: the project a page belongs to, read from its URL.
const scopeProjectId = $derived.by(() => {
const m = page.url.pathname.match(/^\/(?:workbench|project)\/(\d+)/);
return m ? Number(m[1]) : null;
});
function openPalette() {
paletteOpen = true;
}
function handleGlobalKeydown(event: KeyboardEvent) {
if (
(event.ctrlKey || event.metaKey) &&
!event.altKey &&
event.key.toLowerCase() === "k"
) {
event.preventDefault();
paletteOpen = !paletteOpen;
}
}
const mainNav = NAV_MAIN;
const currentNav = $derived(
[...NAV_MAIN, ...NAV_MANAGE].find((item) =>
item.matches(page.url.pathname),
),
);
const scene = $derived(
page.url.pathname === "/video" ||
(page.url.pathname === "/create" &&
page.url.searchParams.get("kind") === "video")
? "video"
: (currentNav?.scene ?? "manage"),
);
let themeOpen = $state(false);
let focus = $state(false);
let expanded = $state(false);
function toggleNavigation() {
expanded = !expanded;
storageSet("calliope.reference.navigation", String(expanded));
}
onMount(() => {
expanded = storageGet("calliope.reference.navigation") === "true";
const listener = (e: Event) => {
focus = (e as CustomEvent<boolean>).detail;
};
window.addEventListener("refract-focus", listener);
return () => window.removeEventListener("refract-focus", listener);
});
</script>
<svelte:head><title>{title} · AIGC工作台</title></svelte:head>
<svelte:window onkeydown={handleGlobalKeydown} />
<div
class="refract-shell"
class:focus
class:expanded
style:--scene={scene === "gold" ? "var(--gold)" : `var(--sc-${scene})`}
>
<aside class="sidebar" aria-label="全站导航">
<a class="brand" href={resolve("/")} aria-label="返回工作室"
><b>片</b>{#if expanded}<small>创作工作台</small>{/if}</a
>
<nav aria-label="主要导航">
{#each mainNav as item (item.id)}<a
class="nav-item"
href={resolve(item.href)}
style:--nav-c={item.scene === "gold"
? "var(--gold)"
: `var(--sc-${item.scene})`}
aria-current={item.matches(page.url.pathname) ? "page" : undefined}
title={item.id === "connections"
? `${item.label} · ${connDetail}`
: item.label}
><Icon name={item.icon} size={18} /><span>{item.label}</span></a
>{/each}
</nav>
<hr />
<nav class="management" aria-label="管理导航">
{#each NAV_MANAGE as item (item.id)}<a
class="nav-item"
href={resolve(item.href)}
style:--nav-c={item.scene === "gold"
? "var(--gold)"
: `var(--sc-${item.scene})`}
aria-current={item.matches(page.url.pathname) ? "page" : undefined}
title={item.id === "connections"
? `${item.label} · ${connDetail}`
: item.label}
><Icon name={item.icon} size={18} /><span>{item.label}</span></a
>{/each}
</nav>
<div class="sidebar-bottom">
<a
class="queue-dot"
href={resolve("/tasks")}
title={`${genJobs.filter((j) => j.status === "running" || j.status === "pending").length} 个任务进行中`}
aria-label="查看生成任务"
>
<Icon name="nav-queue" />
{#if genJobs.some((j) => j.status === "running") && reduceMotion.mode !== "off"}<span
class="queue-ring"
aria-hidden="true"
></span>{/if}
{#if genJobs.some((j) => j.status === "running" || j.status === "pending")}<i
>{genJobs.filter(
(j) => j.status === "running" || j.status === "pending",
).length}</i
>{/if}
</a>
<button
class="nav-item"
onclick={() => (themeOpen = true)}
aria-haspopup="dialog"
aria-expanded={themeOpen}
title="主题"
><span class="theme-dot" aria-hidden="true"></span><span>主题</span
></button
>
<a
class="nav-item"
href={resolve("/settings")}
aria-current={page.url.pathname === "/settings" ? "page" : undefined}
title="设置"><Icon name="nav-gear" size={18} /><span>设置</span></a
>
<button
class="nav-item"
onclick={toggleNavigation}
aria-expanded={expanded}
title={expanded ? "收起导航" : "展开导航"}
><Icon name="nav-palette" size={18} /><span
>{expanded ? "收起" : "导航"}</span
></button
>
{#if expanded}<a
class="connection"
href={resolve("/connections")}
title={connDetail}
aria-label={connDetail}
><span class="status-chip-dot {connDot}"></span>{#if expanded}<span
>{probeState === "ok"
? "本地服务已连接"
: probeState === "testing"
? "正在检测服务"
: probeState === "fail"
? "本地服务离线"
: "配置本地服务"}</span
>{/if}</a
>
{/if}
</div>
</aside>
<div
class="app-body"
style:background={pageHeader ? "var(--manage-page-background)" : undefined}
>
{#if pageHeader}{@render pageHeader({
openSearch: openPalette,
})}{:else if !hideHeader}
<header class="topbar">
<div class="breadcrumb">
{currentNav?.label ?? title}<span>/</span><span class="page-name"
>{title}</span
>
</div>
<div class="grow"></div>
{#if currentNav?.id === "connections"}
<a
class="a-btn secondary sm"
href={resolve("/connections")}
aria-current={page.url.pathname === "/connections"
? "page"
: undefined}>ComfyUI 连接</a
>
<a
class="a-btn secondary sm"
href={resolve("/models")}
aria-current={page.url.pathname === "/models" ? "page" : undefined}
>大语言模型</a
>
{/if}
{#if scopeProjectId}<ProjectScope
projectId={scopeProjectId}
onProjectsList={false}
/>{/if}
{#if actions}<div class="topbar-actions">{@render actions()}</div>{/if}
<button
class="search-trigger"
onclick={openPalette}
aria-label={t("shell.searchTitle")}
aria-keyshortcuts="Control+K Meta+K"
><Icon name="search" size={16} /><span>搜索</span><kbd>Ctrl K</kbd
></button
>
<MotionChoices />
<LanguageSwitcher /><QueueTray jobs={genJobs} />
</header>
{/if}
<div class="body" class:bounded={workspace}>{@render children?.()}</div>
</div>
</div>
<CommandPalette bind:open={paletteOpen} />
<Modal bind:open={themeOpen} title="界面主题" rail>
{#snippet headerActions()}<span class="theme-subtitle">同一布局与功能,只换灯光</span>{/snippet}
<ThemeChoices compact />
{#snippet footer()}
<div class="theme-motion"><span class="theme-foot-note" role="status"><Icon name={$themeStorageUnavailable ? "close" : "check"} size={18} />{$themeStorageUnavailable ? "浏览器禁止本地存储:本次有效,其他操作不受影响" : "已记住,下次打开沿用 · 正在编辑的内容不受影响"}</span><MotionChoices /></div>
{/snippet}
</Modal>
<style>
.theme-dot {
width: 18px;
height: 18px;
border-radius: 50%;
background: conic-gradient(
var(--gold) 0 25%,
var(--sc-image) 0 50%,
var(--bg-3) 0 75%,
var(--tx-1) 0
);
box-shadow: 0 0 0 1px var(--line-3);
}
.theme-subtitle { font-size: var(--theme-pop-note-size); color: var(--tx-3); }
.theme-foot-note { display: flex; align-items: center; gap: var(--theme-pop-footer-gap); }
.theme-motion {
flex-wrap: wrap;
--button-sm-height: var(--theme-pop-button-height);
--button-sm-padding: var(--theme-pop-button-padding);
--button-sm-font-size: var(--theme-pop-button-size);
--button-sm-radius: var(--theme-pop-button-radius);
display: flex;
align-items: center;
justify-content: space-between;
width: 100%;
gap: var(--theme-pop-footer-gap);
font-size: var(--theme-pop-note-size);
color: var(--tx-3);
}
.nav-item[aria-current="page"] :global(svg) {
color: var(--nav-c, var(--gold));
}
.refract-shell {
position: fixed;
inset: 0;
display: grid;
grid-template-columns: var(--rail-w) minmax(0, 1fr);
background: var(--bg);
color: var(--text);
min-width: 0;
}
.refract-shell.expanded {
grid-template-columns: var(--rail-expanded-width) minmax(0, 1fr);
}
.sidebar {
background: var(--rail-background);
border-right: 1px solid var(--line);
--icon-stroke: var(--manage-icon-stroke);
padding: var(--rail-padding);
align-items: center;
gap: 2px;
z-index: 5;
display: flex;
flex-direction: column;
min-height: 0;
overflow: visible;
}
.brand {
width: var(--rail-brand-size);
height: var(--rail-brand-size);
border-radius: var(--rail-brand-radius);
display: grid;
place-items: center;
margin-bottom: var(--rail-brand-margin);
position: relative;
background: var(--brand-grad);
box-shadow: var(--brand-glow);
text-decoration: none;
flex: none;
}
.brand b {
font: 800 15px/1 var(--font);
color: var(--brand-ink);
letter-spacing: -0.5px;
}
.sidebar hr {
width: 28px;
border: 0;
border-top: 1px solid var(--line-2);
margin: var(--rail-divider-margin);
flex: none;
}
.brand small {
display: block;
font-size: 10px;
letter-spacing: 1px;
color: var(--text-3);
font-weight: 400;
margin-top: 4px;
}
.sidebar nav {
display: flex;
flex-direction: column;
gap: 2px;
}
.nav-item {
box-sizing: border-box;
position: relative;
width: var(--rail-nav-width);
display: flex;
flex-direction: column;
align-items: center;
gap: 3px;
font: inherit;
padding: var(--rail-nav-padding);
color: var(--tx-3);
text-decoration: none;
border: 0;
background: none;
border-radius: var(--rail-nav-radius);
cursor: pointer;
transition:
color var(--t-base),
background var(--t-base);
flex-shrink: 0;
}
.nav-item[aria-current="page"] {
color: var(--tx-1);
background: linear-gradient(
180deg,
rgb(var(--ink) / 0.08),
rgb(var(--ink) / 0.03)
);
}
.nav-item[aria-current="page"]::before {
content: "";
position: absolute;
left: -6px;
top: 12px;
bottom: 12px;
width: 3px;
border-radius: 3px;
background: var(--nav-c, var(--gold));
box-shadow: var(
--rail-active-shadow,
0 0 var(--rail-active-shadow-blur) var(--nav-c, var(--gold))
);
}
.nav-item:active {
transform: scale(var(--press-scale));
}
.nav-item > span {
font-size: var(--rail-nav-size);
line-height: 1;
white-space: nowrap;
}
.expanded .sidebar {
overflow-y: auto;
}
.expanded .nav-item {
width: var(--rail-expanded-nav-width);
flex-direction: row;
justify-content: flex-start;
gap: 12px;
font-size: 12px;
padding: 10px 14px;
min-height: 43px;
}
.expanded .nav-item > span {
font-size: var(--rail-expanded-label-size);
}
.expanded .nav-item::before {
display: none;
}
.sidebar-bottom {
display: flex;
flex-direction: column;
align-items: center;
gap: 6px;
margin-top: auto;
}
.queue-dot {
box-sizing: border-box;
width: var(--rail-queue-size);
height: var(--rail-queue-size);
border-radius: var(--rail-brand-radius);
display: grid;
place-items: center;
position: relative;
color: var(--tx-2);
background: var(--bg-2);
border: 1px solid var(--line);
}
.queue-ring {
position: absolute;
inset: -1px;
border-radius: var(--rail-queue-ring-radius);
border: var(--rail-queue-ring-border) solid transparent;
border-top-color: var(--sc-video);
animation: var(--queue-loop, rail-spin) var(--rail-queue-ring-duration) linear infinite;
}
@keyframes -global-rail-spin {
to {
transform: rotate(360deg);
}
}
.queue-dot i {
position: absolute;
top: -4px;
right: -4px;
min-width: 16px;
height: 16px;
padding: 0 4px;
border-radius: 8px;
background: var(--sc-video);
color: var(--on-scene);
font: 700 10px/16px var(--mono);
font-style: normal;
}
.connection {
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
padding: 8px;
color: var(--text-3);
font-size: 10px;
text-decoration: none;
}
.app-body {
animation: a-page-enter var(--page-enter-duration) var(--ease-out);
display: flex;
flex-direction: column;
min-height: 0;
min-width: 0;
}
.topbar {
height: var(--head);
flex-shrink: 0;
background: var(--glass);
border-bottom: 1px solid var(--line);
display: flex;
align-items: center;
gap: 12px;
padding: 0 20px;
}
.breadcrumb {
display: flex;
align-items: center;
gap: 10px;
color: var(--text-3);
font-size: 11px;
}
.page-name {
color: var(--text-2);
}
.grow {
flex: 1;
}
.topbar-actions {
display: flex;
align-items: center;
gap: 8px;
}
.search-trigger {
display: flex;
align-items: center;
gap: 6px;
color: var(--text-3);
border: 1px solid var(--line);
background: var(--bg-3);
padding: 6px 10px;
border-radius: 7px;
font-size: 11px;
cursor: pointer;
}
.search-trigger kbd {
font-size: 9px;
color: var(--tx-3);
margin-left: 8px;
}
.body {
flex: 1;
min-height: 0;
overflow: auto;
}
.body.bounded {
overflow: hidden;
display: flex;
flex-direction: column;
}
.focus .sidebar,
.focus .topbar {
display: none;
}
.refract-shell.focus {
grid-template-columns: minmax(0, 1fr);
}
@media (hover: hover) and (pointer: fine) {
.nav-item:hover {
background-color: rgb(var(--ink) / 0.04);
color: var(--tx-1);
}
.search-trigger:hover {
border-color: var(--line-2);
}
}
@media (max-width: 1150px) {
.topbar {
gap: 8px;
padding: 0 14px;
}
.search-trigger kbd {
display: none;
}
.breadcrumb .page-name {
max-width: 180px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
}
</style>