Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions shell/scripts/watch-local-plugins
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

set -euo pipefail

plugins_dir=$1
canonical_plugins_dir=$(realpath -e -- "$plugins_dir")
watch_paths=("$plugins_dir")

# inotifywait does not traverse a symlink it finds below a recursive watch.
# Passing a direct plugin symlink explicitly makes it report changes through
# the logical plugin path, which the registry already knows how to route.
for plugin_dir in "$plugins_dir"/*; do
[[ -L $plugin_dir && -d $plugin_dir ]] || continue
target=$(realpath -e -- "$plugin_dir") || continue
[[ $target == "$canonical_plugins_dir" || $target == "$canonical_plugins_dir"/* ]] && continue
watch_paths+=("$plugin_dir")
done

exec inotifywait -m -r -q \
-e close_write,create,delete,move \
--format '%w%f' \
"${watch_paths[@]}"
28 changes: 17 additions & 11 deletions shell/services/PluginRegistry.qml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ QtObject {
property int registryRevision: 0
property bool scanning: false
property string lastEnableError: ""
readonly property string localPluginWatcherPath: Quickshell.env("OMARCHY_PATH") + "/shell/scripts/watch-local-plugins"

signal pluginsChanged()
signal scanFinished()
Expand Down Expand Up @@ -634,21 +635,12 @@ QtObject {
}

property Process localPluginWatcher: Process {
command: [
"inotifywait",
"-m",
"-r",
"-q",
"-e",
"close_write,create,delete,move",
"--format",
"%w%f",
registry.pluginsDir
]
command: [registry.localPluginWatcherPath, registry.pluginsDir]
stdout: SplitParser {
onRead: function(path) {
var pluginId = registry.localPluginIdForPath(path)
if (pluginId) registry.localPluginChanged(pluginId)
if (registry.localPluginRootChanged(path)) registry.restartLocalPluginWatcher()
}
}
onExited: localPluginWatcherRestart.restart()
Expand All @@ -659,6 +651,11 @@ QtObject {
onTriggered: localPluginWatcher.running = true
}

function restartLocalPluginWatcher() {
if (localPluginWatcher.running) localPluginWatcher.running = false
localPluginWatcherRestart.restart()
}

function rescan() {
if (scanning) return
scanning = true
Expand Down Expand Up @@ -712,5 +709,14 @@ QtObject {
return slash === -1 ? relative : relative.slice(0, slash)
}

function localPluginRootChanged(filePath) {
var base = pluginsDir.replace(/\/$/, "") + "/"
var path = String(filePath || "").trim()
if (path.indexOf(base) !== 0) return false

var relative = path.slice(base.length)
return relative.length > 0 && relative.charAt(0) !== "." && relative.indexOf("/") === -1
}

Component.onCompleted: ensureUserDir()
}
59 changes: 5 additions & 54 deletions shell/shell.qml
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ ShellRoot {

property var defaultsConfig: builtinShellConfig
property var shellConfig: builtinShellConfig
property bool pluginReloading: false
property bool pluginReloadPending: false

Timer {
id: localPluginReloadTimer
interval: 150
Expand Down Expand Up @@ -246,7 +243,7 @@ ShellRoot {
Loader {
id: pluginBarLoader

active: !shell.pluginReloading && shell.activeBarId !== shell.defaultBarId && shell.activeBarSourceUrl !== ""
active: shell.activeBarId !== shell.defaultBarId && shell.activeBarSourceUrl !== ""
source: shell.activeBarId !== shell.defaultBarId ? shell.activeBarSourceUrl : ""
asynchronous: true
onLoaded: shell.configureBar(item, shell.activeBarManifest)
Expand Down Expand Up @@ -345,17 +342,9 @@ ShellRoot {
}
}

function unloadPluginServices() {
for (var existingId in _services) {
var inst = _services[existingId]
if (inst && typeof inst.destroy === "function") inst.destroy()
}
_services = ({})
}

Connections {
target: shell.pluginRegistry
function onPluginsChanged() { if (!shell.pluginReloading) shell._syncServices() }
function onPluginsChanged() { shell._syncServices() }
}

// Writes inline settings to a bar layout entry or top-level plugin entry in
Expand Down Expand Up @@ -530,14 +519,6 @@ ShellRoot {
panelLoaders = next
}

function unloadPanels() {
for (var id in panelLoaders) hide(id)
panelEntries = []
panelLoaders = ({})
pendingPayloads = ({})
openPanelIds = ({})
}

function deliverIfLoaded(pluginId) {
var loader = panelLoaders[pluginId]
if (!loader || !loader.item) return
Expand Down Expand Up @@ -604,7 +585,7 @@ ShellRoot {

Connections {
target: shell.pluginRegistry
function onPluginsChanged() { if (!shell.pluginReloading) shell.panelEntries = shell.computePanelEntries() }
function onPluginsChanged() { shell.panelEntries = shell.computePanelEntries() }
}

Instantiator {
Expand Down Expand Up @@ -661,7 +642,7 @@ ShellRoot {
// widgets use the same first-party manifest contract as third-party widgets.
Connections {
target: shell.pluginRegistry
function onPluginsChanged() { if (!shell.pluginReloading) shell.syncPluginWidgets() }
function onPluginsChanged() { shell.syncPluginWidgets() }
}

property var pluginWidgetComponents: ({})
Expand Down Expand Up @@ -731,31 +712,8 @@ ShellRoot {
}
}

function unloadPluginWidgets() {
for (var id in pluginWidgetComponents) shell.barWidgetRegistry.unregister(id)
pluginWidgetComponents = ({})
}

function reloadPlugins() {
if (shell.pluginReloading || shell.pluginRegistry.scanning) {
shell.pluginReloadPending = true
return
}
shell.pluginReloading = true
shell.unloadPanels()
shell.unloadPluginServices()
shell.unloadPluginWidgets()
Qt.callLater(shell.finishPluginReload)
}

function finishPluginReload() {
if (!shell.pluginReloading) return
if (shell.pluginRegistry.scanning) {
shell.pluginReloadPending = true
return
}
if (typeof Qt.clearComponentCache === "function") Qt.clearComponentCache()
shell.pluginRegistry.rescan()
Quickshell.reload(false)
}

Connections {
Expand All @@ -765,13 +723,6 @@ ShellRoot {
localPluginReloadTimer.restart()
}
function onScanFinished() {
if (shell.pluginReloadPending) {
shell.pluginReloadPending = false
shell.pluginReloading = false
Qt.callLater(shell.reloadPlugins)
return
}
shell.pluginReloading = false
shell._syncServices()
shell.panelEntries = shell.computePanelEntries()
shell.syncPluginWidgets()
Expand Down
3 changes: 3 additions & 0 deletions test/shell.d/fixtures/plugin-registry/shell.qml
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,9 @@ ShellRoot {
root.assertEqual(registry.localPluginIdForPath(registry.pluginsDir + "/acme.clock/BarWidget.qml"), "acme.clock", "installed plugin changes are watched")
root.assertEqual(registry.localPluginIdForPath(cloneBase + "/.git/index"), "", "plugin git metadata is ignored")
root.assertEqual(registry.localPluginIdForPath(registry.pluginsDir + "/.clone.abc123/manifest.json"), "", "hidden staging and backup dirs are ignored")
root.assertTrue(registry.localPluginRootChanged(registry.pluginsDir + "/acme.clock"), "plugin root changes rebuild the watcher")
root.assertTrue(!registry.localPluginRootChanged(registry.pluginsDir + "/acme.clock/BarWidget.qml"), "plugin file changes keep the watcher running")
root.assertTrue(!registry.localPluginRootChanged(registry.pluginsDir + "/.clone.abc123"), "hidden staging changes keep the watcher running")

root.assertTrue(changeCount > 0, "registry emits change notifications")
writeResult()
Expand Down
53 changes: 53 additions & 0 deletions test/shell.d/local-plugin-watcher-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash

set -euo pipefail

source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"

TMPDIR=""
WATCHER_PID=""

cleanup() {
if [[ -n $WATCHER_PID ]] && kill -0 "$WATCHER_PID" 2>/dev/null; then
kill "$WATCHER_PID" 2>/dev/null || true
wait "$WATCHER_PID" 2>/dev/null || true
fi
if [[ -n $TMPDIR && -d $TMPDIR ]]; then
rm -rf "$TMPDIR"
fi
}
trap cleanup EXIT

require_command inotifywait

TMPDIR=$(mktemp -d)
plugins_root="$TMPDIR/plugins-root"
plugins_dir="$TMPDIR/plugins"
linked_source="$TMPDIR/linked-source"
events="$TMPDIR/events"
mkdir -p "$plugins_root" "$linked_source"
ln -s "$plugins_root" "$plugins_dir"
ln -s "$linked_source" "$plugins_dir/acme.linked"

"$ROOT/shell/scripts/watch-local-plugins" "$plugins_dir" >"$events" 2>&1 &
WATCHER_PID=$!
for _ in {1..30}; do
printf 'updated %s\n' "$_" >"$linked_source/Overlay.qml"
sleep 0.1
grep -Fx -- "$plugins_dir/acme.linked/Overlay.qml" "$events" >/dev/null 2>&1 && break
done
grep -Fx -- "$plugins_dir/acme.linked/Overlay.qml" "$events" >/dev/null ||
fail "linked plugin target writes reach the local plugin watcher"
pass "linked plugin target writes reach the local plugin watcher"

mkdir -p "$TMPDIR/replacement-source"
ln -s "$TMPDIR/replacement-source" "$plugins_dir/.acme.linked-replacement"
mv -T "$plugins_dir/.acme.linked-replacement" "$plugins_dir/acme.linked"

for _ in {1..30}; do
grep -Fx -- "$plugins_dir/acme.linked" "$events" >/dev/null 2>&1 && break
sleep 0.1
done
grep -Fx -- "$plugins_dir/acme.linked" "$events" >/dev/null ||
fail "linked plugin replacement reaches the local plugin watcher"
pass "linked plugin replacement reaches the local plugin watcher"
Loading