repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

quickshell/nuguland/Launcher.qml

Raw
import QtQuick

Item {
    id: root

    property QtObject niriService
    property QtObject clipboardService
    property var wheelTrace: []
    property var lastInteraction: ({
    })
    readonly property var actions: [{
        "id": "lock",
        "title": "Lock",
        "subtitle": "Lock this session",
        "route": "service"
    }, {
        "id": "overview",
        "title": "Overview",
        "subtitle": "Open niri overview",
        "route": "service"
    }, {
        "id": "screenshot",
        "title": "Screenshot",
        "subtitle": "Capture with native niri screenshot",
        "route": "service"
    }, {
        "id": "color",
        "title": "Pick color",
        "subtitle": "Use native niri color picker",
        "route": "service"
    }, {
        "id": "diagnostics",
        "title": "Diagnostics",
        "subtitle": "Open Nuguland diagnostics",
        "route": "service"
    }]
    readonly property var apps: [{
        "id": "firefox",
        "title": "Firefox",
        "subtitle": "Browser",
        "desktopId": "firefox.desktop"
    }, {
        "id": "terminal",
        "title": "Terminal",
        "subtitle": "Shell",
        "desktopId": "org.wezfurlong.wezterm.desktop"
    }, {
        "id": "files",
        "title": "Files",
        "subtitle": "File manager",
        "desktopId": "org.gnome.Nautilus.desktop"
    }]

    function normalize(text) {
        return String(text || "").trim().toLowerCase();
    }

    function prefixIntent(raw) {
        const text = String(raw || "").trim();
        if (text.startsWith("="))
            return {
            "provider": "calculator",
            "query": text.slice(1).trim()
        };

        if (text.startsWith(">"))
            return {
            "provider": "action",
            "query": text.slice(1).trim()
        };

        if (text.toLowerCase().startsWith("clip "))
            return {
            "provider": "clipboard",
            "query": text.slice(5).trim()
        };

        return {
            "provider": "all",
            "query": text
        };
    }

    function fuzzyScore(query, title, providerBoost) {
        const q = normalize(query);
        const t = normalize(title);
        if (q.length === 0)
            return providerBoost;

        if (t === q)
            return providerBoost + 1000;

        if (t.startsWith(q))
            return providerBoost + 800 - t.length;

        if (t.indexOf(q) >= 0)
            return providerBoost + 500 - t.indexOf(q);

        return -1;
    }

    function calculatorValue(expression) {
        const expr = String(expression || "").replace(/\s+/g, "");
        if (!/^[0-9.+\-*/()%]+$/.test(expr) || expr.length === 0)
            return "";

        try {
            const value = Function(`"use strict"; return (${expr})`)();
            if (typeof value !== "number" || !Number.isFinite(value))
                return "";

            return Number.isInteger(value) ? String(value) : String(Math.round(value * 10000) / 10000);
        } catch (error) {
            return "";
        }
    }

    function rowVerb(provider, row) {
        if (row.verb)
            return String(row.verb);

        if (provider === "calculator")
            return "copy-result";

        if (provider === "action")
            return "run-action";

        if (provider === "app")
            return "launch-app";

        if (provider === "clipboard")
            return "copy-clipboard";

        if (provider === "window")
            return "focus-window";

        return "activate";
    }

    function rowIconRole(provider, row) {
        if (row.iconRole)
            return String(row.iconRole);

        if (provider === "calculator")
            return "calculator";

        if (provider === "action")
            return "shell-action";

        if (provider === "app")
            return "desktop-entry";

        if (provider === "clipboard")
            return "clipboard";

        if (provider === "window")
            return "niri-window";

        return "item";
    }

    function previewActionRows(row) {
        const provider = String(row.provider || "item");
        const primaryLabel = provider === "calculator" ? "Copy result" : (provider === "app" ? "Launch" : (provider === "window" ? "Focus" : "Run"));
        return [{
            "id": "primary",
            "label": primaryLabel,
            "iconRole": row.iconRole || provider,
            "verb": row.verb || "activate",
            "primaryAction": true,
            "keyboardReturnActivates": true
        }, {
            "id": "copy-title",
            "label": "Copy title",
            "iconRole": "clipboard",
            "verb": "copy-title",
            "primaryAction": false,
            "keyboardReturnActivates": false
        }, {
            "id": "copy-subtitle",
            "label": "Copy detail",
            "iconRole": "clipboard",
            "verb": "copy-subtitle",
            "primaryAction": false,
            "keyboardReturnActivates": false
        }];
    }

    function recordWheelSelection(before, after, deltaY) {
        const entry = {
            "kind": "wheel-select",
            "before": before,
            "after": after,
            "deltaY": deltaY
        };
        const rows = wheelTrace.slice(Math.max(0, wheelTrace.length - 15));
        rows.push(entry);
        wheelTrace = rows;
        lastInteraction = entry;
        return stateJson();
    }

    function stateJson() {
        return JSON.stringify({
            "backend": "Launcher.qml",
            "wheelTrace": wheelTrace,
            "lastInteraction": lastInteraction,
            "realWheelSelection": lastInteraction.kind === "wheel-select" && lastInteraction.before !== lastInteraction.after,
            "launcherPanelContract": launcherPanelContract()
        });
    }

    function launcherPanelContract() {
        return {
            "source": "Nuguland launcher Content.qml AppList.qml ActionItem.qml plus Nuguland SearchResults.qml ResultPreview plus Nuguland LaunchContent.qml",
            "ownedBy": "PopoutHost.qml inline LauncherPanel",
            "fullLauncherSurface": true,
            "searchFieldDockedBottom": true,
            "singleResultList": true,
            "sidePreviewPanel": true,
            "keyboardNavigation": true,
            "providerChips": true,
            "serviceFedOnly": true,
            "noSettingsCenter": true,
            "resultParts": ["iconRole", "title", "subtitle", "verb", "tags"],
            "previewActions": ["primary", "copy-title", "copy-subtitle"]
        };
    }

    function launcherPreviewPanel(row) {
        if (!row)
            return {
            "source": "Nuguland SearchResults.qml ResultPreview plus Nuguland launcher Content.qml",
            "emptyState": true,
            "sidePanelWidth": 386,
            "actionRows": [],
            "keyboardReturnActivates": "primaryAction"
        };

        return {
            "source": "Nuguland SearchResults.qml ResultPreview plus Nuguland launcher Content.qml",
            "emptyState": false,
            "provider": row.provider,
            "title": row.title || row.id || row.provider,
            "subtitle": row.subtitle || row.provider,
            "iconRole": row.iconRole,
            "verb": row.verb,
            "preview": row.preview,
            "sidePanelWidth": 386,
            "selectedResultDrivesPreview": true,
            "primaryAction": row.verb,
            "keyboardReturnActivates": "primaryAction",
            "settingsBacked": false,
            "actionRows": previewActionRows(row)
        };
    }

    function shapeRow(row, provider, score) {
        const next = Object.assign({
        }, row);
        const route = next.route || provider;
        next.provider = provider;
        next.iconRole = rowIconRole(provider, next);
        next.verb = rowVerb(provider, next);
        next.subtitle = next.subtitle || provider;
        next.tags = next.tags || [provider, route, next.verb];
        next.preview = next.preview || {
            "provider": provider,
            "title": next.title || next.id || provider,
            "subtitle": next.subtitle,
            "route": route
        };
        next.score = score;
        next.keyboard = true;
        return next;
    }

    function pushResult(results, row, provider, score) {
        if (score < 0)
            return ;

        results.push(shapeRow(row, provider, score));
    }

    function scrollableListChrome(rowCount, selectedIndex) {
        const count = Math.max(0, Number(rowCount));
        const selected = Math.max(0, Number(selectedIndex));
        const scrollable = count > 4;
        return {
            "source": "Nuguland ScrollEdgeFade.qml plus Nuguland FlickMonitor.qml",
            "scrollEdgeFade": true,
            "topEdgeFade": scrollable && selected > 0,
            "bottomEdgeFade": scrollable && selected < count - 1,
            "fadeSize": 32,
            "scrollHint": {
                "visible": scrollable,
                "icon": "keyboard_arrow_up_down",
                "side": "right",
                "tooltip": "scroll results"
            },
            "flickMonitor": {
                "enabled": true,
                "velocitySamples": 6,
                "events": ["pressed", "positionChanged", "released"]
            }
        };
    }

    function clipboardProviderRows(query) {
        if (!clipboardService)
            return [];

        try {
            return JSON.parse(clipboardService.launcherRows(query));
        } catch (error) {
            return [];
        }
    }

    function windowRows(query, boost, results) {
        const windows = niriService ? (niriService.windows || []) : [];
        for (let i = 0; i < windows.length; i++) {
            const win = windows[i];
            const title = win.title || win.app_id || win.appId || `window ${win.id}`;
            const score = fuzzyScore(query, title, boost);
            pushResult(results, {
                "id": String(win.id),
                "title": title,
                "subtitle": win.app_id || win.appId || "window",
                "route": "niri-window",
                "windowId": win.id
            }, "window", score);
        }
    }

    function search(raw) {
        const intent = prefixIntent(raw);
        const query = intent.query;
        const results = [];
        const calc = calculatorValue(query);
        if ((intent.provider === "calculator" || intent.provider === "all") && calc.length > 0)
            pushResult(results, {
            "id": `calc:${query}`,
            "title": calc,
            "subtitle": query,
            "value": calc,
            "route": "calculator"
        }, "calculator", intent.provider === "calculator" ? 2000 : 600);

        if (intent.provider === "action" || intent.provider === "all") {
            for (let i = 0; i < actions.length; i++) pushResult(results, actions[i], "action", fuzzyScore(query, actions[i].title, intent.provider === "action" ? 1500 : 300))
        }
        if (intent.provider === "app" || intent.provider === "all") {
            for (let i = 0; i < apps.length; i++) pushResult(results, Object.assign({
                "route": "desktop-entry"
            }, apps[i]), "app", fuzzyScore(query, apps[i].title, 900))
        }
        if (intent.provider === "clipboard" || intent.provider === "all") {
            const clipRows = clipboardProviderRows(query);
            for (let i = 0; i < clipRows.length; i++) pushResult(results, Object.assign({
                "route": "clipboard",
                "backend": "Clipboard.qml"
            }, clipRows[i]), "clipboard", fuzzyScore(query, clipRows[i].title, intent.provider === "clipboard" ? 1400 : 200))
        }
        windowRows(query, intent.provider === "window" ? 1300 : 250, results);
        results.sort((a, b) => {
            return b.score - a.score || String(a.title).localeCompare(String(b.title));
        });
        const selectedIndex = results.length > 0 ? 0 : -1;
        const selectedResult = selectedIndex >= 0 ? results[selectedIndex] : null;
        return JSON.stringify({
            "query": raw,
            "providerIntent": intent.provider,
            "selectedIndex": selectedIndex,
            "singleScoredList": true,
            "keyboardFirst": true,
            "noPluginCommands": true,
            "scrollableListChrome": scrollableListChrome(results.length, selectedIndex),
            "launcherPanelContract": launcherPanelContract(),
            "launcherPreviewPanel": launcherPreviewPanel(selectedResult),
            "results": results
        });
    }

}