Divide physical pixels by the window’s scale to get logical layout units. OnePlatform reports per-monitor DPI on Windows; X11 uses a desktop-wide scale.
dotnet add package RecursiveAI.OnePlatformRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OnePlatform; using var app = PlatformApp.Create(); var window = app.CreateWindow(new() { Title = "DPI demo" }); PrintWidth(); while (true) { app.PumpEvents(100); while (app.TryGetEvent(out var evt)) { switch (evt.Kind) { case PlatformEventKind.WindowScaleChanged: PrintWidth(); break; case PlatformEventKind.WindowCloseRequested: case PlatformEventKind.QuitRequested: return; } } } void PrintWidth() => Console.WriteLine( FormattableString.Invariant($"{window.Width / window.Scale:0.##}"));
The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.
This prints the logical width at startup and after a scale change. Close the window to exit. On X11, changing the desktop scale from 100% to 150% printed:
1280 853.33
Full program
using OnePlatform; using var app = PlatformApp.Create(); var window = app.CreateWindow(new() { Title = "DPI demo" }); ReportSize(); while (true) { app.PumpEvents(100); while (app.TryGetEvent(out var evt)) { if (evt.Kind is PlatformEventKind.WindowCloseRequested or PlatformEventKind.QuitRequested) return; if (evt.Kind is PlatformEventKind.WindowScaleChanged or PlatformEventKind.WindowResized) ReportSize(); } } void ReportSize() { var scale = window.Scale; var width = window.Width / scale; var height = window.Height / scale; Console.WriteLine(FormattableString.Invariant( $"Scale {scale:0.##}: {width:0.##} × {height:0.##} logical")); }
The download reports the logical width and height on startup and again when the scale or client size changes. This run started at Xft.dpi: 96, then changed the X11 desktop setting to Xft.dpi: 144 while the window was open:
Scale 1: 1280 × 720 logical Scale 1.5: 853.33 × 480 logical
A monitor change only yields a new scale if the backend reports a different value. On X11, moving between monitors alone doesn’t change this desktop-wide setting.
Variations
For a renderer that needs a backing buffer in physical pixels, keep the original client size. Add this to ReportSize in the full program:
Console.WriteLine(FormattableString.Invariant( $"Buffer: {window.Width} × {window.Height}"));
Related recipes
The short answer
In a OnePlatform window, divide Width, Height, or a window-local pointer coordinate in pixels by window.Scale to get logical units. Use those units for layout and hit testing. On WindowScaleChanged or WindowResized, read the current window values and update the layout again. Use the physical client size for the renderer’s buffer, so it still has a pixel for each physical pixel in the client area.
How do you detect when a window’s DPI changes?
Read window.Scale when the window opens rather than assuming 100%. Then handle WindowScaleChanged in the event queue. The event carries the new scale and the ID of the affected window. If you’re managing several windows, look up the matching window before updating its layout. Listen for WindowResized too: the available layout area can change even at the same scale. The full program uses the window’s current size and scale after either event so both updates go through one calculation.
On Windows, OnePlatform’s Win32 backend requests Per-Monitor V2 DPI awareness. It converts the window DPI to a scale by dividing by 96. The same event handler can respond to an X11 settings change, but that’s not a per-monitor event: the X11 backend reads Xft.dpi for the desktop. The platform support table lists how each backend gets its scale.
How do you convert physical pixels to logical pixels at fractional DPI?
Keep the scale fractional. At 150%, one logical unit spans 1.5 physical pixels. A 300-pixel client width is 200 logical units; a pointer 75 physical pixels from the client’s left edge is 50 logical units from that edge. Divide when reading from pixel space; multiply when converting back to pixel space. Keep fractions in your layout, then round only where you need a whole pixel. If you divide the buffer size as well as the layout size, you no longer have a buffer sized to the real client area.