import Quickshell import Quickshell.Io import QtQuick // Non-visual app-search model for the launcher. // Set `search`; read `apps` (a ranked, filtered list of DesktopEntry). QtObject { id: root property string search: "" // "usage" (most launched first) | "name". Persisted with the counts. readonly property string sortMode: store.sortMode function toggleSort() { store.sortMode = store.sortMode === "usage" ? "name" : "usage"; } function uses(a) { return store.counts[a.id] ?? 0; } // `keywords`/`categories` come through as string lists, so coerce every // field to a string before matching (String([]) joins with commas). function haystack(a) { return (String(a.name || "") + " " + String(a.genericName || "") + " " + String(a.comment || "") + " " + String(a.keywords || "")).toLowerCase(); } function byOrder(x, y) { if (root.sortMode === "usage") { const d = root.uses(y) - root.uses(x); if (d !== 0) return d; } return String(x.name).localeCompare(String(y.name)); } readonly property var apps: { const all = DesktopEntries.applications.values.filter(a => !a.noDisplay); const q = root.search.trim().toLowerCase(); if (q.length === 0) return all.slice().sort(root.byOrder); const matches = all.filter(a => root.haystack(a).includes(q)); // Prefix matches on the visible name rank first, then the sort mode. return matches.slice().sort((x, y) => { const xs = String(x.name).toLowerCase().startsWith(q) ? 0 : 1; const ys = String(y.name).toLowerCase().startsWith(q) ? 0 : 1; if (xs !== ys) return xs - ys; return root.byOrder(x, y); }); } function launch(index) { const list = root.apps; if (index >= 0 && index < list.length) { const app = list[index]; app.execute(); // Reassigned, not mutated: an in-place write notifies nothing. const counts = Object.assign({}, store.counts); counts[app.id] = (counts[app.id] ?? 0) + 1; store.counts = counts; return true; } return false; } readonly property var file: FileView { path: Quickshell.statePath("launcher.json") onAdapterUpdated: writeAdapter() onLoadFailed: error => { if (error === FileViewError.FileNotFound) writeAdapter(); } JsonAdapter { id: store property string sortMode: "usage" property var counts: ({}) } } }