Files
glassMxrph/quickshell/controlcenter/GridLogic.js
T
mxandClaude Opus 5.5 7f1189606c Place Control Center tiles freely and host plugin widgets as tiles
Tiles keep the cell they are dropped in, so gaps stay where they are
left; moving or resizing onto other tiles pushes those straight down.
Enabled plugins with a Noctalia controlCenterWidget entry point are
offered as tiles in edit mode.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 22:02:18 +02:00

138 lines
5.0 KiB
JavaScript

// Pure helpers for the Control Center grid: free placement, moving with
// push-down, resizing and pixel geometry.
// Tiles are {id, w, h, x?, y?}: a size in cells and an optional position
// (column x, row y). 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;
}
function overlaps(a, b) {
return a.col < b.col + b.w && b.col < a.col + a.w && a.row < b.row + b.h && b.row < a.row + a.h;
}
function rowsOf(items) {
return items.reduce((n, i) => Math.max(n, i.row + i.h), 0);
}
// Places tiles. Positioned tiles keep their spot (an earlier one wins; a later
// one it overlaps moves straight down to the first free row). Tiles without a
// position then take the first free spot in reading order. Gaps stay as set.
function layout(tiles, cols) {
cols = cols || COLS;
const placed = [];
const free = t => !placed.some(p => overlaps(p, t));
const sized = t => {
const w = Math.min(cols, clampSize(t.w)), h = clampSize(t.h);
return {id: t.id, col: 0, row: 0, w: w, h: h};
};
for (const t of tiles) {
if (!Number.isInteger(t.x) || !Number.isInteger(t.y))
continue;
const item = sized(t);
item.col = Math.max(0, Math.min(cols - item.w, t.x));
item.row = Math.max(0, t.y);
while (!free(item))
item.row++;
placed.push(item);
}
for (const t of tiles) {
if (Number.isInteger(t.x) && Number.isInteger(t.y))
continue;
const item = sized(t);
let done = false;
for (let r = 0; !done; r++)
for (let c = 0; c + item.w <= cols && !done; c++) {
item.col = c;
item.row = r;
done = free(item);
}
placed.push(item);
}
// Back in list order.
const items = tiles.map(t => placed.find(p => p.id === t.id));
return {items: items, rows: rowsOf(items)};
}
// First free spot for a new tile, in reading order.
function freeSpot(plan, w, h, cols) {
return layout(plan.items.map(i => ({id: i.id, x: i.col, y: i.row, w: i.w, h: i.h})).concat([{id: "\u0000new", w: w, h: h}]), cols).items.slice(-1)[0];
}
// Moves and/or resizes one tile. Tiles it lands on are pushed straight down
// below it, cascading; nothing moves up, so gaps elsewhere are kept.
function moveTo(plan, id, target, cols) {
cols = cols || COLS;
const items = plan.items.map(i => Object.assign({}, i));
const moved = items.find(i => i.id === id);
if (!moved)
return {items: items, rows: rowsOf(items)};
moved.w = Math.min(cols, clampSize(target.w !== undefined ? target.w : moved.w));
moved.h = clampSize(target.h !== undefined ? target.h : moved.h);
moved.col = Math.max(0, Math.min(cols - moved.w, Math.round(target.col !== undefined ? target.col : moved.col)));
moved.row = Math.max(0, Math.round(target.row !== undefined ? target.row : moved.row));
const queue = [moved];
while (queue.length) {
const a = queue.shift();
for (const b of items) {
if (b === a || !overlaps(a, b))
continue;
if (b === moved) {
// A pushed tile never displaces the moved one: it goes below it.
a.row = moved.row + moved.h;
queue.push(a);
break;
}
b.row = a.row + a.h;
queue.push(b);
}
}
return {items: items, rows: rowsOf(items)};
}
// Cell for a tile whose top-left corner is at pixel (x, y).
function cellAt(x, y, cellW, cellH, gap) {
return {col: Math.round(x / (cellW + gap)), row: Math.round(y / (cellH + gap))};
}
// 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};
}