diff options
Diffstat (limited to 'docs/superpowers/plans')
| -rw-r--r-- | docs/superpowers/plans/2026-11-26-default-apps.md | 827 |
1 files changed, 827 insertions, 0 deletions
diff --git a/docs/superpowers/plans/2026-11-26-default-apps.md b/docs/superpowers/plans/2026-11-26-default-apps.md new file mode 100644 index 0000000..f55106a --- /dev/null +++ b/docs/superpowers/plans/2026-11-26-default-apps.md @@ -0,0 +1,827 @@ +# Default Apps Page Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** A settings page, "Default apps", that shows and sets the default application per MIME type in `~/.config/mimeapps.list`. + +**Architecture:** `MimeApps.js` holds every parse and edit as pure functions on the file text, tested under node. `MimeAppsModule.qml` owns the file (`FileView`), the text copy, and one `grep` `Process` that maps desktop ids to the MIME types they declare. `MimeAppsPage.qml` is a view: role rows, explicit rows, an add search, all built from one inline `Assoc` component with an inline app picker. + +**Tech Stack:** Quickshell 0.3.1 QML, QML JS library (`.pragma library`), node for the test, `grep`. + +Spec: `docs/superpowers/specs/2026-11-26-default-apps-design.md`. + +Facts checked while planning (a throwaway `qs` under `timeout`): +- `DesktopEntry.id` carries no `.desktop`: `DesktopEntries.byId("firefox")` finds Firefox, `byId("firefox.desktop")` is null. mimeapps.list values carry the suffix, so lookups strip it. +- `DesktopEntries.applications.values` reads 0 at first and fills a moment later; bindings repaint. +- `` (nf-fa-external_link) is in Inconsolata Nerd Font (`fc-list ':charset=f08e' family`). + +--- + +### Task 1: MimeApps.js and its test + +**Files:** +- Create: `desktop/modules/mimeapps/MimeApps.js` +- Create: `desktop/modules/mimeapps/test_mimeapps.js` +- Modify: `docs/superpowers/specs/2026-11-26-default-apps-design.md` (stale check, see Step 6) + +- [ ] **Step 1: Write the failing test** + +`desktop/modules/mimeapps/test_mimeapps.js`: + +```js +// Copyright (C) 2026 Danilo M. <danix@danix.xyz> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// Run: node desktop/modules/mimeapps/test_mimeapps.js +// MimeApps.js is a QML library, so its .pragma line is stripped before node +// evaluates it. + +const fs = require("fs"); +const assert = require("assert"); + +const src = fs.readFileSync(__dirname + "/MimeApps.js", "utf8").replace(/^\.pragma library$/m, ""); +const M = new Function(src + "\nreturn { ROLES, parse, set, remove, scan, dirs, role, handlers, allMimes };")(); + +// Added Associations is not read as defaults; comments, blank lines and +// other sections survive every edit byte for byte. +const file = "[Added Associations]\ntext/plain=qpdfview.desktop;\n\n" + + "[Default Applications]\n# browser\ntext/html=firefox.desktop\ntext/plain=featherpad.desktop;okular.desktop;\n\n" + + "[Other]\nk=v\n"; +assert.deepStrictEqual(M.parse(file), { "text/html": "firefox.desktop", "text/plain": "featherpad.desktop" }); +assert.deepStrictEqual(M.parse(""), {}); + +// set: in place, or after the section's last entry (before its blank line), +// or a new section at the end, or the section alone in an empty file. +assert.strictEqual(M.set(file, "text/html", "chromium.desktop"), + file.replace("text/html=firefox.desktop", "text/html=chromium.desktop;")); +assert.strictEqual(M.set(file, "image/png", "lximage-qt.desktop"), + file.replace("okular.desktop;\n", "okular.desktop;\nimage/png=lximage-qt.desktop;\n")); +assert.strictEqual(M.set("[Added Associations]\na/b=x.desktop;\n", "a/b", "y.desktop"), + "[Added Associations]\na/b=x.desktop;\n\n[Default Applications]\na/b=y.desktop;\n"); +assert.strictEqual(M.set("", "a/b", "y.desktop"), "[Default Applications]\na/b=y.desktop;\n"); +// A key that only starts with the type is a different key. +assert.strictEqual(M.set("[Default Applications]\ntext/plainx=a.desktop;\n", "text/plain", "b.desktop"), + "[Default Applications]\ntext/plainx=a.desktop;\ntext/plain=b.desktop;\n"); + +// remove: only that line; the header stays, so the text is never empty. +assert.strictEqual(M.remove(file, "text/html"), file.replace("text/html=firefox.desktop\n", "")); +assert.strictEqual(M.remove(file, "nope/x"), file); +assert.strictEqual(M.remove("[Default Applications]\na/b=y.desktop;\n", "a/b"), "[Default Applications]\n"); + +// dirs: the user's first, trailing slashes dropped. +const dirs = M.dirs("/home/you", "", "/usr/local/share/:/usr/share"); +assert.deepStrictEqual(dirs, ["/home/you/.local/share/applications", "/usr/local/share/applications", "/usr/share/applications"]); +assert.deepStrictEqual(M.dirs("/home/you", "/x", ""), ["/x/applications", "/usr/local/share/applications", "/usr/share/applications"]); + +// scan: the first dir wins for one id; a subdirectory joins with "-". +const out = "/home/you/.local/share/applications/firefox.desktop:MimeType=text/html;\n" + + "/usr/share/applications/firefox.desktop:MimeType=text/html;image/png;\n" + + "/usr/share/applications/kde4/okular.desktop:MimeType=application/pdf;\n"; +assert.deepStrictEqual(M.scan(out, dirs), { "firefox.desktop": ["text/html"], "kde4-okular.desktop": ["application/pdf"] }); +assert.deepStrictEqual(M.scan("", dirs), {}); + +// role: the app most types point at, and how many distinct apps. +assert.deepStrictEqual(M.role({ a: "x.desktop", b: "y.desktop", c: "x.desktop" }, ["a", "b", "c", "d"]), { id: "x.desktop", apps: 2 }); +assert.deepStrictEqual(M.role({}, ["a"]), { id: "", apps: 0 }); + +assert.deepStrictEqual(M.handlers({ "f.desktop": ["text/html"], "o.desktop": ["application/pdf"] }, ["text/html", "x/y"]), ["f.desktop"]); +assert.deepStrictEqual(M.allMimes({ f: ["b", "a"], g: ["a"] }), ["a", "b"]); + +for (const r of M.ROLES) assert.ok(r.label && r.mimes.length > 0, r.label); + +console.log("ok"); +``` + +- [ ] **Step 2: Run it to verify it fails** + +Run: `node desktop/modules/mimeapps/test_mimeapps.js` +Expected: FAIL, `ENOENT: no such file or directory` for `MimeApps.js`. + +- [ ] **Step 3: Write MimeApps.js** + +`desktop/modules/mimeapps/MimeApps.js`: + +```js +.pragma library +// Copyright (C) 2026 Danilo M. <danix@danix.xyz> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// Pure helpers for the default apps page, tested by test_mimeapps.js under +// node. Every edit works on the text of mimeapps.list and touches only the +// one line it means: other sections ([Added Associations] included), +// comments and blank lines survive byte for byte. The file is walked line by +// line, never cut "up to the next [" (AGENTS.md, the INI note). + +const SECTION = "[Default Applications]"; + +// The role rows, each setting its whole group of types at once. +const ROLES = [ + { label: "Browser", mimes: ["text/html", "application/xhtml+xml", "x-scheme-handler/http", "x-scheme-handler/https"] }, + { label: "Mail", mimes: ["x-scheme-handler/mailto"] }, + { label: "Text", mimes: ["text/plain"] }, + { label: "PDF", mimes: ["application/pdf"] }, + { label: "Image", mimes: ["image/png", "image/jpeg", "image/gif", "image/webp", "image/svg+xml"] }, + { label: "Video", mimes: ["video/mp4", "video/x-matroska", "video/webm"] }, + { label: "Audio", mimes: ["audio/mpeg", "audio/flac", "audio/ogg", "audio/x-wav"] }, + { label: "Files", mimes: ["inode/directory"] }, + { label: "Archive", mimes: ["application/zip", "application/x-tar", "application/x-7z-compressed", "application/gzip"] }, +]; + +// The trailing newline is dropped on split and always put back on join. +function lines(text) { return text === "" ? [] : text.replace(/\n$/, "").split("\n"); } +function join(ls) { return ls.join("\n") + "\n"; } + +// The key of a "key=value" line, null for a comment, header or blank. +function keyOf(line) { + const m = /^\s*([^#=\s[][^=]*?)\s*=/.exec(line); + return m ? m[1] : null; +} + +// [header index, end index (exclusive)] of the section, or null. +function section(ls) { + const start = ls.findIndex(l => l.trim() === SECTION); + if (start < 0) return null; + let end = start + 1; + while (end < ls.length && !/^\s*\[/.test(ls[end])) end++; + return [start, end]; +} + +// { mime: desktopId } from [Default Applications]. A value is a ;-list and +// its first item is the default. +function parse(text) { + const ls = lines(text), s = section(ls), out = {}; + if (!s) return out; + for (let i = s[0] + 1; i < s[1]; i++) { + const k = keyOf(ls[i]); + if (k === null) continue; + const id = ls[i].slice(ls[i].indexOf("=") + 1).split(";").map(x => x.trim()).find(x => x); + if (id) out[k] = id; + } + return out; +} + +function set(text, mime, id) { + const ls = lines(text); + let s = section(ls); + if (!s) { + if (ls.length > 0 && ls[ls.length - 1].trim() !== "") ls.push(""); + ls.push(SECTION); + s = [ls.length - 1, ls.length]; + } + const line = mime + "=" + id + ";"; + for (let i = s[0] + 1; i < s[1]; i++) + if (keyOf(ls[i]) === mime) { ls[i] = line; return join(ls); } + // After the last non-blank line, so a blank line before the next header stays. + let at = s[1]; + while (at > s[0] + 1 && ls[at - 1].trim() === "") at--; + ls.splice(at, 0, line); + return join(ls); +} + +// The header stays even when its last entry goes, so the text written is +// never empty (FileView.setText("") writes nothing, AGENTS.md). +function remove(text, mime) { + const ls = lines(text), s = section(ls); + if (!s) return text; + for (let i = s[0] + 1; i < s[1]; i++) + if (keyOf(ls[i]) === mime) { ls.splice(i, 1); return join(ls); } + return text; +} + +// The applications directories in XDG order, the user's first. +function dirs(home, dataHome, dataDirs) { + return [dataHome || home + "/.local/share"] + .concat((dataDirs || "/usr/local/share:/usr/share").split(":")) + .filter(d => d) + .map(d => d.replace(/\/+$/, "") + "/applications"); +} + +// grep -rH '^MimeType=' output -> { desktopId: [mimes] }. The id is the path +// under applications/ with "/" as "-" (desktop entry spec); the first +// directory to list an id wins, which is the user's when it has one. +function scan(out, appDirs) { + const apps = {}; + for (const line of out.split("\n")) { + const i = line.indexOf(":MimeType="); + if (i < 0) continue; + const path = line.slice(0, i); + const dir = appDirs.find(d => path.startsWith(d + "/")); + if (!dir) continue; + const id = path.slice(dir.length + 1).replace(/\//g, "-"); + if (id in apps) continue; + apps[id] = line.slice(i + ":MimeType=".length).split(";").filter(m => m); + } + return apps; +} + +// The app most of a role's types point at ("" when none is set), and how +// many distinct apps they point at. +function role(defaults, mimes) { + const n = {}; + for (const m of mimes) if (defaults[m]) n[defaults[m]] = (n[defaults[m]] || 0) + 1; + const ids = Object.keys(n).sort((a, b) => n[b] - n[a]); + return { id: ids[0] || "", apps: ids.length }; +} + +// The ids declaring at least one of the types. +function handlers(apps, mimes) { + return Object.keys(apps).filter(id => apps[id].some(m => mimes.includes(m))); +} + +// Every type any installed app declares, sorted. +function allMimes(apps) { + const seen = {}; + for (const id in apps) for (const m of apps[id]) seen[m] = true; + return Object.keys(seen).sort(); +} +``` + +- [ ] **Step 4: Run the test to verify it passes** + +Run: `node desktop/modules/mimeapps/test_mimeapps.js` +Expected: `ok` + +- [ ] **Step 5: Run it against the live file, read-only** + +Run: `node -e 'const fs=require("fs");const s=fs.readFileSync("desktop/modules/mimeapps/MimeApps.js","utf8").replace(/^\.pragma library$/m,"");const M=new Function(s+"\nreturn {parse,set,remove};")();const t=fs.readFileSync(process.env.HOME+"/.config/mimeapps.list","utf8");console.log(Object.keys(M.parse(t)).length, M.remove(M.set(t,"x/y","z.desktop"),"x/y")===t)'` +Expected: `17 true` (a count of 17 at planning time; the `true` is what matters: a set then a remove gives the file back exactly). If it prints `false`, the live file does not end with a newline, which is expected and harmless; check with `tail -c1 ~/.config/mimeapps.list | xxd`. + +- [ ] **Step 6: Align the spec's stale check** + +The plan checks for an app that is not installed with `DesktopEntries.byId`, not with the grep scan: an installed app whose `.desktop` declares no `MimeType=` line is missing from the scan and would read as not installed. In the spec's "Explicit section", replace: + +``` +A row whose app is not installed (it has no `.desktop` file in the scan) shows +``` + +with: + +``` +A row whose app is not installed (`DesktopEntries.byId` finds no entry for it) shows +``` + +- [ ] **Step 7: Commit** + +```bash +git add desktop/modules/mimeapps/MimeApps.js desktop/modules/mimeapps/test_mimeapps.js docs/superpowers/specs/2026-11-26-default-apps-design.md +git commit -m "feat(mimeapps): parse and edit mimeapps.list + +Pure functions over the file text for the default apps page: read +[Default Applications], set or remove one type, and map the .desktop +files' MimeType= lines to desktop ids. Every edit touches one line and +leaves [Added Associations], comments and blank lines as they were; a +remove keeps the section header so the text written is never empty. + +The spec's stale-app check moves to DesktopEntries.byId: an installed +app without a MimeType= line is absent from the scan. + +Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>" +``` + +--- + +### Task 2: Module, page, registration, docs + +**Files:** +- Create: `desktop/modules/mimeapps/MimeAppsModule.qml` +- Create: `desktop/modules/mimeapps/MimeAppsPage.qml` +- Create: `desktop/modules/mimeapps/README.md` +- Modify: `desktop/shell.qml` (an import with the other `modules/*` imports; `MimeAppsModule {}` after `NotificationsModule {},`) +- Modify: `AGENTS.md` (the `desktop/` line in "What this is") + +- [ ] **Step 1: Write MimeAppsModule.qml** + +```qml +// Copyright (C) 2026 Danilo M. <danix@danix.xyz> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +import Quickshell +import Quickshell.Io +import QtQuick +import "../.." +import "MimeApps.js" as M + +// The default application per file type, from the user's mimeapps.list, +// the file xdg-mime and xdg-open read. The page owns the file rather than +// running xdg-mime, which can set an entry but not remove one. +Module { + id: mm + name: "mimeapps" + label: "Default apps" + // nf-fa-external_link, present in Inconsolata Nerd Font. + icon: "" + settings: true + group: "Desktop" + keywords: ["xdg", "xdg-mime", "xdg-open", "mime", "mimeapps", "open with", "file type", + "association", "browser", "mail", "pdf", "image", "video", "audio"] + + readonly property string home: Quickshell.env("HOME") + readonly property string file: (Quickshell.env("XDG_CONFIG_HOME") || mm.home + "/.config") + "/mimeapps.list" + readonly property var dirs: M.dirs(mm.home, Quickshell.env("XDG_DATA_HOME"), Quickshell.env("XDG_DATA_DIRS")) + + // The file's text as last read or written: FileView.text() lags + // setText() (AGENTS.md), so edits build on this copy. + property string text: "" + // No write before the first read, which would replace the file with + // only the edit. + property bool loaded: false + property string error: "" + readonly property var defaults: M.parse(mm.text) + // { desktopId: [mimes] }, from the .desktop files. + property var apps: ({}) + + searchItems: M.ROLES.map(r => ({ text: r.label, key: r.label })) + .concat(Object.keys(mm.defaults).sort().map(m => ({ text: m, key: m }))) + + function refresh() { + fv.reload(); + scanner.running = false; + scanner.running = true; + } + function write(t) { + if (!mm.loaded) { mm.error = "mimeapps.list not read yet."; return; } + mm.error = ""; + mm.text = t; + fv.setText(t); + } + + property FileView fv: FileView { + path: mm.file + printErrors: false + watchChanges: true + onFileChanged: reload() + onLoaded: { mm.text = text(); mm.loaded = true; } + // A missing file is an empty one; the first write creates it. + onLoadFailed: err => { + if (err === FileViewError.FileNotFound) { mm.text = ""; mm.loaded = true; } + else mm.error = "Cannot read " + path + ": " + FileViewError.toString(err); + } + onSaveFailed: err => mm.error = "Cannot write " + path + ": " + FileViewError.toString(err) + } + + property Process scanner: Process { + command: ["sh", "-c", 'for d; do [ -d "$d" ] && grep -rH --include="*.desktop" "^MimeType=" "$d"; done; true', "sh"] + .concat(mm.dirs) + stdout: StdioCollector { onStreamFinished: mm.apps = M.scan(text, mm.dirs) } + } + + page: Component { + Page { + title: "Default apps" + MimeAppsPage { width: parent.width; ma: mm } + } + } +} +``` + +- [ ] **Step 2: Write MimeAppsPage.qml** + +```qml +// Copyright (C) 2026 Danilo M. <danix@danix.xyz> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +import Quickshell +import QtQuick +import "../.." +import "MimeApps.js" as M + +// Roles first (one row sets a group of types), then every explicit entry in +// [Default Applications], then a search to add any type an installed app +// declares. Each row opens an app picker below itself; one picker at a time. +Column { + id: page + + spacing: 6 + + // The module, under a name of its own: a property named after the + // module's id binds to itself (AGENTS.md). + required property var ma + // The row whose picker is open, by key; "" when none. + property string editing: "" + property string query: "" + property bool all: false + property string addQuery: "" + + readonly property var defaults: page.ma.defaults + readonly property var explicit: Object.keys(page.defaults).sort() + readonly property bool entriesReady: DesktopEntries.applications.values.length > 0 + // Types matching the add search that are not set yet. + readonly property var addHits: { + const q = page.addQuery.trim().toLowerCase(); + if (q === "") return []; + return M.allMimes(page.ma.apps).filter(m => m.includes(q) && !(m in page.defaults)).slice(0, 8); + } + + // mimeapps.list ids carry ".desktop", DesktopEntry ids do not. + function entry(id) { return id ? DesktopEntries.byId(id.replace(/\.desktop$/, "")) : null; } + function name(id) { const e = page.entry(id); return e ? e.name : id; } + + // The picker's apps: those declaring one of the types, or every + // visible app with "show all apps" on; filtered by the search. + // ponytail: first 10 matches; a narrower search finds the rest. + function candidates(mimes) { + const q = page.query.trim().toLowerCase(); + const ids = page.all + ? DesktopEntries.applications.values.filter(a => !a.noDisplay).map(a => a.id + ".desktop") + : M.handlers(page.ma.apps, mimes); + return ids.map(id => { + const e = page.entry(id); + return { id: id, name: e ? e.name : id, icon: e ? e.icon : "" }; + }) + .filter(a => q === "" || a.name.toLowerCase().includes(q) || a.id.toLowerCase().includes(q)) + .sort((a, b) => a.name.localeCompare(b.name)) + .slice(0, 10); + } + function pick(mimes, id) { + page.ma.write(mimes.reduce((t, m) => M.set(t, m, id), page.ma.text)); + page.editing = ""; + page.query = ""; + page.all = false; + page.addQuery = ""; + } + function toggle(key) { + page.editing = page.editing === key ? "" : key; + page.query = ""; + page.all = false; + } + + component Small: Text { + color: Theme.subtext + font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 4 } + } + + component Heading: Text { + topPadding: 14 + font { family: Theme.fontFamily; pixelSize: Theme.fontSize; bold: true } + color: Theme.text + } + + component Field: Rectangle { + id: field + property string text: "" + property string placeholder: "" + signal edited(string text) + height: 34 + radius: 8 + color: Theme.surface + TextInput { + activeFocusOnTab: true + anchors { fill: parent; leftMargin: 10; rightMargin: 10 } + verticalAlignment: TextInput.AlignVCenter + clip: true + selectByMouse: true + color: Theme.text + font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 } + text: field.text + onTextEdited: field.edited(text) + Text { + anchors.verticalCenter: parent.verticalCenter + visible: parent.text === "" + text: field.placeholder + color: Theme.subtext + font: parent.font + } + } + } + + // One row: the app's icon, a title, what it maps to, Change (and + // Remove for an explicit entry), and its picker when open. Items with + // anchors, not Rows: a Row sizes to its children (AGENTS.md). + component Assoc: Column { + id: assoc + // The picker key and what a search hit scrolls to. + property string key: "" + property string title: "" + property var mimes: [] + property string appId: "" + property string status: "" + property bool stale: false + property bool removable: false + readonly property bool open: page.editing === assoc.key + objectName: assoc.key + width: page.width + spacing: 4 + + Item { + width: parent.width + height: 44 + + Image { + id: appIcon + anchors { left: parent.left; verticalCenter: parent.verticalCenter } + width: 24; height: 24 + sourceSize: Qt.size(24, 24) + source: { const e = page.entry(assoc.appId); return e ? Quickshell.iconPath(e.icon, true) : ""; } + } + Text { + id: titleText + anchors { left: appIcon.right; leftMargin: 10; right: change.left; rightMargin: 8; top: parent.top; topMargin: 4 } + elide: Text.ElideRight + text: assoc.title + color: Theme.text + font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 3 } + } + Small { + anchors { left: titleText.left; right: titleText.right; top: titleText.bottom; topMargin: 2 } + elide: Text.ElideRight + color: assoc.stale ? Theme.red : Theme.subtext + text: assoc.status + } + Button { + id: change + anchors { right: del.visible ? del.left : parent.right; rightMargin: del.visible ? 6 : 0; verticalCenter: parent.verticalCenter } + text: assoc.open ? "Close" : "Change" + onClicked: page.toggle(assoc.key) + } + Button { + id: del + visible: assoc.removable + anchors { right: parent.right; verticalCenter: parent.verticalCenter } + text: "Remove" + danger: true + onClicked: page.ma.write(M.remove(page.ma.text, assoc.mimes[0])) + } + } + + Column { + visible: assoc.open + width: parent.width + spacing: 4 + + Field { + width: parent.width + placeholder: "search apps" + text: page.query + onEdited: t => page.query = t + } + Item { + width: parent.width + height: 32 + Switch { + id: allSwitch + anchors { left: parent.left; verticalCenter: parent.verticalCenter } + checked: page.all + onToggled: on => page.all = on + } + Small { + anchors { left: allSwitch.right; leftMargin: 10; verticalCenter: parent.verticalCenter } + text: "Show all apps, not only those declaring the type" + } + } + Repeater { + id: picks + model: assoc.open ? page.candidates(assoc.mimes) : [] + + delegate: Rectangle { + id: app + required property var modelData + width: page.width + height: 32 + radius: 8 + color: app.modelData.id === assoc.appId ? Qt.alpha(Theme.accent, 0.22) + : appArea.containsMouse ? Qt.alpha(Theme.text, 0.06) : "transparent" + + Image { + id: pickIcon + anchors { left: parent.left; leftMargin: 8; verticalCenter: parent.verticalCenter } + width: 20; height: 20 + sourceSize: Qt.size(20, 20) + source: app.modelData.icon ? Quickshell.iconPath(app.modelData.icon, true) : "" + } + Text { + anchors { left: pickIcon.right; leftMargin: 10; right: parent.right; rightMargin: 8; verticalCenter: parent.verticalCenter } + elide: Text.ElideRight + text: app.modelData.name + " " + app.modelData.id + color: Theme.text + font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 3 } + } + MouseArea { + id: appArea + anchors.fill: parent + hoverEnabled: true + cursorShape: Qt.PointingHandCursor + onClicked: page.pick(assoc.mimes, app.modelData.id) + } + } + } + Small { + visible: picks.count === 0 + width: parent.width + wrapMode: Text.Wrap + text: page.all ? "No app matches." : "No installed app declares this type. Turn on Show all apps." + } + } + } + + Small { + width: parent.width + wrapMode: Text.Wrap + text: "What xdg-open uses, from " + page.ma.file + ". Applies at once." + } + + Text { + visible: page.ma.error !== "" + width: parent.width + wrapMode: Text.Wrap + text: page.ma.error + color: Theme.red + font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 4 } + } + + Heading { text: "Roles" } + + Repeater { + model: M.ROLES + delegate: Assoc { + required property var modelData + readonly property var st: M.role(page.defaults, modelData.mimes) + key: modelData.label + title: modelData.label + mimes: modelData.mimes + appId: st.id + status: st.apps === 0 ? "not set" + : page.name(st.id) + (st.apps > 1 ? " · mixed (" + st.apps + " apps)" : "") + } + } + + Heading { text: "Explicit" } + + Repeater { + model: page.explicit + delegate: Assoc { + required property string modelData + key: modelData + title: modelData + mimes: [modelData] + appId: page.defaults[modelData] + // Only once DesktopEntries has filled, or every row flashes red. + stale: page.entriesReady && page.entry(appId) === null + status: stale ? "not installed: " + appId : page.name(appId) + removable: true + } + } + + Heading { text: "Add" } + + Field { + width: parent.width + placeholder: "search file types, e.g. image/avif" + text: page.addQuery + onEdited: t => page.addQuery = t + } + + // A type is written only once an app is picked; then it moves to Explicit. + Repeater { + model: page.addHits + delegate: Assoc { + required property string modelData + key: "+" + modelData + title: modelData + mimes: [modelData] + status: "not set" + } + } +} +``` + +- [ ] **Step 3: Register the module in shell.qml** + +In `desktop/shell.qml`, add with the other module imports (keep them as they are ordered, after `import "modules/mail"`): + +```qml +import "modules/mimeapps" +``` + +and after ` NotificationsModule {},`: + +```qml + MimeAppsModule {}, +``` + +- [ ] **Step 4: Write README.md** + +`desktop/modules/mimeapps/README.md`: + +```markdown +# mimeapps + +The settings window's **Default apps** page: the default application per +file type, from `$XDG_CONFIG_HOME/mimeapps.list` (the file `xdg-mime` and +`xdg-open` read). + +- **Roles**: Browser, Mail, Text, PDF, Image, Video, Audio, Files, Archive. + Each sets its whole group of types (`ROLES` in `MimeApps.js`) at once and + shows the app most of them point at, or how many apps when they disagree. +- **Explicit**: every `[Default Applications]` entry, with Change and + Remove. An entry whose app is not installed shows its id in red. +- **Add**: search any type an installed app declares; it is written once an + app is picked. + +The picker lists the apps whose `.desktop` declares the type (one `grep` over +the XDG applications directories, since Quickshell's `DesktopEntry` has no +`mimeTypes`), or every app with "Show all apps". + +Only `[Default Applications]` is edited; `[Added Associations]`, comments and +other sections are left as they are. Not handled: system-wide and +desktop-specific `mimeapps.list` files. + +The file is written directly rather than through `xdg-mime default`, which +cannot remove an entry. `DesktopEntry.id` has no `.desktop` suffix while +mimeapps.list ids do, so lookups strip it. + +Test: `node desktop/modules/mimeapps/test_mimeapps.js`. +``` + +- [ ] **Step 5: Update AGENTS.md** + +In "What this is", the `desktop/` settings list currently reads `... the assistant's daemon and presets, qtmaildir, git,`. Change it to: + +``` + the assistant's daemon and presets, qtmaildir, git, default apps, +``` + +- [ ] **Step 6: Lint-free load check** + +The new directory import and the `shell.qml` edit need a restart (AGENTS.md, Reloading), and `reload.sh` must not run from an agent. Ask the user to run `./reload.sh desktop` in their terminal, then: + +Run: `qs log -p desktop --log-times | grep -iE 'mimeapps|MimeApps|error|ReferenceError|TypeError' | tail -20` +Expected: no line naming `mimeapps`, `MimeApps.js`, `MimeAppsModule` or `MimeAppsPage`. + +- [ ] **Step 7: Commit** + +```bash +git add desktop/modules/mimeapps/MimeAppsModule.qml desktop/modules/mimeapps/MimeAppsPage.qml desktop/modules/mimeapps/README.md desktop/shell.qml AGENTS.md +git commit -m "feat(mimeapps): a Default apps settings page + +Shows and sets the default application per file type in mimeapps.list: +role rows that set a group of types at once, every explicit entry with +Change and Remove, and a search to add any type an installed app +declares. The picker offers the apps whose .desktop declares the type, +from one grep over the XDG applications dirs, since DesktopEntry has no +mimeTypes; or every app on request. + +No write before the first read, so a click during load cannot replace +the file with one line. Stale entries, whose app DesktopEntries does +not know, show their id in red. + +Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>" +``` + +--- + +### Task 3: Verify with the user + +**Files:** none. + +- [ ] **Step 1: Snapshot the file** + +Run: `cp ~/.config/mimeapps.list /tmp/claude-1000/-home-danix-Programming-GIT-quickshell/b2dd3c11-ed07-4693-8526-1037f25d9453/scratchpad/mimeapps.before` + +- [ ] **Step 2: Ask the user to check by eye** + +Open settings, Default apps. Check: the role rows show icons and names (Browser: Firefox, Text: FeatherPad, Files: PCManFM-Qt at planning time); Explicit lists the file's entries; the search "default apps" in the settings sidebar finds the page; the Change picker opens under its row only. + +- [ ] **Step 3: Ask the user to set one role and remove one entry** + +Suggest: Video to its current app (a no-op in effect, which exercises the write of all three video types), then Remove one explicit entry they do not care about. + +- [ ] **Step 4: Check what was written** + +Run: `diff /tmp/claude-1000/-home-danix-Programming-GIT-quickshell/b2dd3c11-ed07-4693-8526-1037f25d9453/scratchpad/mimeapps.before ~/.config/mimeapps.list; xdg-mime query default video/webm` +Expected: the diff shows only `[Default Applications]` lines (the video types set or rewritten with a trailing `;`, the removed line gone); `[Added Associations]` untouched. `xdg-mime` prints the app picked for Video. + +- [ ] **Step 5: Restore if the user wants the removed entry back** + +Run: `cp /tmp/claude-1000/-home-danix-Programming-GIT-quickshell/b2dd3c11-ed07-4693-8526-1037f25d9453/scratchpad/mimeapps.before ~/.config/mimeapps.list` only if they ask. |
