Skip to content

Rescale the UI when the screen DPI changes while the app runs (#1162, #1163) - #1183

Merged
buggins merged 1 commit into
masterfrom
runtime-dpi-1162-1163
Nov 24, 2026
Merged

buggins merged 1 commit into
masterfrom
runtime-dpi-1162-1163

Conversation

@buggins

@buggins buggins commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Fixes #1162, fixes #1163.

When the desktop scale changes while the app runs, the UI is now rescaled instead of keeping its startup size (X11) or being stretched as a bitmap (Win32).

Common

  • Platform.setSystemScreenDpi(dpi): sets the DPI detected by the backend. When SCREEN_DPI changes, it reloads the fonts and images cached for the old DPI and lays the windows out again. The SDL backend did this inline and now uses it.
  • isSupportedScreenDpi() in core/types.d: the 72–500 range, shared by the SCREEN_DPI setter, overrideScreenDPI and Win32. A rejected value is logged once.
  • Window.scaleClientSizeLimitsToScreenDpi(): min/max client sizes are scaled from the DPI they were set for before a DPI resize, so they don't hold a window at its old size.

X11 (#1162)

  • PropertyChangeMask on the root window of screen 0, where RESOURCE_MANAGER lives, selected before Xft.dpi is first read. Each change reads Xft.dpi again from the property (XResourceManagerString() keeps the value from when the display was opened). Without Xft.dpi: 96, as at startup.
  • The change is applied in the outermost pumpEvents(), not while getClipboardText() waits for the selection.
  • Windows in normal state are resized by the DPI ratio from their last configured size, and kept in the work area. Maximized, minimized and fullscreen windows keep their size.

Win32 (#1163)

  • Per-monitor DPI awareness (v2) where available (SetProcessDpiAwarenessContext), else SetProcessDPIAware as before.

  • WM_GETDPISCALEDSIZE (Windows' question for the size at the new DPI), answered with the frame for the new DPI plus:

    • during setWindowState(), the client size it is setting (so a restored window doesn't grow);
    • otherwise the current client size scaled from the SCREEN_DPI it was made for (updated on every size change) to the new one.

    That means: with an override, nothing changes; after a system scale change, every window is resized; a dialog created on another monitor and moved over its parent keeps its size. Minimized and maximized windows are left to Windows.

  • WM_DPICHANGED applies the rectangle Windows suggests, as Microsoft documents.

  • Frames are computed for the window's DPI (AdjustWindowRectExForDpi, falling back to AdjustWindowRectEx) when restoring sizes and for min/max sizes. setWindowState() reads the reached rectangle back (normal state only).

  • The first window takes the DPI of the monitor it is created on (GetDpiForWindow), with its client size scaled (not for fullscreen windows). Later windows don't change the global DPI on creation.

Limitations

SCREEN_DPI stays global: with windows on monitors of different scales, the last change wins. Follow-ups: per-window DPI (#1184), Win32 window icon sizes per monitor (#1185), smaller points from the last review (#1187). Found during the review: #1186 (an older Win32 bug with multisampling and OpenGL disabled).

Testing

  • dub test: Linux with LDC 1.36 and DMD 2.107.1, Windows, and -c console; no leak lines. D-Scanner: clean. Builds: default, x11, Win32.
  • New unittest: setSystemScreenDpi() reloads and lays out only on an actual change, and not under an override; checked that it fails with a broken version.
  • X11, on a private Xvfb server (:99, example1 x11 config, Xft.dpi changed with xrdb):
    • 96 → 144 → 96: the UI is rescaled, and the window grows (up to the screen) and shrinks back.
    • 30 runs with four quick changes each (144, 120, 192, 96), then a normal close: all clean, no leak lines.
    • Two early runs didn't close properly (one ignored WM_DELETE_WINDOW, one exited without its exit log). They weren't reproducible afterwards (50+ runs). The window-map iteration that could modify the map during the loop was fixed since; no proof it was the cause.
    • Out-of-range Xft.dpi: 600 logs the warning and keeps the DPI.
    • Needs a window manager's _NET_WM_* atoms; on bare Xvfb they had to be created first (X11: the app dies with BadAtom on an X server without a window manager #1181).
  • Win32, example1 with a temporary hook (not committed) that sends WM_GETDPISCALEDSIZE and WM_DPICHANGED to its own window, as Windows does: 120 → 144 → 120 rescales the UI sharply and resizes the window. With overrideScreenDPI the client size follows the content's scale. No leaks in ui.log.
  • Not verified: a real display-scale change, dragging between monitors with different scales, GetDpiForWindow on a secondary monitor (single-monitor machine here), and desktop environments beyond plain Xft.dpi changes.

🤖 Generated with Claude Code

…1163)

Platform.setSystemScreenDpi() sets the DPI detected by the backend and, when
SCREEN_DPI changes, reloads the fonts and images cached for the old DPI and
lays the windows out again (what the SDL backend did inline). A value
outside the supported range (isSupportedScreenDpi(), now shared with the
SCREEN_DPI setter) is logged once. Windows scale their client size limits
from the DPI they were set for before they are resized.

X11 (#1162): the app selects PropertyChangeMask on the root window of
screen 0 and reads Xft.dpi from RESOURCE_MANAGER again when it changes
(XResourceManagerString() keeps the value from when the display was opened).
The change is applied in the outermost pumpEvents(), not while
getClipboardText() waits. Windows in normal state are resized by the DPI
ratio and kept in the work area.

Win32 (#1163): the process is per-monitor DPI aware (v2) where available,
else system DPI aware as before. WM_GETDPISCALEDSIZE answers the size for
the new DPI: the client size setWindowState() is setting, else the current
one scaled from the SCREEN_DPI it was made for to the new one, with the
frame for the new DPI; WM_DPICHANGED applies the rectangle Windows suggests.
Frames are computed for the window's DPI (AdjustWindowRectExForDpi). The
first window takes the DPI of the monitor it is created on.

SCREEN_DPI stays global: with windows on monitors of different scales, the
last change wins.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Win32: a DPI change while the app runs doesn't rescale the UI X11: a DPI change while the app runs doesn't rescale the UI

1 participant