aboutsummaryrefslogtreecommitdiffstats
path: root/config/hypr/scripts/ChangeLayout.sh
blob: 4e241e85029a12bd613c3066549d8abd2d2ed6b9 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env bash
# ==================================================
#  KoolDots (2026)
#  Project URL: https://github.com/LinuxBeginnings
#  License: GNU GPLv3
#  SPDX-License-Identifier: GPL-3.0-or-later
# ==================================================
# Switch Hyprland layouts per active monitor/workspace.
# This avoids global layout mutations and keeps workspace-specific rules intact.

notif="${XDG_CONFIG_HOME:-$HOME/.config}/swaync/images/ja.png"
persist_layout_script="${XDG_CONFIG_HOME:-$HOME/.config}/hypr/scripts/PersistWorkspaceLayout.sh"
layouts=(master dwindle scrolling monocle)
quiet_mode=0

normalize_layout() {
  case "$1" in
  master | dwindle | scrolling | monocle)
    printf '%s\n' "$1"
    ;;
  *)
    printf '\n'
    ;;
  esac
}

wait_for_layout() {
  local target="$1"
  local actual=""
  local attempt

  for attempt in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
    actual="$(get_layout)"
    if [[ "$actual" == "$target" ]]; then
      printf '%s\n' "$actual"
      return 0
    fi
    sleep 0.03
  done

  printf '%s\n' "$actual"
  return 1
}

persist_current_workspace_layout() {
  local target_layout="$1"

  if [[ ! -x "$persist_layout_script" ]]; then
    return 0
  fi

  "$persist_layout_script" --quiet --layout "$target_layout" >/dev/null 2>&1 || true
}

get_active_workspace_json() {
  hyprctl -j activeworkspace 2>/dev/null || printf '{}'
}

get_layout() {
  local ws_json layout

  ws_json="$(get_active_workspace_json)"
  layout="$(jq -r '.tiledLayout // .tiled_layout // .layout // empty' <<<"$ws_json" 2>/dev/null || true)"
  layout="$(normalize_layout "$layout")"

  if [[ -z "$layout" ]]; then
    layout="$(hyprctl -j getoption general:layout 2>/dev/null | jq -r '.str // empty' 2>/dev/null || true)"
    layout="$(normalize_layout "$layout")"
  fi

  if [[ -z "$layout" ]]; then
    layout="dwindle"
  fi

  printf '%s\n' "$layout"
}

get_workspace_selector() {
  local ws_json="$1"
  local ws_id ws_name

  ws_id="$(jq -r '.id // empty' <<<"$ws_json" 2>/dev/null || true)"
  ws_name="$(jq -r '.name // empty' <<<"$ws_json" 2>/dev/null || true)"

  if [[ "$ws_id" =~ ^[0-9]+$ ]] && ((ws_id > 0)); then
    printf '%s\n' "$ws_id"
    return 0
  fi

  if [[ -n "$ws_name" && "$ws_name" != "null" ]]; then
    if [[ "$ws_name" == name:* || "$ws_name" == special:* ]]; then
      printf '%s\n' "$ws_name"
    else
      printf 'name:%s\n' "$ws_name"
    fi
    return 0
  fi

  return 1
}

get_workspace_label() {
  local ws_json="$1"
  local ws_name ws_id

  ws_name="$(jq -r '.name // empty' <<<"$ws_json" 2>/dev/null || true)"
  ws_id="$(jq -r '.id // empty' <<<"$ws_json" 2>/dev/null || true)"

  if [[ -n "$ws_name" && "$ws_name" != "null" ]]; then
    printf '%s\n' "$ws_name"
    return
  fi

  if [[ -n "$ws_id" && "$ws_id" != "null" ]]; then
    printf '%s\n' "$ws_id"
    return
  fi

  printf 'current\n'
}

get_monitor_name() {
  local ws_json="$1"
  jq -r '.monitor // empty' <<<"$ws_json" 2>/dev/null || true
}

escape_lua_string() {
  local value="$1"
  value="${value//\\/\\\\}"
  value="${value//\"/\\\"}"
  printf '%s' "$value"
}

is_ok_output() {
  local output="$1"
  local normalized

  normalized="$(printf '%s\n' "$output" | tr -d '\r' | sed '/^[[:space:]]*$/d')"
  [[ -z "$normalized" || "$normalized" == "ok" ]]
}

set_workspace_layout_rule() {
  local target="$1"
  local ws_json workspace_selector monitor_name output
  local ws_escaped monitor_escaped target_escaped

  ws_json="$(get_active_workspace_json)"
  workspace_selector="$(get_workspace_selector "$ws_json" || true)"
  monitor_name="$(get_monitor_name "$ws_json")"

  if [[ -z "$workspace_selector" || -z "$monitor_name" ]]; then
    echo "Unable to resolve active workspace context" >&2
    return 1
  fi

  output="$(hyprctl keyword workspace "${workspace_selector}, monitor:${monitor_name}, layout:${target}" 2>&1 || true)"
  if grep -q "keyword can't work with non-legacy parsers" <<<"$output"; then
    ws_escaped="$(escape_lua_string "$workspace_selector")"
    monitor_escaped="$(escape_lua_string "$monitor_name")"
    target_escaped="$(escape_lua_string "$target")"
    output="$(hyprctl eval "hl.workspace_rule({ workspace = \"${ws_escaped}\", monitor = \"${monitor_escaped}\", layout = \"${target_escaped}\" })" 2>&1 || true)"
  fi

  if ! is_ok_output "$output"; then
    echo "$output" >&2
    return 1
  fi
}

next_layout() {
  local current="$1"
  local i
  for i in "${!layouts[@]}"; do
    if [[ "${layouts[i]}" == "$current" ]]; then
      echo "${layouts[((i + 1) % ${#layouts[@]})]}"
      return
    fi
  done
  echo "${layouts[0]}"
}

set_layout() {
  local target="$1"
  local ws_json workspace_label monitor_name actual

  ws_json="$(get_active_workspace_json)"
  workspace_label="$(get_workspace_label "$ws_json")"
  monitor_name="$(get_monitor_name "$ws_json")"

  if ! set_workspace_layout_rule "$target"; then
    if [[ "$quiet_mode" -eq 0 ]]; then
      notify-send -e -u critical -i "$notif" " Layout switch failed: $target"
    fi
    return 1
  fi

  actual="$(wait_for_layout "$target")"
  if [[ "$actual" == "$target" ]]; then
    persist_current_workspace_layout "$target"
    if [[ "$quiet_mode" -eq 0 ]]; then
      notify-send -e -u low -i "$notif" " ${actual^} Layout · WS ${workspace_label}${monitor_name:+ @ ${monitor_name}}"
    fi
  else
    if [[ "$quiet_mode" -eq 0 ]]; then
      notify-send -e -u critical -i "$notif" " Layout switch failed: still ${actual}"
    fi
    return 1
  fi
}

if [[ "${1:-}" == "--quiet" || "${1:-}" == "--no-notify" ]]; then
  quiet_mode=1
  shift
fi

current="$(get_layout)"
arg="${1:-toggle}"

case "$arg" in
init)
  # No startup keybind rebinding required anymore.
  exit 0
  ;;
current | status | get)
  printf '%s\n' "$current"
  ;;
toggle | next)
  set_layout "$(next_layout "$current")"
  ;;
master | dwindle | scrolling | monocle)
  set_layout "$arg"
  ;;
*)
  echo "Usage: $(basename "$0") [--quiet|--no-notify] [toggle|next|init|current|master|dwindle|scrolling|monocle]" >&2
  exit 1
  ;;
esac
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage