aboutsummaryrefslogtreecommitdiffstats
path: root/config/hypr/scripts/LuaCycleWindow.sh
blob: ee64e67a8bc0d05ec08008ebdbe63f50d8220a83 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env bash
# ==================================================
#  KoolDots (2026)
#  Project URL: https://github.com/LinuxBeginnings
#  License: GNU GPLv3
#  SPDX-License-Identifier: GPL-3.0-or-later
# ==================================================
# Cycle focus through visible windows on the active workspace using the
# Hyprland Lua dispatcher API. This avoids cyclenext/focuswindow no-ops on
# Lua config builds.

set -u

direction="${1:-next}"
case "$direction" in
  next|forward|f)
    direction="next"
    ;;
  previous|prev|back|b)
    direction="previous"
    ;;
  *)
    exit 0
    ;;
esac

active_window="$(hyprctl activewindow -j 2>/dev/null || printf '{}')"
active_address="$(jq -r '.address // empty' <<<"$active_window")"
active_workspace="$(jq -r '.workspace.id // empty' <<<"$active_window")"

if [[ -z "$active_address" ]] || ! [[ "$active_workspace" =~ ^-?[0-9]+$ ]]; then
  exit 0
fi

clients="$(hyprctl clients -j 2>/dev/null || printf '[]')"
target_address="$(
  jq -r \
    --arg active_address "$active_address" \
    --argjson active_workspace "$active_workspace" \
    --arg direction "$direction" '
      [
        .[]
        | select((.mapped // false) == true)
        | select((.hidden // false) == false)
        | select(.workspace.id == $active_workspace)
        | {
            address,
            x: (.at[0] // 0),
            y: (.at[1] // 0)
          }
      ]
      | sort_by(.y, .x, .address)
      | if length < 2 then
          empty
        else
          . as $windows
          | ($windows | map(.address) | index($active_address)) as $index
          | if $index == null then
              empty
            elif $direction == "previous" then
              $windows[(($index - 1 + length) % length)].address
            else
              $windows[(($index + 1) % length)].address
            end
        end
    ' <<<"$clients"
)"

if [[ -z "$target_address" ]]; then
  exit 0
fi

hyprctl dispatch "hl.dsp.focus({ window = \"address:${target_address}\" })" >/dev/null 2>&1 || true
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage