1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
// deck-it - windows panel
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// 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.
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;
}
let pop = null;
function closePop() {
if (pop) {
pop.remove();
pop = null;
}
}
function openMenu(anchor, win, workspaces, wsIcons) {
closePop();
pop = document.createElement("div");
pop.className = "wpop";
pop.innerHTML = `
<div class="wpop-title"></div>
<div class="wpop-label">Move to desktop</div>
<div class="wpop-ws"></div>`;
pop.querySelector(".wpop-title").textContent = win.title || win.class;
const targets = pop.querySelector(".wpop-ws");
for (const n of (workspaces ?? []).filter((n) => n !== win.workspace)) {
const b = document.createElement("button");
wsMark(b, n, wsIcons);
b.setAttribute("aria-label", `Desktop ${n}`);
b.onclick = () => {
closePop();
post("move", { address: win.address, n });
};
targets.append(b);
}
document.body.append(pop);
// The press point: the card itself may already be rebuilt (a title that
// changes every second re-renders the grid), and a detached one measures 0.
const r = anchor.getBoundingClientRect ? anchor.getBoundingClientRect() : anchor;
const width = pop.offsetWidth;
const height = pop.offsetHeight;
pop.style.top = `${Math.min(r.bottom + 6, window.innerHeight - height - 8)}px`;
pop.style.left = `${Math.max(8, Math.min(r.left, window.innerWidth - width - 8))}px`;
}
document.addEventListener("pointerdown", (e) => {
if (pop && !pop.contains(e.target)) closePop();
});
// A workspace's icon from keystone's map, the number when it has none or the
// icon theme lacks it.
function wsMark(box, n, icons) {
box.textContent = String(n);
const name = icons?.[String(n)];
if (!name) return;
const img = document.createElement("img");
img.alt = String(n);
img.onload = () => { box.textContent = ""; box.append(img); };
img.src = `/icons/${name}.svg`;
}
// Previews are re-captured on the PC every PREVIEW ms; the URL carries the
// period, so within one the browser's cache answers and nothing flickers.
const PREVIEW = 5000;
const previewUrl = (w) => `/api/windows/image/${w.id}?t=${Math.floor(Date.now() / PREVIEW)}`;
// As the Alt+Tab switcher's card (quickshell window-switcher/WindowCard.qml):
// the preview in its window's shape, the desktop top left, close top right,
// the app's icon bottom left, each on its own scrim; the title below. Tap
// focuses, a long press offers the other desktops.
function card(w, workspaces, wsIcons) {
const c = document.createElement("button");
c.className = `wcard${w.active ? " cur" : ""}`;
c.setAttribute("aria-label", w.title || w.class);
c.innerHTML = `<div class="wbox"><img class="wshot" alt=""><span class="wws"></span>
<span class="wx" role="button" aria-label="Close window">×</span></div><div class="wtitle"></div>`;
const shot = c.querySelector(".wshot");
shot.src = previewUrl(w);
shot.onerror = () => { shot.hidden = true; };
wsMark(c.querySelector(".wws"), w.workspace, wsIcons);
c.querySelector(".wtitle").textContent = w.title || w.class;
const icon = appIcon(w);
if (icon) c.querySelector(".wbox").append(icon);
c.querySelector(".wx").addEventListener("click", (e) => {
e.stopPropagation();
post("close", { address: w.address });
});
let timer = null;
let long = false;
c.addEventListener("pointerdown", (e) => {
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);
});
// Chrome's own long-press menu (copy or share the image) is not wanted here.
c.addEventListener("contextmenu", (e) => e.preventDefault());
for (const ev of ["pointerup", "pointercancel", "pointerleave"]) {
c.addEventListener(ev, () => clearTimeout(timer));
}
c.onclick = () => {
if (long) return;
post("focus", { address: w.address });
};
return c;
}
function appIcon(w) {
if (!w.icons?.length) return null;
const img = document.createElement("img");
img.className = "wicon";
img.alt = "";
let i = 0;
const next = () => {
if (i >= w.icons.length) {
img.remove();
return;
}
img.src = `/icons/${w.icons[i++]}.svg`;
};
img.addEventListener("error", next);
next();
return img;
}
export default {
title: "Windows",
tab: "Windows",
interval: 1,
mount(el) {
el.innerHTML = `<div class="windows">
<section class="deck-block"><h3>Workspaces</h3><div class="ws"></div></section>
<section class="deck-block"><h3>Launches</h3><div class="launches"></div></section>
<section class="deck-block"><h3>Windows</h3><div class="wgrid"></div></section>
</div>`;
},
update(st, el) {
// Polled every second: rebuilding unchanged rows reloads every icon
// through its fallbacks, which flickers, so only a change re-renders.
const key = JSON.stringify(st);
if (key === el._last) {
for (const img of el.querySelectorAll(".wshot")) {
const url = previewUrl({ id: img.closest(".wcard").dataset.id });
if (!img.src.endsWith(url)) { img.hidden = false; img.src = url; }
}
return;
}
el._last = key;
const ws = el.querySelector(".ws");
ws.textContent = "";
for (const id of st.workspaces ?? []) {
const b = tile("", () => post("workspace", { n: id }));
b.setAttribute("aria-label", `Desktop ${id}`);
wsMark(b, id, st.wsIcons);
b.classList.toggle("busy", (st.windows ?? []).some((w) => w.workspace === id));
ws.append(b);
}
const launches = el.querySelector(".launches");
launches.textContent = "";
for (const item of st.launches ?? []) {
const b = document.createElement("button");
b.className = "wtile launch";
b.setAttribute("aria-label", item.label || item.key);
b.title = item.label || item.key;
if (item.icon) {
const icon = document.createElement("img");
icon.className = "laticon";
icon.src = `/icons/${item.icon}.svg`;
icon.alt = "";
// An app whose icon the theme lacks shows its name instead.
icon.onerror = () => { b.textContent = item.label || item.key; };
b.append(icon);
} else {
b.textContent = item.label || item.key;
}
b.onclick = () => post("launch", { key: item.key });
launches.append(b);
}
const grid = el.querySelector(".wgrid");
grid.textContent = "";
for (const w of st.windows ?? []) {
const c = card(w, st.workspaces, st.wsIcons);
c.dataset.id = w.id;
grid.append(c);
}
},
};
|