repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

quickshell/nuguland/Usage.qml

Raw
import QtQuick
import Quickshell
import Quickshell.Io

// Persistent widget-usage tracker: counts how often each bar widget is
// opened and exposes a usage-sorted layout order. The order is snapshotted
// at file load so the bar stays stable within a session and re-sorts across
// restarts, while counts keep accumulating live and persist to disk.
Item {
    id: root

    // Canonical widget set; array order is the tie-break for equal counts.
    // "clock" is intentionally absent: it is pinned at a constant bar-center
    // position by Bar.qml, so it must never drift in the usage-sorted order.
    readonly property var widgets: ["launcher", "media", "system", "clipboard", "audio", "brightness", "connectivity", "power", "notifications", "tray", "idle"]

    // Snapshotted once at load (see FileView.onLoaded); the bar binds to this.
    property var layoutOrder: widgets
    property bool snapshotted: false

    function snapshot() {
        if (root.snapshotted)
            return ;

        root.snapshotted = true;
        root.layoutOrder = root.ordered();
    }

    function countFor(name) {
        const counts = usageAdapter.counts || ({
        });
        return Number(counts[String(name)] || 0);
    }

    // widgets sorted by usage desc, ties broken by canonical order
    function ordered() {
        return root.widgets.slice().map((name, index) => {
            return {
                "name": name,
                "index": index,
                "count": root.countFor(name)
            };
        }).sort((a, b) => {
            return (b.count - a.count) || (a.index - b.index);
        }).map((entry) => {
            return entry.name;
        });
    }

    function record(name) {
        const key = String(name);
        if (root.widgets.indexOf(key) < 0)
            return root.stateJson();

        const next = Object.assign({
        }, usageAdapter.counts);
        next[key] = (Number(next[key]) || 0) + 1;
        usageAdapter.counts = next;
        return root.stateJson();
    }

    function stateJson() {
        return JSON.stringify({
            "backend": "Usage.qml",
            "path": usageFile.path,
            "counts": usageAdapter.counts || ({
            }),
            "layoutOrder": root.layoutOrder,
            "liveOrder": root.ordered()
        });
    }

    FileView {
        id: usageFile

        // Single writer (this shell), so no watchChanges: a reload on our own
        // write would re-snapshot layoutOrder mid-session.
        path: Quickshell.statePath("nuguland-widget-usage.json")
        onAdapterUpdated: writeAdapter()
        onLoaded: root.snapshot()
        onLoadFailed: root.snapshot()

        JsonAdapter {
            id: usageAdapter

            property var counts: ({
            })
        }

    }

}