aboutsummaryrefslogtreecommitdiffstats
path: root/config/hypr/scripts/HyprLayoutModule.sh
blob: 14e3b5247b5c94f578416c63b15b80c47a0d2fa4 (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
#!/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
}

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=()

	current="$(get_layout)"
	default_row=0

	for i in "${!layouts[@]}"; do
		local layout="${layouts[i]}"
		local prefix="  "

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

		options+=("${prefix}$(layout_icon "$layout")  $(layout_name "$layout")")
	done

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

	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