aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/modules/mimeapps/MimeAppsPage.qml
blob: 8ebe8807170e867fe12df6076a85a4d334fbd683 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
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.stage(mimes.reduce((t, m) => M.set(t, m, id), page.ma.draft));
        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.stage(M.remove(page.ma.draft, 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 + ". Nothing is written until Save."
    }

    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"
        }
    }
}