aboutsummaryrefslogtreecommitdiffstats
path: root/config/hypr/scripts/HyprLayoutModule.sh
blob: bbfb92464b65569485274df4778018b2653a7bd4 (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
238
239
240
241
242
243
#!/usr/bin/env bash
# ==================================================
#  KoolDots (2026)
#  Project URL: https://github.com/LinuxBeginnings
#  License: GNU GPLv3
#  SPDX-License-Identifier: GPL-3.0-or-later
# ==================================================
# Waybar module for Hyprland layouts

IFS=$'\n\t'

SCRIPTSDIR="${XDG_CONFIG_HOME:-$HOME/.config}/hypr/scripts"
rofi_config="${XDG_CONFIG_HOME:-$HOME/.config}/rofi/config-layout.rasi"
change_layout="${SCRIPTSDIR}/ChangeLayout.sh"
layouts=(dwindle scrolling monocle master)

layout_icon() {
	case "$1" in
	dwindle) echo "šŸ„³" ;;
	scrolling) echo "šŸ…‚" ;;
	monocle) echo "šŸ„¼" ;;
	master) echo "ā“œ" ;;
	*) echo "󰹑" ;;
	esac
}

layout_name() {
	case "$1" in
	dwindle) echo "Dwindle" ;;
	scrolling) echo "Scrolling" ;;
	monocle) echo "Monocle" ;;
	master) echo "Master" ;;
	*) echo "Unknown" ;;
	esac
}

# Fallback shortcut labels used when live Hyprland bind data is unavailable.
layout_shortcut_fallback() {
	case "$1" in
	dwindle) echo "SUPER+ALT+1" ;;
	master) echo "SUPER+ALT+2" ;;
	scrolling) echo "SUPER+ALT+3" ;;
	monocle) echo "SUPER+ALT+4" ;;
	*) echo "" ;;
	esac
}

# Resolve live shortcut label from currently loaded Hyprland binds.
# Works for both Hyprlang and Lua config modes since it reads runtime bind state.
layout_shortcut_live() {
	local target="$1"
	local shortcuts

	if ! command -v hyprctl >/dev/null 2>&1 || ! command -v jq >/dev/null 2>&1; then
		return 1
	fi

	shortcuts="$(
		hyprctl -j binds 2>/dev/null | jq -r --arg target "$target" '
			# Decode integer modmask into modifier string (SUPER+ALT+CTRL+SHIFT).
			# Bit values: SHIFT=1, CTRL=4, ALT=8, SUPER=64
			def has_bit(mask; bit): ((mask / bit) | floor) % 2 == 1;
			def modmask_str(mask):
				[
					if has_bit(mask; 64) then "SUPER" else empty end,
					if has_bit(mask; 8)  then "ALT"   else empty end,
					if has_bit(mask; 4)  then "CTRL"  else empty end,
					if has_bit(mask; 1)  then "SHIFT" else empty end
				] | join("+");
			[
				.[]?
				| select(
					(
						(.dispatcher // "") == "exec"
						and (
							(.arg // .args // .argument // "")
							| tostring
							| test("(^|[[:space:];])([^;]*ChangeLayout\\.sh[[:space:]]+" + $target + "([[:space:];]|$))")
						)
					)
					or (
						(.description // "")
						| tostring
						| ascii_downcase
						== ("layout " + $target)
					)
				)
				| (
					# Prefer display_key if present and non-empty (newer Hyprland versions).
					(.display_key | if . != null and length > 0 then . else null end)
					// (
						# Fall back to decoding modmask bitmask + key name.
						[modmask_str(.modmask // 0), (.key // "")]
						| map(select(. != null and . != ""))
						| join("+")
					)
				)
				| tostring
				| gsub("\\s*\\+\\s*"; "+")
				| select(length > 0)
			]
			| unique
			| join(" / ")
		' 2>/dev/null
	)"

	[[ -n "$shortcuts" ]] || return 1
	printf '%s\n' "$shortcuts"
}

layout_shortcut() {
	local target="$1"
	local live_value

	live_value="$(layout_shortcut_live "$target" || true)"
	if [[ -n "$live_value" ]]; then
		printf '%s\n' "$live_value"
		return
	fi

	layout_shortcut_fallback "$target"
}

get_layout() {
	local layout

	if [[ -x "$change_layout" ]]; then
		layout="$("$change_layout" --quiet current 2>/dev/null || true)"
		if [[ -n "$layout" ]]; then
			printf '%s\n' "$layout"
			return
		fi
	fi

	hyprctl -j activeworkspace 2>/dev/null | jq -r '.tiledLayout // .tiled_layout // "unknown"' 2>/dev/null
}

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]}"
}

refresh_waybar() {
	pkill -RTMIN+8 waybar 2>/dev/null || true
}

set_layout() {
	local target="$1"

	"$change_layout" "$target" && refresh_waybar
}

show_status() {
	local current icon name tooltip

	current="$(get_layout)"
	icon="$(layout_icon "$current")"
	name="$(layout_name "$current")"
	tooltip="Workspace layout: ${name} (${icon})\n\nLeft click: Select layout for active workspace\nRight click: Cycle active workspace layout\n\nOptions:\nšŸ„³  Dwindle\nšŸ…‚  Scrolling\nšŸ„¼  Monocle\nā“œ   Master"

	printf '{"text":"%s","tooltip":"%s","class":"%s"}\n' "$icon" "$tooltip" "$current"
}

show_menu() {
	local current default_row choice target i
	local options=()
	local left_width=0
	local row left_text shortcut

	current="$(get_layout)"
	default_row=0

	# First pass: collect left-column text and shortcuts, track max left width.
	for i in "${!layouts[@]}"; do
		local layout="${layouts[i]}"
		local prefix="  "

		if [[ "$layout" == "$current" ]]; then
			prefix="ā— "
			default_row="$i"
		fi

		shortcut="$(layout_shortcut "$layout")"
		left_text="$(printf '%s%s  %s' "$prefix" "$(layout_icon "$layout")" "$(layout_name "$layout")")"
		(( ${#left_text} > left_width )) && left_width=${#left_text}
		options+=("$left_text|$shortcut")
	done

	# Second pass: align the separator and right-side shortcut column.
	for i in "${!options[@]}"; do
		row="${options[i]}"
		left_text="${row%%|*}"
		shortcut="${row#*|}"
		options[i]="$(printf "%-${left_width}s     %s" "$left_text" "$shortcut")"
	done

	if pgrep -x rofi >/dev/null; then
		pkill rofi
		return 0
	fi

	"${XDG_CONFIG_HOME:-$HOME/.config}/hypr/scripts/RofiFocusedWallpaperLink.sh" >/dev/null 2>&1 || true
	choice="$(printf '%s\n' "${options[@]}" | rofi -i -dmenu -p "Workspace layout" -mesg "Select layout for this workspace" -selected-row "$default_row" -config "$rofi_config")"
	[[ -z "$choice" ]] && exit 0

	case "$choice" in
	*Dwindle*) target="dwindle" ;;
	*Scrolling*) target="scrolling" ;;
	*Monocle*) target="monocle" ;;
	*Master*) target="master" ;;
	*) exit 1 ;;
	esac

	set_layout "$target"
}

case "${1:-status}" in
status)
	show_status
	;;
menu)
	show_menu
	;;
next|toggle)
	set_layout "$(next_layout "$(get_layout)")"
	;;
dwindle|scrolling|monocle|master)
	set_layout "$1"
	;;
*)
	echo "Usage: $(basename "$0") [status|menu|next|dwindle|scrolling|monocle|master]" >&2
	exit 1
	;;
esac
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage