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>
This commit is contained in:
1 parent
72a21c9363
commit
7f1189606c
14 files changed
+412
-157
No files matched your search
@@ -1,6 +1,7 @@
|
||||
// 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.
|
||||
// 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;
|
||||
@@ -10,66 +11,92 @@ function clampSize(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) {
|
||||
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 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;
|
||||
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) {
|
||||
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;
|
||||
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: rows};
|
||||
return {items: items, rows: rowsOf(items)};
|
||||
}
|
||||
|
||||
// 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;
|
||||
// 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.
|
||||
|
||||
Reference in new issue
Block a user