diff --git a/ndrop b/ndrop index 62b3cb0..2199082 100755 --- a/ndrop +++ b/ndrop @@ -78,6 +78,15 @@ wait_online() { 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() { while true; do case "$1" in @@ -144,7 +153,10 @@ fi CLASS="$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 epiphany) @@ -197,26 +209,37 @@ if [[ -n $CLASS_OVERRIDE ]]; then fi WINDOW_ID=$(niri msg --json windows | jq -r ".[] | select(.app_id==\"$CLASS\") | .id") - -### OVERRIDE ### -FOCUS=true - -if [[ -n $(niri msg --json windows | jq -r ".[] | select(.app_id==\"$CLASS\" and .workspace_id!=\"$ACTIVE_WORKSPACE\")") ]]; then - if [[ $FOCUS == false ]]; then - # shellcheck disable=SC2140 # erroneous warning - niri msg action move-window-to-workspace --window-id "$WINDOW_ID" "$ACTIVE_WORKSPACE" || notify "ndrop: Error moving '$COMMANDLINE' to current workspace" - if $VERBOSE; then notify_low "ndrop: Matched class '$CLASS' on another workspace and moved it to current workspace"; 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" +FOCUSED=$(niri msg --json windows | jq -r ".[] | select(.app_id==\"$CLASS\") | .is_focused") +if [[ -n ${WINDOW_ID} ]]; then + if [[ "${ACTIVE_WORKSPACE}" == "ndrop" ]]; 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 we are on ws 'ndrop'; focus previous ws" + fi + niri msg action focus-workspace-previous + 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 else if $ONLINE; then wait_online; fi