// Pure helpers for the Control Center grid: packing, drag reordering and // resizing. // Tiles are {id, w, h} in grid cells; the grid is `cols` wide and grows down. var COLS = 4; var MAX = 4; function clampSize(n) { n = Math.round(Number(n)); return Number.isFinite(n) ? Math.max(1, Math.min(MAX, n)) : 1; } // Packs tiles first-fit in list order: each takes the first free spot, scanning // rows top to bottom and columns left to right, so small tiles backfill gaps. function pack(tiles, cols) { cols = cols || COLS; const occ = [], items = []; let rows = 0; function free(r, c, w, h) { for (let y = r; y < r + h; y++) for (let x = c; x < c + w; x++) if (occ[y] && occ[y][x]) return false; return true; } for (const t of tiles) { const w = Math.min(cols, clampSize(t.w)), h = clampSize(t.h); let placed = false; for (let r = 0; !placed; r++) { for (let c = 0; c + w <= cols; c++) { if (!free(r, c, w, h)) continue; for (let y = r; y < r + h; y++) { occ[y] = occ[y] || []; for (let x = c; x < c + w; x++) occ[y][x] = true; } items.push({id: t.id, col: c, row: r, w: w, h: h}); rows = Math.max(rows, r + h); placed = true; break; } } } return {items: items, rows: rows}; } // The packed tile covering a cell, or null. function at(plan, col, row) { return plan.items.find(i => col >= i.col && col < i.col + i.w && row >= i.row && row < i.row + i.h) || null; } // Order after dragging `id` over the cell (col, row): it takes the index of the // tile under the pointer. Over empty space it goes after the last tile that // starts at or before that cell in reading order. function reorder(ids, plan, id, col, row) { if (ids.indexOf(id) < 0) return ids.slice(); const hit = at(plan, col, row); if (hit && hit.id === id) return ids.slice(); const next = ids.filter(i => i !== id); let to; if (hit) { // Moving forward lands after the hit tile, backward lands before it. to = next.indexOf(hit.id) + (ids.indexOf(id) < ids.indexOf(hit.id) ? 1 : 0); } else { const before = plan.items.filter(i => i.id !== id && (i.row < row || (i.row === row && i.col <= col))); to = before.length ? Math.max(...before.map(i => next.indexOf(i.id))) + 1 : 0; } next.splice(to, 0, id); return next; } // Cells for a tile resized by a pixel delta from its start size. function resize(start, dx, dy, cellW, cellH, gap) { return { w: clampSize(start.w + dx / (cellW + gap)), h: clampSize(start.h + dy / (cellH + gap)) }; } // Pixel geometry. `extras` maps tile id -> extra height below it (an expanded // tile); everything starting at or below that tile's bottom row moves down. function geometry(plan, cellW, cellH, gap, extras) { extras = extras || {}; const boundary = {}; for (const i of plan.items) { const e = extras[i.id] || 0; if (e > 0) boundary[i.row + i.h] = Math.max(boundary[i.row + i.h] || 0, e + gap); } function shift(row) { let s = 0; for (const b in boundary) if (row >= Number(b)) s += boundary[b]; return s; } const out = {}; for (const i of plan.items) out[i.id] = { x: i.col * (cellW + gap), y: i.row * (cellH + gap) + shift(i.row), w: i.w * cellW + (i.w - 1) * gap, h: i.h * cellH + (i.h - 1) * gap + ((extras[i.id] || 0) > 0 ? extras[i.id] + gap : 0) }; const height = plan.rows ? plan.rows * (cellH + gap) - gap + shift(plan.rows) : 0; return {tiles: out, height: height}; }