diff --git a/bin/omarchy-default-browser b/bin/omarchy-default-browser index b6bbf8cbcf..a76e1ad175 100755 --- a/bin/omarchy-default-browser +++ b/bin/omarchy-default-browser @@ -32,9 +32,14 @@ zen) desktop_id="zen.desktop"; name="Zen"; glyph="󰖟" ;; ;; esac +prev="$(xdg-settings get default-web-browser)" + xdg-settings set default-web-browser "$desktop_id" xdg-mime default "$desktop_id" x-scheme-handler/http xdg-mime default "$desktop_id" x-scheme-handler/https xdg-mime default "$desktop_id" text/html +# A different browser may have different profiles, so drop remembered webapp selections. +[[ $prev != "$desktop_id" ]] && omarchy-webapp-profile-store clear-all + notify-send -u low "$glyph $name is now the default browser" diff --git a/bin/omarchy-launch-webapp b/bin/omarchy-launch-webapp index 6ee0f84a25..ecea185125 100755 --- a/bin/omarchy-launch-webapp +++ b/bin/omarchy-launch-webapp @@ -1,7 +1,7 @@ #!/bin/bash # omarchy:summary=Launch a URL as a web app in the default supported browser -# omarchy:args= +# omarchy:args= [extra-flags...] browser=$(xdg-settings get default-web-browser) @@ -10,4 +10,54 @@ google-chrome* | brave* | microsoft-edge* | opera* | vivaldi* | helium*) ;; *) browser="chromium.desktop" ;; esac -exec setsid uwsm-app -- $(sed -n 's/^Exec=\([^ ]*\).*/\1/p' {~/.local,~/.nix-profile,/usr}/share/applications/$browser 2>/dev/null | head -1) --app="$1" "${@:2}" +browser_exec=$(sed -n 's/^Exec=\([^ ]*\).*/\1/p' {~/.local,~/.nix-profile,/usr}/share/applications/$browser 2>/dev/null | head -1) + +url="$1" +shift +remember_key="${OMARCHY_WEBAPP_REMEMBER_KEY:-$url}" +profile_arg=() + +# Build parallel arrays of profile dir + display name for the effective browser. +dirs=() +names=() +while IFS=$'\t' read -r d n; do + [[ -n $d ]] && dirs+=("$d") && names+=("$n") +done < <(omarchy-webapp-profiles) + +# Only offer a profile choice when there is more than one to choose from. +if ((${#dirs[@]} > 1)); then + chosen_dir="" + + # Use a remembered profile if it still exists. + remembered="$(omarchy-webapp-profile-store get "$remember_key")" + if [[ -n $remembered ]]; then + for d in "${dirs[@]}"; do [[ $d == "$remembered" ]] && chosen_dir="$d" && break; done + fi + + # Otherwise ask, listing each profile (use once) plus an "Always use…" variant (remember). + if [[ -z $chosen_dir ]]; then + options=("${names[@]}" "──────────") + for n in "${names[@]}"; do options+=("Always use $n"); done + + picked="$(omarchy-menu-select "Choose profile" "${options[@]}")" + [[ -z $picked || $picked == "──────────" ]] && exit 0 + + remember=false + sel="$picked" + if [[ $picked == "Always use "* ]]; then + remember=true + sel="${picked#Always use }" + fi + + for i in "${!names[@]}"; do + [[ ${names[$i]} == "$sel" ]] && chosen_dir="${dirs[$i]}" && break + done + [[ -z $chosen_dir ]] && exit 0 + + $remember && omarchy-webapp-profile-store set "$remember_key" "$chosen_dir" + fi + + profile_arg=(--profile-directory="$chosen_dir") +fi + +exec setsid uwsm-app -- "$browser_exec" "${profile_arg[@]}" --app="$url" "$@" diff --git a/bin/omarchy-webapp-handler-hey b/bin/omarchy-webapp-handler-hey index b7c773d684..81f8f25b85 100755 --- a/bin/omarchy-webapp-handler-hey +++ b/bin/omarchy-webapp-handler-hey @@ -12,4 +12,7 @@ if [[ $url =~ ^mailto: ]]; then web_url="https://app.hey.com/messages/new?to=$email" fi +# Keep the remembered profile stable across the dynamic mailto: URLs. +export OMARCHY_WEBAPP_REMEMBER_KEY="https://app.hey.com" + exec omarchy-launch-webapp "$web_url" diff --git a/bin/omarchy-webapp-handler-zoom b/bin/omarchy-webapp-handler-zoom index 6692041f0b..02eb6f3955 100755 --- a/bin/omarchy-webapp-handler-zoom +++ b/bin/omarchy-webapp-handler-zoom @@ -20,4 +20,7 @@ if [[ $url =~ ^zoom(mtg|us):// ]]; then fi fi +# Keep the remembered profile stable across the dynamic meeting URLs. +export OMARCHY_WEBAPP_REMEMBER_KEY="https://app.zoom.us/wc/home" + exec omarchy-launch-webapp "$web_url" diff --git a/bin/omarchy-webapp-install b/bin/omarchy-webapp-install index dbf0060979..046ec035e4 100755 --- a/bin/omarchy-webapp-install +++ b/bin/omarchy-webapp-install @@ -24,6 +24,23 @@ if (( $# < 3 )); then ICON_REF=$(gum input --prompt "Icon URL> " --placeholder "Could not fetch favicon automatically. Enter PNG icon URL (see https://dashboardicons.com)") fi + # Offer a target browser profile when the effective browser has more than one. + PROFILE_DIRS=() + PROFILE_NAMES=() + while IFS=$'\t' read -r d n; do + [[ -n $d ]] && PROFILE_DIRS+=("$d") && PROFILE_NAMES+=("$n") + done < <(omarchy-webapp-profiles) + + if ((${#PROFILE_DIRS[@]} > 1)); then + CHOICE=$(gum choose --header "Which browser profile?" "Ask me each time" "${PROFILE_NAMES[@]}") + if [[ -n $CHOICE && $CHOICE != "Ask me each time" ]]; then + for i in "${!PROFILE_NAMES[@]}"; do + [[ ${PROFILE_NAMES[$i]} == "$CHOICE" ]] && + omarchy-webapp-profile-store set "$APP_URL" "${PROFILE_DIRS[$i]}" && break + done + fi + fi + CUSTOM_EXEC="" MIME_TYPES="" INTERACTIVE_MODE=true diff --git a/bin/omarchy-webapp-profile-store b/bin/omarchy-webapp-profile-store new file mode 100755 index 0000000000..52d450c5d5 --- /dev/null +++ b/bin/omarchy-webapp-profile-store @@ -0,0 +1,28 @@ +#!/bin/bash + +# omarchy:summary=Read/write the remembered Chromium profile for a webapp +# omarchy:args= [key] [dirname] + +DIR="$HOME/.config/omarchy/webapps/profiles" +STAMP="$HOME/.config/omarchy/webapps/profiles-browser" + +key_for() { printf '%s' "$1" | sha256sum | cut -d' ' -f1; } + +cmd="$1" +shift + +case "$cmd" in +get) # get -> dirname or nothing + [[ -f $STAMP && "$(cat "$STAMP")" == "$(omarchy-webapp-profiles --browser)" ]] || exit 0 + f="$DIR/$(key_for "$1")" + [[ -f $f ]] && cat "$f" + ;; +set) # set + mkdir -p "$DIR" + printf '%s' "$2" >"$DIR/$(key_for "$1")" + omarchy-webapp-profiles --browser >"$STAMP" + ;; +clear-all) + rm -rf "$DIR" "$STAMP" + ;; +esac diff --git a/bin/omarchy-webapp-profiles b/bin/omarchy-webapp-profiles new file mode 100755 index 0000000000..cf84d4b93c --- /dev/null +++ b/bin/omarchy-webapp-profiles @@ -0,0 +1,38 @@ +#!/bin/bash + +# omarchy:summary=List Chromium profiles (dirnamename) for the effective webapp browser +# omarchy:args=[--browser] + +effective_browser() { + case "$(omarchy-default-browser)" in + chromium | chrome | brave | brave-origin | edge | opera | vivaldi | helium) omarchy-default-browser ;; + *) echo "chromium" ;; # firefox/zen/unknown → webapps fall back to chromium + esac +} + +config_dir_for() { + case "$1" in + chromium) echo "$HOME/.config/chromium" ;; + chrome) echo "$HOME/.config/google-chrome" ;; + brave) echo "$HOME/.config/BraveSoftware/Brave-Browser" ;; + brave-origin) echo "$HOME/.config/BraveSoftware/Brave-Browser-Origin-Beta" ;; + edge) echo "$HOME/.config/microsoft-edge" ;; + opera) echo "$HOME/.config/opera" ;; + vivaldi) echo "$HOME/.config/vivaldi" ;; + helium) echo "$HOME/.config/net.imput.helium" ;; + *) echo "" ;; + esac +} + +browser="$(effective_browser)" +[[ ${1:-} == "--browser" ]] && { + echo "$browser" + exit 0 +} + +cfg="$(config_dir_for "$browser")" +[[ -n $cfg && -f "$cfg/Local State" ]] || exit 0 +command -v jq >/dev/null || exit 0 + +jq -r '(.profile.info_cache // {}) | to_entries[] | "\(.key)\t\(.value.name // .key)"' \ + "$cfg/Local State" 2>/dev/null | sort diff --git a/migrations/1781657000.sh b/migrations/1781657000.sh new file mode 100644 index 0000000000..c83f21c1ae --- /dev/null +++ b/migrations/1781657000.sh @@ -0,0 +1,4 @@ +echo "Webapps can now launch in a chosen browser profile" +echo "With multiple profiles, you'll be asked which to use the next time a webapp launches" + +# Nothing to backfill: profile selections live outside .desktop files and are created on demand.