Files
glassMxrph/quickshell/plugins/PluginHost.qml
T
mxandClaude Opus 5.5 d7f0498ba0 Keep plugin services alive across config writes
Every ConfigService write rebuilt PluginHost's hosted array, and the
Instantiator recreated all plugins for it. A wallpaper change thus
restarted the screen recorder's replay buffer and left the new
instance's IpcHandler shadowed by the old one. Mirror the list into a
ListModel that only adds or removes plugins whose state changed.

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

122 lines
5.5 KiB
QML

pragma ComponentBehavior: Bound
import QtQuick
import Quickshell
import "../services"
import "../compat/noctalia/Host" as NoctaliaHost
// Not referenced by name: importing the directory makes Quickshell index the
// Noctalia compatibility modules (see the Loader below).
// qmllint disable unused-imports
import "../compat/noctalia" as NoctaliaCompat
// qmllint enable unused-imports
// Loads enabled plugins that are not bar widgets: services stay loaded while
// enabled; panels, overlays and menus load when summoned (or always, with
// "keepLoaded") and receive open(payload), like in Omarchy. Bar widgets are
// hosted by bar/modules/PluginModule.qml.
Scope {
id: root
// The directory import above puts the Noctalia compatibility modules in
// Quickshell's qs.* index; loading them by URL keeps any failure there a
// warning instead of breaking the whole shell.
Loader {
active: PluginService.plugins.some(p => p.ecosystem === "noctalia" && PluginService.isEnabled(p.id))
source: active ? Qt.resolvedUrl("../compat/noctalia/Modules.qml") : ""
}
readonly property var hosted: PluginService.plugins.filter(p => PluginService.isEnabled(p.id)).map(p => p.id)
// Every config write (a wallpaper change, say) rebuilds `hosted`, and an
// Instantiator handed a new array recreates all its delegates, restarting
// long-running services such as a replay buffer. Mirror it into a model
// that only changes for plugins that were actually enabled or disabled.
ListModel {
id: hostedModel
}
function syncHosted() {
for (let i = hostedModel.count - 1; i >= 0; i--) {
if (!hosted.includes(hostedModel.get(i).pluginId))
hostedModel.remove(i);
}
const present = [];
for (let i = 0; i < hostedModel.count; i++)
present.push(hostedModel.get(i).pluginId);
for (const id of hosted) {
if (!present.includes(id))
hostedModel.append({ pluginId: id });
}
}
onHostedChanged: syncHosted()
Component.onCompleted: syncHosted()
Instantiator {
model: hostedModel
delegate: Scope {
id: slot
required property string pluginId
readonly property var kinds: PluginService.kinds(pluginId)
readonly property var manifest: (PluginService.plugin(pluginId) || {}).manifest || {}
readonly property bool noctalia: PluginService.ecosystem(pluginId) === "noctalia"
Loader {
id: serviceLoader
active: slot.kinds.includes("service")
// Noctalia's Main.qml gets its pluginApi at creation.
onActiveChanged: {
if (!active) {
const next = Object.assign({}, PluginService.services);
delete next[slot.pluginId];
PluginService.services = next;
} else if (slot.noctalia) {
setSource(PluginService.entryUrl(slot.pluginId, "service"), {
pluginApi: PluginService.noctaliaApi(slot.pluginId)
});
} else {
source = PluginService.entryUrl(slot.pluginId, "service");
}
}
Component.onCompleted: if (active)
activeChanged()
onLoaded: {
if (slot.noctalia) {
// Noctalia Main.qml: shared state reached via pluginApi.mainInstance.
const napi = PluginService.noctaliaApi(slot.pluginId);
if (napi)
napi.mainInstance = item;
} else {
if ("shell" in item)
item.shell = PluginService.shellFor(slot.pluginId);
if ("pluginId" in item)
item.pluginId = slot.pluginId;
}
const next = Object.assign({}, PluginService.services);
next[slot.pluginId] = item;
PluginService.services = next;
}
}
// First matching surface kind (panel, overlay or menu).
readonly property string surfaceKind: ["panel", "overlay", "menu"].find(k => kinds.includes(k)) || ""
Loader {
id: surfaceLoader
active: !slot.noctalia && slot.surfaceKind !== "" && (slot.manifest.keepLoaded === true || PluginService.openSurfaces[slot.pluginId] === true)
source: active ? PluginService.entryUrl(slot.pluginId, slot.surfaceKind) : ""
onLoaded: {
if ("shell" in item)
item.shell = PluginService.shellFor(slot.pluginId);
if ("pluginId" in item)
item.pluginId = slot.pluginId;
PluginService.registerSurface(slot.pluginId, item);
}
}
// Noctalia panels: a glassMxrph bar drawer driven by pluginApi.openPanel().
Loader {
active: slot.noctalia && slot.kinds.includes("panel")
sourceComponent: NoctaliaHost.PanelHost {
api: PluginService.noctaliaApi(slot.pluginId)
source: PluginService.entryUrl(slot.pluginId, "panel")
}
}
Component.onDestruction: PluginService.registerSurface(slot.pluginId, null)
}
}
}