diff --git a/bin/omarchy-hyprland-monitor-scaling b/bin/omarchy-hyprland-monitor-scaling index 291c4f73..d109c97c 100755 --- a/bin/omarchy-hyprland-monitor-scaling +++ b/bin/omarchy-hyprland-monitor-scaling @@ -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] + } }' } diff --git a/test/shell.d/monitor-scaling-test.sh b/test/shell.d/monitor-scaling-test.sh index abf8e0dc..b80cfc5c 100644 --- a/test/shell.d/monitor-scaling-test.sh +++ b/test/shell.d/monitor-scaling-test.sh @@ -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"