| 1 | import { InlineSelect } from "./inline-select"; |
| 2 | import React, { useEffect, useRef, useState } from "react"; |
| 3 | import { |
| 4 | Download, |
| 5 | Monitor, |
| 6 | PanelLeftOpen, |
| 7 | Power, |
| 8 | RefreshCw, |
| 9 | } from "lucide-react"; |
| 10 | import { BrowserUploads } from "./browser-uploads"; |
| 11 | import { ProjectDialog } from "./project-dialog"; |
| 12 | import { api } from "./api"; |
| 13 | import { SafetyMessage } from "./safety-message"; |
| 14 | import { RemoteView } from "./views"; |
| 15 | import { useWindowVisibility } from "./window-visibility"; |
| 16 | import "./claude-desktop.css"; |
| 17 | |
| 18 | const desktopZooms = [0.75, 0.9, 1, 1.1, 1.25, 1.5]; |
| 19 | |
| 20 | export function useDesktopZoom() { |
| 21 | const [zoom, setZoom] = useState(() => { |
| 22 | try { |
| 23 | const value = Number(localStorage.getItem("cage-desktop-zoom")); |
| 24 | return desktopZooms.includes(value) ? value : 1; |
| 25 | } catch { return 1; } |
| 26 | }); |
| 27 | return [zoom, setZoom]; |
| 28 | } |
| 29 | |
| 30 | export function DesktopToolbar({ desktop, platform, zoom, setZoom, onConfirm, onStop, busy, refresh, sidebarCollapsed, onExpand, children }) { |
| 31 | return ( |
| 32 | <header className="claude-desktop-toolbar"> |
| 33 | {sidebarCollapsed && ( |
| 34 | <button |
| 35 | className="icon-button" |
| 36 | aria-label="展开侧栏" |
| 37 | onClick={onExpand} |
| 38 | > |
| 39 | <PanelLeftOpen size={17} /> |
| 40 | </button> |
| 41 | )} |
| 42 | <Monitor size={18} /> |
| 43 | <strong>Claude Desktop</strong> |
| 44 | <span className="desktop-version"> |
| 45 | {desktop?.version ? `v${desktop.version}` : platform} |
| 46 | </span> |
| 47 | <span className="desktop-toolbar-spacer" /> |
| 48 | {children} |
| 49 | {desktop?.running && ( |
| 50 | <label className="desktop-display-size"> |
| 51 | 显示大小 |
| 52 | <InlineSelect aria-label="Desktop 显示大小" value={zoom} onValueChange={(next) => { |
| 53 | const value = Number(next); |
| 54 | setZoom(value); |
| 55 | try { localStorage.setItem("cage-desktop-zoom", String(value)); } catch { /* Session-only preference. */ } |
| 56 | }}> |
| 57 | {desktopZooms.map((value) => <option key={value} value={value}>{Math.round(value * 100)}%{value === 1 ? "(默认)" : ""}</option>)} |
| 58 | </InlineSelect> |
| 59 | </label> |
| 60 | )} |
| 61 | {desktop?.running && ( |
| 62 | <button |
| 63 | className="quiet-button" |
| 64 | disabled={busy} |
| 65 | onClick={async () => { |
| 66 | if ( |
| 67 | await onConfirm({ |
| 68 | title: "关闭 Claude Desktop?", |
| 69 | body: "请先保存应用内的工作。浏览器和 Cage 终端会继续运行。", |
| 70 | confirm: "关闭应用", |
| 71 | }) |
| 72 | ) |
| 73 | onStop(); |
| 74 | }} |
| 75 | > |
| 76 | <Power size={15} /> |
| 77 | 关闭应用 |
| 78 | </button> |
| 79 | )} |
| 80 | <button |
| 81 | className="icon-button" |
| 82 | aria-label="刷新 Claude Desktop 状态" |
| 83 | disabled={busy} |
| 84 | onClick={refresh} |
| 85 | > |
| 86 | <RefreshCw size={15} /> |
| 87 | </button> |
| 88 | </header> |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | export function ClaudeDesktop({ |
| 93 | uploads = [], |
| 94 | status, |
| 95 | onProjectOpened, |
| 96 | visible, |
| 97 | remoteRef, |
| 98 | onStatus, |
| 99 | onFallback, |
| 100 | onConfirm, |
| 101 | onDetails, |
| 102 | onExpand, |
| 103 | sidebarCollapsed, |
| 104 | }) { |
| 105 | const [closedChooser, setClosedChooser] = useState(""); |
| 106 | const directoryRequest = uploads.find((item) => item.directory && item.id !== closedChooser); |
| 107 | const [zoom, setZoom] = useDesktopZoom(); |
| 108 | const windowVisible = useWindowVisibility(); |
| 109 | const screenVisible = visible && windowVisible; |
| 110 | const [desktop, setDesktop] = useState(null); |
| 111 | const [error, setError] = useState(""); |
| 112 | const [statusError, setStatusError] = useState(""); |
| 113 | const [submitting, setSubmitting] = useState(false); |
| 114 | const [jobID, setJobID] = useState(""); |
| 115 | const opened = useRef(false); |
| 116 | const statusRequest = useRef(null); |
| 117 | const statusRevision = useRef(0); |
| 118 | const ownJob = status.job?.kind?.startsWith("claude-desktop-"); |
| 119 | const busy = submitting || Boolean(status.job?.running) || Boolean(jobID); |
| 120 | const refresh = async (valid = () => true) => { |
| 121 | const revision = statusRevision.current; |
| 122 | const request = statusRequest.current ??= api("/claude-desktop"); |
| 123 | try { |
| 124 | const value = await request; |
| 125 | if (!valid() || revision !== statusRevision.current) return null; |
| 126 | setDesktop(value); |
| 127 | setStatusError(""); |
| 128 | return value; |
| 129 | } catch (e) { |
| 130 | if (valid() && revision === statusRevision.current) setStatusError(e.message); |
| 131 | return null; |
| 132 | } finally { |
| 133 | if (statusRequest.current === request) statusRequest.current = null; |
| 134 | } |
| 135 | }; |
| 136 | const run = async (action) => { |
| 137 | statusRevision.current++; |
| 138 | statusRequest.current = null; |
| 139 | setSubmitting(true); |
| 140 | setError(""); |
| 141 | try { |
| 142 | const job = await api(`/claude-desktop/${action}`, {}); |
| 143 | setJobID(job.id); |
| 144 | } catch (e) { |
| 145 | setError(e.message); |
| 146 | } finally { |
| 147 | setSubmitting(false); |
| 148 | } |
| 149 | }; |
| 150 | useEffect(() => { |
| 151 | if (!screenVisible || !status.vm_running || status.job?.running) return; |
| 152 | let disposed = false; |
| 153 | let timer; |
| 154 | const poll = async () => { |
| 155 | const value = await refresh(() => !disposed); |
| 156 | if (disposed) return; |
| 157 | if (value && !opened.current) { |
| 158 | opened.current = true; |
| 159 | if (value.installed && !value.running && !value.failed && !jobID) |
| 160 | await run("start"); |
| 161 | } |
| 162 | if (!disposed) timer = setTimeout(poll, 5000); |
| 163 | }; |
| 164 | poll(); |
| 165 | return () => { |
| 166 | disposed = true; |
| 167 | clearTimeout(timer); |
| 168 | }; |
| 169 | }, [screenVisible, status.vm_running, status.job?.running]); |
| 170 | useEffect(() => { |
| 171 | if (!jobID || status.job?.id !== jobID || status.job.running) return; |
| 172 | setJobID(""); |
| 173 | if (status.job.error) { |
| 174 | setError(status.job.error); |
| 175 | return; |
| 176 | } |
| 177 | if (status.job.kind === "claude-desktop-install") run("start"); |
| 178 | else refresh(); |
| 179 | }, [status.job]); |
| 180 | return ( |
| 181 | <section className="claude-desktop-view" aria-label="Claude Desktop"> |
| 182 | <BrowserUploads requests={uploads} onStatus={onStatus} /> |
| 183 | {directoryRequest && <ProjectDialog |
| 184 | key={directoryRequest.id} |
| 185 | status={status} |
| 186 | activate={false} |
| 187 | onClose={() => { |
| 188 | setClosedChooser(directoryRequest.id); |
| 189 | void api("/input/files/cancel", { target: "desktop", id: directoryRequest.id }).catch((e) => onStatus(e.message)); |
| 190 | }} |
| 191 | onOpened={async (workspace) => { |
| 192 | await api("/input/files/directory", { target: "desktop", id: directoryRequest.id, workspace }); |
| 193 | setClosedChooser(directoryRequest.id); |
| 194 | await onProjectOpened(workspace); |
| 195 | }} |
| 196 | />} |
| 197 | <DesktopToolbar desktop={desktop} platform={status.guest_system === "windows-11" ? "Windows" : "Linux"} zoom={zoom} setZoom={setZoom} |
| 198 | onConfirm={onConfirm} onStop={() => run("stop")} busy={busy} refresh={() => refresh()} sidebarCollapsed={sidebarCollapsed} onExpand={onExpand} /> |
| 199 | {(error || statusError || desktop?.failed) && ( |
| 200 | <div className="connection-banner" role="alert"> |
| 201 | <SafetyMessage message={error || statusError || (desktop?.failure_reason === "oom-kill" |
| 202 | ? "环境内存不足,Claude Desktop 已被系统关闭。请在设置中检查计算资源后重新打开。" |
| 203 | : "Claude Desktop 未能启动,可重试打开或修复安装。")} /> |
| 204 | <button onClick={onDetails}>操作详情</button> |
| 205 | </div> |
| 206 | )} |
| 207 | {desktop?.running && desktop.managed === false && !desktop.native_user_session && <div className="connection-banner" role="status">当前应用尚未接入 Cage 账户解锁。请先保存并关闭应用,再重新打开。</div>} |
| 208 | {busy && ownJob && ( |
| 209 | <div className="desktop-progress" role="status"> |
| 210 | <RefreshCw size={16} className="spin" /> |
| 211 | {status.job.message} |
| 212 | <button className="text-button" onClick={onDetails}> |
| 213 | 查看进度 |
| 214 | </button> |
| 215 | </div> |
| 216 | )} |
| 217 | <div className="desktop-screen"> |
| 218 | {desktop?.running && status.vm_running ? ( |
| 219 | <RemoteView |
| 220 | ref={remoteRef} |
| 221 | desktop |
| 222 | zoom={zoom} |
| 223 | visible={visible} |
| 224 | keyboard={status.keyboard} |
| 225 | guestPlatform={status.guest_system?.startsWith("windows") ? "windows" : status.guest_system} |
| 226 | onStatus={onStatus} |
| 227 | onFallback={onFallback} |
| 228 | /> |
| 229 | ) : ( |
| 230 | <div className="desktop-empty"> |
| 231 | <Monitor size={36} /> |
| 232 | <h2> |
| 233 | {desktop?.installed |
| 234 | ? "Claude Desktop" |
| 235 | : "在 Cage 中使用 Claude Desktop"} |
| 236 | </h2> |
| 237 | <p> |
| 238 | Chat 和 Code 共用一个官方应用。在 Cage |
| 239 | 内安装、登录并使用,项目文件来自已打开的文件夹。 |
| 240 | </p> |
| 241 | {!desktop?.installed && ( |
| 242 | <p> |
| 243 | 首次使用时从 Anthropic 官方源下载,不增加 Cage 默认安装包体积。 |
| 244 | </p> |
| 245 | )} |
| 246 | <div className="desktop-actions"> |
| 247 | {desktop && ( |
| 248 | <button |
| 249 | className="primary" |
| 250 | disabled={busy || !status.vm_running} |
| 251 | onClick={() => run(desktop.installed ? "start" : "install")} |
| 252 | > |
| 253 | {desktop.installed ? ( |
| 254 | <Monitor size={16} /> |
| 255 | ) : ( |
| 256 | <Download size={16} /> |
| 257 | )} |
| 258 | {desktop.installed ? "打开 Claude Desktop" : "下载并安装"} |
| 259 | </button> |
| 260 | )} |
| 261 | {desktop?.installed && ( |
| 262 | <button |
| 263 | className="quiet-button" |
| 264 | disabled={busy} |
| 265 | onClick={() => run("install")} |
| 266 | > |
| 267 | 更新 / 修复安装 |
| 268 | </button> |
| 269 | )} |
| 270 | {!desktop && ( |
| 271 | <button |
| 272 | className="quiet-button" |
| 273 | disabled={busy} |
| 274 | onClick={refresh} |
| 275 | > |
| 276 | 重新读取状态 |
| 277 | </button> |
| 278 | )} |
| 279 | </div> |
| 280 | <p className="desktop-note"> |
| 281 | 关闭应用后释放图形会话资源,登录状态保留在 Cage |
| 282 | 环境中。需要额外虚拟机的 Cowork 功能暂未适配。 |
| 283 | </p> |
| 284 | </div> |
| 285 | )} |
| 286 | </div> |
| 287 | </section> |
| 288 | ); |
| 289 | } |