635 lines
17 KiB
Svelte
635 lines
17 KiB
Svelte
<script lang="ts">
|
||
import { resolve } from "$app/paths";
|
||
import { onMount } from "svelte";
|
||
import { spotlight } from "$lib/actions/spotlight";
|
||
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 Switch from "$lib/components/ui/Switch.svelte";
|
||
import { settings, jobsApi, comfy } from "$lib/api";
|
||
import { t } from "$lib/i18n.svelte";
|
||
import { reduceMotion, setReduceMotion } 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";
|
||
|
||
/** Desktop studio shell: six named sections, project scope, search,
|
||
* real queue and connection status. Individual tools keep their routes. */
|
||
interface Props {
|
||
workspace?: boolean;
|
||
pageHeader?: import("svelte").Snippet;
|
||
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,
|
||
}: 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={`var(--sc-${scene})`}
|
||
>
|
||
<aside class="sidebar" aria-label="全站导航">
|
||
<a class="brand" href={resolve('/')} aria-label="返回工作室"
|
||
>AIGC{#if expanded}<small>创作工作台</small>{/if}</a
|
||
>
|
||
<nav aria-label="主要导航">
|
||
{#each mainNav as item (item.id)}<a
|
||
class="nav-item reference-option"
|
||
use:spotlight
|
||
href={resolve(item.href)}
|
||
style:--nav-c={`var(--sc-${item.scene})`}
|
||
aria-current={item.matches(page.url.pathname) ? "page" : undefined}
|
||
title={item.label}
|
||
><Icon name={item.icon} size={20} /><span>{item.label}</span></a
|
||
>{/each}
|
||
</nav>
|
||
<nav class="management" aria-label="管理导航">
|
||
{#each NAV_MANAGE as item (item.id)}<a
|
||
class="nav-item reference-option"
|
||
use:spotlight
|
||
href={resolve(item.href)}
|
||
style:--nav-c={`var(--sc-${item.scene})`}
|
||
aria-current={item.matches(page.url.pathname) ? "page" : undefined}
|
||
title={item.label}
|
||
><Icon name={item.icon} size={18} /><span>{item.label}</span></a
|
||
>{/each}
|
||
</nav>
|
||
<div class="sidebar-bottom">
|
||
<button
|
||
class="nav-item reference-option"
|
||
use:spotlight
|
||
onclick={toggleNavigation}
|
||
aria-expanded={expanded}
|
||
title={expanded ? "收起导航" : "展开导航"}
|
||
><Icon name="chevron-right" size={18} /><span
|
||
>{expanded ? "收起导航" : "展开导航"}</span
|
||
></button
|
||
>
|
||
<button
|
||
class="nav-item reference-option"
|
||
use:spotlight
|
||
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 reference-option"
|
||
use:spotlight
|
||
href={resolve('/settings')}
|
||
aria-current={page.url.pathname === "/settings" ? "page" : undefined}
|
||
title="设置"><Icon name="settings" size={18} /><span>设置</span></a
|
||
>
|
||
<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
|
||
>
|
||
</div>
|
||
</aside>
|
||
<div
|
||
class="app-body"
|
||
style:background={pageHeader ? "var(--manage-page-background)" : undefined}
|
||
>
|
||
{#if pageHeader}{@render pageHeader()}{:else}
|
||
<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
|
||
>
|
||
<label class="motion"
|
||
><Switch
|
||
checked={reduceMotion.system || reduceMotion.manual}
|
||
disabled={reduceMotion.system}
|
||
label={t("shell.reduceMotion")}
|
||
onchange={setReduceMotion}
|
||
/><span>减少动效</span></label
|
||
>
|
||
<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="外观主题">
|
||
<ThemeChoices compact />
|
||
<label class="theme-motion"
|
||
><Switch
|
||
checked={reduceMotion.system || reduceMotion.manual}
|
||
disabled={reduceMotion.system}
|
||
label={t("shell.reduceMotion")}
|
||
onchange={setReduceMotion}
|
||
/><span>减少动效</span></label
|
||
>
|
||
</Modal>
|
||
|
||
<style>
|
||
.management {
|
||
border-top: 1px solid var(--line-2);
|
||
margin-top: 8px;
|
||
padding-top: 8px;
|
||
}
|
||
.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-motion {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 8px;
|
||
margin-top: 14px;
|
||
padding-top: 12px;
|
||
border-top: 1px solid var(--line);
|
||
font-size: 12px;
|
||
color: var(--tx-2);
|
||
}
|
||
.nav-item[aria-current="page"] :global(svg) {
|
||
color: var(--nav-c, var(--scene));
|
||
}
|
||
|
||
.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: 174px minmax(0, 1fr);
|
||
}
|
||
.sidebar {
|
||
background: var(--bg-1);
|
||
border-right: 1px solid var(--line);
|
||
padding: 16px 8px 9px;
|
||
display: flex;
|
||
flex-direction: column;
|
||
min-height: 0;
|
||
overflow: auto;
|
||
scrollbar-width: thin;
|
||
}
|
||
.brand {
|
||
font-size: 19px;
|
||
font-weight: 800;
|
||
color: var(--gold);
|
||
text-align: center;
|
||
text-decoration: none;
|
||
letter-spacing: -1px;
|
||
display: block;
|
||
margin: 2px 0 18px;
|
||
text-shadow: var(--brand-glow);
|
||
}
|
||
.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 {
|
||
min-height: 44px;
|
||
display: flex;
|
||
flex-direction: column;
|
||
justify-content: center;
|
||
align-items: center;
|
||
gap: 5px;
|
||
font-size: 10px;
|
||
line-height: 1;
|
||
padding: 7px 3px;
|
||
color: var(--tx-3);
|
||
text-decoration: none;
|
||
border: 0;
|
||
background: none;
|
||
border-radius: var(--r-md);
|
||
cursor: pointer;
|
||
font-family: inherit;
|
||
transition:
|
||
background-color 180ms ease,
|
||
color 180ms ease,
|
||
transform 160ms var(--ease-out);
|
||
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: -2px;
|
||
top: 12px;
|
||
bottom: 12px;
|
||
width: 3px;
|
||
border-radius: 3px;
|
||
background: var(--nav-c, var(--scene));
|
||
}
|
||
.nav-item:active {
|
||
transform: scale(var(--press-scale));
|
||
}
|
||
.nav-item > span {
|
||
white-space: nowrap;
|
||
}
|
||
.expanded .nav-item {
|
||
flex-direction: row;
|
||
justify-content: flex-start;
|
||
gap: 12px;
|
||
font-size: 12px;
|
||
padding: 10px 14px;
|
||
min-height: 43px;
|
||
}
|
||
.expanded .nav-item::before {
|
||
display: none;
|
||
}
|
||
.sidebar-bottom {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 2px;
|
||
margin-top: auto;
|
||
padding-top: 8px;
|
||
}
|
||
.sidebar-bottom .nav-item {
|
||
min-height: 45px;
|
||
font-size: 9px;
|
||
padding: 7px 3px;
|
||
gap: 4px;
|
||
}
|
||
.expanded .sidebar-bottom .nav-item {
|
||
min-height: 39px;
|
||
padding: 9px 14px;
|
||
font-size: 12px;
|
||
}
|
||
.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 {
|
||
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;
|
||
}
|
||
.motion {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 6px;
|
||
font-size: 10px;
|
||
color: var(--text-3);
|
||
}
|
||
.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;
|
||
}
|
||
}
|
||
@media (max-height: 780px) {
|
||
.sidebar {
|
||
padding-top: 11px;
|
||
}
|
||
.brand {
|
||
margin-bottom: 8px;
|
||
}
|
||
.nav-item {
|
||
min-height: 40px;
|
||
padding: 5px 0 3px;
|
||
gap: 3px;
|
||
}
|
||
.sidebar-bottom {
|
||
gap: 0;
|
||
padding-top: 12px;
|
||
}
|
||
.sidebar-bottom .nav-item {
|
||
min-height: 37px;
|
||
}
|
||
}
|
||
|
||
@media (max-height: 740px) {
|
||
.sidebar {
|
||
padding-top: 10px;
|
||
}
|
||
.brand {
|
||
margin-bottom: 12px;
|
||
}
|
||
.sidebar nav {
|
||
gap: 4px;
|
||
}
|
||
.nav-item {
|
||
min-height: 38px;
|
||
padding: 4px 2px;
|
||
gap: 3px;
|
||
}
|
||
.sidebar-bottom {
|
||
padding-top: 8px;
|
||
}
|
||
.sidebar-bottom .nav-item {
|
||
min-height: 31px;
|
||
padding: 4px 2px;
|
||
}
|
||
}
|
||
</style>
|