diff --git a/bin/omarchy-file-select b/bin/omarchy-file-select index 7228071b5d20..8330bb012670 100755 --- a/bin/omarchy-file-select +++ b/bin/omarchy-file-select @@ -14,6 +14,8 @@ # bash has no way to hold one. import argparse +import fcntl +import hashlib import os import subprocess import sys @@ -33,6 +35,56 @@ EXIT_NOTHING_PICKED = 1 EXIT_CHOOSER_FAILED = 2 +LEGACY_FILE_CHOOSER = "org.freedesktop.impl.portal.FileChooser=folio;gtk" + + +# Files' portal chooser does not answer Cancel, so the portal must be asked +# through the GTK chooser. A portal config the folio switch left behind (a +# fresh install seeded from /etc/skel, an install whose migration has not +# run) still names folio: name gtk instead, and restart the portal so the +# change is picked up before the request below. +def use_gtk_chooser(): + config_dir = os.environ.get("XDG_CONFIG_HOME") or os.path.expanduser("~/.config") + config = os.path.join(config_dir, "xdg-desktop-portal", "hyprland-portals.conf") + if not os.path.exists(config): + return + + real_config = os.path.realpath(config) + lock_dir = os.environ.get("XDG_RUNTIME_DIR") or os.path.dirname(real_config) + lock_key = hashlib.sha256(real_config.encode()).hexdigest()[:16] + lock_path = os.path.join(lock_dir, ".omarchy-file-select-%s.lock" % lock_key) + try: + lock_fd = os.open(lock_path, os.O_CREAT | os.O_RDWR | os.O_NOFOLLOW, 0o600) + except OSError: + return + + try: + fcntl.flock(lock_fd, fcntl.LOCK_EX) + with open(config) as config_file: + lines = config_file.readlines() + + updated = [ + "org.freedesktop.impl.portal.FileChooser=gtk\n" + if line.rstrip("\r\n") == LEGACY_FILE_CHOOSER + else line + for line in lines + ] + if updated == lines: + return + + with open(config, "w") as config_file: + config_file.writelines(updated) + + subprocess.run( + ["systemctl", "--user", "restart", "xdg-desktop-portal.service"], + capture_output=True, + ) + except OSError: + return + finally: + os.close(lock_fd) + + # The dialog's own title when the caller names none, in the Omarchy language. def default_title(): try: @@ -59,6 +111,8 @@ def main(): if args.title is None: args.title = default_title() + use_gtk_chooser() + bus = Gio.bus_get_sync(Gio.BusType.SESSION, None) loop = GLib.MainLoop() uris = [] diff --git a/config/xdg-desktop-portal/hyprland-portals.conf b/config/xdg-desktop-portal/hyprland-portals.conf index c8fbbd45be8e..fd802cbd7afe 100644 --- a/config/xdg-desktop-portal/hyprland-portals.conf +++ b/config/xdg-desktop-portal/hyprland-portals.conf @@ -1,4 +1,3 @@ [preferred] default=hyprland;gtk -org.freedesktop.impl.portal.FileChooser=folio;gtk org.freedesktop.impl.portal.Secret=gnome-keyring diff --git a/migrations/1794861758.sh b/migrations/1794861758.sh new file mode 100644 index 000000000000..2569fe628296 --- /dev/null +++ b/migrations/1794861758.sh @@ -0,0 +1,24 @@ +echo "Use GTK for other apps' Open and Save dialogs" + +portal_config="${XDG_CONFIG_HOME:-$HOME/.config}/xdg-desktop-portal/hyprland-portals.conf" + +# Only undo the Files choice migration 1794775358 made: a user's own file +# chooser choice stays as it is. +if [[ ! -f $portal_config ]]; then + echo "No Omarchy portal config, skipping..." + exit 0 +fi +if ! grep -qx 'org\.freedesktop\.impl\.portal\.FileChooser=folio;gtk' "$portal_config"; then + echo "File chooser not set to Files, skipping..." + exit 0 +fi + +# Files' chooser does not answer Cancel, so the GTK chooser answers again. +sed --follow-symlinks -i '/^org\.freedesktop\.impl\.portal\.FileChooser=folio;gtk$/d' "$portal_config" + +# Activate it immediately in a live desktop +if systemctl --user is-active --quiet xdg-desktop-portal.service 2>/dev/null; then + if ! systemctl --user restart xdg-desktop-portal.service; then + echo "Failed to restart xdg-desktop-portal" >&2 + fi +fi diff --git a/test/shell.d/file-select-test.sh b/test/shell.d/file-select-test.sh new file mode 100755 index 000000000000..f74fdc9c9b93 --- /dev/null +++ b/test/shell.d/file-select-test.sh @@ -0,0 +1,131 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +require_command python3 + +test_tmp=$(mktemp -d) +trap 'rm -rf "$test_tmp"' EXIT + +mock_bin="$test_tmp/bin" +runtime_dir="$test_tmp/run" +mkdir -p "$mock_bin" "$runtime_dir" + +cat >"$mock_bin/systemctl" <<'STUB' +#!/bin/bash +printf 'start %s\n' "$*" >>"$TEST_SYSTEMCTL_LOG" +if [[ -n ${TEST_SYSTEMCTL_SLEEP:-} ]]; then + sleep "$TEST_SYSTEMCTL_SLEEP" +fi +printf 'done %s\n' "$*" >>"$TEST_SYSTEMCTL_LOG" +STUB +chmod +x "$mock_bin/systemctl" + +run_use_gtk_chooser() { + local config_home="$1" + local log="$2" + local delay="${3:-}" + XDG_CONFIG_HOME="$config_home" XDG_RUNTIME_DIR="$runtime_dir" \ + PATH="$mock_bin:$PATH" TEST_SYSTEMCTL_LOG="$log" TEST_SYSTEMCTL_SLEEP="$delay" \ + python3 - "$ROOT/bin/omarchy-file-select" <<'PY' +import importlib.machinery +import importlib.util +import sys + +loader = importlib.machinery.SourceFileLoader("omarchy_file_select", sys.argv[1]) +spec = importlib.util.spec_from_loader("omarchy_file_select", loader) +module = importlib.util.module_from_spec(spec) +loader.exec_module(module) +module.use_gtk_chooser() +PY +} + +# 1. Commented legacy setting stays commented and does not restart the portal. +cfg="$test_tmp/cfg-comment" +log="$test_tmp/log-comment" +mkdir -p "$cfg/xdg-desktop-portal" +: >"$log" +cat >"$cfg/xdg-desktop-portal/hyprland-portals.conf" <<'CONF' +[preferred] +default=hyprland;gtk +# org.freedesktop.impl.portal.FileChooser=folio;gtk +org.freedesktop.impl.portal.Secret=gnome-keyring +CONF +expected=$(<"$cfg/xdg-desktop-portal/hyprland-portals.conf") +run_use_gtk_chooser "$cfg" "$log" +[[ $(<"$cfg/xdg-desktop-portal/hyprland-portals.conf") == "$expected" ]] || + fail "commented FileChooser line must stay untouched" "$(<"$cfg/xdg-desktop-portal/hyprland-portals.conf")" +[[ ! -s $log ]] || fail "commented FileChooser line must not restart the portal" "$(<"$log")" +pass "commented legacy FileChooser line stays commented without restarting the portal" + +# 2. Custom FileChooser=folio setting is untouched and does not restart the portal. +cfg="$test_tmp/cfg-custom" +log="$test_tmp/log-custom" +mkdir -p "$cfg/xdg-desktop-portal" +: >"$log" +cat >"$cfg/xdg-desktop-portal/hyprland-portals.conf" <<'CONF' +[preferred] +default=hyprland;gtk +org.freedesktop.impl.portal.FileChooser=folio +CONF +expected=$(<"$cfg/xdg-desktop-portal/hyprland-portals.conf") +run_use_gtk_chooser "$cfg" "$log" +[[ $(<"$cfg/xdg-desktop-portal/hyprland-portals.conf") == "$expected" ]] || + fail "custom FileChooser=folio line must stay untouched" "$(<"$cfg/xdg-desktop-portal/hyprland-portals.conf")" +[[ ! -s $log ]] || fail "custom FileChooser=folio line must not restart the portal" "$(<"$log")" +pass "custom FileChooser=folio line stays untouched without restarting the portal" + +# 3. Exact uncommented legacy line is rewritten to gtk and restarts the portal once. +cfg="$test_tmp/cfg-legacy" +log="$test_tmp/log-legacy" +mkdir -p "$cfg/xdg-desktop-portal" +: >"$log" +cat >"$cfg/xdg-desktop-portal/hyprland-portals.conf" <<'CONF' +[preferred] +default=hyprland;gtk +org.freedesktop.impl.portal.FileChooser=folio;gtk +org.freedesktop.impl.portal.Secret=gnome-keyring +CONF +run_use_gtk_chooser "$cfg" "$log" +grep -qx 'org\.freedesktop\.impl\.portal\.FileChooser=gtk' "$cfg/xdg-desktop-portal/hyprland-portals.conf" || + fail "legacy FileChooser line is updated to gtk" "$(<"$cfg/xdg-desktop-portal/hyprland-portals.conf")" +(( $(grep -c '^start --user restart xdg-desktop-portal.service$' "$log") == 1 )) || + fail "legacy FileChooser repair restarts the portal once" "$(<"$log")" +run_use_gtk_chooser "$cfg" "$log" +(( $(grep -c '^start --user restart xdg-desktop-portal.service$' "$log") == 1 )) || + fail "subsequent invocation does not restart the portal again" "$(<"$log")" +pass "exact uncommented legacy line is rewritten to gtk and restarts the portal once" + +# 4. Concurrent pickers serialize under the lock, restart only once, and wait until restart finishes. +cfg="$test_tmp/cfg-concurrent" +log="$test_tmp/log-concurrent" +mkdir -p "$cfg/xdg-desktop-portal" +: >"$log" +cat >"$cfg/xdg-desktop-portal/hyprland-portals.conf" <<'CONF' +[preferred] +default=hyprland;gtk +org.freedesktop.impl.portal.FileChooser=folio;gtk +CONF + +( + run_use_gtk_chooser "$cfg" "$log" 0.3 + printf 'picker-done\n' >>"$log" +) & +p1=$! +( + run_use_gtk_chooser "$cfg" "$log" 0.3 + printf 'picker-done\n' >>"$log" +) & +p2=$! +wait "$p1" "$p2" + +(( $(grep -c '^start --user restart xdg-desktop-portal.service$' "$log") == 1 )) || + fail "concurrent pickers restart the portal only once" "$(<"$log")" +# Both pickers must finish only after the single restart completed. +first_done_line=$(grep -n '^done --user restart xdg-desktop-portal.service$' "$log" | cut -d: -f1) +first_picker_line=$(grep -n '^picker-done$' "$log" | head -n 1 | cut -d: -f1) +(( first_done_line < first_picker_line )) || + fail "second concurrent picker must wait until the portal restart completes" "$(<"$log")" +pass "concurrent pickers serialize repair, restart once, and wait for restart completion"