diff --git a/dot_config/niri/cfg/keybinds.kdl b/dot_config/niri/cfg/keybinds.kdl index 365b0ca..2d0219f 100644 --- a/dot_config/niri/cfg/keybinds.kdl +++ b/dot_config/niri/cfg/keybinds.kdl @@ -100,8 +100,8 @@ binds { Mod+O repeat=false { toggle-overview; } // --- Discord keybinds dirty trick --- - CTRL+BracketLeft { spawn "~/.local/scripts/discord/focus-discord-toggle-mute.sh"; } - CTRL+BracketRight { spawn "~/.local/scripts/discord/focus-discord-toggle-deafen.sh"; } + CTRL+BracketLeft repeat=false { spawn "~/.local/scripts/discord/toggle-key.sh" "m"; } + CTRL+BracketRight repeat=false { spawn "~/.local/scripts/discord/toggle-key.sh" "d"; } // --- Screnshot with grim+slurp+satty --- Mod+S hotkey-overlay-title="Screenshot (region)" { spawn "~/.local/scripts/gss-screenshot.sh" "region"; } diff --git a/dot_local/scripts/discord/executable_focus-discord-toggle-deafen.sh b/dot_local/scripts/discord/executable_focus-discord-toggle-deafen.sh deleted file mode 100644 index 248ded2..0000000 --- a/dot_local/scripts/discord/executable_focus-discord-toggle-deafen.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/sh -APP_NAME="discord-canary" -PREV_ID=$(niri msg focused-window 2>/dev/null | awk ' - /Window ID/ { gsub(":", "", $3); print $3; exit } -') -DISCORD_ID=$(niri msg windows 2>/dev/null | awk -v app="$APP_NAME" ' - /^Window ID/ { id = $3; sub(":", "", id) } - $0 ~ "App ID: \"" app "\"" { print id; exit } -') - -if [ -z "$DISCORD_ID" ]; then - notify-send "Discord not found" "Could not find $APP_NAME window" - exit 1 -fi - -niri msg action focus-window --id "$DISCORD_ID" -wtype -M ctrl -M shift -k d -m shift -m ctrl # default discord keybind - -if [ -n "$PREV_ID" ]; then - niri msg action focus-window --id "$PREV_ID" -fi diff --git a/dot_local/scripts/discord/executable_focus-discord-toggle-mute.sh b/dot_local/scripts/discord/executable_focus-discord-toggle-mute.sh deleted file mode 100644 index 2bc1bd9..0000000 --- a/dot_local/scripts/discord/executable_focus-discord-toggle-mute.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/sh -APP_NAME="discord-canary" -PREV_ID=$(niri msg focused-window 2>/dev/null | awk ' - /Window ID/ { gsub(":", "", $3); print $3; exit } -') -DISCORD_ID=$(niri msg windows 2>/dev/null | awk -v app="$APP_NAME" ' - /^Window ID/ { id = $3; sub(":", "", id) } - $0 ~ "App ID: \"" app "\"" { print id; exit } -') - -if [ -z "$DISCORD_ID" ]; then - notify-send "Discord not found" "Could not find $APP_NAME window" - exit 1 -fi - -niri msg action focus-window --id "$DISCORD_ID" -wtype -M ctrl -M shift -k m -m shift -m ctrl # default discord keybind - -if [ -n "$PREV_ID" ]; then - niri msg action focus-window --id "$PREV_ID" -fi diff --git a/dot_local/scripts/discord/executable_toggle-key.sh b/dot_local/scripts/discord/executable_toggle-key.sh new file mode 100644 index 0000000..85040fe --- /dev/null +++ b/dot_local/scripts/discord/executable_toggle-key.sh @@ -0,0 +1,49 @@ +#!/bin/sh +set -eu + +APP_NAME="discord-canary" +KEY="${1:-${KEY:-}}" + +if [ -z "$KEY" ]; then + echo "usage: $0 (e.g. m or d)" >&2 + notify-send "Discord toggle" "Missing key (m/d)" + exit 1 +fi + +PREV_FOCUS=$(niri msg -j focused-window | jq -r '.id // empty') +WINS=$(niri msg -j windows) +WSS=$(niri msg -j workspaces) +DISCORD_ID=$(printf '%s' "$WINS" | jq -r --arg a "$APP_NAME" \ + '.[] | select(.app_id == $a) | .id' | head -n1) + +if [ -z "$DISCORD_ID" ]; then + notify-send "Discord not found" "Could not find $APP_NAME window" + exit 1 +fi + +# monitor-level shown window +SHOWN_WIN=$(printf '%s' "$WINS" | jq -r --argjson did "$DISCORD_ID" --argjson wss "$WSS" ' + (.[] | select(.id == $did) | .workspace_id) as $wsid + | ($wss[] | select(.id == $wsid) | .output) as $out + | ($wss[] | select(.output == $out and .is_active) | .active_window_id // empty) +') + +# focus Discord only if needed +if [ "$PREV_FOCUS" != "$DISCORD_ID" ]; then + niri msg action focus-window --id "$DISCORD_ID" + sleep 0.05 +fi + +wtype -M ctrl -M shift -k "$KEY" -m shift -m ctrl +sleep 0.05 + +# restore what that monitor was showing +if [ -n "${SHOWN_WIN:-}" ] && [ "$SHOWN_WIN" != "$DISCORD_ID" ]; then + niri msg action focus-window --id "$SHOWN_WIN" + sleep 0.05 +fi + +# # restore original global focus +if [ -n "$PREV_FOCUS" ] && [ "$PREV_FOCUS" != "$DISCORD_ID" ] && [ "$PREV_FOCUS" != "${SHOWN_WIN:-}" ]; then + niri msg action focus-window --id "$PREV_FOCUS" +fi