From 896163d7961d86b4e023a50ce4f6e19a30006a3f Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Fri, 13 Nov 2026 20:49:56 +0100 Subject: feat(vm): tablet tiles with screen, trends and a power button Each VM is a tile: its screen from virsh screenshot (JPEG through ffmpeg, every 5s while the tab shows), booting until sshd answers at the ssh -G host and port, CPU and RAM trends, and a big power button, tap for the gentle action and hold to force off. A tap on the tile opens a sheet with every action as an icon and the snapshots as a timeline with their age. hold.js is now the one press and hold, K1C's stop included, and vibrates the tablet when it fires; the Windows long-press buzzes too. Co-Authored-By: Claude Opus 5.5 --- static/deck.css | 63 ++++++--- static/hold.js | 46 +++++++ static/modules/k1c.js | 27 +--- static/modules/vm.js | 333 +++++++++++++++++++++++++++++++++------------- static/modules/windows.js | 4 +- 5 files changed, 344 insertions(+), 129 deletions(-) create mode 100644 static/hold.js (limited to 'static') diff --git a/static/deck.css b/static/deck.css index c316ff3..9f44eb1 100644 --- a/static/deck.css +++ b/static/deck.css @@ -813,23 +813,56 @@ select { animation: border-rotate 3s linear infinite; } -/* VMs tab. Destructive buttons are press and hold, filled over --hold. */ -.vm-list { display: flex; flex-direction: column; gap: 12px; padding: 4px 2px; } -.vm-list[hidden], .vm-off[hidden], .vm-use[hidden], .vm-err[hidden] { display: none; } -.vm-card { display: flex; flex-direction: column; gap: 10px; padding: 12px 14px; border-radius: 12px; background: var(--udt-surface0); } -.vm-card.busy { opacity: .6; pointer-events: none; } -.vm-head { display: flex; align-items: center; gap: 12px; } -.vm-name { font-size: 1.286rem; font-weight: 600; } +/* VMs tab: a tile per VM, its screen on top, a big power button below. + Hold buttons fill red over --hold (hold.js). Controls are 56px or more. */ +.vm-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 14px; padding: 4px 2px; } +.vm-grid[hidden], .vm-off[hidden], .vm-err[hidden], .vm-snapacts[hidden], .vm-screen img[hidden] { display: none; } +.vm-grid.empty::before { content: "No VMs defined."; color: var(--udt-subtext0); } +.vm-tile, .vm-sheet { display: flex; flex-direction: column; gap: 10px; } +.vm-tile { padding: 12px; border-radius: 16px; background: var(--udt-surface0); border: 2px solid transparent; } +.vm-tile.on { border-color: var(--udt-border-1); } +.vm-tile.busy, .vm-sheet.busy { opacity: .6; pointer-events: none; } +.vm-screen { position: relative; aspect-ratio: 16 / 10; border-radius: 10px; background: var(--udt-crust); + display: grid; place-items: center; overflow: hidden; } +.vm-screen img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; } +.vm-glyph { font-size: 4rem; color: var(--udt-surface2); } +.vm-screen .vm-state { position: absolute; top: 8px; left: 8px; } .vm-state { padding: 2px 10px; border-radius: 999px; background: var(--udt-surface2); font-size: 0.857rem; } .vm-state.good { background: var(--udt-green); color: var(--udt-crust); } .vm-state.warn { background: var(--udt-yellow); color: var(--udt-crust); } .vm-state.bad { background: var(--udt-red); color: var(--udt-crust); } -.vm-ip { margin-left: auto; color: var(--udt-subtext0); } -.vm-use { display: grid; grid-template-columns: auto 1fr auto auto auto; align-items: center; gap: 10px; } +.vm-info { display: flex; align-items: baseline; gap: 10px; min-width: 0; } +.vm-name { font-size: 1.286rem; } +.vm-status { color: var(--udt-subtext0); font-size: 0.857rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } +.vm-trends { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; } +.vm-trend { position: relative; height: 54px; border-radius: 10px; background: var(--udt-surface1); overflow: hidden; + display: flex; align-items: flex-end; justify-content: space-between; padding: 4px 8px; } +.vm-trend svg { position: absolute; inset: 0; width: 100%; height: 100%; } +.vm-trend path.hy { fill: url(#vmfill); } +.vm-trend path.hy.ln { fill: none; stroke: url(#vmline); stroke-width: 2; vector-effect: non-scaling-stroke; } +.vm-trend .k, .vm-snaphead .k { position: relative; font-size: 0.714rem; text-transform: uppercase; letter-spacing: .08em; color: var(--udt-overlay0); } +.vm-trend .v { position: relative; font-weight: 600; } .vm-err { color: var(--udt-red); font-size: 0.857rem; } -.vm-acts { margin-bottom: 0; } -.vm-snap { display: flex; align-items: center; gap: 8px; padding: 4px 0; border-top: 1px solid var(--udt-surface1); } -.vm-snap > span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } -.vm .hold { position: relative; overflow: hidden; isolation: isolate; border-color: var(--udt-red); touch-action: none; } -.vm .hold::before { content: ""; position: absolute; inset: 0; z-index: -1; background: var(--udt-red); transform: scaleX(0); transform-origin: left; } -.vm .hold.holding::before { transform: scaleX(1); transition: transform var(--hold, 800ms) linear; } +.vm-bar { display: flex; align-items: center; gap: 12px; } +.vm .ib, .vm-sheet .ib { min-width: 56px; min-height: 56px; font-size: 1.5rem; border-radius: 14px; padding: 0 12px; } +.vm-more { margin-left: auto; } +.vm-power { width: 72px; height: 72px; border-radius: 50%; font-size: 2rem; padding: 0; } +.vm-power.go { background: color-mix(in srgb, var(--udt-green) 30%, transparent); border-color: var(--udt-green); } +.vm-sshb.ready { border-color: var(--udt-accent); color: var(--udt-accent); } +.vm .hold, .vm-sheet .hold { position: relative; overflow: hidden; isolation: isolate; touch-action: none; } +.vm .hold:not(.vm-power), .vm-sheet .hold { border-color: var(--udt-red); } +.vm .hold::before, .vm-sheet .hold::before { content: ""; position: absolute; inset: 0; z-index: -1; background: var(--udt-red); + transform: scaleX(0); transform-origin: left; } +.vm .hold.holding::before, .vm-sheet .hold.holding::before { transform: scaleX(1); transition: transform var(--hold, 800ms) linear; } +dialog.vm-sheet { margin: auto; width: min(760px, 92vw); max-height: 85vh; overflow-y: auto; overscroll-behavior: contain; + padding: 18px; border: 0; border-radius: 18px; background: var(--udt-base); color: var(--udt-text); } +dialog.vm-sheet:not([open]) { display: none; } +dialog.vm-sheet::backdrop { background: color-mix(in srgb, var(--udt-crust) 55%, transparent); } +.vm-acts, .vm-snapacts { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; } +.vm-snaphead { display: flex; align-items: center; justify-content: space-between; margin-top: 6px; } +.vm-timeline { display: flex; gap: 10px; overflow-x: auto; padding-bottom: 4px; color: var(--udt-subtext0); } +.vm-snap { flex: none; display: flex; flex-direction: column; align-items: flex-start; gap: 2px; min-height: 56px; } +.vm-snap span { font-size: 0.786rem; color: var(--udt-subtext0); } +.vm-snap.current { border-color: var(--udt-accent); } +.vm-snap.on { background: var(--udt-surface2); } +.vm-snapname { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; } diff --git a/static/hold.js b/static/hold.js new file mode 100644 index 0000000..e937a25 --- /dev/null +++ b/static/hold.js @@ -0,0 +1,46 @@ +// deck-it - press and hold, for buttons a stray touch must not fire +// Copyright (C) 2026 Danilo M. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +export const HOLD_MS = 800; + +// The tablet buzzes when a hold fires (or a long press opens a menu), so +// the finger knows it can let go. +export const buzz = () => navigator.vibrate?.(40); + +// fire runs after the button is held ms; releasing early cancels, and runs +// tap instead when one is given. The button gets .holding meanwhile and +// --hold for the CSS fill. +export function hold(btn, fire, { ms = HOLD_MS, tap } = {}) { + let timer = null; + const cancel = () => { + clearTimeout(timer); + timer = null; + btn.classList.remove("holding"); + }; + btn.style.setProperty("--hold", `${ms}ms`); + btn.addEventListener("pointerdown", (e) => { + e.preventDefault(); + e.stopPropagation(); + btn.setPointerCapture(e.pointerId); + btn.classList.add("holding"); + timer = setTimeout(() => { cancel(); buzz(); fire(); }, ms); + }); + btn.addEventListener("pointerup", () => { + if (timer && tap) tap(); + cancel(); + }); + btn.addEventListener("pointercancel", cancel); +} diff --git a/static/modules/k1c.js b/static/modules/k1c.js index 261ef4d..6b41a01 100644 --- a/static/modules/k1c.js +++ b/static/modules/k1c.js @@ -15,10 +15,11 @@ // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import { offline } from "../offline.js"; +import { hold } from "../hold.js"; -// Holding the stop for this long fires it: fast in an emergency, but a +// Holding the stop (hold.js's 800 ms) fires it: fast in an emergency, but a // stray touch on an always-on tablet cannot kill a print. -const HOLD_MS = 800; +const armHold = (btn) => hold(btn, () => emergencyStop(btn)); const STATES = { printing: ["Printing", "good"], @@ -64,28 +65,6 @@ async function emergencyStop(btn) { }, 4000); } -// Press and hold: the ring fills over HOLD_MS, releasing early cancels. -function armHold(btn) { - let timer = null; - const cancel = () => { - clearTimeout(timer); - timer = null; - btn.classList.remove("holding"); - }; - btn.addEventListener("pointerdown", (e) => { - e.preventDefault(); - btn.setPointerCapture(e.pointerId); - btn.classList.add("holding"); - timer = setTimeout(() => { - cancel(); - emergencyStop(btn); - }, HOLD_MS); - }); - btn.addEventListener("pointerup", cancel); - btn.addEventListener("pointercancel", cancel); - btn.style.setProperty("--hold", `${HOLD_MS}ms`); -} - // The dashboard's running slot: file, percent, time left, layer and // temperatures, the press-and-hold stop and a progress bar. Wide slots show // the slicer thumbnail in place of the icon (deck.css container query). diff --git a/static/modules/vm.js b/static/modules/vm.js index fdffd09..df68a07 100644 --- a/static/modules/vm.js +++ b/static/modules/vm.js @@ -15,135 +15,290 @@ // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import { offline } from "../offline.js"; +import { hold } from "../hold.js"; +import { area, line, push } from "../dashboard.js"; -// Destructive actions fire only after a hold this long, so a stray touch on -// an always-on tablet cannot force a VM off. -const HOLD_MS = 800; +// One tile per VM: its screen, a big power button (tap for the gentle +// action, hold to force it off), SSH, and CPU/RAM trends. A tap on the tile +// opens a sheet with every action and the snapshot timeline. Buttons are +// Nerd Font glyphs; destructive ones fire only on a press and hold. +const G = { + power: "", ssh: "", more: "", start: "", shutdown: "", + reboot: "", suspend: "", resume: "", reset: "", destroy: "", + discardsave: "", snapshot: "", revert: "", snapdelete: "", vm: "", +}; +const LABEL = { + ssh: "SSH", start: "Start", shutdown: "Shut down", reboot: "Reboot", suspend: "Suspend", + resume: "Resume", reset: "Reset (hold)", destroy: "Force stop (hold)", + discardsave: "Discard saved state (hold)", snapshot: "Take a snapshot", + revert: "Revert to this snapshot (hold)", snapdelete: "Delete this snapshot (hold)", +}; const HOLD = new Set(["reset", "destroy", "discardsave", "revert", "snapdelete"]); const PILL = { running: "good", paused: "warn", suspended: "warn", "shutting down": "warn", crashed: "bad" }; +const SHOT = 5000; // the screen preview's refresh, ms +const HIST = 90; // trend samples: three minutes at the 2 s poll -// The buttons per state, as keystone's drawer offers them. +// The sheet's buttons per state, as keystone's drawer offers them. function actionsFor(st, saved) { - if (st === "running") - return [["ssh", "SSH"], ["shutdown", "Shutdown"], ["reboot", "Reboot"], ["suspend", "Suspend"], - ["reset", "Reset"], ["destroy", "Force stop"]]; - if (st === "paused" || st === "suspended") - return [["resume", "Resume"], ["shutdown", "Shutdown"], ["destroy", "Force stop"]]; - return saved ? [["start", "Start"], ["discardsave", "Discard saved state"]] : [["start", "Start"]]; + if (st === "running") return ["ssh", "shutdown", "reboot", "suspend", "reset", "destroy"]; + if (st === "paused" || st === "suspended") return ["resume", "shutdown", "destroy"]; + if (st === "shutting down" || st === "crashed") return ["destroy"]; + return saved ? ["start", "discardsave"] : ["start"]; +} + +// The tile's power button: [tap, hold]; either may be absent. +function powerFor(st) { + if (st === "running") return ["shutdown", "destroy"]; + if (st === "paused" || st === "suspended") return ["resume", "destroy"]; + if (st === "shutting down" || st === "crashed") return [null, "destroy"]; + return ["start", null]; } -const gib = (b) => (b == null ? "—" : `${(b / 2 ** 30).toFixed(1)} GiB`); +function ago(sec) { + const s = Math.max(0, Date.now() / 1000 - sec); + for (const [n, u] of [[31536000, "y"], [2592000, "mo"], [86400, "d"], [3600, "h"], [60, "min"]]) { + if (s >= n) return `${Math.floor(s / n)} ${u} ago`; + } + return "just now"; +} + +const gib = (b) => (b == null ? "—" : `${(b / 2 ** 30).toFixed(1)}`); + +function status(vm) { + if (vm.state === "running") { + if (vm.ready) return `ssh ready${vm.ip ? ` · ${vm.ip}` : ""}`; + return vm.ready === false ? "booting…" : vm.ip; + } + return vm.saved && vm.state === "shut off" ? "saved state, start restores it" : ""; +} -async function post(action, body, card) { - const err = card.querySelector(".vm-err"); - card.classList.add("busy"); +async function post(action, body, v) { + v.busy(true); + let msg = ""; try { const r = await fetch(`/api/vm/${action}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }); - err.textContent = r.ok ? "" : (await r.text()) || r.statusText; + if (!r.ok) msg = (await r.text()) || r.statusText; } catch (e) { - err.textContent = String(e); + msg = String(e); } - err.hidden = !err.textContent; - card.classList.remove("busy"); + v.busy(false); + v.error(msg); } -function button(label, action, body, card) { +function iconButton(action, body, v, cls = "") { const b = document.createElement("button"); - b.textContent = label; - const fire = () => post(action, body, card); - if (!HOLD.has(action)) { - b.onclick = fire; - return b; + b.className = `ib nf ${cls}`; + b.textContent = G[action]; + b.setAttribute("aria-label", LABEL[action]); + b.title = LABEL[action]; + const fire = () => post(action, body, v); + if (HOLD.has(action)) { + b.classList.add("hold"); + hold(b, fire); + } else { + b.addEventListener("click", (e) => { e.stopPropagation(); fire(); }); } - // Press and hold: the fill grows over HOLD_MS, releasing early cancels. - b.className = "hold"; - b.title = "Press and hold"; - b.style.setProperty("--hold", `${HOLD_MS}ms`); - let timer = null; - const cancel = () => { clearTimeout(timer); b.classList.remove("holding"); }; - b.addEventListener("pointerdown", (e) => { - e.preventDefault(); - b.setPointerCapture(e.pointerId); - b.classList.add("holding"); - timer = setTimeout(() => { cancel(); fire(); }, HOLD_MS); - }); - b.addEventListener("pointerup", cancel); - b.addEventListener("pointercancel", cancel); return b; } -function card() { - const c = document.createElement("div"); - c.className = "vm-card"; - c.innerHTML = `
-
-
CPU
- RAM
-
+const SPARK = ``; + +// A VM's tile and sheet, built once; update() sets text every poll and +// rebuilds buttons only when what they depend on changed, so a hold in +// progress is never torn down under the finger. +function view(name, root) { + const tile = document.createElement("div"); + tile.className = "vm-tile"; + tile.innerHTML = `
${G.vm} +
+
+ -
`; - return c; -} +
+
`; + const sheet = document.createElement("dialog"); + sheet.setAttribute("closedby", "any"); + sheet.className = "card vm-sheet"; + sheet.innerHTML = `
+
+
+ +
Snapshots
+
+ `; + root.append(tile, sheet); + const $ = (sel) => [tile.querySelector(sel), sheet.querySelector(sel)].filter(Boolean); + const open = () => sheet.showModal(); + tile.querySelector(".vm-screen").addEventListener("click", open); + tile.querySelector(".vm-info").addEventListener("click", open); + tile.querySelector(".vm-more").addEventListener("click", open); + + const v = { + tile, sheet, + hist: { cpu: [], mem: [] }, + keys: {}, + pick: null, // the snapshot chip tapped in the sheet + busy(on) { tile.classList.toggle("busy", on); sheet.classList.toggle("busy", on); }, + error(msg) { + for (const e of $(".vm-err")) { e.textContent = msg; e.hidden = !msg; } + }, + }; + sheet.querySelector(".vm-snaphead").append(iconButton("snapshot", { vm: name }, v)); + + const power = tile.querySelector(".vm-power"); + power.textContent = G.power; + let powerAct = [null, null]; + hold(power, () => powerAct[1] && post(powerAct[1], { vm: name }, v), + { tap: () => powerAct[0] && post(powerAct[0], { vm: name }, v) }); + + const changed = (k, val) => { + const s = JSON.stringify(val); + if (v.keys[k] === s) return false; + v.keys[k] = s; + return true; + }; -// Text every poll; buttons only when what they depend on changed, so a -// hold in progress is not torn down under the finger. -function fill(c, vm) { - c.querySelector(".vm-name").textContent = vm.name; - const pill = c.querySelector(".vm-state"); - pill.textContent = vm.saved && vm.state === "shut off" ? "saved" : vm.state; - pill.className = `vm-state ${PILL[vm.state] ?? ""}`; - c.querySelector(".vm-ip").textContent = vm.ip; - const on = vm.state === "running"; - c.querySelector(".vm-use").hidden = !on; - c.querySelector(".gbar > i").style.width = `${vm.cpu ?? 0}%`; - c.querySelector(".vm-cpu").textContent = vm.cpu == null ? "—" : `${Math.round(vm.cpu)}% of ${vm.vcpus}`; - c.querySelector(".vm-mem").textContent = gib(vm.mem); - - const key = JSON.stringify([vm.state, vm.saved, vm.snapshots]); - if (c.dataset.key === key) return; - c.dataset.key = key; - const acts = c.querySelector(".vm-acts"); - acts.replaceChildren(...actionsFor(vm.state, vm.saved).map(([a, l]) => button(l, a, { vm: vm.name }, c)), - button("Snapshot", "snapshot", { vm: vm.name }, c)); - c.querySelector(".vm-snaps").replaceChildren(...vm.snapshots.map((s) => { - const row = document.createElement("div"); - row.className = "vm-snap"; - const name = document.createElement("span"); - name.textContent = s; - row.append(name, button("Revert", "revert", { vm: vm.name, snap: s }, c), - button("Delete", "snapdelete", { vm: vm.name, snap: s }, c)); - return row; - })); + v.update = (vm, visible) => { + for (const e of $(".vm-name")) e.textContent = vm.name; + for (const e of $(".vm-state")) { + e.textContent = vm.state; + e.className = `vm-state ${PILL[vm.state] ?? ""}`; + } + for (const e of $(".vm-status")) e.textContent = status(vm); + const on = vm.state === "running"; + tile.classList.toggle("on", on); + + // Tap does the gentle thing, hold forces: the fill shows only when a + // hold does something. + powerAct = powerFor(vm.state); + power.classList.toggle("hold", Boolean(powerAct[1])); + power.classList.toggle("go", !on && Boolean(powerAct[0])); + const pl = [powerAct[0] && LABEL[powerAct[0]], powerAct[1] && LABEL[powerAct[1]]].filter(Boolean).join(", "); + power.setAttribute("aria-label", pl); + power.title = pl; + + if (changed("ssh", [on, vm.ready])) { + const slot = tile.querySelector(".vm-ssh"); + slot.replaceChildren(); + if (on) { + const b = iconButton("ssh", { vm: vm.name }, v, "vm-sshb"); + b.classList.toggle("ready", vm.ready === true); + slot.append(b); + } + } + + // Screen: refreshed every SHOT ms, only while the tab is on screen. + const img = tile.querySelector("img"); + if (on && visible) { + const url = `/api/vm/image/${encodeURIComponent(vm.name)}?t=${Math.floor(Date.now() / SHOT)}`; + if (img.dataset.src !== url) { + img.dataset.src = url; + img.onload = () => { img.hidden = false; }; + img.onerror = () => { img.hidden = true; }; + img.src = url; + } + } else if (!on) { + img.hidden = true; + delete img.dataset.src; + } + + // Trends: history in the page, cleared while the VM is off. + if (on) { + push(v.hist.cpu, vm.cpu ?? 0, HIST); + push(v.hist.mem, vm.used != null && vm.mem ? (vm.used / vm.mem) * 100 : 0, HIST); + } else { + v.hist.cpu.length = 0; + v.hist.mem.length = 0; + } + for (const [k, hist] of Object.entries(v.hist)) { + const [fill, stroke] = tile.querySelectorAll(`.t-${k} path`); + fill.setAttribute("d", area(hist, 100, HIST)); + stroke.setAttribute("d", line(hist, 100, HIST)); + } + tile.querySelector(".t-cpu .v").textContent = on && vm.cpu != null ? `${Math.round(vm.cpu)}%` : "—"; + tile.querySelector(".t-mem .v").textContent = !on ? "—" + : vm.used != null ? `${gib(vm.used)}/${gib(vm.mem)} GiB` : `${gib(vm.mem)} GiB`; + + if (changed("acts", [vm.state, vm.saved])) { + sheet.querySelector(".vm-acts").replaceChildren( + ...actionsFor(vm.state, vm.saved).map((a) => iconButton(a, { vm: vm.name }, v))); + } + + // Snapshot timeline, oldest first: a chip per snapshot with its age, + // the current one marked. A tap picks one, and its revert and delete + // appear below. + const names = vm.snapshots.map((s) => s.name); + if (v.pick && !names.includes(v.pick)) v.pick = null; + if (changed("snaps", [vm.snapshots, v.pick])) { + const tl = sheet.querySelector(".vm-timeline"); + tl.replaceChildren(...vm.snapshots.map((s) => { + const c = document.createElement("button"); + c.className = `vm-snap${s.current ? " current" : ""}${s.name === v.pick ? " on" : ""}`; + c.innerHTML = ""; + c.firstChild.textContent = s.name; + c.lastChild.textContent = ago(s.created); + c.title = new Date(s.created * 1000).toLocaleString(); + c.onclick = () => { v.pick = s.name === v.pick ? null : s.name; v.update(vm, visible); }; + return c; + })); + if (!vm.snapshots.length) tl.textContent = "None yet."; + const acts = sheet.querySelector(".vm-snapacts"); + acts.hidden = !v.pick; + acts.querySelector(".vm-snapname").textContent = v.pick ?? ""; + acts.querySelectorAll("button").forEach((b) => b.remove()); + if (v.pick) { + acts.append(iconButton("revert", { vm: vm.name, snap: v.pick }, v), + iconButton("snapdelete", { vm: vm.name, snap: v.pick }, v)); + } + } + }; + return v; } +const views = new Map(); + export default { title: "VMs", tab: "VMs", interval: 2, mount(el) { - el.innerHTML = `
`; + el.innerHTML = `
+ +
`; }, update(st, el) { const off = el.querySelector(".vm-off"); - const list = el.querySelector(".vm-list"); + const grid = el.querySelector(".vm-grid"); off.hidden = !st.offline; - list.hidden = Boolean(st.offline); + grid.hidden = Boolean(st.offline); if (st.offline) { offline(off, { icon: "/icons/virt-manager.svg", title: "libvirt is not answering", detail: "Start libvirtd and the VMs appear here.", reason: st.reason }); return; } - const cards = new Map([...list.children].map((c) => [c.dataset.vm, c])); - list.replaceChildren(...st.vms.map((vm) => { - const c = cards.get(vm.name) ?? card(); - c.dataset.vm = vm.name; - fill(c, vm); - return c; - })); - if (!st.vms.length) list.textContent = "No VMs defined."; + const visible = !document.hidden && el.offsetParent !== null; + const seen = new Set(); + for (const vm of st.vms) { + seen.add(vm.name); + if (!views.has(vm.name)) views.set(vm.name, view(vm.name, grid)); + views.get(vm.name).update(vm, visible); + } + for (const [name, v] of views) { + if (!seen.has(name)) { v.tile.remove(); v.sheet.remove(); views.delete(name); } + } + grid.classList.toggle("empty", !st.vms.length); }, }; diff --git a/static/modules/windows.js b/static/modules/windows.js index 0657a16..639137e 100644 --- a/static/modules/windows.js +++ b/static/modules/windows.js @@ -14,6 +14,8 @@ // with this program; if not, write to the Free Software Foundation, Inc., // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import { buzz } from "../hold.js"; + async function post(action, body = {}) { try { await fetch(`/api/windows/${action}`, { @@ -120,7 +122,7 @@ function card(w, workspaces, wsIcons) { if (e.target.closest(".wx")) return; long = false; const at = { left: e.clientX, bottom: e.clientY }; - timer = setTimeout(() => { long = true; openMenu(at, w, workspaces, wsIcons); }, 600); + timer = setTimeout(() => { long = true; buzz(); openMenu(at, w, workspaces, wsIcons); }, 600); }); // Chrome's own long-press menu (copy or share the image) is not wanted here. c.addEventListener("contextmenu", (e) => e.preventDefault()); -- cgit v1.2.3