Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/features/launcher/Launcher.qml

Raw
import QtQuick
import Quickshell
import Quickshell.Io
import "launch.js" as Launch

Item {
    id: root

    property var lastInteraction: ({})
    // Decaying launch score per desktop id (launch.js); ranks apps inside a match tier.
    property var frecency: ({})

    readonly property var apps: Launch.buildApps(DesktopEntries.applications ? DesktopEntries.applications.values : [])

    function stateJson() {
        return JSON.stringify({
            "lastInteraction": lastInteraction,
            "appCount": apps.length
        });
    }

    function rememberActivation(record, executed) {
        lastInteraction = {
            "kind": "activate",
            "record": record,
            "executed": !!executed
        };
        // Desktop actions carry their app's desktop id, so they count for the app.
        if (executed && record.desktopId) {
            frecency = Launch.recordLaunch(frecency, record.desktopId, Date.now());
            frecencyFile.setText(Launch.serializeFrecency(frecency));
        }
        return stateJson();
    }

    function sliceForRow(row) {
        return row.terminal ? "work.slice" : "app-graphical.slice";
    }

    // The launch record for a row object: what runs, where, and how it is matched.
    function recordFor(row) {
        const title = String(row.title || row.id || "app");
        const id = String(row.id || row.desktopId || title);
        return {
            "id": id,
            "label": title,
            "kind": "app",
            "provider": "app",
            "desktopId": row.desktopId || "",
            "desktopPath": row.desktopPath || "",
            "argv": row.desktopId ? ["uwsm", "app", "-s", row.terminal ? "work.slice" : "a", "--", String(row.desktopId).replace(/\.desktop$/, "") + ".desktop"] : [],
            "workingDirectory": row.workingDirectory || "",
            "environment": {},
            "slice": sliceForRow(row),
            "startupNotify": true,
            "windowMatching": {
                "desktopId": row.desktopId || "",
                "title": title,
                "appId": row.desktopId ? String(row.desktopId).replace(/\.desktop$/, "") : ""
            },
            "osdFeedback": `app:${title}`,
            "route": "desktop-entry",
            "terminal": !!row.terminal
        };
    }

    function execRecord(record) {
        if (!record.argv || record.argv.length === 0)
            return false;

        Quickshell.execDetached(record.argv);
        return true;
    }

    // row: a row object from viewRows().
    function activate(row) {
        const record = recordFor(row);
        return rememberActivation(record, execRecord(record));
    }

    function activateAction(row, index) {
        const action = (row.actions || [])[Number(index) || 0];
        if (!action) {
            lastInteraction = {
                "kind": "activate-missing-action",
                "row": row.id || row.title || "app",
                "index": Number(index) || 0
            };
            return stateJson();
        }

        const record = recordFor(row);
        record.id = `${record.id}:${action.id || action.title}`;
        record.label = `${record.label}: ${action.title || action.id}`;
        if (!action.id || record.argv.length === 0)
            record.argv = [];
        else
            record.argv[record.argv.length - 1] += `:${action.id}`;
        record.desktopPath = "";
        record.osdFeedback = `app-action:${record.label}`;
        return rememberActivation(record, execRecord(record));
    }

    function ranked(raw) {
        return Launch.rankResults(raw, {
            "apps": apps,
            "frecency": frecency,
            "now": Date.now()
        });
    }

    // Ranked result rows for a query; the panel binds it.
    function viewRows(raw) {
        return ranked(raw).results;
    }

    FileView {
        id: frecencyFile

        // Single writer (this shell), like Usage.qml: no watchChanges.
        path: Quickshell.statePath("nuguland-launcher-frecency.json")
        printErrors: false
        onLoaded: root.frecency = Launch.parseFrecency(text())
    }
}