summaryrefslogtreecommitdiff
path: root/services/AppSearch.qml
blob: 9cd10d4fd2111f018010962acc67553806fda372 (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
pragma Singleton

import "../utils/levendist.js" as Levendist
import Quickshell
import Quickshell.Io
import qs.config

/**
 * - Eases fuzzy searching for applications by name
 * - Guesses icon name for window class name
 */
Singleton {
    id: root

    property int scoreThreshold: General.appSearchFuzzySearchThreshold

    property var substitutions: ({
            "code-url-handler": "visual-studio-code",
            "Code": "visual-studio-code",
            "gnome-tweaks": "org.gnome.tweaks",
            "pavucontrol-qt": "pavucontrol",
            "wps": "wps-office2019-kprometheus",
            "wpsoffice": "wps-office2019-kprometheus",
            "footclient": "foot",
            "zen": "zen-browser",
            "brave-browser": "brave-desktop"
        })

    property var regexSubstitutions: [
        {
            regex: /^steam_app_(\d+)$/,
            replace: "steam_icon_$1"
        },
        {
            regex: /Minecraft.*/,
            replace: "minecraft"
        },
        {
            regex: /.*polkit.*/,
            replace: "system-lock-screen"
        },
        {
            regex: /gcr.prompter/,
            replace: "system-lock-screen"
        }
    ]

    property var fuzzyQueryCache: ({})
    property var iconExistsCache: ({})
    property var guessIconCache: ({})

    readonly property list<DesktopEntry> list: Array.from(DesktopEntries.applications.values).sort((a, b) => a.name.localeCompare(b.name))

    /**
   * Returns an Array<DesktopEntry> for a given search.
   * Results are cached by exact search string and, if the
   * search starts with a previously cached key, uses that
   * cached result as the source for filtering.
   */
    function fuzzyQuery(search) { // search is expected to be .toLowerCase()
        if (root.fuzzyQueryCache[search]) {
            return root.fuzzyQueryCache[search];
        }

        let source = list;
        let previousLists = Object.keys(root.fuzzyQueryCache).filter(key => key && search.startsWith(key));
        if (previousLists.length > 0) {
            var longest = previousLists.reduce((a, b) => a.length >= b.length ? a : b);
            source = root.fuzzyQueryCache[longest];
        }

        const p = [], f = [];
        for (const s of source) {
            const t = s.name.toLowerCase();
            if (t.startsWith(search)) {
                p.push(s);
                continue;
            }
            let pos = 0;
            for (const c of search) {
                pos = t.indexOf(c, pos);
                if (pos < 0) {
                    pos = 0;
                    break;
                }
                pos++;
            }
            if (pos)
                f.push({
                    s,
                    score: Levendist.distance(search, t)
                });
        }
        const results = p.concat(f.sort((a, b) => a.score - b.score).map(({
                s
            }) => s));

        root.fuzzyQueryCache[search] = results;
        return results;
    }

    function iconExists(iconName) {
        if (!iconName || iconName.length === 0) {
            return false;
        }
        if (root.iconExistsCache.hasOwnProperty(iconName)) {
            return root.iconExistsCache[iconName];
        }
        const exists = Quickshell.iconPath(iconName, true).length > 0 && !iconName.includes("image-missing");
        root.iconExistsCache[iconName] = exists;
        return exists;
    }

    function guessIcon(str) {
        if (!str || str.length === 0) {
            return "image-missing";
        }
        if (root.guessIconCache[str]) {
            return root.guessIconCache[str];
        }

        var result;

        if (substitutions[str]) {
            result = substitutions[str];
        } else {
            result = null;
            for (var i = 0; i < regexSubstitutions.length; i++) {
                var {
                    regex,
                    replace
                } = regexSubstitutions[i];
                var replaced = str.replace(regex, replace);
                if (replaced !== str) {
                    result = replaced;
                    break;
                }
            }
        }

        if (!result && root.iconExists(str)) {
            result = str;
        }

        if (!result) {
            var guessStr = str.split(".").slice(-1)[0].toLowerCase();
            if (root.iconExists(guessStr)) {
                result = guessStr;
            }
        }

        if (!result) {
            var kebab = str.toLowerCase().replace(/\s+/g, "-");
            if (root.iconExists(kebab)) {
                result = kebab;
            }
        }

        if (!result) {
            const matches = root.fuzzyQuery(str);
            if (matches.length > 0) {
                const candidate = matches[0].icon;
                if (root.iconExists(candidate)) {
                    result = candidate;
                }
            }
        }

        if (!result) {
            result = str;
        }

        root.guessIconCache[str] = result;
        return result;
    }
}