pragma Singleton import QtQuick import Quickshell import Quickshell.Io import "../services" import "../core" import "../services/WidgetLogic.js" as Logic // Desktop widget items plus editor state. While a widget is dragged or resized // its geometry lives in `drafts`, so the real surface follows live; the result // is written to the configuration on release. Singleton { id: root readonly property var items: ConfigService.ready ? ConfigService.data.desktopWidgets.items : [] readonly property bool grid: ConfigService.ready && ConfigService.data.desktopWidgets.grid readonly property int gridSize: 20 property bool editing: false property string selected: "" property var drafts: ({}) // Live surfaces by widget id and editor windows, for diagnostics and tests. property var surfaces: ({}) property var editors: [] function item(id) { return items.find(i => i.id === id) || null; } function rect(id) { const draft = drafts[id]; if (draft) return draft; const i = item(id); return i ? { x: i.x, y: i.y, w: i.w, h: i.h } : { x: 0, y: 0, w: 0, h: 0 }; } function limits(type) { const t = WidgetRegistry.type(type); return { minW: t ? t.min[0] : 60, minH: t ? t.min[1] : 60, maxW: 4000, maxH: 4000 }; } function setDraft(id, r) { const next = Object.assign({}, drafts); next[id] = { x: r.x, y: r.y, w: r.w, h: r.h }; drafts = next; } function commit(id) { const r = drafts[id]; if (!r) return; update(id, { x: Math.round(r.x), y: Math.round(r.y), w: Math.round(r.w), h: Math.round(r.h) }); const next = Object.assign({}, drafts); delete next[id]; drafts = next; } function update(id, patch) { return ConfigService.setValue("desktopWidgets.items", Logic.updateItem(items, id, patch)); } function setOption(id, key, value) { const i = item(id); if (!i) return false; const options = Object.assign({}, i.options || {}); options[key] = value; return update(id, { options: options }); } // Adds a widget centred on the given output and selects it. function add(type, monitor) { const t = WidgetRegistry.type(type); const screen = Quickshell.screens.find(s => s.name === monitor) || ScreenService.active; if (!t || !screen) return ""; const id = Logic.newId(items); let x = Math.round((screen.width - t.size[0]) / 2), y = Math.round((screen.height - t.size[1]) / 2); if (grid) { x = Math.round(x / gridSize) * gridSize; y = Math.round(y / gridSize) * gridSize; } // Offset when another widget already sits exactly there. while (items.some(i => i.monitor === screen.name && i.x === x && i.y === y)) { x += 24; y += 24; } const added = ConfigService.setValue("desktopWidgets.items", Logic.addItem(items, { id: id, type: type, variant: WidgetRegistry.defaultVariant(type), monitor: screen.name, x: x, y: y, w: t.size[0], h: t.size[1], opacity: 1, options: {} })); if (added) selected = id; return added ? id : ""; } function duplicate(id) { const i = item(id); if (!i) return ""; const copy = Object.assign({}, i, { id: Logic.newId(items), x: i.x + 24, y: i.y + 24, options: Object.assign({}, i.options || {}) }); if (ConfigService.setValue("desktopWidgets.items", Logic.addItem(items, copy))) selected = copy.id; return copy.id; } function remove(id) { if (selected === id) selected = ""; return ConfigService.setValue("desktopWidgets.items", Logic.removeItem(items, id)); } function edit(id) { ShellState.settingsOpen = false; ShellState.controlCenterOpen = false; ShellState.launcherOpen = false; ShellState.wallpaperPickerOpen = false; selected = id || ""; editing = true; } function done() { editing = false; selected = ""; drafts = {}; } IpcHandler { target: "widgets" function edit(): void { root.edit(""); } function toggle(): void { if (root.editing) root.done(); else root.edit(""); } function done(): void { root.done(); } function add(type: string): string { return root.add(type, ScreenService.active ? ScreenService.active.name : ""); } function list(): string { return JSON.stringify({ editing: root.editing, selected: root.selected, items: root.items }); } } }