Skip to content

Preserve monitor position and per-output scale when scaling - #13142

Closed
f1n3d wants to merge 11 commits into
omacom:quattrofrom
f1n3d:fix-monitor-scale-preserve-position
Closed

f1n3d wants to merge 11 commits into
omacom:quattrofrom
f1n3d:fix-monitor-scale-preserve-position

Conversation

@f1n3d

@f1n3d f1n3d commented Sep 24, 2026 •

Copy link
Copy Markdown

Setting the display scale from the Display panel destroys explicit multi-monitor layouts and does not survive a reboot for per-output configs.

What was wrong (bin/omarchy-hyprland-monitor-scaling):

  • Every scale change sent position = "auto", collapsing explicit layouts such as DP-1 at 0x0 plus DP-2 at 1920x0 into overlapping outputs.
  • Persistence only knew the generic catch-all (local omarchy_monitor_scale / output = ""), so with explicit hl.monitor entries for DP-1/DP-2 the new scale was lost on the next reload.

What this changes:

  • Keeps the focused monitor's current position (x/y from hyprctl monitors -j) in the hl.monitor eval instead of resetting to auto; validates geometry and falls back to 0x0.
  • Keeps the current @refreshrate in the mode (thanks @omarchybot for catching this on a worker): a rate-less mode makes Hyprland request 60Hz and can drop a high-refresh display to it. Hyprland still picks the closest real mode, so a rate reported as 164.91701 lands on the same mode as the 164.92 pinned in monitors.lua.
  • Persists the scale into the matching explicit hl.monitor entry in ~/.config/hypr/monitors.lua when one sets its own scale (leaving the generic catch-all alone), plus the GDK scale; an entry that reads omarchy_monitor_scale updates that variable instead. The rewrite understands nested tables, ; separators and comments, and only replaces a whole scalar value — an expression stays whole. Generic configs behave exactly as before.

Scope: recomputing positions when a scale change alters the focused monitor's logical size is not addressed here. Keeping the position stops the collapse, but a neighbour at a fixed x keeps its old coordinate — a gap when scaling up, an overlap when scaling down — which is #13062's request; #11854 carries that recompute. This PR deliberately only stops explicit layouts from being destroyed.

Tests: extended test/shell.d/monitor-scaling-test.sh (stub now reports x/y) with cases for position preserved, refresh rate kept, explicit per-output persistence (including entries that read omarchy_monitor_scale, nested tables, semicolon separators, and trailing/block comments), scale expressions left whole, and automatic scales — monitor-scaling is 24/24. All other monitor suites pass (monitor, monitor-state, monitor-output-name, monitor-recovery, monitor-clamshell-scale, monitor-modeless, hw-external-monitors, sleep-monitor). Full ./test/shell shows only the pre-existing environment failures also present without this change. Also verified end-to-end against a real 2x DP 1920x1080 layout: DP-2 at 1920x0 scales to 1.6 while DP-1 is untouched.

Related: #10922, #13062, #11854, #13605, #7625, #6673. The display on/off toggle itself (#6968, PR #7036) is deliberately out of scope here.

Fixes #13229

@f1n3d

f1n3d commented Sep 24, 2026

Copy link
Copy Markdown
Author

Live verification for the two riskier choices in this PR, on real hardware (Omarchy 4.0.4-1, Hyprland 0.56.2, DP-1 1920x1080 at 0x0 scale 1, DP-2 same mode at 1920x0):

Re-applied the already-active scale (1) through the patched script and compared hyprctl monitors -j before/after: mode, refreshRate, position and scale are all byte-identical afterwards (rate stays at the running 164.91701 Hz), and monitors.lua is untouched. So the rate-less mode = WxH is accepted and keeps the current rate instead of fighting float formatting — note monitors.lua pins 164.92 while Hyprland reports 164.91701, which is exactly the mismatch the old @-suffix carried back in.

On preferred/auto as an alternative for the scaling path: that would change semantics, not just formatting. set_scale must never change the resolution (non-native modes, VMs), and mode = preferred would do exactly that as a side effect — which is why this keeps the exact WxH and only drops the rate suffix.

omarchybot and others added 4 commits October 3, 2026 03:39
Hyprland 0.56 reads a mode with no @Rate as a request for 60Hz and picks the mode closest to it, so every scale change dropped a high-refresh display to 60Hz until the next reload. On a worker at [email protected] the rate-less mode left it at 60.00Hz; with the rate restored it stays at 74.99Hz. A rate Hyprland reports with more precision than monitors.lua pins still resolves to the same mode, since Hyprland matches the closest one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
An explicit hl.monitor entry for the focused output took the per-output branch whatever its scale said, so an entry with scale = omarchy_monitor_scale (the layout in omacom#13062) matched no number, nothing was written, and the scale reverted on reload; before, the variable was updated. Only an entry with its own number or "auto" is rewritten in place now, and one that reads the variable falls through to updating it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The greedy .*scale = reached past the closing }) into a trailing comment, so a line like scale = omarchy_monitor_scale }) -- was scale = 1 rewrote the comment, took the per-output branch, and never updated the variable the entry actually reads. Stopping at the first } keeps the match to the table's own scale field.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Codex Medium <[email protected]>
An entry with scale = 3 / 2 matched its leading 3, so choosing 2 wrote scale = 2 / 2 and the output came back at 1 on reload. The value must now end at a comma or the closing brace; anything else falls through as before this change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Codex Medium <[email protected]>
@greptile-apps

greptile-apps Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no blocking issue remains.

Summary

Preserves the focused monitor’s position and refresh rate when changing scale, and saves scalar scales in matching per-output rules.

  • Scaling keeps a monitor’s current screen position.
  • Scale changes update the matching monitor rule.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Choose scale] --> B[Apply scale with current position and refresh rate]
  B --> C{Matching per-output scalar?}
  C -->|Yes| D[Save that output's scale and GDK scale]
  C -->|No| E[Use existing shared-scale fallback]
Loading

Reviews (7) · Last reviewed commit: "Match comments and long strings atomical..." · Reviewed by Greptile

Comment thread bin/omarchy-hyprland-monitor-scaling Resolved
Comment thread bin/omarchy-hyprland-monitor-scaling Resolved
@omarchybot omarchybot added the verified Omarchy Triage has verified that this issue is ready for final review label Oct 3, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

Reviewed and tested on an Omarchy worker running Hyprland 0.56.2. The position fix holds: with two outputs side by side, a scale change on quattro moved the other output from 0x0 to 3200x0, this branch keeps both where they were, and reverting just the position change brings the move back.

Three things were wrong, and I pushed a commit to this branch for each:

  • The refresh rate dropped to 60Hz. Hyprland reads a mode with no @rate as a request for 60Hz and picks the mode closest to it (m_refreshRate = 60 in MonitorRule.hpp, and parseMode only overrides it when there is an @). On the worker, the rate-less mode took a 74.99Hz display to 60.00Hz on every scale change. fef2ddb puts the current rate back in the mode. Hyprland still picks the closest real mode, so a rate reported as 164.91701 lands on the same mode as 164.92.
  • An explicit entry that reads omarchy_monitor_scale lost the change. In a layout like the one in Monitor positions don't auto-adjust when scale changes, causing overlap #13062 (hl.monitor({ output = "DP-1", ..., scale = omarchy_monitor_scale })), the new branch matched the output name, the sed found no number to replace, and nothing was written. quattro updates the variable in that case. fda33ae only rewrites an entry whose scale is a number or "auto", and lets one that reads the variable fall through to updating it.
  • The scale match could land outside the entry. .*scale = could reach into a trailing comment after }), and scale = 3 / 2 was half-rewritten to 2 / 2. ff71531 and 88d37eb keep the match inside the table and only replace a whole value. Each has a test in monitor-scaling-test.sh.

What ran on the worker: monitor-scaling-test.sh (now 21 cases), the other monitor suites, the full shell suite and ./test/cli. The scaling tests all pass. The full suite fails the same three tests it fails on quattro (ascii, the About animation, kernel headers), none of them in files this touches, so it is not green and this is not marked ready. With #13229's layout (a wildcard plus an explicit per-output entry), this branch writes the new scale into the explicit entry and leaves the wildcard alone, so I linked it as fixed here. It does not address #13062's request to recompute positions when the scale changes.

#13605 is a draft fixing the same persistence bug. Its per-output branch is chosen by output name alone, so it has the omarchy_monitor_scale gap described above, and this branch also covers the position bug. Which one lands is the maintainer's call.

Codex Medium gave the second opinion over three rounds. It found the trailing-comment and expression cases, which are fixed above, and its last round, on 88d37eb, found nothing open. It agreed that this branch is the better persistence fix than #13605; its independence is not guaranteed. This now waits on the maintainer.

Comment thread bin/omarchy-hyprland-monitor-scaling Resolved
@jandrusk

jandrusk commented Oct 8, 2026

Copy link
Copy Markdown

Two small things:

  • The description still says the mode drops the @refreshRate suffix, but after fef2ddb1 the rate is kept again (correctly, since Hyprland otherwise requests 60Hz). Could you update the description so it matches?
  • Keeping the position fixes the collapse, but changing the left monitor's scale now leaves a gap (scale up) or an overlap (scale down) with a neighbour placed at a fixed x, which is the Monitor positions don't auto-adjust when scale changes, causing overlap #13062 case. Is that meant as a follow-up, or should it be called out here?

Also, #13605 persists the scale to the focused output rule for the same #13229, so a maintainer will need to pick one persistence path.

Two per-output layouts still lost the chosen scale on reload: an entry
whose table contains a nested table (reserved_area = { top = 24 }) ended
the match at the first }, and ;-separated fields failed the [,}] value
terminator. In both cases the scale was not written, and the fallback
could move the shared omarchy_monitor_scale instead.

Replace the entry match with an awk pass that blanks comments while
keeping offsets, counts braces so the entry's own scale is found, accepts
, and ; as field separators, and rewrites only a whole scalar value
("auto" or a number). Expressions and variable-reading entries fall
through to the existing paths unchanged.

Adds nested-table, semicolon and block-comment cases to
test/shell.d/monitor-scaling-test.sh (24 cases).
@f1n3d

f1n3d commented Oct 8, 2026

Copy link
Copy Markdown
Author

Thanks for the review.

Also pushed a fix for the two remaining greptile findings on the persistence path: an explicit entry with a nested table (reserved_area = { top = 24 }) or ; separators was matched by neither branch, so the chosen scale was silently lost on reload. Both now rewrite correctly, with tests.

@omarchybot omarchybot added verified Omarchy Triage has verified that this issue is ready for final review and removed verified Omarchy Triage has verified that this issue is ready for final review labels Oct 8, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

Re-tested at f177c9d on an Omarchy worker running Hyprland 0.56.2, with two outputs side by side.

  • Position: still fixed. On quattro a scale change moves the focused output from 0x0 to 3200x0; this branch keeps it at 0x0, and reverting just the position change brings the move back.
  • Nested tables: the new commit fixes a real loss. With hl.monitor({ output = "Virtual-1", reserved_area = { top = 24 }, ..., scale = 1 }), the previous head (88d37eb) wrote the new scale into omarchy_monitor_scale, so after a reload the other output was scaled and the focused one was back at 1. This head writes it into the entry, and it survives the reload.
  • Tests: monitor-scaling-test.sh passes 24/24, and so do the other monitor suites. ./test/cli and ./test/shell fail only tests that also fail on the base (the VS Code theme reference, ascii, the About animation, kernel headers).

One thing is left, and it is size rather than a bug. The scanner this commit adds is about 90 lines of awk that tokenise Lua to find one key, and it is still not a full parser: it reads one line at a time, so an entry commented out inside a multi-line --[[ ]] block gets rewritten as if it were live, and it only understands double-quoted strings. omarchy-hyprland-monitor-clamshell already reads these same entries with a single sed in configured_monitor_value, which copes with nested tables and ;. A match in that shape (a separator before scale, then a plain number or "auto" followed by ,, ; or }) would cover the nested-table and semicolon cases at the size of the rest of this PR, and the tests you added would still apply. Could you swap the awk for that?

Codex Medium gave the second opinion on this commit. It found the new tests are real, not vacuous, and reached the same view on size: it called the awk excessive for these two cases, with the clamshell sed as the smaller fix. It also found the multi-line comment and single-quote gaps above. Its other findings need Lua nobody writes in monitors.lua (a function expression as a scale, "auto" .. ""), so I have left them out. Its independence is not guaranteed.

This waits on you for the smaller match. After that, which of this and #13605 lands is the maintainer's call; this one is older and also fixes the position bug.

Replace the Lua-like awk scanner with a smaller sed rewrite for single-line monitor rules and whole scalar values. Preserve the nested-table and semicolon regression cases, skip multi-line block comments, and accept single-quoted outputs and automatic scales.

Add regression coverage for commented rules, single quotes, misleading comment fields, and quoted scale expressions. All 28 scaling checks and all nine monitor-related suites pass.
@f1n3d

f1n3d commented Oct 13, 2026

Copy link
Copy Markdown
Author

Thanks for the re-test and the suggestion. Pushed f02eb58 to replace the AWK scanner with a smaller sed rewrite, following the separator-bounded scalar match used by the clamshell reader. bin/omarchy-hyprland-monitor-scaling is now 79 lines shorter, including removal of the temporary-file/status-code handling.

The existing nested reserved_area table, semicolon, inline-comment, variable-based scale, and 3 / 2 regression tests still pass. The rewrite targets single-line monitor rules and whole scalar values.

Also covered the two gaps you identified: rules inside multi-line --[[ ... ]] comments are skipped, and single-quoted output names and 'auto' are accepted. Four additional regression cases cover those, scale-like text in inline/trailing comments, and leaving "auto" .. "" intact.

Validation for this commit:

  • monitor-scaling-test.sh: 28/28.
  • All nine monitor-related suites pass: scaling, monitor, monitor-state, monitor-output-name, monitor-recovery, monitor-clamshell-scale, monitor-modeless, hw-external-monitors, and sleep-monitor.
  • bash -n on both changed files and git diff --check pass.

Comment thread bin/omarchy-hyprland-monitor-scaling Resolved
Comment thread bin/omarchy-hyprland-monitor-scaling Resolved
@omarchybot omarchybot removed the verified Omarchy Triage has verified that this issue is ready for final review label Oct 13, 2026
Treat comments as whitespace around scale fields and keep live rules before and after block comments reachable. Use a compact Perl match that skips complete comments and strings, preserving their original contents and replacing only whole scalar scales.

Add eight regression cases for the reported boundaries, adjacent comments, long comments, quoted comment markers, and expressions containing comments. All 36 scaling checks and all nine monitor-related suites pass.
@f1n3d

f1n3d commented Oct 13, 2026

Copy link
Copy Markdown
Author

Confirmed both new findings and pushed 3ee4918 to address comments around scale and live code at block-comment boundaries.

The previous simplification missed three cases that worked before it; code after a closing block-comment marker was already unsupported. All four now have regression coverage.

  • Comments are accepted as whitespace before scale, around =, and after the scalar value.
  • Live monitor rules before an opening block comment and after its closing marker are updated.
  • Comments and quoted strings are consumed whole, so monitor rules or scale-like text inside them cannot become rewrite targets. Expressions containing comments remain intact.

The line-based sed skip is replaced by a compact Perl match over the file, using named captures and comment/string skipping. This avoids discarding entire boundary lines and keeps the script at the same line count as f02eb58, still 79 lines shorter than the AWK version.

Added eight regression cases. The new persistence fixtures compare the complete saved config, checking that only the intended scale changes and that comments, other outputs, and the shared default are preserved.

Validation:

  • monitor-scaling-test.sh: 36/36.
  • All nine monitor-related suites pass.
  • bash -n on both changed files and git diff --check pass.

Comment thread bin/omarchy-hyprland-monitor-scaling Resolved
omarchybot and others added 2 commits October 13, 2026 13:54
The explicit-entry rewrite ran perl -i on every scale change, so a config with nothing to persist into was still rewritten. Hyprland reloads on any write to its config, and the reload reapplies monitors.lua, undoing the scale that had just been set: on a worker, scaling an output whose entry reads `scale = 1 * 1` left it at 1 instead of 1.25. Look for a match first and write only when there is one.

The test compares inode and mtime, since an inode alone can be reused by a rewrite, and checks the scale was still applied.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Codex Medium <[email protected]>
Backtracking could end a long string or comment at a later closing bracket, or end a line comment early. Scaling eDP-1 then rewrote DP-2's scale when both entries carried a `--[[ ]]` comment or a `[[...]]` string, and a `scale = 1.25` inside a line comment in a multi-line rule was rewritten as if it were live, which also skipped the omarchy_monitor_scale fallback. The same freedom made a failed match exponential: a rule with twenty inline comments took four seconds and forty did not finish. Atomic groups fix all three.

The unwritten-config test also clears the eval output, so a stale one from the case before cannot satisfy it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Codex Medium <[email protected]>
@omarchybot omarchybot added verified Omarchy Triage has verified that this issue is ready for final review ready Good to merge labels Oct 13, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

Re-tested at 751ee58 on an Omarchy worker running Hyprland 0.56.2, with two outputs side by side.

  • Position: still fixed. On quattro a scale change moves the focused output from 0x0 to 3200x0; this branch keeps it at 0x0, and reverting just the position change brings the move back.
  • Per-output scale: an explicit hl.monitor entry keeps its new scale across a reload. Thanks for swapping the awk for a single match; it is now the size of the rest of the change.
  • Two fixes pushed to your branch:
    • 2713f3a writes monitors.lua only when a rule actually matches. Hyprland reloads on any write to it, so an unconditional rewrite undid the scale that had just been applied.
    • 751ee58 makes comments and long strings atomic in the match. Without that, backtracking could end a --[[ ]] comment or a [[...]] string at a later bracket, so scaling eDP-1 rewrote DP-2's scale, and a scale = 1.25 inside a line comment in a multi-line rule was rewritten as if it were live. The same freedom made a failed match exponential: twenty inline comments took four seconds and forty never finished. With the atomic groups forty take about 60ms. Three regression tests cover those cases.
  • Tests: monitor-scaling-test.sh passes, as do the other monitor suites. ./test/cli and ./test/shell fail only tests that also fail on the base.

Codex Medium gave the second opinion on this head. It found no remaining defect in the matching, confirmed the atomic $long_string keeps \g{-1} working with [=[...]=] and [==[...]==] delimiters, and found that the three new tests fail when the atomic groups are removed, so none of them can pass without the fix. Its independence is not guaranteed.

Nothing is left for you. #13605 fixes the same scale bug a different way. This one is older and also keeps the position, so it is the one recommended, but which lands is the maintainer's call.

@omarchybot omarchybot removed ready Good to merge verified Omarchy Triage has verified that this issue is ready for final review labels Oct 29, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

This branch now conflicts with quattro, and quattro has since fixed both problems it addresses in a different way, so I haven't ported it.

The Perl rewrite of monitors.lua and the scale-to-GDK handling this branch adds would now edit a path that quattro no longer has. One part of it still applies: the check that refuses to scale a monitor whose width, height or refresh rate is not numeric (bin/omarchy-hyprland-monitor-scaling:93 here). quattro reads those values without checking them. Both reviewers suggested it as a small separate change, which should also reject zero dimensions.

This was checked by reading quattro at 7eedd17 against this branch at 751ee58. Nothing was run this time. Codex Medium and muse-wonder (pi, thinking high) each read the same code and reached the same conclusion, though their independence is not guaranteed. Both also flagged the geometry check above.

Thanks @f1n3d for the work here; the earlier rounds did find real cases. Closing this or keeping just the geometry check is now the maintainer's decision.

@f1n3d f1n3d closed this Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

monitor-scaling up/down persists the scale change to the wrong monitor's config when per-monitor rules are used

3 participants