App workspaces disappear when empty
Browse files at this commit182dcbce0c40497e8d7a0b3e9e8639bfdf9d7092Parent: b4b2a3c- feat
- app workspaces disappear when empty
4 changed files +184 -49
- .config/niri/config.kdl +14 -20
- .config/scripts/niri/app_workspaces.sh +21 -0
- .config/scripts/niri/workspace_app.sh +55 -29
- .config/scripts/niri/workspace_reaper.sh +94 -0
.config/niri/config.kdlmodified+14 -20
| @@ -109,6 +109,7 @@ environment { | |||
| 109 | // starts them. Hyprland ignored that target, which is why it needed exec-once. | 109 | // starts them. Hyprland ignored that target, which is why it needed exec-once. |
| 110 | // The sleeps preserve Hyprland's ordering (tray applets after waybar). | 110 | // The sleeps preserve Hyprland's ordering (tray applets after waybar). |
| 111 | spawn-at-startup "sh" "-c" "bash ~/.config/scripts/niri/wallpaper_restore.sh" | 111 | spawn-at-startup "sh" "-c" "bash ~/.config/scripts/niri/wallpaper_restore.sh" |
| 112 | +spawn-at-startup "sh" "-c" "bash ~/.config/scripts/niri/workspace_reaper.sh" | ||
| 112 | spawn-at-startup "sh" "-c" "bash ~/.config/scripts/shared/song_detail_writer.sh" | 113 | spawn-at-startup "sh" "-c" "bash ~/.config/scripts/shared/song_detail_writer.sh" |
| 113 | // waybar needs its own config here: the shared one uses hyprland/workspaces | 114 | // waybar needs its own config here: the shared one uses hyprland/workspaces |
| 114 | // and hyprland/window, which do nothing under niri. config-niri swaps those | 115 | // and hyprland/window, which do nothing under niri. config-niri swaps those |
| @@ -149,18 +150,11 @@ window-rule { | |||
| 149 | open-floating true | 150 | open-floating true |
| 150 | } | 151 | } |
| 151 | 152 | ||
| 152 | -// App workspaces, replacing Hyprland's 100/200-206. Reachable ONLY by their | 153 | +// The app workspaces (code, github, ...) are deliberately NOT declared here. |
| 153 | -// Alt+<letter> binds: number, direction and wheel navigation all go through | 154 | +// A declared `workspace "name"` node is immortal, which left eight empty |
| 154 | -// workspace_nav.sh, which skips named workspaces entirely. Each one homes itself | 155 | +// workspaces lying around. workspace_app.sh names a fresh workspace when it |
| 155 | -// on the monitor of its first launch (see workspace_app.sh). | 156 | +// launches the app, and workspace_reaper.sh un-names it once it empties so niri |
| 156 | -workspace "code" | 157 | +// reaps it. Names live in ~/.config/scripts/niri/app_workspaces.sh. |
| 157 | -workspace "github" | ||
| 158 | -workspace "whatsapp" | ||
| 159 | -workspace "discord" | ||
| 160 | -workspace "deezer" | ||
| 161 | -workspace "youtube" | ||
| 162 | -workspace "mail" | ||
| 163 | -workspace "signal" | ||
| 164 | 158 | ||
| 165 | binds { | 159 | binds { |
| 166 | // ─── launchers (Alt trigger, Hyprland $my_trigger) ─────────────────────── | 160 | // ─── launchers (Alt trigger, Hyprland $my_trigger) ─────────────────────── |
| @@ -194,14 +188,14 @@ binds { | |||
| 194 | Alt+P hotkey-overlay-title="Workspace: mail" { spawn-sh "~/.config/scripts/niri/workspace_app.sh mail ~/.config/scripts/shared/webapp.sh https://mail.proton.me/ eIpzVtL8"; } | 188 | Alt+P hotkey-overlay-title="Workspace: mail" { spawn-sh "~/.config/scripts/niri/workspace_app.sh mail ~/.config/scripts/shared/webapp.sh https://mail.proton.me/ eIpzVtL8"; } |
| 195 | Alt+N hotkey-overlay-title="Workspace: signal" { spawn-sh "~/.config/scripts/niri/workspace_app.sh signal flatpak run org.signal.Signal"; } | 189 | Alt+N hotkey-overlay-title="Workspace: signal" { spawn-sh "~/.config/scripts/niri/workspace_app.sh signal flatpak run org.signal.Signal"; } |
| 196 | 190 | ||
| 197 | - Mod+Shift+C { move-column-to-workspace "code"; } | 191 | + Mod+Shift+C { spawn-sh "~/.config/scripts/niri/workspace_app.sh --move code"; } |
| 198 | - Mod+Shift+G { move-column-to-workspace "github"; } | 192 | + Mod+Shift+G { spawn-sh "~/.config/scripts/niri/workspace_app.sh --move github"; } |
| 199 | - Mod+Shift+W { move-column-to-workspace "whatsapp"; } | 193 | + Mod+Shift+W { spawn-sh "~/.config/scripts/niri/workspace_app.sh --move whatsapp"; } |
| 200 | - Mod+Shift+D { move-column-to-workspace "discord"; } | 194 | + Mod+Shift+D { spawn-sh "~/.config/scripts/niri/workspace_app.sh --move discord"; } |
| 201 | - Mod+Shift+S { move-column-to-workspace "deezer"; } | 195 | + Mod+Shift+S { spawn-sh "~/.config/scripts/niri/workspace_app.sh --move deezer"; } |
| 202 | - Mod+Shift+Y { move-column-to-workspace "youtube"; } | 196 | + Mod+Shift+Y { spawn-sh "~/.config/scripts/niri/workspace_app.sh --move youtube"; } |
| 203 | - Mod+Shift+P { move-column-to-workspace "mail"; } | 197 | + Mod+Shift+P { spawn-sh "~/.config/scripts/niri/workspace_app.sh --move mail"; } |
| 204 | - Mod+Shift+N { move-column-to-workspace "signal"; } | 198 | + Mod+Shift+N { spawn-sh "~/.config/scripts/niri/workspace_app.sh --move signal"; } |
| 205 | 199 | ||
| 206 | // ─── session / system ─────────────────────────────────────────────────── | 200 | // ─── session / system ─────────────────────────────────────────────────── |
| 207 | // hyprlock works here: ext-session-lock-v1, no Hyprland IPC dependency. | 201 | // hyprlock works here: ext-session-lock-v1, no Hyprland IPC dependency. |
.config/scripts/niri/app_workspaces.shadded+21 -0
| @@ -0,0 +1,21 @@ | |||
| 1 | +# Names of the app workspaces, sourced by the niri workspace scripts. | ||
| 2 | +# Not executable on purpose. | ||
| 3 | +# | ||
| 4 | +# These are NOT declared in config.kdl: a declared `workspace "name"` node makes | ||
| 5 | +# niri keep that workspace alive forever, which left eight empty workspaces | ||
| 6 | +# sitting around. Instead workspace_app.sh names a fresh workspace on launch and | ||
| 7 | +# workspace_reaper.sh un-names it once it empties, letting niri reap it. | ||
| 8 | +# | ||
| 9 | +# Only names in this list are ever un-named, so a workspace you name by hand is | ||
| 10 | +# left alone. | ||
| 11 | + | ||
| 12 | +APP_WORKSPACES=(code github whatsapp discord deezer youtube mail signal) | ||
| 13 | + | ||
| 14 | +# True if $1 is one of the app workspaces. | ||
| 15 | +is_app_workspace() { | ||
| 16 | + local candidate="$1" name | ||
| 17 | + for name in "${APP_WORKSPACES[@]}"; do | ||
| 18 | + [ "$name" = "$candidate" ] && return 0 | ||
| 19 | + done | ||
| 20 | + return 1 | ||
| 21 | +} | ||
.config/scripts/niri/workspace_app.shmodified+55 -29
| @@ -2,48 +2,74 @@ | |||
| 2 | # Emulates Hyprland's `workspace = N, on-created-empty: <cmd>` for niri. | 2 | # Emulates Hyprland's `workspace = N, on-created-empty: <cmd>` for niri. |
| 3 | # | 3 | # |
| 4 | # usage: workspace_app.sh <workspace-name> <command> [args...] | 4 | # usage: workspace_app.sh <workspace-name> <command> [args...] |
| 5 | +# workspace_app.sh --move <workspace-name> | ||
| 5 | # | 6 | # |
| 6 | -# Focuses the named workspace, then launches the command only if that workspace | 7 | +# Behaviour: |
| 7 | -# currently holds no windows. niri has no on-created-empty, and named workspaces | 8 | +# * workspace already exists (app is running) -> just focus it. It stays on the |
| 8 | -# declared in the config are persistent (never "created"), so the emptiness check | 9 | +# monitor it was launched on; the key moves FOCUS there rather than dragging |
| 9 | -# is what makes this lazy instead of duplicating apps. | 10 | +# the workspace to the monitor you happen to be on. |
| 11 | +# * workspace does not exist -> take the trailing empty workspace of the | ||
| 12 | +# focused monitor, name it, and launch the app there. | ||
| 10 | # | 13 | # |
| 11 | -# An app's workspace sticks to the monitor it was first launched on. Pressing the | 14 | +# The workspaces are deliberately NOT declared in config.kdl: a declared |
| 12 | -# key again moves FOCUS there rather than pulling the workspace to the monitor | 15 | +# `workspace "name"` node is immortal, so eight empty workspaces would always be |
| 13 | -# you happen to be on. | 16 | +# present. Naming happens here and workspace_reaper.sh un-names an app workspace |
| 17 | +# once it empties, after which niri reaps it. | ||
| 14 | set -euo pipefail | 18 | set -euo pipefail |
| 15 | 19 | ||
| 16 | -ws="$1" | 20 | +. "$(dirname "$(realpath "$0")")/app_workspaces.sh" |
| 17 | -shift | 21 | + |
| 22 | +mode="focus" | ||
| 23 | +if [ "${1:-}" = "--move" ]; then | ||
| 24 | + mode="move" | ||
| 25 | + shift | ||
| 26 | +fi | ||
| 27 | + | ||
| 28 | +ws="${1:?usage: workspace_app.sh [--move] <workspace-name> [command...]}" | ||
| 29 | +shift || true | ||
| 18 | 30 | ||
| 19 | ws_json=$(niri msg -j workspaces) | 31 | ws_json=$(niri msg -j workspaces) |
| 32 | +exists=$(jq -r --arg w "$ws" '[.[] | select(.name == $w)] | length' <<<"$ws_json") | ||
| 20 | 33 | ||
| 21 | -id=$(jq -r --arg w "$ws" '.[] | select(.name == $w) | .id' <<<"$ws_json") | 34 | +if [ "$exists" = "0" ]; then |
| 35 | + # Claim the last empty unnamed workspace on this monitor. niri always keeps | ||
| 36 | + # one at the end of every output, so there is something to claim. | ||
| 37 | + output=$(jq -r '.[] | select(.is_focused == true) | .output' <<<"$ws_json") | ||
| 38 | + win_json=$(niri msg -j windows) | ||
| 22 | 39 | ||
| 23 | -if [ -z "$id" ]; then | 40 | + idx=$(jq -r --arg o "$output" --argjson w "$win_json" ' |
| 24 | - notify-send "workspace_app" "No workspace named '$ws' — is it declared in config.kdl?" | 41 | + [ .[] |
| 25 | - exit 1 | 42 | + | select(.output == $o and .name == null) |
| 26 | -fi | 43 | + | . as $ws |
| 44 | + | select(([$w[] | select(.workspace_id == $ws.id)] | length) == 0) | ||
| 45 | + ] | last | .idx // empty' <<<"$ws_json") | ||
| 27 | 46 | ||
| 28 | -count=$(niri msg -j windows | jq --argjson id "$id" '[.[] | select(.workspace_id == $id)] | length') | 47 | + if [ -z "$idx" ]; then |
| 48 | + notify-send "workspace_app" "No empty workspace free on $output for '$ws'" | ||
| 49 | + exit 1 | ||
| 50 | + fi | ||
| 29 | 51 | ||
| 30 | -here=$(jq -r '.[] | select(.is_focused == true) | .output' <<<"$ws_json") | 52 | + # A freshly named workspace is still empty until the app maps its window, |
| 31 | -there=$(jq -r --arg w "$ws" '.[] | select(.name == $w) | .output' <<<"$ws_json") | 53 | + # and workspace_reaper.sh would un-name it in that gap. The marker tells the |
| 54 | + # reaper to leave this one alone for a while; see GRACE_SECONDS there. | ||
| 55 | + mkdir -p "${XDG_RUNTIME_DIR:-/tmp}/niri-ws-grace" | ||
| 56 | + : > "${XDG_RUNTIME_DIR:-/tmp}/niri-ws-grace/$ws" | ||
| 32 | 57 | ||
| 33 | -# The app's workspace HOME is the monitor it was first launched on, and it stays | 58 | + niri msg action set-workspace-name --workspace "$idx" "$ws" >/dev/null |
| 34 | -# there: pressing the key again just sends focus to that monitor instead of | 59 | +fi |
| 35 | -# dragging the workspace over. Only an empty (never-used) workspace gets moved, | 60 | + |
| 36 | -# so a first launch happens where you are. | 61 | +if [ "$mode" = "move" ]; then |
| 37 | -# | 62 | + niri msg action move-column-to-workspace "$ws" >/dev/null |
| 38 | -# --reference avoids a focus change for the move: focusing it on the other | 63 | + exit 0 |
| 39 | -# monitor and dragging it away afterwards leaves that monitor stranded on some | ||
| 40 | -# unrelated workspace. | ||
| 41 | -if [ "$count" -eq 0 ] && [ -n "$here" ] && [ "$here" != "$there" ]; then | ||
| 42 | - niri msg action move-workspace-to-monitor --reference "$ws" "$here" | ||
| 43 | fi | 64 | fi |
| 44 | 65 | ||
| 45 | -niri msg action focus-workspace "$ws" | 66 | +niri msg action focus-workspace "$ws" >/dev/null |
| 67 | + | ||
| 68 | +# Launch only when the workspace holds nothing, so a second press is "go there" | ||
| 69 | +# rather than a duplicate app. | ||
| 70 | +id=$(niri msg -j workspaces | jq -r --arg w "$ws" '.[] | select(.name == $w) | .id') | ||
| 71 | +count=$(niri msg -j windows | jq --argjson id "$id" '[.[] | select(.workspace_id == $id)] | length') | ||
| 46 | 72 | ||
| 47 | -if [ "$count" -eq 0 ]; then | 73 | +if [ "$count" -eq 0 ] && [ "$#" -gt 0 ]; then |
| 48 | setsid "$@" >/dev/null 2>&1 & | 74 | setsid "$@" >/dev/null 2>&1 & |
| 49 | fi | 75 | fi |
.config/scripts/niri/workspace_reaper.shadded+94 -0
| @@ -0,0 +1,94 @@ | |||
| 1 | +#!/usr/bin/env bash | ||
| 2 | +# Un-names app workspaces once they are empty, so niri reaps them. | ||
| 3 | +# | ||
| 4 | +# Runs for the whole session (spawn-at-startup in config.kdl). Follows | ||
| 5 | +# `niri msg event-stream` rather than polling, so it is idle unless windows or | ||
| 6 | +# workspaces actually change. | ||
| 7 | +# | ||
| 8 | +# Why: a named workspace is never reaped, whether the name came from a config | ||
| 9 | +# `workspace "..."` node or from set-workspace-name at runtime. The app | ||
| 10 | +# workspaces are named on launch by workspace_app.sh; without this they would | ||
| 11 | +# pile up empty exactly as the declared ones did. | ||
| 12 | +# | ||
| 13 | +# Only names listed in app_workspaces.sh are touched, so hand-named workspaces | ||
| 14 | +# survive. | ||
| 15 | +set -uo pipefail | ||
| 16 | + | ||
| 17 | +. "$(dirname "$(realpath "$0")")/app_workspaces.sh" | ||
| 18 | + | ||
| 19 | +# A workspace named by workspace_app.sh sits empty until the app maps its window, | ||
| 20 | +# and reaping it in that gap would drop the name the app was launched under. The | ||
| 21 | +# marker file it leaves means "not populated yet, hands off". | ||
| 22 | +# | ||
| 23 | +# The marker is cleared as soon as the workspace holds a window, so the NEXT time | ||
| 24 | +# it goes empty it is reaped at once. A plain timer would not do: if the app is | ||
| 25 | +# closed while the timer is still running, that close event gets skipped and no | ||
| 26 | +# further event arrives to retry. | ||
| 27 | +# | ||
| 28 | +# GRACE_SECONDS is only a backstop for a launch that never produced a window at | ||
| 29 | +# all (crash, wrong command). Flatpak cold starts are slow, hence the size. | ||
| 30 | +GRACE_SECONDS=60 | ||
| 31 | +GRACE_DIR="${XDG_RUNTIME_DIR:-/tmp}/niri-ws-grace" | ||
| 32 | + | ||
| 33 | +in_grace() { | ||
| 34 | + local marker="$GRACE_DIR/$1" age | ||
| 35 | + [ -f "$marker" ] || return 1 | ||
| 36 | + age=$(( $(date +%s) - $(stat -c %Y "$marker") )) | ||
| 37 | + if [ "$age" -lt "$GRACE_SECONDS" ]; then | ||
| 38 | + return 0 | ||
| 39 | + fi | ||
| 40 | + rm -f "$marker" | ||
| 41 | + return 1 | ||
| 42 | +} | ||
| 43 | + | ||
| 44 | +reap() { | ||
| 45 | + local ws_json win_json name | ||
| 46 | + ws_json=$(niri msg -j workspaces 2>/dev/null) || return 0 | ||
| 47 | + win_json=$(niri msg -j windows 2>/dev/null) || return 0 | ||
| 48 | + | ||
| 49 | + # Any named workspace that now holds windows has served its purpose: drop the | ||
| 50 | + # marker so emptying it later reaps immediately. | ||
| 51 | + while read -r name; do | ||
| 52 | + [ -n "$name" ] && rm -f "$GRACE_DIR/$name" | ||
| 53 | + done < <(jq -r --argjson w "$win_json" ' | ||
| 54 | + .[] | ||
| 55 | + | select(.name != null) | ||
| 56 | + | . as $ws | ||
| 57 | + | select(([$w[] | select(.workspace_id == $ws.id)] | length) > 0) | ||
| 58 | + | .name | ||
| 59 | + ' <<<"$ws_json") | ||
| 60 | + | ||
| 61 | + # Named workspaces holding no windows. | ||
| 62 | + while read -r name; do | ||
| 63 | + [ -n "$name" ] || continue | ||
| 64 | + is_app_workspace "$name" || continue | ||
| 65 | + in_grace "$name" && continue | ||
| 66 | + # The reference is POSITIONAL here. set-workspace-name takes --workspace | ||
| 67 | + # and move-workspace-to-monitor takes --reference, but this one does not. | ||
| 68 | + niri msg action unset-workspace-name "$name" >/dev/null 2>&1 || true | ||
| 69 | + done < <(jq -r --argjson w "$win_json" ' | ||
| 70 | + .[] | ||
| 71 | + | select(.name != null) | ||
| 72 | + | . as $ws | ||
| 73 | + | select(([$w[] | select(.workspace_id == $ws.id)] | length) == 0) | ||
| 74 | + | .name | ||
| 75 | + ' <<<"$ws_json") | ||
| 76 | +} | ||
| 77 | + | ||
| 78 | +# Sweep once at startup: workspaces can be left named and empty by a previous | ||
| 79 | +# session, a config reload, or a crash of this script. | ||
| 80 | +reap | ||
| 81 | + | ||
| 82 | +# Any window or workspace event. Matching loosely on purpose: niri emits | ||
| 83 | +# WindowOpenedOrChanged (singular) when a window maps and WindowsChanged (plural) | ||
| 84 | +# only for the bulk snapshot, so a narrow list missed the moment a workspace | ||
| 85 | +# became populated — which is exactly when the grace marker must be dropped. | ||
| 86 | +# Ignored by omission: ConfigLoaded, KeyboardLayoutsChanged, OverviewOpenedOrClosed, | ||
| 87 | +# CastsChanged. | ||
| 88 | +niri msg -j event-stream 2>/dev/null | while read -r line; do | ||
| 89 | + case "$line" in | ||
| 90 | + *Window* | *Workspace*) | ||
| 91 | + reap | ||
| 92 | + ;; | ||
| 93 | + esac | ||
| 94 | +done | ||