Give each default-application role a whole family of types
Every role carried a single representative type, so setting "Images" changed image/png and left image/jpeg wherever it landed. That is how this desktop ended up opening PDFs in GIMP, PNGs in a pixel-art editor and MP3s in a video transcoder: nobody chose any of it, applications registered themselves for everything they could read, and the roles governed one type each. Roles now own families and write every type when set, the settings page exposes the documents, text and archives roles it never offered, and a new seed command curates a fresh machine during setup while always keeping a choice the user has already made. The shipped editor entry launches kitty explicitly. The stock nvim.desktop sets Terminal=true, which defers to whatever the system considers default rather than the terminal this desktop themes. Claude-Session: https://claude.ai/code/session_01BRvzt4H8XXLPVH5MyYdk9L
This commit is contained in:
@@ -65,6 +65,9 @@ write_application org.gnome.Ptyxis.desktop Ptyxis Terminal 'System;TerminalEmula
|
||||
write_application org.gnome.Rhythmbox3.desktop Rhythmbox 'Music Player' 'AudioVideo;Audio;Player;'
|
||||
write_application org.gnome.Loupe.desktop Loupe 'Image Viewer' 'Graphics;Viewer;'
|
||||
write_application org.gnome.Totem.desktop Videos 'Video Player' 'AudioVideo;Video;Player;'
|
||||
write_application org.gnome.Papers.desktop Papers 'Document Viewer' 'Office;Viewer;'
|
||||
write_application panama-nvim.desktop Neovim 'Text Editor' 'Utility;TextEditor;'
|
||||
write_application org.gnome.FileRoller.desktop 'Archive Manager' 'Archive Manager' 'Utility;Archiving;'
|
||||
|
||||
cat >"$config_home/autostart/nextcloud.desktop" <<'EOF'
|
||||
[Desktop Entry]
|
||||
@@ -108,6 +111,9 @@ if [[ "$1" == "query" && "$2" == "default" ]]; then
|
||||
audio/mpeg) printf '%s\n' 'org.gnome.Rhythmbox3.desktop' ;;
|
||||
image/png) printf '%s\n' 'org.gnome.Loupe.desktop' ;;
|
||||
video/mp4) printf '%s\n' 'org.gnome.Totem.desktop' ;;
|
||||
application/pdf) printf '%s\n' 'org.gnome.Papers.desktop' ;;
|
||||
text/plain) printf '%s\n' 'panama-nvim.desktop' ;;
|
||||
application/zip) printf '%s\n' 'org.gnome.FileRoller.desktop' ;;
|
||||
*) exit 91 ;;
|
||||
esac
|
||||
exit 0
|
||||
@@ -125,7 +131,7 @@ export PATH="$fake_bin:$PATH"
|
||||
snapshot="$($helper snapshot)" || fail 'snapshot command failed'
|
||||
[[ "$(rg --count '^get$' "$call_log")" == "1" ]] \
|
||||
|| fail 'browser handler was queried more than once'
|
||||
[[ "$(rg --count '^query$' "$call_log")" == "6" ]] \
|
||||
[[ "$(rg --count '^query$' "$call_log")" == "9" ]] \
|
||||
|| fail 'MIME handlers were queried more than once'
|
||||
jq -e '
|
||||
.handlers == {
|
||||
@@ -135,7 +141,10 @@ jq -e '
|
||||
terminal: "org.gnome.Ptyxis.desktop",
|
||||
music: "org.gnome.Rhythmbox3.desktop",
|
||||
images: "org.gnome.Loupe.desktop",
|
||||
video: "org.gnome.Totem.desktop"
|
||||
video: "org.gnome.Totem.desktop",
|
||||
documents: "org.gnome.Papers.desktop",
|
||||
text: "panama-nvim.desktop",
|
||||
archives: "org.gnome.FileRoller.desktop"
|
||||
} and
|
||||
.autostartEntries == [{id: "nextcloud.desktop", name: "Nextcloud", enabled: false}] and
|
||||
(.luaAutostartEntries | length == 2) and
|
||||
@@ -163,28 +172,49 @@ assert_call() {
|
||||
$helper set-default browser org.mozilla.firefox.desktop
|
||||
assert_call $'set\ndefault-web-browser\norg.mozilla.firefox.desktop'
|
||||
|
||||
roles=(mail files terminal music images video)
|
||||
desktop_ids=(
|
||||
org.gnome.Geary.desktop
|
||||
org.gnome.Nautilus.desktop
|
||||
org.gnome.Ptyxis.desktop
|
||||
org.gnome.Rhythmbox3.desktop
|
||||
org.gnome.Loupe.desktop
|
||||
org.gnome.Totem.desktop
|
||||
)
|
||||
mime_types=(
|
||||
x-scheme-handler/mailto
|
||||
inode/directory
|
||||
x-scheme-handler/terminal
|
||||
audio/mpeg
|
||||
image/png
|
||||
video/mp4
|
||||
)
|
||||
for index in "${!roles[@]}"; do
|
||||
# Setting a role must write EVERY type in its family, in order.
|
||||
#
|
||||
# The families are spelled out here rather than read from the helper: a test
|
||||
# that derives its expectation from the code it is testing would have passed
|
||||
# just as happily when "images" governed image/png alone.
|
||||
assert_family() {
|
||||
local role="$1" desktop_id="$2"
|
||||
shift 2
|
||||
local expected=""
|
||||
local mime
|
||||
for mime in "$@"; do
|
||||
expected+=$'default\n'"$desktop_id"$'\n'"$mime"$'\n'
|
||||
done
|
||||
: >"$call_log"
|
||||
$helper set-default "${roles[$index]}" "${desktop_ids[$index]}"
|
||||
assert_call $'default\n'"${desktop_ids[$index]}"$'\n'"${mime_types[$index]}"
|
||||
done
|
||||
$helper set-default "$role" "$desktop_id"
|
||||
local actual
|
||||
actual="$(cat "$call_log")"
|
||||
[[ "$actual" == "${expected%$'\n'}" ]] || {
|
||||
printf 'expected argv:\n%s\nactual argv:\n%s\n' "${expected%$'\n'}" "$actual" >&2
|
||||
fail "the \"$role\" role was applied to the wrong set of types"
|
||||
}
|
||||
}
|
||||
|
||||
assert_family mail org.gnome.Geary.desktop x-scheme-handler/mailto
|
||||
assert_family files org.gnome.Nautilus.desktop inode/directory
|
||||
assert_family terminal org.gnome.Ptyxis.desktop x-scheme-handler/terminal
|
||||
assert_family music org.gnome.Rhythmbox3.desktop \
|
||||
audio/mpeg audio/flac audio/x-vorbis+ogg audio/ogg \
|
||||
audio/x-wav audio/mp4 audio/aac audio/opus
|
||||
assert_family images org.gnome.Loupe.desktop \
|
||||
image/png image/jpeg image/gif image/webp \
|
||||
image/tiff image/bmp image/svg+xml image/avif
|
||||
assert_family video org.gnome.Totem.desktop \
|
||||
video/mp4 video/x-matroska video/webm video/quicktime \
|
||||
video/x-msvideo video/mpeg
|
||||
assert_family documents org.gnome.Papers.desktop \
|
||||
application/pdf application/epub+zip
|
||||
assert_family text panama-nvim.desktop \
|
||||
text/plain text/markdown text/x-python text/x-csrc text/x-chdr \
|
||||
text/x-c++src text/x-shellscript application/json application/x-yaml text/xml
|
||||
assert_family archives org.gnome.FileRoller.desktop \
|
||||
application/zip application/x-tar application/gzip \
|
||||
application/x-7z-compressed application/vnd.rar
|
||||
|
||||
: >"$call_log"
|
||||
if $helper set-default unknown org.mozilla.firefox.desktop >/dev/null 2>&1; then
|
||||
@@ -251,4 +281,36 @@ if $helper add-autostart ../escape.desktop >/dev/null 2>&1; then
|
||||
fail 'an unsafe desktop id was accepted for autostart'
|
||||
fi
|
||||
|
||||
# Seeding fills roles nobody has chosen for, and never overrules a choice.
|
||||
#
|
||||
# The distinction matters because seeding runs on every setup: someone who has
|
||||
# deliberately pointed PNGs at an editor must not have that undone the next time
|
||||
# they re-link their dotfiles.
|
||||
: >"$call_log"
|
||||
seed_output="$($helper seed)" || fail 'seed command failed'
|
||||
rg --quiet '^images: org\.gnome\.Loupe\.desktop$' <<<"$seed_output" \
|
||||
|| fail 'seeding did not curate an unclaimed role'
|
||||
rg --quiet '^documents: org\.gnome\.Papers\.desktop$' <<<"$seed_output" \
|
||||
|| fail 'seeding did not curate documents'
|
||||
rg --quiet '^music: no preferred application installed' <<<"$seed_output" \
|
||||
|| fail 'seeding must leave a role alone when none of its candidates is installed'
|
||||
rg --quiet '^default$' "$call_log" \
|
||||
|| fail 'seeding never reached xdg-mime'
|
||||
|
||||
# A recorded choice wins, and only that role is skipped.
|
||||
cat >"$config_home/mimeapps.list" <<'MIMEAPPS'
|
||||
[Default Applications]
|
||||
image/png=org.gnome.Totem.desktop
|
||||
MIMEAPPS
|
||||
: >"$call_log"
|
||||
seed_output="$($helper seed)" || fail 'seed command failed on a machine with recorded choices'
|
||||
rg --quiet '^images: keeping the existing choice$' <<<"$seed_output" \
|
||||
|| fail 'seeding overruled a choice the user had already made'
|
||||
rg --quiet '^documents: org\.gnome\.Papers\.desktop$' <<<"$seed_output" \
|
||||
|| fail 'one recorded choice suppressed an unrelated role'
|
||||
rg --quiet 'image/png' "$call_log" \
|
||||
&& fail 'seeding rewrote a type the user had already chosen for'
|
||||
|
||||
rm -f "$config_home/mimeapps.list"
|
||||
|
||||
printf 'default apps contract: PASS\n'
|
||||
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# A default-application role owns a FAMILY of types, and every type in it must
|
||||
# agree.
|
||||
#
|
||||
# Each role used to map to a single representative type, so setting "images"
|
||||
# changed image/png and left image/jpeg wherever it happened to land. That is
|
||||
# how this desktop ended up opening PNGs in a pixel-art editor, MP3s in a video
|
||||
# transcoder and PDFs in an image editor -- nobody chose any of it, the
|
||||
# applications registered themselves and the roles governed one type each.
|
||||
#
|
||||
# The failure is invisible until someone double-clicks a file, which is the
|
||||
# worst possible moment to discover it.
|
||||
#
|
||||
# Read-only: this inspects the schema of the roles and the state of the machine.
|
||||
# It never changes a default, because doing so on the daily driver would be
|
||||
# rude and the point is to detect drift, not create it.
|
||||
|
||||
set -uo pipefail
|
||||
|
||||
repo_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
|
||||
helper="$repo_dir/config/dot/quickshell/scripts/panama-default-apps"
|
||||
|
||||
fail() {
|
||||
printf 'default apps family contract: %s\n' "$1" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
[[ -x "$helper" ]] || fail 'panama-default-apps is missing or not executable'
|
||||
|
||||
# The role table, read from the helper itself so the two cannot disagree.
|
||||
families="$(python3 - "$helper" <<'PY'
|
||||
import ast, re, sys
|
||||
source = open(sys.argv[1]).read()
|
||||
match = re.search(r"ROLE_TARGETS = (\{.*?\n\})", source, re.S)
|
||||
if not match:
|
||||
raise SystemExit("ROLE_TARGETS not found")
|
||||
table = ast.literal_eval(match.group(1))
|
||||
for role, (kind, targets) in table.items():
|
||||
if kind != "mime":
|
||||
continue
|
||||
print(role + "\t" + ",".join(targets))
|
||||
PY
|
||||
)" || fail 'could not read ROLE_TARGETS from the helper'
|
||||
|
||||
[[ -n "$families" ]] || fail 'no mime-backed roles found'
|
||||
|
||||
command -v xdg-mime >/dev/null 2>&1 || { printf 'default apps family contract: SKIP (no xdg-mime)\n'; exit 0; }
|
||||
|
||||
checked=0
|
||||
while IFS=$'\t' read -r role types; do
|
||||
[[ -n "$role" ]] || continue
|
||||
|
||||
first=""
|
||||
disagreeing=""
|
||||
for mime in ${types//,/ }; do
|
||||
handler="$(xdg-mime query default "$mime" 2>/dev/null)"
|
||||
# A type nothing claims is not drift; it is simply unclaimed, and
|
||||
# forcing a handler for every exotic type is not this role's job.
|
||||
[[ -n "$handler" ]] || continue
|
||||
if [[ -z "$first" ]]; then
|
||||
first="$handler"
|
||||
continue
|
||||
fi
|
||||
[[ "$handler" == "$first" ]] || disagreeing+="$mime->$handler "
|
||||
done
|
||||
|
||||
[[ -z "$disagreeing" ]] \
|
||||
|| fail "the \"$role\" role is split: its first type opens with $first but $disagreeing-- setting the role must write every type in the family"
|
||||
|
||||
checked=$((checked + 1))
|
||||
done <<<"$families"
|
||||
|
||||
# Every role the helper can set must be reachable from the settings page, and
|
||||
# every row on the page must name a role the helper knows. A role that exists
|
||||
# only in the helper is unreachable from the UI; a row naming a role the helper
|
||||
# does not have does nothing when someone taps it.
|
||||
page="$repo_dir/config/dot/quickshell/modules/settings/ApplicationsPage.qml"
|
||||
[[ -r "$page" ]] || fail 'ApplicationsPage.qml is missing'
|
||||
|
||||
helper_roles="$(python3 - "$helper" <<'ROLES'
|
||||
import ast, re, sys
|
||||
source = open(sys.argv[1]).read()
|
||||
table = ast.literal_eval(re.search(r"ROLE_TARGETS = (\{.*?\n\})", source, re.S).group(1))
|
||||
print("\n".join(sorted(table)))
|
||||
ROLES
|
||||
)"
|
||||
page_roles="$(grep -o 'key: "[a-z]*"' "$page" | sed 's/key: "//; s/"//' | sort -u)"
|
||||
|
||||
missing="$(comm -23 <(printf '%s\n' "$helper_roles") <(printf '%s\n' "$page_roles") | tr '\n' ' ')"
|
||||
extra="$(comm -13 <(printf '%s\n' "$helper_roles") <(printf '%s\n' "$page_roles") | tr '\n' ' ')"
|
||||
[[ -z "${missing// }" ]] || fail "the helper can set these roles but the settings page never offers them: $missing"
|
||||
[[ -z "${extra// }" ]] || fail "the settings page offers roles the helper cannot set: $extra"
|
||||
|
||||
# The editor Panama ships must launch in Panama's terminal. The stock
|
||||
# nvim.desktop sets Terminal=true, which hands the launch to whatever the
|
||||
# system considers default -- not necessarily the terminal this desktop themes.
|
||||
entry="$repo_dir/config/local/share/applications/panama-nvim.desktop"
|
||||
[[ -r "$entry" ]] || fail 'panama-nvim.desktop is missing'
|
||||
grep -q '^Exec=kitty ' "$entry" || fail 'the shipped editor entry does not launch kitty explicitly'
|
||||
grep -q '^Terminal=false' "$entry" \
|
||||
|| fail 'the editor entry sets Terminal=true, which defers to the system terminal rather than kitty'
|
||||
|
||||
printf 'default apps family contract: PASS (%d role families consistent)\n' "$checked"
|
||||
Reference in New Issue
Block a user