Fix monitor scaling down getting stuck at 3x due to floating point drift

Hyprland can report a scale set to 3 as something like 3.0000000000000004.
The previous directional comparison treated that as above the 3 preset, so
Super+Alt+Slash (scale down) would stay at 3 instead of reaching 2.

Snap the reported scale to the nearest preset first, then step up/down.
Add a regression test for the floating-point case.
This commit is contained in:
David Heinemeier Hansson
2026-07-19 09:10:14 -07:00
parent c030337cb6
commit 6ba0e9c234
2 changed files with 17 additions and 17 deletions
+11 -17
View File
@@ -90,30 +90,24 @@ scale_from_current() {
if (!found) exit 1
n = split(list, scales, " ")
if (direction == "next") {
for (i = 1; i <= n; i++) {
if (scale < scales[i]) { print scales[i]; exit }
}
print scales[n]
exit
}
if (direction == "previous") {
for (i = n; i >= 1; i--) {
if (scale > scales[i]) { print scales[i]; exit }
}
print scales[1]
exit
}
# Find the preset closest to the current scale.
# Snap to the nearest preset first. Hyprland reports scales as floating
# point values, so a scale set to 3 can come back as 3.0000000000000004.
# Without snapping, the directional comparisons below can misidentify
# the current preset and refuse to step down (or up) any further.
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 }
}
print scales[best]
if (direction == "next") {
print scales[(best < n ? best + 1 : n)]
} else if (direction == "previous") {
print scales[(best > 1 ? best - 1 : 1)]
} else {
print scales[best]
}
}'
}
+6
View File
@@ -57,6 +57,12 @@ grep -F 'scale = 2' "$eval_out" >/dev/null || fail "monitor scaling down recover
grep -Fx 'local omarchy_monitor_scale = 2' "$monitor_lua" >/dev/null || fail "monitor scaling down persists 2x from 3x"
pass "monitor scaling down recovers 3x to 2x"
write_monitor_config
OMARCHY_TEST_MONITOR_SCALE=3.0000000000000004 run_scaling down
grep -F 'scale = 2' "$eval_out" >/dev/null || fail "monitor scaling down snaps floating point 3x to 2x"
grep -Fx 'local omarchy_monitor_scale = 2' "$monitor_lua" >/dev/null || fail "monitor scaling down persists 2x from floating point 3x"
pass "monitor scaling down snaps floating point 3x to 2x"
write_monitor_config
OMARCHY_TEST_MONITOR_SCALE=2 run_scaling 3
grep -F 'scale = 3' "$eval_out" >/dev/null || fail "monitor scaling explicit 3x remains available"