Settings > Apps: choose the default browser, email, files, terminal, editor, viewers and players
CI / test (push) Failing after 45s

- A sixth Settings tab, Apps (key 6, SettingsTab::Apps; the popup is 390 px wide for it): Web
  browser, Email, File manager, Terminal, Text editor, Image viewer, Video player, Music player,
  PDF and documents, Archive manager. Each row shows the current default with its real icon (the
  icon-theme lookup the task manager uses; a letter tile when there is none). Click a row to list
  the installed apps that can take it, click one to make it the default. A chosen app that has
  been uninstalled since is named in red under its row ("gthumb is not installed any more").
- Installed apps come from the .desktop files in the XDG data dirs (and flatpak's exports), with
  MimeType= and Categories= (desktop_entry_keys, now shared with the launcher's parse_desktop).
  A kind lists the shown apps that handle one of its MIME types; the terminal lists
  TerminalEmulator apps; browsers are only offered as browsers.
- Reading follows the mimeapps.list spec: $XDG_CONFIG_HOME/[chandra-]mimeapps.list, then
  $XDG_CONFIG_DIRS (/etc/xdg), then $XDG_DATA_HOME and $XDG_DATA_DIRS applications/; the first
  installed [Default Applications] entry wins, else [Added Associations], else mimeinfo.cache
  (minus [Removed Associations]), else the first installed app that handles the type. A hidden
  (NoDisplay) default still counts and its name is shown.
- Setting writes [Default Applications] in ~/.config/mimeapps.list for each of the kind's MIME
  types the app handles (always the main one: http/https/text/html for browsers, mailto,
  inode/directory, text/plain, image/png..., video/mp4..., audio/mpeg..., application/pdf...,
  application/zip...), as xdg-mime default / xdg-settings do, without running them. Every other
  section, entry, comment and blank line is kept; the app leaves those types' [Removed
  Associations]; a chandra-mimeapps.list that already names one of those types is updated too.
- The terminal has no MIME type: state.edn :terminal "kitty" (desktop id), also written first in
  ~/.config/xdg-terminals.list (xdg-terminal-exec). Terminal=true apps from the launcher (and
  session restore) now start in it (foot -e, kgx/gnome-terminal/ptyxis --, kitty <cmd>, wezterm
  start --) instead of always foot; with no choice: xdg-terminals.list, then foot, then any
  installed terminal. Terminal apps are listed when any terminal emulator is installed, not only
  foot.
- New `default-apps` capability (categories, apps, set-default, refresh): host tables 11 (sss:
  name current missing) and 12 (issii: category id name icon current), request 19 (category *
  1000 + app, -1 rescans) in the wasmer/wasmtime stub, host-wasm and the MoonBit wasmhost
  (chandra:host/plasmoids default-app(s32)). Nothing is scanned until the tab opens (refresh).
- Tests with a temporary XDG tree: categories and current defaults (user file, system file,
  Added Associations, mimeinfo.cache, missing apps, hidden defaults), writing that keeps other
  entries, desktop-specific files, the terminal list and commands, the Apps tab end to end, and
  the :terminal round trip through state.edn.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hgywj7RPmFdFhpzGZj
This commit is contained in:
Claude committed 2026-11-25 21:27:30 +00:00
1 parent 442f9477b4
commit bb7c9b947f
26 files changed
+1605 -73

No files matched your search

+237
View File
@@ -0,0 +1,237 @@
///| Default apps: a temporary XDG tree (applications, mimeinfo.cache, mimeapps.list,
/// xdg-terminals.list), the categories' choices and current defaults, setting them (other entries
/// kept), the terminal choice, and the settings module's Apps tab.
///|
fn da_write(path : String, text : String) -> Unit {
let slash = path.rev_find("/").unwrap()
ignore(@sys.mkdir_p(path[:slash].to_string()))
ignore(@sys.write_file_bytes(path, @sys.str_to_utf8_bytes(text)))
}
///|
fn da_read(path : String) -> String {
let raw = @sys.read_file_bytes(path)
let sb = StringBuilder::new()
for i = 0; i < raw.length(); i = i + 1 {
sb.write_char(Int::unsafe_to_char(raw[i].to_int())) // the files are ASCII
}
sb.to_string()
}
///| A desktop entry.
fn da_app(name : String, exec : String, mimes : String, cats : String, extra? : String = "") -> String {
"[Desktop Entry]\nType=Application\nName=" +
name +
"\nExec=" +
exec +
"\nMimeType=" +
mimes +
"\nCategories=" +
cats +
"\n" +
extra
}
///| A fresh XDG tree under /tmp: user config + data, one system config dir, one system data dir.
fn da_tree(tag : String) -> (String, @shell.XdgDirs) {
let root = "/tmp/chandra-default-apps-" + tag + "-" + @sys.getpid().to_string()
let sys = root + "/usr/share/applications"
da_write(sys + "/firefox.desktop", da_app("Firefox", "firefox %u", "text/html;x-scheme-handler/http;x-scheme-handler/https;image/png;application/pdf;", "Network;WebBrowser;"))
da_write(sys + "/chromium.desktop", da_app("Chromium", "chromium %U", "text/html;x-scheme-handler/http;x-scheme-handler/https;", "Network;WebBrowser;"))
da_write(sys + "/thunderbird.desktop", da_app("Thunderbird", "thunderbird %u", "x-scheme-handler/mailto;message/rfc822;", "Network;Email;"))
da_write(sys + "/org.gnome.Nautilus.desktop", da_app("Files", "nautilus --new-window %U", "inode/directory;", "GNOME;Utility;FileManager;"))
da_write(sys + "/foot.desktop", da_app("Foot", "foot", "", "System;TerminalEmulator;"))
da_write(sys + "/kitty.desktop", da_app("kitty", "kitty", "", "System;TerminalEmulator;"))
da_write(sys + "/org.gnome.gedit.desktop", da_app("Text Editor", "gedit %U", "text/plain;text/markdown;", "Utility;TextEditor;"))
da_write(sys + "/eog.desktop", da_app("Image Viewer", "eog %U", "image/png;image/jpeg;", "Graphics;Viewer;"))
da_write(sys + "/vlc.desktop", da_app("VLC", "vlc %U", "video/mp4;audio/mpeg;", "AudioVideo;"))
da_write(sys + "/evince.desktop", da_app("Document Viewer", "evince %U", "application/pdf;", "Office;Viewer;"))
da_write(sys + "/evince-previewer.desktop", da_app("Print Preview", "evince-previewer %U", "application/pdf;", "Office;", extra="NoDisplay=true\n"))
da_write(sys + "/htop.desktop", "[Desktop Entry]\nType=Application\nName=htop\nExec=htop\nTerminal=true\nCategories=System;Monitor;\n")
// The system's cache says Chromium for mail links (it doesn't handle them: not installed for
// that), Files for folders.
da_write(sys + "/mimeinfo.cache", "[MIME Cache]\ninode/directory=org.gnome.Nautilus.desktop;\napplication/pdf=evince-previewer.desktop;evince.desktop;\n")
// A system-wide default: VLC for video.
da_write(root + "/etc/xdg/mimeapps.list", "[Default Applications]\nvideo/mp4=vlc.desktop\n")
// The user's own file: Chromium for web pages, a removed viewer for PNGs, notes to keep.
da_write(
root + "/config/mimeapps.list",
"# mine\n[Added Associations]\ntext/plain=org.gnome.gedit.desktop;\n\n[Default Applications]\ntext/html=chromium.desktop;\nx-scheme-handler/http=chromium.desktop;\nimage/png=gthumb.desktop;eog.desktop;\n\n[Removed Associations]\nx-scheme-handler/https=firefox.desktop;\n",
)
da_write(root + "/config/xdg-terminals.list", "# preferred\nfoot.desktop\n")
let dirs : @shell.XdgDirs = {
config_home: root + "/config",
config_dirs: [root + "/etc/xdg"],
data_home: root + "/home-data",
data_dirs: [root + "/usr/share"],
desktops: ["chandra"],
}
(root, dirs)
}
///|
fn da_cat(id : String) -> Int {
let mut n = -1
for i, c in @shell.app_categories {
if c.id == id {
n = i
}
}
n
}
///|
fn da_names(ch : @shell.CategoryChoices) -> Array[String] {
ch.apps.map(a => a.name)
}
///|
test "each category lists its installed apps and reads the current default" {
let (_, dirs) = da_tree("read")
let installed = @shell.scan_handler_apps(dirs)
let ch = @shell.default_app_choices(dirs, installed, "")
assert_eq(ch.length(), @shell.app_categories.length())
// Browsers: both, by name; the user's file says Chromium.
let b = ch[da_cat("browser")]
assert_eq(da_names(b), ["Chromium", "Firefox"])
assert_eq(b.current_id, "chromium")
assert_eq(b.current, 0)
assert_eq(b.missing, "")
// Firefox handles PNGs and PDFs but is only a browser.
assert_eq(da_names(ch[da_cat("images")]), ["Image Viewer"])
// PNGs: gThumb was chosen but is gone; the next entry, Image Viewer, is used.
assert_eq(ch[da_cat("images")].current_id, "eog")
assert_eq(ch[da_cat("images")].missing, "gthumb")
// Mail: no default anywhere, the only app that handles it.
assert_eq(ch[da_cat("email")].current_id, "thunderbird")
// Folders: from mimeinfo.cache.
assert_eq(ch[da_cat("files")].current_id, "org.gnome.Nautilus")
// Video: the system-wide mimeapps.list in XDG_CONFIG_DIRS.
assert_eq(ch[da_cat("video")].current_id, "vlc")
assert_eq(da_names(ch[da_cat("music")]), ["VLC"])
// PDFs: the cache's first entry is hidden from menus but installed, so it is the default;
// only the shown viewer is offered.
assert_eq(ch[da_cat("documents")].current_id, "evince-previewer")
assert_eq(ch[da_cat("documents")].current, -1)
assert_eq(da_names(ch[da_cat("documents")]), ["Document Viewer"])
// Text: Added Associations.
assert_eq(ch[da_cat("editor")].current_id, "org.gnome.gedit")
// Terminal: xdg-terminals.list says foot.
let t = ch[da_cat("terminal")]
assert_eq(da_names(t), ["Foot", "kitty"])
assert_eq(t.current_id, "foot")
// Nothing handles archives.
assert_eq(ch[da_cat("archives")].apps.length(), 0)
assert_eq(ch[da_cat("archives")].current_id, "")
// The terminal chosen in state.edn wins; a chosen one that is gone is reported.
assert_eq(@shell.current_terminal(dirs, installed, "kitty"), ("kitty", ""))
assert_eq(@shell.current_terminal(dirs, installed, "alacritty"), ("foot", "alacritty"))
}
///|
test "setting a default writes mimeapps.list and keeps everything else" {
let (root, dirs) = da_tree("write")
// A desktop-specific file that names a browser would win over mimeapps.list: it is updated too.
da_write(root + "/config/chandra-mimeapps.list", "[Default Applications]\ntext/html=chromium.desktop;\napplication/pdf=evince.desktop;\n")
let comp = new_comp("da-write")
comp.shell.xdg_dirs = dirs
comp.shell.refresh_default_apps()
let b = da_cat("browser")
assert_eq(comp.shell.default_app_choices()[b].current_id, "chromium")
assert_true(comp.shell.set_default_app(b, 1)) // Firefox
assert_eq(comp.shell.default_app_choices()[b].current_id, "firefox")
assert_eq(
da_read(root + "/config/mimeapps.list"),
"# mine\n[Added Associations]\ntext/plain=org.gnome.gedit.desktop;\n\n[Default Applications]\ntext/html=firefox.desktop;\nx-scheme-handler/http=firefox.desktop;\nimage/png=gthumb.desktop;eog.desktop;\nx-scheme-handler/https=firefox.desktop;\n\n[Removed Associations]\n",
)
assert_eq(
da_read(root + "/config/chandra-mimeapps.list"),
"[Default Applications]\ntext/html=firefox.desktop;\napplication/pdf=evince.desktop;\nx-scheme-handler/http=firefox.desktop;\nx-scheme-handler/https=firefox.desktop;\n",
)
// Picking the image viewer again clears the missing gThumb.
let im = da_cat("images")
assert_true(comp.shell.set_default_app(im, 0))
assert_eq(comp.shell.default_app_choices()[im].missing, "")
assert_true(da_read(root + "/config/mimeapps.list").contains("image/png=eog.desktop;\nx-scheme-handler/https=firefox.desktop;\nimage/jpeg=eog.desktop;\n"))
// Out of range: nothing happens.
assert_false(comp.shell.set_default_app(im, 5))
assert_false(comp.shell.set_default_app(99, 0))
}
///|
test "the terminal choice runs terminal apps and goes first in xdg-terminals.list" {
let (root, dirs) = da_tree("term")
let comp = new_comp("da-term")
comp.shell.xdg_dirs = dirs
comp.shell.refresh_default_apps()
assert_eq(comp.shell.in_terminal("htop"), "foot -e htop")
let t = da_cat("terminal")
assert_true(comp.shell.set_default_app(t, 1)) // kitty
assert_eq(comp.shell.terminal, "kitty")
assert_eq(comp.shell.in_terminal("htop"), "kitty htop")
assert_eq(da_read(root + "/config/xdg-terminals.list"), "kitty.desktop\n# preferred\nfoot.desktop\n")
assert_true(comp.shell.set_default_app(t, 0)) // foot
assert_eq(da_read(root + "/config/xdg-terminals.list"), "foot.desktop\nkitty.desktop\n# preferred\n")
}
///|
test "the settings module's Apps tab shows each kind's default and sets it" {
let (root, dirs) = da_tree("tab")
let comp = new_comp("da-tab")
comp.shell.xdg_dirs = dirs
let slot = comp.shell.slot_of("session-settings")
assert_true(comp.shell.open_settings(Apps))
ignore(comp.compose_frame())
// Ten kinds, plus the line saying gThumb is gone.
let kinds = @shell.app_categories.length()
let pr = comp.shell.slot_popup_rect(slot)
assert_eq(pr.h, 40 + (kinds + 1) * 28 + 6)
let texts = comp.shell.slot_popup_texts(slot).map(t => t.0)
assert_true(texts.contains("Web browser"))
assert_true(texts.contains("Chromium"))
assert_true(texts.contains("! gthumb is not installed any more"))
assert_true(texts.contains("None installed"))
// Row i's middle (the popup grows upwards from the panel, so read where it is now).
let row_y = fn(i : Int) { comp.shell.slot_popup_rect(slot).y + 40 + i * 28 + 13 }
// Click "Web browser": its two apps are listed under it.
ignore(@sys.write_file_bytes("_build/default_apps_tab.png", @renderer.encode_png(comp.compose_frame())))
click_at(comp, pr.x + 40, row_y(0), 5U)
assert_eq(comp.shell.slot_popup_rect(slot).h, pr.h + 2 * 28)
ignore(@sys.write_file_bytes("_build/default_apps_open.png", @renderer.encode_png(comp.compose_frame())))
assert_true(comp.shell.slot_popup_texts(slot).map(t => t.0).contains("Firefox"))
// Click Firefox: it is the browser now, and the list folds up.
click_at(comp, pr.x + 60, row_y(2), 10U)
ignore(comp.compose_frame())
assert_eq(comp.shell.default_app_choices()[da_cat("browser")].current_id, "firefox")
assert_true(da_read(root + "/config/mimeapps.list").contains("text/html=firefox.desktop;"))
assert_eq(comp.shell.slot_popup_rect(slot).h, pr.h)
assert_true(comp.shell.slot_popup_texts(slot).map(t => t.0).contains("Firefox"))
// A kind with nothing installed doesn't open.
let arch = da_cat("archives") + 1 // under the gThumb line
click_at(comp, pr.x + 40, row_y(arch), 20U)
assert_eq(comp.shell.slot_popup_rect(slot).h, pr.h)
}
///|
test "the chosen terminal is saved to state.edn and restored" {
if @sys.getenv("CHANDRA_TEST_CONFIG") == "1" {
write_state_edn("{:note \"keep\"}\n")
let (_, dirs) = da_tree("save")
let a = new_comp("da-save-a")
a.shell.persist = true
a.restore_state()
a.shell.xdg_dirs = dirs
a.shell.refresh_default_apps()
assert_true(a.shell.set_default_app(da_cat("terminal"), 1))
let text = read_state_edn()
assert_true(text.contains(":terminal \"kitty\""))
assert_true(text.contains(":note \"keep\""))
let b = new_comp("da-save-b")
b.shell.persist = true
b.shell.xdg_dirs = dirs
b.restore_state()
assert_eq(b.shell.terminal, "kitty")
assert_eq(b.shell.default_app_choices()[da_cat("terminal")].current_id, "kitty")
}
}