[patch] Add native macOS DPI detection path#263
Merged
Conversation
Modern macOS has no X11, but GetActualScaleFactor routed macOS into the X11 branch because XDG_SESSION_TYPE is null and matched `null or "x11"`. That P/Invoked libX11.so.6, which does not exist on macOS, throwing DllNotFoundException. Add a macOS branch that reads the main display backing scale via CoreGraphics (CGDisplayModeGetPixelWidth / CGDisplayModeGetWidth), giving 1.0 on standard displays and 2.0 on Retina. Falls back to the standard DPI scale when the display mode is unavailable or CoreGraphics cannot be loaded (for example, a headless host).
The macOS DPI branch was all native CoreGraphics interop that only runs on a Mac with a display, so it registered 0% new-code coverage on the Windows coverage runner and tripped the SonarCloud coverage gate. Extract the pure pixel-to-point scale arithmetic into an internal MacOSBackingScaleToDpi helper and unit-test it (standard, Retina, fractional, zero-width fallback). Mark the thin native wrapper GetMacOSDpiScale with [ExcludeFromCodeCoverage] and a justification, matching the repo's treatment of other C ABI boundary code. Microsoft Code Coverage honors the attribute, removing the unreachable lines from the denominator.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Problem
On modern macOS there is no X11 (XQuartz is not present by default). But
ForceDpiAware.GetActualScaleFactor()only special-cased Windows, then readXDG_SESSION_TYPEand fell into the X11 branch whenever it wasnull— which it always is on macOS — via thenull or "x11"match. That P/InvokedlibX11.so.6, a Linux-only library absent on macOS, throwingDllNotFoundExceptionand taking the app down on startup DPI detection.Fix
Add a native macOS branch before the X11/Wayland fork.
GetMacOSDpiScale()reads the main display's current mode via CoreGraphics and takes the pixel-width to point-width ratio as the backing scale factor:CGDisplayModeGetPixelWidth / CGDisplayModeGetWidth→1.0on a standard display,2.0on Retina.scale * StandardDpiScale(96).finally(Core Foundation "Copy" ownership rule).StandardDpiScaleon a null mode, zero width, or a missing library/entry point (for example, a headless host), rather than crashing.The CoreGraphics P/Invokes load by full framework path since macOS frameworks are not on the default dylib search path, and they omit
DefaultDllImportSearchPaths(that attribute controls the Windows loader only).OperatingSystem.IsMacOS()is a recognized platform guard, so the native calls stay analyzer-clean, andForceDpiAware.csis already<Compile Remove>d onnet10.0-ios, so there is no Mac Catalyst / iOS overlap.Verification
dotnet buildclean, zero warnings (warnings-as-errors is on).ForceDpiAwaretests: 7/7 pass on Windows, confirming the existing Windows path is unchanged.Not runtime-verified on macOS. The new branch only executes on a Mac and could not be exercised from the Windows dev environment. Manual check on a Mac:
dotnet run --project examples/ImGuiAppDemoshould launch (previously crashed) and scale correctly on a Retina display.🤖 Generated with Claude Code