async function post(action, body = {}) {
try {
await fetch(`/api/windows/${action}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
} catch {
// the next poll shows the real state
}
}
function tile(label, onclick) {
const b = document.createElement("button");
b.className = "wtile";
b.textContent = label;
b.onclick = onclick;
return b;
}
export default {
title: "Windows",
tab: "Windows",
interval: 1,
mount(el) {
el.innerHTML = `
`;
},
update(st, el) {
const ws = el.querySelector(".ws");
ws.textContent = "";
for (const id of st.workspaces ?? []) {
const b = tile(String(id), () => post("workspace", { n: id }));
b.classList.toggle("on", (st.windows ?? []).some((w) => w.workspace === id));
ws.append(b);
}
const list = el.querySelector(".winlist");
list.textContent = "";
for (const w of (st.windows ?? []).slice(0, 8)) {
const row = document.createElement("div");
row.className = "wrow";
row.classList.toggle("on", Boolean(w.active));
const title = document.createElement("span");
title.className = "wt";
title.textContent = w.title || w.class;
title.onclick = () => post("focus", { address: w.address });
const close = document.createElement("button");
close.className = "wclose";
close.textContent = "×";
close.setAttribute("aria-label", "Close window");
close.onclick = () => post("close", { address: w.address });
row.append(title, close);
list.append(row);
}
const launches = el.querySelector(".launches");
launches.textContent = "";
for (const key of st.launches ?? []) {
const b = tile(key, () => post("launch", { key }));
b.classList.add("launch");
launches.append(b);
}
},
};