Skip to content

Give each screen its own scale factor when the screens differ - #1019

Open
probonopd wants to merge 1 commit into
gnustep:masterfrom
probonopd:fix-per-screen-scale
Open

probonopd wants to merge 1 commit into
gnustep:masterfrom
probonopd:fix-per-screen-scale

Conversation

@probonopd

@probonopd probonopd commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

With two monitors of different pixel density, for example a 4K laptop panel next to a 1080p monitor, GNUstep can only use one scale factor: every NSScreen returns GSScaleFactor and every window converts its frame with the factor of the main screen, so windows are either too large on one monitor or too small on the other. This follows up on the discussion in #191 and #189 and keeps the model settled there: window and screen frames stay in device pixels and the scale stays in libs-gui.

This PR belongs together with gnustep/libs-back#253, which makes the X11 backend report a resolution per monitor (from the RandR output property "DPI" that XLibre creates on every output, 96 = 1.0x). Please merge this one first: on its own it changes nothing, because no backend reports different resolutions for different screens yet (x11 answers 72 dpi, win32 the same system DPI for every screen).

When the backend reports different resolutions for the screens, each NSScreen takes its factor from its own resolution. A window keeps the factor it is laid out with in _scaleFactor (the former _reserved_1, same size, so no ABI change), and the frame/content conversions of GSWindowDecorationView use the window's factor and its own decoration offsets instead of the main screen's. When a move, a screen parameter change or ordering in gives a window a screen of another factor, it converts its frame so the content keeps its size in points, lays itself out again and posts NSWindowDidChangeBackingPropertiesNotification with NSBackingPropertyOldScaleFactorKey. A window switches only when it would cover more of the other screen by 8 pixels, so a window on the edge switches once; desktop windows keep their factor, and a window that keeps switching stops and logs a warning. When all screens report the same resolution, which includes every single-monitor setup, nothing changes.

Two new tests, run against this branch's library (LD_LIBRARY_PATH=Source/obj, checked with ldd) on Xvfb:

$ cd Tests/gui && gnustep-tests NSScreen/scaleFactor.m && gnustep-tests NSWindow/scaleFactor.m
      5 Passed tests
     35 Passed tests

The NSScreen test against unpatched master (cacd5cb):

Passed test:     scaleFactor.m:32 ... two screens
Failed test:     scaleFactor.m:34 ... a screen of 144 dpi next to one of 72 dpi has the factor 2
Failed test:     scaleFactor.m:36 ... the screen of 72 dpi next to it has the factor 1
Passed test:     scaleFactor.m:44 ... screens of the same resolution use GSScaleFactor
Passed test:     scaleFactor.m:52 ... a single screen uses GSScaleFactor

Live, on an Xorg with two dummy outputs (DPI 192 and 96) and both PRs applied, a window with a 300x200 content rect is 600x400 pixels on the first output and 300x200 pixels on the second, its content stays 300x200 points in both directions, and changing DPI at run time converts open windows.

Known limits: font hinting still follows the global GSScaleFactor; saved frames are pixels, so a frame saved on a 2x monitor is converted only when the window is ordered in; a window manager that ignores the client's resize during an interactive move leaves the old pixel size until the next resize; applications that cache [[NSScreen mainScreen] userSpaceScaleFactor] still assume one factor.

cc @pkgdemon

-userSpaceScaleFactor returned GSScaleFactor for every screen, and the
frame and content rect conversions of every window used the factor of
the main screen, so a monitor of high pixel density next to a normal
one could only get one factor for both.

When the backend reports different resolutions for the screens, each
screen takes its factor from its own resolution. A window keeps the
factor it is laid out with in _scaleFactor (the former _reserved_1, so
the instance size does not change) and converts its frame and its
decoration offsets with it. When a move, a screen parameter change or
ordering in gives a window a screen of another factor, the window
converts its frame so its content keeps its size in points, lays itself
out again and posts NSWindowDidChangeBackingPropertiesNotification with
NSBackingPropertyOldScaleFactorKey. A window switches only when it would
cover more of the other screen by a margin of 8 pixels, desktop windows
keep their factor, and a window that keeps switching stops and logs a
warning. -resetCursorRects compares the pointer with the frame of the
decoration view rather than its bounds, which are points.

When all screens report the same resolution, which includes every
single-monitor setup, nothing changes and GSScaleFactor is used as
before.
@probonopd
probonopd force-pushed the fix-per-screen-scale branch from 7b94a76 to 98993da Compare October 26, 2026 19:25
@rfm

rfm commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

This looks good to me, but I guess this and all the outstanding screen related issues in gui and back, really ought to be applied more or less together, and I think we need to take care about the order they are applied in too.
I don't think we can reasonably do this before the next release, unless we (particularly you and Fred) have availability at some point for us to get together on a call and work through it all. But I think the chances of that are small: I'm away this coming weekend, and busy the following weekend too, and I think Fred has commitments next week as well). But maybe we will want to push the release right to the end of November anyway?

@probonopd

Copy link
Copy Markdown
Contributor Author

Agree that this should be looked at after the release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants