From 182dcbce0c40497e8d7a0b3e9e8639bfdf9d7092 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 30 Jul 2026 00:40:23 +0300 Subject: [PATCH] feat(niri): app workspaces disappear when empty A declared `workspace "name"` node in config.kdl is immortal, so all eight app workspaces sat there permanently even though each is reachable by its own keybind. The declarations are gone; workspaces are now named at launch and un-named once empty, after which niri reaps them. - workspace_app.sh claims the trailing empty workspace of the focused monitor, names it, and launches there. Existing workspace -> focus only, no duplicate. --move creates it on demand so Mod+Shift+ keeps working. - workspace_reaper.sh follows `niri msg event-stream` and un-names any empty app workspace. Sweeps once at startup too, for leftovers from a previous session or a config reload. - app_workspaces.sh holds the name list, so only these are ever un-named and a hand-named workspace is left alone. Two races found by testing, both fixed: - a freshly named workspace is empty until the app maps its window, and was being un-named in that gap. A marker file protects it; the marker is dropped as soon as the workspace holds a window, so later emptiness reaps at once. A plain timer failed here: closing the app during the timer meant that close event was skipped and nothing retried. - niri emits WindowOpenedOrChanged (singular) on map, while WindowsChanged (plural) is only the bulk snapshot. The narrow event filter missed the moment a workspace became populated, so the marker was never cleared. Now matches *Window*|*Workspace*. Also note `unset-workspace-name` takes its reference POSITIONALLY, unlike set-workspace-name (--workspace) and move-workspace-to-monitor (--reference). --- .config/niri/config.kdl | 34 ++++----- .config/scripts/niri/app_workspaces.sh | 21 ++++++ .config/scripts/niri/workspace_app.sh | 84 +++++++++++++-------- .config/scripts/niri/workspace_reaper.sh | 94 ++++++++++++++++++++++++ 4 files changed, 184 insertions(+), 49 deletions(-) create mode 100644 .config/scripts/niri/app_workspaces.sh create mode 100755 .config/scripts/niri/workspace_reaper.sh diff --git a/.config/niri/config.kdl b/.config/niri/config.kdl index 24a6a34..5aa8917 100644 --- a/.config/niri/config.kdl +++ b/.config/niri/config.kdl @@ -109,6 +109,7 @@ environment { // starts them. Hyprland ignored that target, which is why it needed exec-once. // The sleeps preserve Hyprland's ordering (tray applets after waybar). spawn-at-startup "sh" "-c" "bash ~/.config/scripts/niri/wallpaper_restore.sh" +spawn-at-startup "sh" "-c" "bash ~/.config/scripts/niri/workspace_reaper.sh" spawn-at-startup "sh" "-c" "bash ~/.config/scripts/shared/song_detail_writer.sh" // waybar needs its own config here: the shared one uses hyprland/workspaces // and hyprland/window, which do nothing under niri. config-niri swaps those @@ -149,18 +150,11 @@ window-rule { open-floating true } -// App workspaces, replacing Hyprland's 100/200-206. Reachable ONLY by their -// Alt+ binds: number, direction and wheel navigation all go through -// workspace_nav.sh, which skips named workspaces entirely. Each one homes itself -// on the monitor of its first launch (see workspace_app.sh). -workspace "code" -workspace "github" -workspace "whatsapp" -workspace "discord" -workspace "deezer" -workspace "youtube" -workspace "mail" -workspace "signal" +// The app workspaces (code, github, ...) are deliberately NOT declared here. +// A declared `workspace "name"` node is immortal, which left eight empty +// workspaces lying around. workspace_app.sh names a fresh workspace when it +// launches the app, and workspace_reaper.sh un-names it once it empties so niri +// reaps it. Names live in ~/.config/scripts/niri/app_workspaces.sh. binds { // ─── launchers (Alt trigger, Hyprland $my_trigger) ─────────────────────── @@ -194,14 +188,14 @@ binds { 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"; } Alt+N hotkey-overlay-title="Workspace: signal" { spawn-sh "~/.config/scripts/niri/workspace_app.sh signal flatpak run org.signal.Signal"; } - Mod+Shift+C { move-column-to-workspace "code"; } - Mod+Shift+G { move-column-to-workspace "github"; } - Mod+Shift+W { move-column-to-workspace "whatsapp"; } - Mod+Shift+D { move-column-to-workspace "discord"; } - Mod+Shift+S { move-column-to-workspace "deezer"; } - Mod+Shift+Y { move-column-to-workspace "youtube"; } - Mod+Shift+P { move-column-to-workspace "mail"; } - Mod+Shift+N { move-column-to-workspace "signal"; } + Mod+Shift+C { spawn-sh "~/.config/scripts/niri/workspace_app.sh --move code"; } + Mod+Shift+G { spawn-sh "~/.config/scripts/niri/workspace_app.sh --move github"; } + Mod+Shift+W { spawn-sh "~/.config/scripts/niri/workspace_app.sh --move whatsapp"; } + Mod+Shift+D { spawn-sh "~/.config/scripts/niri/workspace_app.sh --move discord"; } + Mod+Shift+S { spawn-sh "~/.config/scripts/niri/workspace_app.sh --move deezer"; } + Mod+Shift+Y { spawn-sh "~/.config/scripts/niri/workspace_app.sh --move youtube"; } + Mod+Shift+P { spawn-sh "~/.config/scripts/niri/workspace_app.sh --move mail"; } + Mod+Shift+N { spawn-sh "~/.config/scripts/niri/workspace_app.sh --move signal"; } // ─── session / system ─────────────────────────────────────────────────── // hyprlock works here: ext-session-lock-v1, no Hyprland IPC dependency. diff --git a/.config/scripts/niri/app_workspaces.sh b/.config/scripts/niri/app_workspaces.sh new file mode 100644 index 0000000..abe1853 --- /dev/null +++ b/.config/scripts/niri/app_workspaces.sh @@ -0,0 +1,21 @@ +# Names of the app workspaces, sourced by the niri workspace scripts. +# Not executable on purpose. +# +# These are NOT declared in config.kdl: a declared `workspace "name"` node makes +# niri keep that workspace alive forever, which left eight empty workspaces +# sitting around. Instead workspace_app.sh names a fresh workspace on launch and +# workspace_reaper.sh un-names it once it empties, letting niri reap it. +# +# Only names in this list are ever un-named, so a workspace you name by hand is +# left alone. + +APP_WORKSPACES=(code github whatsapp discord deezer youtube mail signal) + +# True if $1 is one of the app workspaces. +is_app_workspace() { + local candidate="$1" name + for name in "${APP_WORKSPACES[@]}"; do + [ "$name" = "$candidate" ] && return 0 + done + return 1 +} diff --git a/.config/scripts/niri/workspace_app.sh b/.config/scripts/niri/workspace_app.sh index 07508a8..59e5b2b 100755 --- a/.config/scripts/niri/workspace_app.sh +++ b/.config/scripts/niri/workspace_app.sh @@ -2,48 +2,74 @@ # Emulates Hyprland's `workspace = N, on-created-empty: ` for niri. # # usage: workspace_app.sh [args...] +# workspace_app.sh --move # -# Focuses the named workspace, then launches the command only if that workspace -# currently holds no windows. niri has no on-created-empty, and named workspaces -# declared in the config are persistent (never "created"), so the emptiness check -# is what makes this lazy instead of duplicating apps. +# Behaviour: +# * workspace already exists (app is running) -> just focus it. It stays on the +# monitor it was launched on; the key moves FOCUS there rather than dragging +# the workspace to the monitor you happen to be on. +# * workspace does not exist -> take the trailing empty workspace of the +# focused monitor, name it, and launch the app there. # -# An app's workspace sticks to the monitor it was first launched on. Pressing the -# key again moves FOCUS there rather than pulling the workspace to the monitor -# you happen to be on. +# The workspaces are deliberately NOT declared in config.kdl: a declared +# `workspace "name"` node is immortal, so eight empty workspaces would always be +# present. Naming happens here and workspace_reaper.sh un-names an app workspace +# once it empties, after which niri reaps it. set -euo pipefail -ws="$1" -shift +. "$(dirname "$(realpath "$0")")/app_workspaces.sh" + +mode="focus" +if [ "${1:-}" = "--move" ]; then + mode="move" + shift +fi + +ws="${1:?usage: workspace_app.sh [--move] [command...]}" +shift || true ws_json=$(niri msg -j workspaces) +exists=$(jq -r --arg w "$ws" '[.[] | select(.name == $w)] | length' <<<"$ws_json") -id=$(jq -r --arg w "$ws" '.[] | select(.name == $w) | .id' <<<"$ws_json") +if [ "$exists" = "0" ]; then + # Claim the last empty unnamed workspace on this monitor. niri always keeps + # one at the end of every output, so there is something to claim. + output=$(jq -r '.[] | select(.is_focused == true) | .output' <<<"$ws_json") + win_json=$(niri msg -j windows) -if [ -z "$id" ]; then - notify-send "workspace_app" "No workspace named '$ws' — is it declared in config.kdl?" - exit 1 -fi + idx=$(jq -r --arg o "$output" --argjson w "$win_json" ' + [ .[] + | select(.output == $o and .name == null) + | . as $ws + | select(([$w[] | select(.workspace_id == $ws.id)] | length) == 0) + ] | last | .idx // empty' <<<"$ws_json") -count=$(niri msg -j windows | jq --argjson id "$id" '[.[] | select(.workspace_id == $id)] | length') + if [ -z "$idx" ]; then + notify-send "workspace_app" "No empty workspace free on $output for '$ws'" + exit 1 + fi -here=$(jq -r '.[] | select(.is_focused == true) | .output' <<<"$ws_json") -there=$(jq -r --arg w "$ws" '.[] | select(.name == $w) | .output' <<<"$ws_json") + # A freshly named workspace is still empty until the app maps its window, + # and workspace_reaper.sh would un-name it in that gap. The marker tells the + # reaper to leave this one alone for a while; see GRACE_SECONDS there. + mkdir -p "${XDG_RUNTIME_DIR:-/tmp}/niri-ws-grace" + : > "${XDG_RUNTIME_DIR:-/tmp}/niri-ws-grace/$ws" -# The app's workspace HOME is the monitor it was first launched on, and it stays -# there: pressing the key again just sends focus to that monitor instead of -# dragging the workspace over. Only an empty (never-used) workspace gets moved, -# so a first launch happens where you are. -# -# --reference avoids a focus change for the move: focusing it on the other -# monitor and dragging it away afterwards leaves that monitor stranded on some -# unrelated workspace. -if [ "$count" -eq 0 ] && [ -n "$here" ] && [ "$here" != "$there" ]; then - niri msg action move-workspace-to-monitor --reference "$ws" "$here" + niri msg action set-workspace-name --workspace "$idx" "$ws" >/dev/null +fi + +if [ "$mode" = "move" ]; then + niri msg action move-column-to-workspace "$ws" >/dev/null + exit 0 fi -niri msg action focus-workspace "$ws" +niri msg action focus-workspace "$ws" >/dev/null + +# Launch only when the workspace holds nothing, so a second press is "go there" +# rather than a duplicate app. +id=$(niri msg -j workspaces | jq -r --arg w "$ws" '.[] | select(.name == $w) | .id') +count=$(niri msg -j windows | jq --argjson id "$id" '[.[] | select(.workspace_id == $id)] | length') -if [ "$count" -eq 0 ]; then +if [ "$count" -eq 0 ] && [ "$#" -gt 0 ]; then setsid "$@" >/dev/null 2>&1 & fi diff --git a/.config/scripts/niri/workspace_reaper.sh b/.config/scripts/niri/workspace_reaper.sh new file mode 100755 index 0000000..d0cf0e1 --- /dev/null +++ b/.config/scripts/niri/workspace_reaper.sh @@ -0,0 +1,94 @@ +#!/usr/bin/env bash +# Un-names app workspaces once they are empty, so niri reaps them. +# +# Runs for the whole session (spawn-at-startup in config.kdl). Follows +# `niri msg event-stream` rather than polling, so it is idle unless windows or +# workspaces actually change. +# +# Why: a named workspace is never reaped, whether the name came from a config +# `workspace "..."` node or from set-workspace-name at runtime. The app +# workspaces are named on launch by workspace_app.sh; without this they would +# pile up empty exactly as the declared ones did. +# +# Only names listed in app_workspaces.sh are touched, so hand-named workspaces +# survive. +set -uo pipefail + +. "$(dirname "$(realpath "$0")")/app_workspaces.sh" + +# A workspace named by workspace_app.sh sits empty until the app maps its window, +# and reaping it in that gap would drop the name the app was launched under. The +# marker file it leaves means "not populated yet, hands off". +# +# The marker is cleared as soon as the workspace holds a window, so the NEXT time +# it goes empty it is reaped at once. A plain timer would not do: if the app is +# closed while the timer is still running, that close event gets skipped and no +# further event arrives to retry. +# +# GRACE_SECONDS is only a backstop for a launch that never produced a window at +# all (crash, wrong command). Flatpak cold starts are slow, hence the size. +GRACE_SECONDS=60 +GRACE_DIR="${XDG_RUNTIME_DIR:-/tmp}/niri-ws-grace" + +in_grace() { + local marker="$GRACE_DIR/$1" age + [ -f "$marker" ] || return 1 + age=$(( $(date +%s) - $(stat -c %Y "$marker") )) + if [ "$age" -lt "$GRACE_SECONDS" ]; then + return 0 + fi + rm -f "$marker" + return 1 +} + +reap() { + local ws_json win_json name + ws_json=$(niri msg -j workspaces 2>/dev/null) || return 0 + win_json=$(niri msg -j windows 2>/dev/null) || return 0 + + # Any named workspace that now holds windows has served its purpose: drop the + # marker so emptying it later reaps immediately. + while read -r name; do + [ -n "$name" ] && rm -f "$GRACE_DIR/$name" + done < <(jq -r --argjson w "$win_json" ' + .[] + | select(.name != null) + | . as $ws + | select(([$w[] | select(.workspace_id == $ws.id)] | length) > 0) + | .name + ' <<<"$ws_json") + + # Named workspaces holding no windows. + while read -r name; do + [ -n "$name" ] || continue + is_app_workspace "$name" || continue + in_grace "$name" && continue + # The reference is POSITIONAL here. set-workspace-name takes --workspace + # and move-workspace-to-monitor takes --reference, but this one does not. + niri msg action unset-workspace-name "$name" >/dev/null 2>&1 || true + done < <(jq -r --argjson w "$win_json" ' + .[] + | select(.name != null) + | . as $ws + | select(([$w[] | select(.workspace_id == $ws.id)] | length) == 0) + | .name + ' <<<"$ws_json") +} + +# Sweep once at startup: workspaces can be left named and empty by a previous +# session, a config reload, or a crash of this script. +reap + +# Any window or workspace event. Matching loosely on purpose: niri emits +# WindowOpenedOrChanged (singular) when a window maps and WindowsChanged (plural) +# only for the bulk snapshot, so a narrow list missed the moment a workspace +# became populated — which is exactly when the grace marker must be dropped. +# Ignored by omission: ConfigLoaded, KeyboardLayoutsChanged, OverviewOpenedOrClosed, +# CastsChanged. +niri msg -j event-stream 2>/dev/null | while read -r line; do + case "$line" in + *Window* | *Workspace*) + reap + ;; + esac +done