Simplify scaling

This commit is contained in:
David Heinemeier Hansson
2026-05-17 21:44:08 +02:00
parent 96f356af27
commit 89a6517018
9 changed files with 92 additions and 89 deletions
-5
View File
@@ -1,5 +0,0 @@
#!/bin/bash
# omarchy:summary=Print the monitor scale of the internal monitor or first other monitor
hyprctl monitors -j | jq -r '([.[] | select(.name | contains("eDP"))][0] // .[0]).scale // empty | tonumber | if . == floor then floor else . end'
+87
View File
@@ -0,0 +1,87 @@
#!/bin/bash
# omarchy:summary=Show, set, or adjust focused Hyprland monitor scaling
# omarchy:args=[up|down|1|1.25|1.6|2|3|4]
# omarchy:examples=omarchy hyprland monitor scaling | omarchy hyprland monitor scaling 1.6 | omarchy hyprland monitor scaling up | omarchy hyprland monitor scaling down
SCALES=(1 1.25 1.6 2 3 4)
usage() {
echo "Usage: omarchy-hyprland-monitor-scaling [up|down|1|1.25|1.6|2|3|4]"
}
focused_monitor_scale() {
hyprctl monitors -j | jq -er '.[] | select(.focused == true) | .scale'
}
set_scale() {
local new_scale="$1"
local monitor_info="$(hyprctl monitors -j | jq -e -c '.[] | select(.focused == true)')"
local active_monitor="$(echo "$monitor_info" | jq -r '.name')"
local width="$(echo "$monitor_info" | jq -r '.width')"
local height="$(echo "$monitor_info" | jq -r '.height')"
local refresh_rate="$(echo "$monitor_info" | jq -r '.refreshRate')"
local monitor_lua="$HOME/.config/hypr/monitors.lua"
hyprctl eval "hl.monitor({ output = \"$active_monitor\", mode = \"${width}x${height}@${refresh_rate}\", position = \"auto\", scale = $new_scale })" >/dev/null
# Persist to monitors.lua if the user still has Omarchy's generic catch-all
# defaults, so the scale survives reboots.
if [[ -f $monitor_lua ]] && grep -q '^local omarchy_monitor_scale = ' "$monitor_lua"; then
sed -i -E \
-e "s|^local omarchy_monitor_scale = .*|local omarchy_monitor_scale = ${new_scale}|" \
-e "s|^local omarchy_gdk_scale = .*|local omarchy_gdk_scale = ${new_scale}|" \
"$monitor_lua"
elif [[ -f $monitor_lua ]] && grep -Eq '^hl\.monitor\(\{ output = "", mode = "preferred", position = "auto", scale = ("auto"|[0-9.]+) \}\)' "$monitor_lua"; then
sed -i -E \
-e "s|^(hl\.monitor\(\{ output = \"\", mode = \"preferred\", position = \"auto\", scale = )([^ ]+)( \}\))|\\1${new_scale}\\3|" \
-e 's|^hl\.env\("GDK_SCALE", ".*"\)|hl.env("GDK_SCALE", "'"$new_scale"'")|' \
"$monitor_lua"
fi
}
scale_from_current() {
local direction="${1:-}"
# Find the preset closest to the current scale (Hyprland may snap fractional
# scales to nearby values, so we can't match exactly).
awk -v direction="$direction" -v list="${SCALES[*]}" '
NR == 1 { scale = $0; found = 1 }
END {
if (!found) exit 1
n = split(list, scales, " ")
best = 1; best_diff = 1e9
for (i = 1; i <= n; i++) {
diff = scale - scales[i]; if (diff < 0) diff = -diff
if (diff < best_diff) { best_diff = diff; best = i }
}
if (direction == "next" && best < n) best++
else if (direction == "previous" && best > 1) best--
print scales[best]
}'
}
case "${1:-}" in
"")
focused_monitor_scale | scale_from_current
;;
-h | --help)
usage
;;
up)
set_scale "$(focused_monitor_scale | scale_from_current next)"
;;
down)
set_scale "$(focused_monitor_scale | scale_from_current previous)"
;;
1 | 1.25 | 1.6 | 2 | 3 | 4)
set_scale "$1"
;;
*)
usage >&2
exit 1
;;
esac
@@ -1,29 +0,0 @@
#!/bin/bash
# omarchy:summary=Cycle focused Hyprland monitor scaling through 1x, 1.25x, 1.6x, 2x, 3x, and 4x
# omarchy:args=[--reverse]
# omarchy:examples=omarchy-hyprland-monitor-scaling-cycle | omarchy-hyprland-monitor-scaling-cycle --reverse
CURRENT_SCALE=$(hyprctl monitors -j | jq -r '.[] | select(.focused == true) | .scale')
SCALES=(1 1.25 1.6 2 3 4)
# Find the index of the scale closest to the current one (Hyprland may
# snap fractional scales to nearby values, so we can't match exactly).
CURRENT_IDX=$(awk -v s="$CURRENT_SCALE" -v list="${SCALES[*]}" 'BEGIN {
n = split(list, arr, " ")
best = 0; best_diff = 1e9
for (i = 1; i <= n; i++) {
d = s - arr[i]; if (d < 0) d = -d
if (d < best_diff) { best_diff = d; best = i - 1 }
}
print best
}')
if [[ $1 == "--reverse" ]]; then
NEW_IDX=$(( (CURRENT_IDX - 1 + ${#SCALES[@]}) % ${#SCALES[@]} ))
else
NEW_IDX=$(( (CURRENT_IDX + 1) % ${#SCALES[@]} ))
fi
exec omarchy-hyprland-monitor-scaling-set "${SCALES[$NEW_IDX]}"
-6
View File
@@ -1,6 +0,0 @@
#!/bin/bash
# omarchy:summary=Get focused Hyprland monitor scaling
# omarchy:examples=omarchy-hyprland-monitor-scaling-get
hyprctl monitors -j | jq -r '.[] | select(.focused == true) | .scale'
-43
View File
@@ -1,43 +0,0 @@
#!/bin/bash
# omarchy:summary=Set focused Hyprland monitor scaling to a specific value
# omarchy:args=<1|1.25|1.6|2|3|4>
# omarchy:examples=omarchy-hyprland-monitor-scaling-set 1.6 | omarchy-hyprland-monitor-scaling-set 2
NEW_SCALE="$1"
case "$NEW_SCALE" in
1) NEW_GDK_SCALE=1 ;;
1.25) NEW_GDK_SCALE=1.25 ;;
1.6) NEW_GDK_SCALE=1.75 ;;
2) NEW_GDK_SCALE=2 ;;
3) NEW_GDK_SCALE=3 ;;
4) NEW_GDK_SCALE=4 ;;
*)
echo "Usage: omarchy-hyprland-monitor-scaling-set <1|1.25|1.6|2|3|4>" >&2
exit 1
;;
esac
MONITOR_INFO=$(hyprctl monitors -j | jq -r '.[] | select(.focused == true)')
ACTIVE_MONITOR=$(echo "$MONITOR_INFO" | jq -r '.name')
WIDTH=$(echo "$MONITOR_INFO" | jq -r '.width')
HEIGHT=$(echo "$MONITOR_INFO" | jq -r '.height')
REFRESH_RATE=$(echo "$MONITOR_INFO" | jq -r '.refreshRate')
hyprctl eval "hl.monitor({ output = \"$ACTIVE_MONITOR\", mode = \"${WIDTH}x${HEIGHT}@${REFRESH_RATE}\", position = \"auto\", scale = $NEW_SCALE })" >/dev/null
# Persist to monitors.lua if the user still has Omarchy's generic catch-all
# defaults, so the scale survives reboots.
MONITOR_LUA="$HOME/.config/hypr/monitors.lua"
if [[ -f $MONITOR_LUA ]] && grep -q '^local omarchy_monitor_scale = ' "$MONITOR_LUA"; then
sed -i -E \
-e "s|^local omarchy_monitor_scale = .*|local omarchy_monitor_scale = ${NEW_SCALE}|" \
-e "s|^local omarchy_gdk_scale = .*|local omarchy_gdk_scale = ${NEW_GDK_SCALE}|" \
"$MONITOR_LUA"
elif [[ -f $MONITOR_LUA ]] && grep -Eq '^hl\.monitor\(\{ output = "", mode = "preferred", position = "auto", scale = ("auto"|[0-9.]+) \}\)' "$MONITOR_LUA"; then
sed -i -E \
-e "s|^(hl\.monitor\(\{ output = \"\", mode = \"preferred\", position = \"auto\", scale = )([^ ]+)( \}\))|\\1${NEW_SCALE}\\3|" \
-e 's|^hl\.env\("GDK_SCALE", ".*"\)|hl.env("GDK_SCALE", "'"$NEW_GDK_SCALE"'")|' \
"$MONITOR_LUA"
fi