diff options
Diffstat (limited to 'desktop')
| -rw-r--r-- | desktop/modules/mimeapps/MimeAppsModule.qml | 88 | ||||
| -rw-r--r-- | desktop/modules/mimeapps/MimeAppsPage.qml | 313 | ||||
| -rw-r--r-- | desktop/modules/mimeapps/README.md | 27 | ||||
| -rw-r--r-- | desktop/shell.qml | 2 |
4 files changed, 430 insertions, 0 deletions
diff --git a/desktop/modules/mimeapps/MimeAppsModule.qml b/desktop/modules/mimeapps/MimeAppsModule.qml new file mode 100644 index 0000000..7aa9ad9 --- /dev/null +++ b/desktop/modules/mimeapps/MimeAppsModule.qml @@ -0,0 +1,88 @@ +// 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 } + } + } +} 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. <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" + } + } +} diff --git a/desktop/modules/mimeapps/README.md b/desktop/modules/mimeapps/README.md new file mode 100644 index 0000000..e63b7bb --- /dev/null +++ b/desktop/modules/mimeapps/README.md @@ -0,0 +1,27 @@ +# 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`. diff --git a/desktop/shell.qml b/desktop/shell.qml index a4e2764..db7a165 100644 --- a/desktop/shell.qml +++ b/desktop/shell.qml @@ -33,6 +33,7 @@ import "modules/git" import "modules/hyprland" import "modules/kdeconnect" import "modules/mail" +import "modules/mimeapps" import "modules/monitors" import "modules/network" import "modules/notes" @@ -108,6 +109,7 @@ ShellRoot { SunsetModule {}, IdleModule {}, NotificationsModule {}, + MimeAppsModule {}, AutostartModule {}, BindsModule {}, OptionsModule {}, |
