// Pure helpers for the workspace overview: the grid of workspace slots, // keyboard movement through it and where a window sits in its thumbnail. // Up to five slots a row; rows are balanced (9 → 5 + 4, 6 → 3 + 3). function grid(count) { const n = Math.max(1, Math.floor(count) || 1); const rows = Math.ceil(n / 5); const columns = Math.ceil(n / rows); return {count: n, rows: rows, columns: columns}; } // Slot index → row and (fractional) column. A short last row is centred. function cell(index, g) { const row = Math.floor(index / g.columns); const inRow = row === g.rows - 1 ? g.count - row * g.columns : g.columns; return {row: row, column: index % g.columns + (g.columns - inRow) / 2}; } // Next slot for an arrow key ("left", "right", "up", "down"). Left/right // wrap through every slot; up/down pick the nearest slot in the next row, // wrapping from the last row to the first. function step(index, direction, g) { const n = g.count; if (index < 0 || index >= n) return 0; if (direction === "left") return (index - 1 + n) % n; if (direction === "right") return (index + 1) % n; if (g.rows < 2) return index; const here = cell(index, g); const row = (here.row + (direction === "up" ? -1 : 1) + g.rows) % g.rows; let best = -1, distance = Infinity; for (let i = row * g.columns; i < Math.min(n, (row + 1) * g.columns); i++) { const d = Math.abs(cell(i, g).column - here.column); if (d < distance - 1e-6) { best = i; distance = d; } } return best; } // A window's rectangle inside a width × height thumbnail of its monitor. // The monitor (logical {x, y, width, height}) is fitted into the thumbnail, // centred when the aspect ratios differ, and the window clipped to it. // Returns null when nothing of the window is on the monitor. function project(win, monitor, width, height) { if (!monitor || monitor.width <= 0 || monitor.height <= 0 || win.width <= 0 || win.height <= 0) return null; const s = Math.min(width / monitor.width, height / monitor.height); const ox = (width - monitor.width * s) / 2, oy = (height - monitor.height * s) / 2; const left = Math.max(win.x, monitor.x), top = Math.max(win.y, monitor.y); const right = Math.min(win.x + win.width, monitor.x + monitor.width); const bottom = Math.min(win.y + win.height, monitor.y + monitor.height); if (right <= left || bottom <= top) return null; return { x: ox + (left - monitor.x) * s, y: oy + (top - monitor.y) * s, width: (right - left) * s, height: (bottom - top) * s }; }