diff --git a/src/main/ipc.ts b/src/main/ipc.ts index cc4e1d8..80b9427 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -3,7 +3,7 @@ import { existsSync, readFileSync } from 'node:fs' import { cpus, totalmem } from 'node:os' import { isAbsolute } from 'node:path' import { defaultConfig, validPort, type VmConfig } from '@shared/config' -import { workText, type AppSettings, type AutostartIssue, type DiskInfo, type DiskWork, type HostInfo, type PowerAction, type Profile, type SaveStateResult, type StartResult, type VmWork } from '@shared/ipc' +import { workText, type AppSettings, type AutostartIssue, type DiskInfo, type DiskWork, type HostInfo, type PowerAction, type Profile, type SaveStateResult, type StartResult, type VmWork, type VmWorkState } from '@shared/ipc' import { agentHibernate } from './agent' import { cloneVm, compactDisk, resizeDisk } from './disks' import { runAutostart } from './autostart' @@ -51,8 +51,8 @@ export function registerIpc(ctx: IpcContext): Backend { const send = (channel: string, payload?: unknown) => getWindow()?.webContents.send(channel, payload) // the renderer and the tray menu both follow which VMs have work running const workListeners = new Set<() => void>() - const announceWork = (name: string, work: VmWork | null) => { - send('vm:work', { name, work }) + const announceWork = (name: string, work: VmWork | null, tag?: string) => { + send('vm:work', { name, work, tag }) for (const cb of workListeners) cb() } const profiles = new Profiles(paths, runner, announceWork) @@ -308,7 +308,7 @@ export function registerIpc(ctx: IpcContext): Backend { ipcMain.handle('states', () => runner.allStates()) const vmWork = (): Record => ({ ...profiles.work(), ...Object.fromEntries(diskWork) }) - ipcMain.handle('vmWork', vmWork) + ipcMain.handle('vmWork', (): VmWorkState => ({ work: vmWork(), tags: profiles.workTags() })) // the Summary tab's live screen ipcMain.handle('preview', async (_e, name: string): Promise => { diff --git a/src/main/profiles.ts b/src/main/profiles.ts index 3c5918c..acc69e1 100644 --- a/src/main/profiles.ts +++ b/src/main/profiles.ts @@ -35,6 +35,8 @@ function cleanLabel(label: string): string { export class Profiles { /** VMs with a take/launch/remove in flight */ private busy = new Map() + /** the profile a launch or delete is working on, so the Profiles tab can mark its row */ + private tags = new Map() /** VMs with an edit in flight. Edits are quick, so a take/launch/remove waits for them instead of refusing */ private editing = new Map>() @@ -42,7 +44,7 @@ export class Profiles { private readonly paths: DataPaths, private readonly runner: VmRunner, /** told when a VM starts or finishes a profile operation, so the toolbar can show it */ - private readonly onWork: (vm: string, work: ProfileWork | null) => void = () => undefined + private readonly onWork: (vm: string, work: ProfileWork | null, tag?: string) => void = () => undefined ) {} private dir(vm: string): string { @@ -53,16 +55,18 @@ export class Profiles { * One profile operation per VM at a time. A take waits minutes for the guest to hibernate, * and a launch or a second take during that would kill it or ask it to hibernate twice. */ - private async exclusive(vm: string, work: ProfileWork, fn: () => Promise): Promise { + private async exclusive(vm: string, work: ProfileWork, fn: () => Promise, tag?: string): Promise { if (this.busy.has(vm)) throw new Error(`${vm} is still busy with another profile. Wait for it to finish.`) this.busy.set(vm, work) - this.onWork(vm, work) + if (tag) this.tags.set(vm, tag) + this.onWork(vm, work, tag) try { // an edit that checked the profile exists could otherwise write its meta entry back after a remove await this.editing.get(vm)?.catch(() => undefined) return await fn() } finally { this.busy.delete(vm) + this.tags.delete(vm) this.onWork(vm, null) } } @@ -82,6 +86,11 @@ export class Profiles { return Object.fromEntries(this.busy) } + /** for a running launch or delete, the profile it works on, by VM */ + workTags(): Record { + return Object.fromEntries(this.tags) + } + private shotFile(vm: string, tag: string): string { checkTag(tag) return join(this.dir(vm), `${tag}.png`) @@ -190,7 +199,7 @@ export class Profiles { /** Rolls back to the profile and boots it. A session profile resumes everything that was open. */ launch(cfg: VmConfig, tag: string, force: boolean): Promise { - return this.exclusive(cfg.name, 'launching', () => this.launchNow(cfg, tag, force)) + return this.exclusive(cfg.name, 'launching', () => this.launchNow(cfg, tag, force), tag) } private async launchNow(cfg: VmConfig, tag: string, force: boolean): Promise { @@ -219,7 +228,7 @@ export class Profiles { } remove(cfg: VmConfig, tag: string): Promise { - return this.exclusive(cfg.name, 'removing', () => this.removeNow(cfg, tag)) + return this.exclusive(cfg.name, 'removing', () => this.removeNow(cfg, tag), tag) } private async removeNow(cfg: VmConfig, tag: string): Promise { diff --git a/src/renderer/src/App.tsx b/src/renderer/src/App.tsx index de50fdf..e628c53 100644 --- a/src/renderer/src/App.tsx +++ b/src/renderer/src/App.tsx @@ -12,7 +12,7 @@ import Modal from './components/Modal' import CloneDialog from './components/CloneDialog' import Wizard from './components/Wizard' import Settings from './components/Settings' -import { errText, fileName, filterVms, isTyping, workLabel } from './util' +import { errText, fileName, filterVms, isTyping, withSnapshot, workLabel } from './util' const HISTORY = 60 // usage samples kept per VM, 2 s apart = two minutes @@ -30,6 +30,8 @@ export default function App() { const [hibernating, setHibernating] = useState>(new Set()) // profile saves, launches and deletes and disk work, also the ones started from another VM's tabs const [vmWork, setVmWork] = useState>({}) + // for a profile launch or delete, which profile, so the Profiles tab can mark its row + const [workTags, setWorkTags] = useState>({}) const [setupFor, setSetupFor] = useState<{ vm: VmConfig; reason: string } | null>(null) const [dragging, setDragging] = useState(false) const dragDepth = useRef(0) // dragenter/dragleave fire for every child, count them @@ -55,10 +57,17 @@ export default function App() { if (s.status === 'off') setUsage(({ [s.name]: _gone, ...rest }) => rest) if (s.status === 'off') setHibernating((cur) => (cur.has(s.name) ? new Set([...cur].filter((n) => n !== s.name)) : cur)) }) - window.vmm.vmWork().then(setVmWork) - const offWork = window.vmm.onVmWork(({ name, work }) => + // VMs a vm:work event has reported since. Those events are newer than the vmWork() answer + const reported = new Set() + window.vmm.vmWork().then((w) => { + setVmWork((cur) => withSnapshot(cur, w.work, reported)) + setWorkTags((cur) => withSnapshot(cur, w.tags, reported)) + }) + const offWork = window.vmm.onVmWork(({ name, work, tag }) => { + reported.add(name) setVmWork(({ [name]: _done, ...rest }) => (work ? { ...rest, [name]: work } : rest)) - ) + setWorkTags(({ [name]: _done, ...rest }) => (work && tag ? { ...rest, [name]: tag } : rest)) + }) const offCrash = window.vmm.onCrash((c) => toasts.push('error', `${c.name} failed to start`, c.message)) // an ISO was added to or removed from the ISOs folder const offUsage = window.vmm.onUsage((u) => @@ -326,6 +335,7 @@ export default function App() { vm={current} status={status} work={current ? vmWork[current.name] ?? null : null} + workTag={current ? workTags[current.name] ?? null : null} host={host} usage={usage[current.name] ?? []} onSave={onSave} diff --git a/src/renderer/src/components/Profiles.tsx b/src/renderer/src/components/Profiles.tsx index a5db684..4796b6d 100644 --- a/src/renderer/src/components/Profiles.tsx +++ b/src/renderer/src/components/Profiles.tsx @@ -13,6 +13,8 @@ interface Props { * VM is selected and this one comes back, this isn't */ work: VmWork | null + /** for a launch or delete, which profile it is. Marks the right row after the tab was remounted */ + workTag: string | null toast: PushToast /** the guest agent didn't answer, show the one-time setup */ onNeedsSetup: (reason: string) => void @@ -24,7 +26,7 @@ const KIND: Record = { disk: 'disk only, boots fresh' } -export default function Profiles({ vm, status, work, toast, onNeedsSetup }: Props) { +export default function Profiles({ vm, status, work, workTag, toast, onNeedsSetup }: Props) { const [list, setList] = useState(null) const [label, setLabel] = useState('') const [note, setNote] = useState('') @@ -87,11 +89,19 @@ export default function Profiles({ vm, status, work, toast, onNeedsSetup }: Prop } const savingSession = busy === 'take' - // work this tab didn't start, or started before it was last unmounted. Which profile it is - // isn't known here, so the hint says what is going on and every button waits + // work this tab didn't start, or started before it was last unmounted. For a launch or delete + // the backend says which profile, so its row can show it. Every button waits either way const otherWork = busy === null && work !== null + const workProfile = otherWork && workTag ? list?.find((p) => p.tag === workTag) : undefined + /** what is happening to this row's profile, whoever started it */ + const rowWork = (p: Profile): 'launching' | 'removing' | null => + busy === `l${p.tag}` || (otherWork && work === 'launching' && workTag === p.tag) + ? 'launching' + : busy === `d${p.tag}` || (otherWork && work === 'removing' && workTag === p.tag) + ? 'removing' + : null const hint = otherWork - ? `${workLabel[work]} on ${vm.name}. Wait for it to finish.` + ? `${workLabel[work]}${workProfile ? ` "${workProfile.label}"` : ''} on ${vm.name}. Wait for it to finish.` : status === 'running' || savingSession ? `Saves everything that's open in ${vm.name}. It closes for a moment while it saves, then comes right back. Launch the profile any time to return to exactly this point.` : status === 'paused' @@ -139,7 +149,7 @@ export default function Profiles({ vm, status, work, toast, onNeedsSetup }: Prop ) : (
    {list.map((p) => ( -
  • +
  • {editing === p.tag ? ( @@ -160,6 +170,7 @@ export default function Profiles({ vm, status, work, toast, onNeedsSetup }: Prop {when(p.created)} · {KIND[p.kind]} {p.note && {p.note}} + {rowWork(p) === 'removing' && Deleting…} )}
    @@ -172,7 +183,7 @@ export default function Profiles({ vm, status, work, toast, onNeedsSetup }: Prop onClick={() => launch(p)} title="Go back to this profile and start it" > - {busy === `l${p.tag}` ? 'Launching…' : 'Launch'} + {rowWork(p) === 'launching' ? 'Launching…' : 'Launch'}