fix: problems with named workspaces and multi-monitor

* Named workspaces:
** if so, use the name for reference
** if not, use the workspace id for reference
This commit is contained in:
Julian Stiller 2025-04-10 11:59:31 +02:00
parent 12c4f87f56
commit 6c10c2cb7b

63
ndrop
View file

@ -78,6 +78,15 @@ wait_online() {
done done
} }
active_workspace() {
active_workspace_name=$(niri msg --json workspaces | jq -r ".[] | select(.is_focused==true) | .name")
if [[ "$active_workspace_name" != "null" ]]; then
echo "$active_workspace_name"
else
niri msg --json workspaces | jq -r ".[] | select(.is_focused==true) | .id"
fi
}
ndrop_flags() { ndrop_flags() {
while true; do while true; do
case "$1" in case "$1" in
@ -144,7 +153,10 @@ fi
CLASS="$1" CLASS="$1"
COMMANDLINE="${*:1}" COMMANDLINE="${*:1}"
ACTIVE_WORKSPACE=$(niri msg --json workspaces | jq -r ".[] | select(.is_focused==true) | .id") || notify "ndrop: Error executing dependencies 'niri msg' or 'jq'" "Check terminal output of 'ndrop $COMMANDLINE'" ACTIVE_WORKSPACE=$(active_workspace)
ACTIVE_WORKSPACE_ID=$(niri msg --json workspaces | jq -r ".[] | select(.is_focused==true) | .id")
ACTIVE_OUTPUT=$(niri msg --json focused-output | jq -r ".name")
NDROP_OUTPUT=$(niri msg --json workspaces | jq -r ".[] | select(.name==\"ndrop\") | .output")
case "$1" in case "$1" in
epiphany) epiphany)
@ -197,26 +209,37 @@ if [[ -n $CLASS_OVERRIDE ]]; then
fi fi
WINDOW_ID=$(niri msg --json windows | jq -r ".[] | select(.app_id==\"$CLASS\") | .id") WINDOW_ID=$(niri msg --json windows | jq -r ".[] | select(.app_id==\"$CLASS\") | .id")
FOCUSED=$(niri msg --json windows | jq -r ".[] | select(.app_id==\"$CLASS\") | .is_focused")
### OVERRIDE ### if [[ -n ${WINDOW_ID} ]]; then
FOCUS=true if [[ "${ACTIVE_WORKSPACE}" == "ndrop" ]]; then
if ${VERBOSE}; then
if [[ -n $(niri msg --json windows | jq -r ".[] | select(.app_id==\"$CLASS\" and .workspace_id!=\"$ACTIVE_WORKSPACE\")") ]]; then notify_low "ndrop: CLASS: ${CLASS}, ACTIVE_WORKSPACE_ID: ${ACTIVE_WORKSPACE_ID}, ACTIVE_WORKSPACE: ${ACTIVE_WORKSPACE}, WINDOW_ID: ${WINDOW_ID}"
if [[ $FOCUS == false ]]; then notify_low "ndrop: Matched we are on ws 'ndrop'; focus previous ws"
# shellcheck disable=SC2140 # erroneous warning fi
niri msg action move-window-to-workspace --window-id "$WINDOW_ID" "$ACTIVE_WORKSPACE" || notify "ndrop: Error moving '$COMMANDLINE' to current workspace" niri msg action focus-workspace-previous
if $VERBOSE; then notify_low "ndrop: Matched class '$CLASS' on another workspace and moved it to current workspace"; fi exit 0
else
if [[ $FOCUSED == true ]]; then
if $VERBOSE; then
notify_low "ndrop: CLASS: ${CLASS}, ACTIVE_WORKSPACE_ID: ${ACTIVE_WORKSPACE_ID}, ACTIVE_WORKSPACE: ${ACTIVE_WORKSPACE}, WINDOW_ID: ${WINDOW_ID}"
notify_low "ndrop: Matched focused target on active ws '${ACTIVE_WORKSPACE}'; move target to 'ndrop'"
fi
niri msg action move-window-to-workspace --window-id "${WINDOW_ID}" "ndrop"
# For good multi-monitor support, we need something like this in niri.
# if [[ "$ACTIVE_OUTPUT" != "$NDROP_OUTPUT" ]]; then
# niri msg action focus-monitor "${ACTIVE_OUTPUT}"
# fi
niri msg action focus-workspace "${ACTIVE_WORKSPACE}"
exit 0
else
if [[ $VERBOSE == true ]]; then
notify_low "ndrop: CLASS: ${CLASS}, ACTIVE_WORKSPACE_ID: ${ACTIVE_WORKSPACE_ID}, ACTIVE_WORKSPACE: ${ACTIVE_WORKSPACE}, WINDOW_ID: ${WINDOW_ID}"
# notify_low "ndrop: Matched unfocused target on active ws '${ACTIVE_WORKSPACE}'; focu'"
fi
niri msg action move-window-to-workspace --window-id "${WINDOW_ID}" "${ACTIVE_WORKSPACE}"
niri msg action focus-window --id "${WINDOW_ID}"
exit 0
fi fi
niri msg action focus-window --id "$WINDOW_ID" || notify "ndrop: Error focusing '$COMMANDLINE' on current workspace"
elif [[ -n $(niri msg --json windows | jq -r ".[] | select(.app_id==\"$CLASS\" and .workspace_id==\"$ACTIVE_WORKSPACE\")") ]]; then
if [[ $FOCUS == false ]]; then
# shellcheck disable=SC2086 # integers won't be split
niri msg action move-window-to-workspace --window-id "$WINDOW_ID" "ndrop" || notify "ndrop: Error moving '$COMMANDLINE' to workspace 'ndrop'"
niri msg action focus-workspace "$ACTIVE_WORKSPACE"
if $VERBOSE; then notify_low "ndrop: Matched class '$CLASS' on current workspace and moved it to workspace 'ndrop'"; fi
# else
elif [[ -n $(niri msg --json windows | jq -r ".[] | select(.app_id==\"$CLASS\" and .workspace_id!=\"ndrop\")") ]]; then
niri msg action focus-window --id "$WINDOW_ID" || notify "ndrop: Error focusing '$COMMANDLINE' on current workspace"
fi fi
else else
if $ONLINE; then wait_online; fi if $ONLINE; then wait_online; fi