Skip to content

Commit 4c69349

Browse files
authored
fix(search): Check exec field when searching, remove hard-coded search directories
* Check exec field when searching * Remove hard-coded search directory list in favor of XDG_DATA_DIRS
1 parent a4a25ef commit 4c69349

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

src/dialog_launcher.ts

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,6 @@ const { OK } = result;
2323
const HOME_DIR: string = GLib.get_home_dir();
2424
const DATA_DIRS: string = GLib.get_system_data_dirs();
2525

26-
/// Search paths for finding applications
27-
const SEARCH_PATHS: Array<[string, string]> = [
28-
// System-wide
29-
["System", "/usr/share/applications/"],
30-
["System (Local)", "/usr/local/share/applications/"],
31-
// User-local
32-
["User", HOME_DIR + "/.local/share/applications/"],
33-
// System-wide flatpaks
34-
["Flatpak (System)", "/var/lib/flatpak/exports/share/applications/"],
35-
// User-local flatpaks
36-
["Flatpak (User)", HOME_DIR + "/.local/share/flatpak/exports/share/applications/"],
37-
// System-wide Snaps
38-
["Snap (System)", "/var/lib/snapd/desktop/applications/"]
39-
];
40-
4126
export class Launcher extends search.Search {
4227
options: Array<launch.SearchOption>
4328
desktop_apps: Array<[string, AppInfo]>
@@ -110,7 +95,9 @@ export class Launcher extends search.Search {
11095
for (const [where, app] of this.desktop_apps) {
11196
const name = app.name()
11297
const keywords = app.keywords()
113-
const app_items = keywords !== null ? name.split().concat(keywords) : [name]
98+
const exec = app.exec()
99+
const app_items = keywords !== null ?
100+
name.split().concat(keywords).concat(exec) : name.split().concat(exec)
114101

115102
for (const item of app_items) {
116103
const item_match = item.toLowerCase()
@@ -125,6 +112,7 @@ export class Launcher extends search.Search {
125112
{ gicon: app.icon() },
126113
this.icon_size(),
127114
{ app },
115+
exec,
128116
keywords
129117
)
130118

@@ -139,9 +127,12 @@ export class Launcher extends search.Search {
139127
const sorter = (a: launch.SearchOption, b: launch.SearchOption) => {
140128
const a_name = a.title.toLowerCase()
141129
const b_name = b.title.toLowerCase()
130+
const a_exec = a.exec ? a.exec.toLowerCase() : ""
131+
const b_exec = b.exec ? b.exec.toLowerCase() : ""
142132

143133
let a_weight = 0, b_weight = 0;
144134

135+
// Sort by metadata (name, description, keywords)
145136
if (!a_name.startsWith(pattern)) {
146137
a_weight = 1
147138
if (!a_name.includes(pattern) {
@@ -160,7 +151,17 @@ export class Launcher extends search.Search {
160151
}
161152
}
162153
}
154+
// Sort by command (exec)
155+
if (a_exec.includes(pattern)) {
156+
if (a_exec.startsWith(pattern) {
157+
a_weight = Math.min(a_weight, 2)
158+
} else {
159+
a_weight = Math.min(a_weight, levenshtein.compare(pattern, a_exec))
160+
}
161+
}
162+
163163

164+
// Sort by metadata (name, description, keywords)
164165
if (!b_name.startsWith(pattern)) {
165166
b_weight = 1
166167
if (!b_name.includes(pattern)) {
@@ -179,6 +180,14 @@ export class Launcher extends search.Search {
179180
}
180181
}
181182
}
183+
// Sort by command (exec)
184+
if (b_exec.includes(pattern)) {
185+
if (b_exec.startsWith(pattern) {
186+
b_weight = Math.min(b_weight, 2)
187+
} else {
188+
b_weight = Math.min(b_weight, levenshtein.compare(pattern, b_exec))
189+
}
190+
}
182191

183192
return a_weight === b_weight
184193
? a_name.length > b_name.length ? 1 : 0
@@ -311,17 +320,6 @@ export class Launcher extends search.Search {
311320
load_desktop_files() {
312321
lib.bench("load_desktop_files", () => {
313322
this.desktop_apps.splice(0);
314-
for (const [where, path] of SEARCH_PATHS) {
315-
for (const result of app_info.load_desktop_entries(path)) {
316-
if (result.kind == OK) {
317-
const value = result.value;
318-
this.desktop_apps.push([where, value]);
319-
} else {
320-
const why = result.value;
321-
log.warn(why.context(`failed to load desktop app`).format());
322-
}
323-
}
324-
}
325323
for (const _path of DATA_DIRS) {
326324
const path = _path.replace(/\/$/, '') + "/applications";
327325
for (const result of app_info.load_desktop_entries(path)) {

src/launcher_service.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,19 @@ export class SearchOption {
178178
title: string
179179
description: null | string
180180
id: Identity
181+
exec: null | string
181182
keywords: null | array
182183

183184
widget: St.Button
184185

185186
shortcut: St.Widget = new St.Label({ text: "", y_align: Clutter.ActorAlign.CENTER, style: "padding-left: 6px;padding-right: 6px" })
186187

187-
constructor(title: string, description: null | string, category_icon: string, icon: null | IconSrc, icon_size: number, id: Identity, keywords: null | array) {
188+
constructor(title: string, description: null | string, category_icon: string, icon: null | IconSrc, icon_size: number, id: Identity,
189+
exec: null | string, keywords: null | array) {
188190
this.title = title
189191
this.description = description
190192
this.id = id
193+
this.exec = exec
191194
this.keywords = keywords
192195

193196
let cat_icon

0 commit comments

Comments
 (0)