Launcher: pick up web apps as they are installed, categories, scrolling
CI / test (push) Failing after 49s

- Read the app catalog again when the Applications menu opens if .desktop files were added or removed (and on Reload); it was only read at login, so a PWA installed during the session was missing from the menu and its search
- List a Chromium browser's web apps in their own category ("Chrome Apps", "Chromium Apps", ...), as Plasma does; don't take one for the default browser
- Category column and a scrollable list (wheel, scrollbar) when the catalog doesn't fit the menu
- Show menu-spec main category names instead of the first Categories= entry; sort apps A-Z (they were sorted shortest name first)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Opus 5.5 committed 2026-11-28 23:42:10 -08:00
1 parent aa92aaac6f
commit 68baf1a467
11 files changed
+578 -109

No files matched your search

+138 -36
View File
@@ -18,29 +18,6 @@ priv struct DesktopApp {
terminal_emulator : Bool
}
///| Directories scanned, lowest priority first (later ones override by file name).
fn app_dirs() -> Array[String] {
let home = @sys.getenv("HOME")
let data_home = {
let x = @sys.getenv("XDG_DATA_HOME")
if x.length() > 0 { x } else { home + "/.local/share" }
}
let dirs : Array[String] = []
let xdg = @sys.getenv("XDG_DATA_DIRS")
let list = if xdg.length() > 0 { xdg } else { "/usr/local/share:/usr/share" }
let parts : Array[String] = list.split(":").map(x => x.to_string()).collect()
// XDG_DATA_DIRS is highest priority first; we want lowest first.
for i = parts.length() - 1; i >= 0; i = i - 1 {
if parts[i].length() > 0 {
dirs.push(parts[i] + "/applications")
}
}
dirs.push("/var/lib/flatpak/exports/share/applications")
dirs.push(home + "/.local/share/flatpak/exports/share/applications")
dirs.push(data_home + "/applications")
dirs
}
///| Drop Exec field codes (%f %U ...), keep a literal %%.
fn clean_exec(exec : String) -> String {
let sb = StringBuilder::new()
@@ -60,6 +37,57 @@ fn clean_exec(exec : String) -> String {
sb.to_string().trim(chars=" ").to_string()
}
///| The menu category of a web app a Chromium browser installed (its Exec starts the browser with
/// --app-id), named the way the browser names its own menu folder ("Chrome Apps"); None for any
/// other command. These files carry no Categories=: the browsers sort them into their folder with
/// a desktop menu file instead.
fn web_app_category(command : String) -> String? {
match command.find("--app-id=") {
None => None
Some(i) => {
// Only the browser's part: an app id may spell anything in a-p.
let browser = command[:i].to_string().to_lower()
for
it in [
("brave", "Brave Apps"),
("microsoft", "Edge Apps"),
("msedge", "Edge Apps"),
("vivaldi", "Vivaldi Apps"),
("opera", "Opera Apps"),
("chromium", "Chromium Apps"),
("chrome", "Chrome Apps"),
] {
if browser.contains(it.0) {
return Some(it.1)
}
}
Some("Web Apps")
}
}
}
///| The menu category for a Categories= value: the first of the menu spec's main categories in
/// it, by the name menus show, else "Other" (toolkit names such as Qt and GTK are no category).
fn menu_category(categories : String) -> String {
for c in semi_list(categories) {
match c {
"AudioVideo" | "Audio" | "Video" => return "Multimedia"
"Development" => return "Development"
"Education" => return "Education"
"Game" => return "Games"
"Graphics" => return "Graphics"
"Network" => return "Internet"
"Office" => return "Office"
"Science" => return "Science"
"Settings" => return "Settings"
"System" => return "System"
"Utility" => return "Utilities"
_ => ()
}
}
"Other"
}
///| Parse one .desktop file; None when it should not be listed. A Terminal=true app keeps its
/// own command (`terminal`): it is started inside the chosen terminal (`DesktopShell::command_for`).
fn parse_desktop(id : String, text : String, have_terminal : Bool) -> DesktopApp? {
@@ -77,10 +105,7 @@ fn parse_desktop(id : String, text : String, have_terminal : Bool) -> DesktopApp
return None
}
let cats = get("Categories")
let category = match cats.split(";").next() {
Some(c) if c.length() > 0 => c.to_string()
_ => "Other"
}
let category = web_app_category(exec).unwrap_or(menu_category(cats))
let comment = get("Comment")
let comment = if comment.length() > 0 { comment } else { get("GenericName") }
Some({
@@ -96,11 +121,65 @@ fn parse_desktop(id : String, text : String, have_terminal : Bool) -> DesktopApp
})
}
///| Scan every applications dir; later dirs override earlier ones, result sorted by name.
fn scan_desktop_apps() -> Array[DesktopApp] {
///| The .desktop file names in the applications dirs, to tell when apps were added or removed.
fn desktop_files_in(dirs : Array[String]) -> Array[String] {
let out : Array[String] = []
for dir in dirs {
out.push(dir)
out.append(@sys.list_desktop_files(dir))
}
out
}
///| Fill the app catalog from the installed .desktop files.
fn DesktopShell::load_apps(self : DesktopShell) -> Unit {
let dirs = self.xdg_dirs.app_dirs()
self.app_files = desktop_files_in(dirs)
let old_icons = self.icon_names.copy()
self.apps.clear()
self.exec.clear()
self.wm_classes.clear()
self.icon_names.clear()
self.terminal_apps.clear()
for a in scan_desktop_apps(dirs) {
if a.terminal {
self.terminal_apps.set(a.id, true)
}
if a.icon.length() > 0 {
self.icon_names.set(a.id, a.icon)
}
if a.wm_class.length() > 0 {
self.wm_classes.set(a.wm_class.to_lower(), a.id)
}
let glyph = match a.name.to_array().get(0) {
Some(c) => c.to_string().to_upper()
None => "?"
}
self.apps.push({ id: a.id, name: a.name, comment: a.comment, category: a.category, glyph })
self.exec.set(a.id, a.command)
// A new app (or one with a new icon) may have been looked up before it had one.
if old_icons.get(a.id) != self.icon_names.get(a.id) {
self.icon_handles.remove(a.id)
}
}
self.order_apps()
self.apps_dirty = true
}
///| Read the catalog again if apps were installed or removed since (a web app installed from the
/// browser, say). Called as the Applications menu opens; only lists the dirs when nothing changed.
fn DesktopShell::refresh_apps(self : DesktopShell) -> Unit {
if desktop_files_in(self.xdg_dirs.app_dirs()) != self.app_files {
self.load_apps()
}
}
///| Scan every applications dir in `dirs` (highest priority first: a file there overrides one of
/// the same name further on), result sorted by name.
fn scan_desktop_apps(dirs : Array[String]) -> Array[DesktopApp] {
let by_id : Map[String, DesktopApp] = {}
let removed : Map[String, Bool] = {}
for dir in app_dirs() {
for dir in dirs.rev() {
for file in @sys.list_desktop_files(dir) {
let bytes = @sys.read_file_bytes(dir + "/" + file)
let id = file[:file.length() - 8].to_string()
@@ -126,11 +205,8 @@ fn scan_desktop_apps() -> Array[DesktopApp] {
apps.push(a)
}
}
apps.sort_by(fn(a, b) {
let x = a.name.to_lower()
let y = b.name.to_lower()
if x < y { -1 } else if x > y { 1 } else { 0 }
})
// Strings compare shortest first; menus want A-Z.
apps.sort_by(fn(a, b) { a.name.to_lower().lexical_compare(b.name.to_lower()) })
apps
}
@@ -141,7 +217,7 @@ test "parse_desktop reads a normal entry and strips field codes" {
Some(a) => {
assert_eq(a.name, "Firefox")
assert_eq(a.command, "firefox")
assert_eq(a.category, "Network")
assert_eq(a.category, "Internet")
assert_eq(a.comment, "Browse")
}
None => fail("expected entry")
@@ -162,6 +238,32 @@ test "parse_desktop keeps StartupWMClass" {
}
}
///|
test "parse_desktop puts a browser's web apps in their own category" {
let web = fn(exec : String) {
let t = "[Desktop Entry]\nType=Application\nName=YouTube\nExec=" + exec + "\nStartupWMClass=crx_agim\n" +
"Actions=Shorts\n[Desktop Action Shorts]\nName=Shorts\nExec=other\n"
match parse_desktop("chrome-agim-Default", t, true) {
Some(a) => (a.name, a.category, a.command)
None => ("", "", "")
}
}
let chrome = "/opt/google/chrome/google-chrome --profile-directory=Default --app-id=edgeop"
assert_eq(web(chrome), ("YouTube", "Chrome Apps", chrome))
assert_eq(web("/usr/bin/chromium --app-id=abc").1, "Chromium Apps")
assert_eq(web("/usr/bin/flatpak run --command=/app/bin/chrome com.google.Chrome --app-id=abc").1, "Chrome Apps")
assert_eq(web("/opt/microsoft/msedge/microsoft-edge --app-id=abc").1, "Edge Apps")
assert_eq(web("firefox"), ("YouTube", "Other", "firefox"))
}
///|
test "menu_category skips toolkit names and names the main category" {
assert_eq(menu_category("Qt;KDE;System;TerminalEmulator;"), "System")
assert_eq(menu_category("Game;Network;"), "Games")
assert_eq(menu_category("GTK;X-Own;"), "Other")
assert_eq(menu_category(""), "Other")
}
///|
test "parse_desktop hides NoDisplay entries" {
let t = "[Desktop Entry]\nType=Application\nName=X\nExec=x\nNoDisplay=true\n"