diff --git a/changes/vm-create-progress-view.json b/changes/vm-create-progress-view.json new file mode 100644 index 0000000..3185b16 --- /dev/null +++ b/changes/vm-create-progress-view.json @@ -0,0 +1,7 @@ +{ + "group": "improvements", + "order": 0, + "authors": ["Nwflower"], + "zh": "新建虚拟机时在左侧栏显示正在创建的条目,并在主界面直接展示创建进度、步骤说明与取消入口 (#120)。", + "en": "Show in-progress VM creation in the sidebar and display its progress, current step, and cancel action directly in the main view (#120)." +} diff --git a/frontend/scripts/test-tasks.mjs b/frontend/scripts/test-tasks.mjs index 28f5f47..7629b25 100644 --- a/frontend/scripts/test-tasks.mjs +++ b/frontend/scripts/test-tasks.mjs @@ -2,7 +2,7 @@ import assert from 'node:assert/strict' import test from 'node:test' import { loadTestModule } from './load-test-module.mjs' -const { currentTask, findActiveOfflineTask, pruneFinishedTasks, taskDetail, upsertTask } = +const { creatingVMTasks, currentTask, findActiveOfflineTask, pruneFinishedTasks, taskDetail, upsertTask } = await loadTestModule(`export * from './lib/tasks'`) const task = (id, overrides = {}) => ({ @@ -40,3 +40,13 @@ test('taskDetail formats download speeds under 1 MB/s in KB/s', () => { assert.equal(taskDetail(task('fast', { done: 2e9, total: 10e9, rate: 12.5e6 })), '2.0 GB / 10.0 GB · 12.5 MB/s') assert.equal(taskDetail(task('gig', { done: 2e9, total: 10e9, rate: 1.2e9 })), '2.0 GB / 10.0 GB · 1.2 GB/s') }) + +test('creatingVMTasks lists only running vm-create tasks not yet present in vms, oldest first', () => { + const first = task('t1', { subject: 'macos-a', started_at: 10 }) + const second = task('t2', { subject: 'macos-b', started_at: 20 }) + const registered = task('t3', { subject: 'macos-ready', started_at: 5 }) + const finished = task('t4', { subject: 'macos-done', status: 'done', started_at: 1 }) + const download = task('t5', { kind: 'installer-download', subject: '26.7.3', started_at: 2 }) + const got = creatingVMTasks({ second, first, registered, finished, download }, new Set(['macos-ready'])) + assert.deepEqual(got.map((x) => x.subject), ['macos-a', 'macos-b']) +}) diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index b79ab96..31fb25a 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -11,6 +11,7 @@ import { TitleMenus } from './components/TitleMenus' import { UpdateCapsule } from './components/UpdateCapsule' import { DataRootReadOnly } from './components/DataRootReadOnly' import { ABOUT_ID, HOST_ID, INSTALLERS_ID, PAGE_IDS, PLUGINS_ID, SETTINGS_ID, Sidebar } from './components/Sidebar' +import { CreatingVMView } from './components/CreatingVMView' import { VMPage } from './components/VMPage' import { buildFallbackChain, I18nKey, localeDir, locText, parseAppError, resolveActiveLocale, setPluginLocales, t } from './i18n' import { defaultGuestLang, recommendedMemoryGB, recommendedResolution } from './lib/format' @@ -18,7 +19,7 @@ import { isMacOSDriver, liveVMInfo, splitEndpoints, vmFeatures } from './lib/end import { resolveFullscreenHotkey } from './lib/fullscreenHotkey' import { LogLine, appendLog, createLogLine } from './lib/logs' import { macOSMajor } from './macos/macosRelease' -import { currentTask, finishedTaskTTL, isActiveInstallerTask, pruneFinishedTasks, upsertTask } from './lib/tasks' +import { creatingVMTasks, currentTask, finishedTaskTTL, isActiveInstallerTask, pruneFinishedTasks, upsertTask } from './lib/tasks' import { applyActiveLocaleFonts, applyActiveTheme, disposeActiveThemeAndLocaleFonts, notifyInteractiveThemeState } from './lib/themeEngine' import type { UIStateSnapshot } from './lib/uiSdk' import { hasVisibleDialog } from './lib/dialogState' @@ -113,6 +114,7 @@ export const App: React.FC = () => { const [detailRevision, setDetailRevision] = useState(0) const [showWizard, setShowWizard] = useState(false) const [wizardDraft, setWizardDraft] = useState(null) + const [pendingCreates, setPendingCreates] = useState([]) const [showSettings, setShowSettings] = useState(false) const [affinityTarget, setAffinityTarget] = useState<{ name: string; driverID: string } | null>(null) const [affinityVersion, setAffinityVersion] = useState(0) @@ -238,10 +240,22 @@ export const App: React.FC = () => { }, [refresh]) const vms = state?.vms ?? [] + const existingVMNames = new Set(vms.map((v) => v.name)) + const creatingTasks = creatingVMTasks(taskMap, existingVMNames) + const creatingByName = new Map(creatingTasks.map((t) => [t.subject, t])) + const creatingItems: { name: string; task?: Task }[] = [ + ...creatingTasks.map((t) => ({ name: t.subject, task: t })), + ...pendingCreates + .filter((name) => !existingVMNames.has(name) && !creatingByName.has(name)) + .map((name) => ({ name })), + ] const runningVMCount = vms.filter((v) => (state?.transitions[vmRefKey(v)] ?? v.state) === 'running').length const selectedVM = !PAGE_IDS.includes(selected) ? vms.find((v) => vmRefKey(v) === selected) ?? vms.find((v) => v.name === selected) : undefined + const selectedCreating = !selectedVM && !PAGE_IDS.includes(selected) + ? creatingItems.find((item) => item.name === selected) + : undefined const selectedIsMacOS = !!selectedVM && isMacOSDriver(selectedVM.driver_id) const selectedTransition = state?.transitions[selectedVM ? vmRefKey(selectedVM) : ''] @@ -969,13 +983,15 @@ export const App: React.FC = () => { setShowWizard(true) } - // The wizard closes as soon as the create starts; progress lives in the status bar - // (vm-create task). On failure or cancel the toast offers returning to the wizard. + // The wizard closes as soon as the create starts; progress shows in the sidebar, + // main view, and status bar. On failure or cancel the toast offers returning to the wizard. const submitCreate = (payload: CreateOpts, draft: WizardDraft) => { setShowWizard(false) setWizardDraft(null) const name = payload.name const hasDownload = payload.source_mode === 'offline' || payload.source_mode === 'apple' + setPendingCreates((prev) => (prev.includes(name) ? prev : [...prev, name])) + setSelected(name) api.CreateVM(payload) .then(() => { toasts.push({ kind: 'ok', title: tr('toastCreated', { name }) }) @@ -1008,6 +1024,9 @@ export const App: React.FC = () => { action: { label: tr('backToEdit'), onClick: () => { setWizardDraft(draft); setShowWizard(true) } }, }) }) + .finally(() => { + setPendingCreates((prev) => prev.filter((item) => item !== name)) + }) } const copy = async (text: string) => { @@ -1212,6 +1231,7 @@ export const App: React.FC = () => { safeMode={state?.safe_mode} updateDot={!!update.info?.has_update} pluginViews={sidebarPluginViews} + creatingVMs={creatingItems.map((item) => ({ name: item.name, percent: item.task?.percent ?? -1 }))} updateCard={update.shown ? ( { onOpenView={openView} onNavigate={navigateHost} /> + ) : selectedCreating ? ( + openLogs('app')} + /> ) : selectedVM ? ( <> {renderVMTabs()} diff --git a/frontend/src/components/CreatingVMView.tsx b/frontend/src/components/CreatingVMView.tsx new file mode 100644 index 0000000..7242fe2 --- /dev/null +++ b/frontend/src/components/CreatingVMView.tsx @@ -0,0 +1,71 @@ +import { LoaderCircle, Terminal, X } from 'lucide-react' +import React from 'react' +import '../i18n/domains/vm.gen' +import { I18nKey, t } from '../i18n' +import { taskDetail, taskTitle } from '../lib/tasks' +import { Locale, Task } from '../types' +import { Button, StatusShape } from '../ui/controls' + +interface Props { + locale: Locale + name: string + task?: Task + onCancel?: (taskId: string) => void + onViewLogs: () => void +} + +export const CreatingVMView: React.FC = ({ locale, name, task, onCancel, onViewLogs }) => { + const tr = (k: I18nKey, v?: Record) => t(locale, k, v) + const percent = task && typeof task.percent === 'number' ? task.percent : -1 + const detail = task ? taskDetail(task) : '' + const stepText = task?.step?.trim() || tr('creatingPreparing') + const heading = task ? taskTitle(locale, task) : tr('taskCreate', { name }) + + return ( +
+
+
+

{name}

+
+ + {tr('state_creating')} +
+
+
+ + {task?.cancelable && onCancel && ( + + )} +
+
+ +
+
{tr('creatingProgressTitle')}
+
+
+
+ + + {percent >= 0 && {Math.floor(percent)}%} +
+ {percent >= 0 && ( +
+
+
+
+ ) +} diff --git a/frontend/src/components/Sidebar.tsx b/frontend/src/components/Sidebar.tsx index 54ddeea..65faf5a 100644 --- a/frontend/src/components/Sidebar.tsx +++ b/frontend/src/components/Sidebar.tsx @@ -49,15 +49,17 @@ interface Props { updateDot?: boolean /** Declared plugin pages shown after the built-in Plugins entry. */ pluginViews?: { id: string; title: string; badge?: string; icon?: string }[] + /** VMs currently being created (not yet in `vms`). */ + creatingVMs?: { name: string; percent: number }[] } export const Sidebar: React.FC = ({ - locale, vms, transitions, busyActions = {}, selected, hostReady, canSuspend, onSelect, displayNames = {}, macosMajors = {}, onActivate, onNew, onVMContextAction, downloading, safeMode, updateCard, updateDot, pluginViews = [], + locale, vms, transitions, busyActions = {}, selected, hostReady, canSuspend, onSelect, displayNames = {}, macosMajors = {}, onActivate, onNew, onVMContextAction, downloading, safeMode, updateCard, updateDot, pluginViews = [], creatingVMs = [], }) => { const tr = (k: I18nKey) => t(locale, k) const listRef = useRef(null) const ctxOpenedByPointerRef = useRef(false) - const ids = [...vms.map((v) => vmRefKey(v)), INSTALLERS_ID, PLUGINS_ID, ...pluginViews.map((view) => view.id), HOST_ID, SETTINGS_ID, ABOUT_ID] + const ids = [...vms.map((v) => vmRefKey(v)), ...creatingVMs.map((v) => v.name), INSTALLERS_ID, PLUGINS_ID, ...pluginViews.map((view) => view.id), HOST_ID, SETTINGS_ID, ABOUT_ID] // Listbox keyboard model: ↑/↓/Home/End move selection and focus together. const onKeyDown = (e: React.KeyboardEvent) => { @@ -131,7 +133,7 @@ export const Sidebar: React.FC = ({
- {vms.length === 0 &&
{tr('emptyVMs')}
} + {vms.length === 0 && creatingVMs.length === 0 &&
{tr('emptyVMs')}
} {vms.map((vm) => { const key = vmRefKey(vm) const st = transitions[key] ?? vm.state @@ -261,6 +263,18 @@ export const Sidebar: React.FC = ({ ) })} + {creatingVMs.map((item) => navBtn( + item.name, + item.name, + , + 'nav-icon-art', + t(locale, 'state_creating'), + statusKind('creating'), + undefined, + undefined, + { state: 'creating', driver: '', busy: true, family: 'macos' }, + item.percent >= 0 ? `${Math.floor(item.percent)}%` : undefined, + ))}
{navBtn( diff --git a/frontend/src/i18n/domains/vm.gen.ts b/frontend/src/i18n/domains/vm.gen.ts index e4b185d..6061102 100644 --- a/frontend/src/i18n/domains/vm.gen.ts +++ b/frontend/src/i18n/domains/vm.gen.ts @@ -13,6 +13,8 @@ export const vmZh: Record = { "connGroup": "连接", "copyScreenshot": "截屏并复制", "cores": "{n, plural, other {# 核}}", + "creatingPreparing": "正在准备虚拟机环境与磁盘…", + "creatingProgressTitle": "创建进度", "deleteConfirm": "删除", "deleteDesc": "配置和虚拟磁盘会被移除,无法撤销", "deleteDescKeep": "只移除配置,磁盘文件留在原目录", @@ -271,6 +273,8 @@ export const vmEn: Record = { "connGroup": "Connection", "copyScreenshot": "Copy screenshot", "cores": "{n, plural, one {# core} other {# cores}}", + "creatingPreparing": "Preparing the virtual machine and disk…", + "creatingProgressTitle": "Creation progress", "deleteConfirm": "Delete", "deleteDesc": "Its configuration and virtual disk are removed. This can’t be undone", "deleteDescKeep": "Only the configuration is removed; disk files stay in place", diff --git a/frontend/src/i18n/domains/wizard.gen.ts b/frontend/src/i18n/domains/wizard.gen.ts index 93b1c61..01d2016 100644 --- a/frontend/src/i18n/domains/wizard.gen.ts +++ b/frontend/src/i18n/domains/wizard.gen.ts @@ -5,7 +5,7 @@ export const wizardZh: Record = { "commandToggle": "等效命令", "commandToggleHint": "在终端运行下面这条命令效果相同", "createNow": "创建", - "createStartedHint": "创建后窗口会关闭,进度在底部状态栏", + "createStartedHint": "创建后窗口会关闭,进度会显示在侧栏与主界面", "issuesCount": "{n, plural, other {有 # 项需要修改}}", "offlineCached": "已下载", "offlineDownloading": "下载中", @@ -93,7 +93,7 @@ export const wizardEn: Record = { "commandToggle": "Equivalent command", "commandToggleHint": "Running this in a terminal does the same thing", "createNow": "Create", - "createStartedHint": "The window closes once creation starts; progress shows in the status bar", + "createStartedHint": "The window closes once creation starts; progress shows in the sidebar and main view", "issuesCount": "{n, plural, one {# issue to fix} other {# issues to fix}}", "offlineCached": "Downloaded", "offlineDownloading": "Downloading", diff --git a/frontend/src/i18n/keys.gen.ts b/frontend/src/i18n/keys.gen.ts index 6a69726..5f5133e 100644 --- a/frontend/src/i18n/keys.gen.ts +++ b/frontend/src/i18n/keys.gen.ts @@ -148,6 +148,8 @@ export type I18nKey = | "cpuRecHint" | "createNow" | "createStartedHint" + | "creatingPreparing" + | "creatingProgressTitle" | "dataRootChooseOther" | "dataRootChooseTitle" | "dataRootReadOnlyDetail" diff --git a/frontend/src/lib/tasks.ts b/frontend/src/lib/tasks.ts index 96b6a67..407f41b 100644 --- a/frontend/src/lib/tasks.ts +++ b/frontend/src/lib/tasks.ts @@ -33,6 +33,13 @@ export function currentTask(all: Record): { task: Task | null; run return { task: running[0] ?? null, running: running.length } } +/** Running vm-create tasks whose VM has not appeared in the VM list yet, oldest first. */ +export function creatingVMTasks(all: Record, existingNames: ReadonlySet): Task[] { + return Object.values(all) + .filter((x) => x.status === 'running' && x.kind === 'vm-create' && !!x.subject && !existingNames.has(x.subject)) + .sort((a, b) => a.started_at - b.started_at || a.subject.localeCompare(b.subject)) +} + /** Localized title; the upstream step (English) goes in the tooltip. */ export function taskTitle(locale: Locale, task: Task): string { switch (task.kind) { diff --git a/frontend/src/styles/app.css b/frontend/src/styles/app.css index c5dfc5f..54b1c92 100644 --- a/frontend/src/styles/app.css +++ b/frontend/src/styles/app.css @@ -911,6 +911,9 @@ button.status-issue + .status-metric { margin-left: 6px; } background: var(--accent); transform-origin: left center; transition: transform var(--dur) var(--ease-out); } +.creating-row { flex-direction: column; align-items: stretch; gap: var(--sp-2); } +.creating-head { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-2); } +.creating-progress { width: 100%; height: 6px; } .logs-body-wrap { position: relative; border-top: 1px solid var(--border); } .logs-body { overflow-y: auto; overscroll-behavior: contain; padding: var(--sp-2) var(--sp-4); diff --git a/locales/en-US/vm.json b/locales/en-US/vm.json index bc3cee7..e426643 100644 --- a/locales/en-US/vm.json +++ b/locales/en-US/vm.json @@ -249,6 +249,8 @@ "specLangTip": "macOS system language: {label}", "specLangTipDetail": "macOS system language: {label} ({detail})", "state_creating": "Creating", + "creatingProgressTitle": "Creation progress", + "creatingPreparing": "Preparing the virtual machine and disk…", "state_driver_unavailable": "Driver unavailable", "endpointsTitle": "Access", "endpointKindSSH": "SSH", diff --git a/locales/en-US/wizard.json b/locales/en-US/wizard.json index 4491ef1..5d7efbe 100644 --- a/locales/en-US/wizard.json +++ b/locales/en-US/wizard.json @@ -80,7 +80,7 @@ "commandToggle": "Equivalent command", "commandToggleHint": "Running this in a terminal does the same thing", "createNow": "Create", - "createStartedHint": "The window closes once creation starts; progress shows in the status bar", + "createStartedHint": "The window closes once creation starts; progress shows in the sidebar and main view", "wizardDriverGroup": "Guest driver", "wizardDriverType": "Driver", "wizardBasics": "Basics", diff --git a/locales/zh-CN/vm.json b/locales/zh-CN/vm.json index 610ce6e..23c5d18 100644 --- a/locales/zh-CN/vm.json +++ b/locales/zh-CN/vm.json @@ -249,6 +249,8 @@ "specLangTip": "macOS 系统语言:{label}", "specLangTipDetail": "macOS 系统语言:{label}({detail})", "state_creating": "正在创建", + "creatingProgressTitle": "创建进度", + "creatingPreparing": "正在准备虚拟机环境与磁盘…", "state_driver_unavailable": "驱动不可用", "endpointsTitle": "访问方式", "endpointKindSSH": "SSH", diff --git a/locales/zh-CN/wizard.json b/locales/zh-CN/wizard.json index 56708ca..034e809 100644 --- a/locales/zh-CN/wizard.json +++ b/locales/zh-CN/wizard.json @@ -80,7 +80,7 @@ "commandToggle": "等效命令", "commandToggleHint": "在终端运行下面这条命令效果相同", "createNow": "创建", - "createStartedHint": "创建后窗口会关闭,进度在底部状态栏", + "createStartedHint": "创建后窗口会关闭,进度会显示在侧栏与主界面", "wizardDriverGroup": "客体平台驱动", "wizardDriverType": "驱动类型", "wizardBasics": "基本信息",