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
@@ -46,9 +46,11 @@ PanelWindow {
|
||||
Component.onCompleted: ShellState.controlCenterWindow = root
|
||||
Component.onDestruction: ShellState.controlCenterWindow = null
|
||||
|
||||
// Modules on a 4-column grid, packed first-fit in the configured order;
|
||||
// Notifications is a section under it. Edit mode reorders by dragging,
|
||||
// resizes from the corner handle, removes with the badge and adds from a tray.
|
||||
// Modules sit freely on a 4-column grid at their saved cell, so gaps stay
|
||||
// where they are left; tiles without a position take the first free spot.
|
||||
// Notifications is a section under it. Edit mode moves tiles by dragging and
|
||||
// resizes them from the corner handle (tiles in the way are pushed down),
|
||||
// removes them with the badge and adds them from a tray.
|
||||
readonly property int gap: 10
|
||||
readonly property int cellHeight: 68
|
||||
readonly property real gridWidth: panel.targetWidth - 28
|
||||
@@ -56,27 +58,29 @@ PanelWindow {
|
||||
readonly property var entries: ConfigService.ready ? ConfigService.data.controlCenter.modules : []
|
||||
readonly property var tiles: entries.filter(m => m.enabled && !!ControlRegistry.entry(m.id) && !ControlRegistry.entry(m.id).section)
|
||||
readonly property bool notifications: entries.some(m => m.id === "notifications" && m.enabled)
|
||||
readonly property var addable: Object.keys(ControlRegistry.modules).filter(id => !entries.some(m => m.id === id && m.enabled))
|
||||
readonly property var addable: ControlRegistry.ids.filter(id => !entries.some(m => m.id === id && m.enabled))
|
||||
property bool editing: false
|
||||
// Live edit previews: the order while a tile is dragged, a size while resized.
|
||||
property var dragOrder: null
|
||||
// Live edit preview while a tile is dragged or resized.
|
||||
property var preview: null
|
||||
property string dragId: ""
|
||||
property point dragPoint
|
||||
property point grabOffset
|
||||
property string resizeId: ""
|
||||
property var resizeSize: null
|
||||
// Tile id -> pixels a tile extends below its cells.
|
||||
property var extras: ({})
|
||||
readonly property var order: dragOrder ? dragOrder.filter(id => tiles.some(m => m.id === id)) : tiles.map(m => m.id)
|
||||
readonly property var plan: Grid.pack(order.map(id => {
|
||||
const m = tiles.find(t => t.id === id);
|
||||
const size = id === resizeId && resizeSize ? resizeSize : ControlRegistry.size(m);
|
||||
readonly property var savedPlan: Grid.layout(tiles.map(m => {
|
||||
const size = ControlRegistry.size(m);
|
||||
return {
|
||||
id: id,
|
||||
id: m.id,
|
||||
w: size.w,
|
||||
h: size.h
|
||||
h: size.h,
|
||||
x: m.x,
|
||||
y: m.y
|
||||
};
|
||||
}))
|
||||
readonly property var plan: preview || savedPlan
|
||||
// Edit mode shows one empty row below the tiles to drop into.
|
||||
readonly property int shownRows: plan.rows + (editing ? 1 : 0)
|
||||
readonly property var geometry: Grid.geometry(plan, cellWidth, cellHeight, gap, editing ? {} : extras)
|
||||
function setExtra(id, value) {
|
||||
if ((extras[id] || 0) === value)
|
||||
@@ -85,30 +89,58 @@ PanelWindow {
|
||||
next[id] = value;
|
||||
extras = next;
|
||||
}
|
||||
function cellAt(p) {
|
||||
return Qt.point(Math.floor(p.x / (cellWidth + gap)), Math.floor(p.y / (cellHeight + gap)));
|
||||
}
|
||||
// Writes the module list back, keeping entries that are not grid tiles.
|
||||
function saveOrder(ids) {
|
||||
const rest = entries.filter(m => !ids.includes(m.id));
|
||||
ConfigService.setValue("controlCenter.modules", ids.map(id => entries.find(m => m.id === id)).concat(rest));
|
||||
}
|
||||
function saveSize(id, size) {
|
||||
ConfigService.setValue("controlCenter.modules", entries.map(m => m.id === id ? Object.assign({}, m, {
|
||||
w: size.w,
|
||||
h: size.h
|
||||
}) : m));
|
||||
// Writes every tile's cell (and changed sizes) back, so nothing repacks
|
||||
// and gaps stay.
|
||||
function save(plan, patch) {
|
||||
ConfigService.setValue("controlCenter.modules", entries.map(m => {
|
||||
const i = plan.items.find(t => t.id === m.id);
|
||||
let next = Object.assign({}, m);
|
||||
if (i) {
|
||||
const def = ControlRegistry.size({
|
||||
id: m.id
|
||||
});
|
||||
next.x = i.col;
|
||||
next.y = i.row;
|
||||
if (m.w !== undefined || m.h !== undefined || i.w !== def.w || i.h !== def.h) {
|
||||
next.w = i.w;
|
||||
next.h = i.h;
|
||||
}
|
||||
}
|
||||
if (patch && patch[m.id])
|
||||
next = patch[m.id](next);
|
||||
return next;
|
||||
}));
|
||||
}
|
||||
function setEnabled(id, on) {
|
||||
const known = entries.some(m => m.id === id);
|
||||
ConfigService.setValue("controlCenter.modules", known ? entries.map(m => m.id === id ? Object.assign({}, m, {
|
||||
enabled: on
|
||||
}) : m) : entries.concat([
|
||||
{
|
||||
id: id,
|
||||
enabled: on
|
||||
}
|
||||
]));
|
||||
if (!on) {
|
||||
// The rest keep their cells; the removed tile forgets its own.
|
||||
save(savedPlan, {
|
||||
[id]: m => {
|
||||
const next = Object.assign({}, m, {
|
||||
enabled: false
|
||||
});
|
||||
delete next.x;
|
||||
delete next.y;
|
||||
return next;
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
const e = ControlRegistry.entry(id);
|
||||
const known = entries.find(m => m.id === id);
|
||||
const size = ControlRegistry.size(known || {
|
||||
id: id
|
||||
});
|
||||
const spot = e && !e.section ? Grid.freeSpot(savedPlan, size.w, size.h) : null;
|
||||
const added = Object.assign({}, known || {
|
||||
id: id
|
||||
}, {
|
||||
enabled: true
|
||||
}, spot ? {
|
||||
x: spot.col,
|
||||
y: spot.row
|
||||
} : {});
|
||||
ConfigService.setValue("controlCenter.modules", known ? entries.map(m => m.id === id ? added : m) : entries.concat([added]));
|
||||
}
|
||||
function reset() {
|
||||
ConfigService.setValue("controlCenter.modules", ConfigService.defaults.controlCenter.modules);
|
||||
@@ -297,8 +329,8 @@ PanelWindow {
|
||||
id: grid
|
||||
objectName: "controlGrid"
|
||||
Layout.fillWidth: true
|
||||
implicitHeight: root.geometry.height
|
||||
visible: root.tiles.length > 0
|
||||
implicitHeight: root.geometry.height + (root.editing ? root.cellHeight + root.gap : 0)
|
||||
visible: root.tiles.length > 0 || root.editing
|
||||
Behavior on implicitHeight {
|
||||
enabled: panel.settled && !root.dragId
|
||||
NumberAnimation {
|
||||
@@ -306,6 +338,36 @@ PanelWindow {
|
||||
easing.type: Easing.OutCubic
|
||||
}
|
||||
}
|
||||
// Edit mode: faint cells show the grid, and where a dragged tile lands.
|
||||
Repeater {
|
||||
// Only the empty cells: tiles are translucent.
|
||||
model: root.editing ? Array.from({
|
||||
length: Grid.COLS * root.shownRows
|
||||
}, (_, i) => i).filter(i => !root.plan.items.some(t => i % Grid.COLS >= t.col && i % Grid.COLS < t.col + t.w && Math.floor(i / Grid.COLS) >= t.row && Math.floor(i / Grid.COLS) < t.row + t.h)) : []
|
||||
Rectangle {
|
||||
required property int modelData
|
||||
x: (modelData % Grid.COLS) * (root.cellWidth + root.gap)
|
||||
y: Math.floor(modelData / Grid.COLS) * (root.cellHeight + root.gap)
|
||||
width: root.cellWidth
|
||||
height: root.cellHeight
|
||||
radius: 16
|
||||
color: Qt.alpha(Theme.text, 0.03)
|
||||
border.width: 1
|
||||
border.color: Qt.alpha(Theme.text, 0.07)
|
||||
}
|
||||
}
|
||||
Rectangle {
|
||||
readonly property var target: root.dragId ? root.geometry.tiles[root.dragId] : null
|
||||
visible: !!target
|
||||
x: target ? target.x : 0
|
||||
y: target ? target.y : 0
|
||||
width: target ? target.w : 0
|
||||
height: target ? target.h : 0
|
||||
radius: Math.min(30, height / 2, width / 2)
|
||||
color: Qt.alpha(Theme.accent, 0.12)
|
||||
border.width: 2
|
||||
border.color: Qt.alpha(Theme.accent, 0.6)
|
||||
}
|
||||
Repeater {
|
||||
// Keyed by id so moving or resizing keeps each module alive.
|
||||
model: ScriptModel {
|
||||
@@ -368,6 +430,12 @@ PanelWindow {
|
||||
// Edit mode shows every tile at rest.
|
||||
opacity: root.editing ? 0.85 : 1
|
||||
}
|
||||
Binding {
|
||||
target: loader.item
|
||||
when: !!loader.item
|
||||
property: "moduleId"
|
||||
value: slot.modelData
|
||||
}
|
||||
Binding {
|
||||
target: loader.item
|
||||
when: !!loader.item && !!slot.cell
|
||||
@@ -434,12 +502,12 @@ PanelWindow {
|
||||
hoverEnabled: true
|
||||
preventStealing: true
|
||||
cursorShape: pressed ? Qt.ClosedHandCursor : Qt.OpenHandCursor
|
||||
property point lastCell: Qt.point(-1, -1)
|
||||
property var lastCell: null
|
||||
onPressed: mouse => {
|
||||
const p = mapToItem(grid, mouse.x, mouse.y);
|
||||
root.grabOffset = Qt.point(p.x - slot.x, p.y - slot.y);
|
||||
root.dragPoint = p;
|
||||
lastCell = Qt.point(-1, -1);
|
||||
lastCell = null;
|
||||
}
|
||||
onPositionChanged: mouse => {
|
||||
if (!pressed)
|
||||
@@ -448,27 +516,28 @@ PanelWindow {
|
||||
if (!root.dragId) {
|
||||
if (Math.hypot(p.x - root.dragPoint.x, p.y - root.dragPoint.y) < 6)
|
||||
return;
|
||||
root.dragOrder = root.order.slice();
|
||||
root.dragId = slot.modelData;
|
||||
}
|
||||
root.dragPoint = p;
|
||||
const c = root.cellAt(p);
|
||||
if (c.x === lastCell.x && c.y === lastCell.y)
|
||||
// The cell nearest the tile's top-left corner, at most one row below the rest.
|
||||
const c = Grid.cellAt(p.x - root.grabOffset.x, p.y - root.grabOffset.y, root.cellWidth, root.cellHeight, root.gap);
|
||||
c.row = Math.min(c.row, root.savedPlan.rows);
|
||||
if (lastCell && c.col === lastCell.col && c.row === lastCell.row)
|
||||
return;
|
||||
lastCell = c;
|
||||
root.dragOrder = Grid.reorder(root.order, root.plan, slot.modelData, Math.max(0, Math.min(Grid.COLS - 1, c.x)), Math.max(0, c.y));
|
||||
// Always from the saved layout, so tiles pushed aside come back.
|
||||
root.preview = Grid.moveTo(root.savedPlan, slot.modelData, c);
|
||||
}
|
||||
onReleased: {
|
||||
const order = root.dragOrder;
|
||||
const moved = !!root.dragId && order && order.join() !== root.tiles.map(m => m.id).join();
|
||||
const plan = root.preview;
|
||||
root.dragId = "";
|
||||
if (moved)
|
||||
root.saveOrder(order);
|
||||
root.dragOrder = null;
|
||||
root.preview = null;
|
||||
if (plan)
|
||||
root.save(plan);
|
||||
}
|
||||
onCanceled: {
|
||||
root.dragId = "";
|
||||
root.dragOrder = null;
|
||||
root.preview = null;
|
||||
}
|
||||
}
|
||||
Badge {
|
||||
@@ -523,7 +592,6 @@ PanelWindow {
|
||||
w: slot.cell.w,
|
||||
h: slot.cell.h
|
||||
};
|
||||
root.resizeSize = startSize;
|
||||
root.resizeId = slot.modelData;
|
||||
}
|
||||
onPositionChanged: mouse => {
|
||||
@@ -531,19 +599,19 @@ PanelWindow {
|
||||
return;
|
||||
const p = mapToItem(grid, mouse.x, mouse.y);
|
||||
const next = Grid.resize(startSize, p.x - start.x, p.y - start.y, root.cellWidth, root.cellHeight, root.gap);
|
||||
if (next.w !== root.resizeSize.w || next.h !== root.resizeSize.h)
|
||||
root.resizeSize = next;
|
||||
if (next.w !== slot.cell.w || next.h !== slot.cell.h)
|
||||
root.preview = Grid.moveTo(root.savedPlan, slot.modelData, next);
|
||||
}
|
||||
onReleased: {
|
||||
const size = root.resizeSize;
|
||||
const plan = root.preview;
|
||||
root.resizeId = "";
|
||||
root.resizeSize = null;
|
||||
if (size && (size.w !== startSize.w || size.h !== startSize.h))
|
||||
root.saveSize(slot.modelData, size);
|
||||
root.preview = null;
|
||||
if (plan)
|
||||
root.save(plan);
|
||||
}
|
||||
onCanceled: {
|
||||
root.resizeId = "";
|
||||
root.resizeSize = null;
|
||||
root.preview = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user