From f19453f25a04b0760ff1303388d136a8b60c2c04 Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Thu, 26 Nov 2026 11:10:41 +0100 Subject: 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 Sonnet 5.5 --- desktop/modules/mimeapps/MimeAppsPage.qml | 313 ++++++++++++++++++++++++++++++ 1 file changed, 313 insertions(+) create mode 100644 desktop/modules/mimeapps/MimeAppsPage.qml (limited to 'desktop/modules/mimeapps/MimeAppsPage.qml') diff --git a/desktop/modules/mimeapps/MimeAppsPage.qml b/desktop/modules/mimeapps/MimeAppsPage.qml new file mode 100644 index 0000000..6103e5c --- /dev/null +++ b/desktop/modules/mimeapps/MimeAppsPage.qml @@ -0,0 +1,313 @@ +// Copyright (C) 2026 Danilo M. +// +// 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" + } + } +} -- cgit v1.2.3