Is there a way to determine the DPI of the monitor...
# compose-desktop
s
Is there a way to determine the DPI of the monitor where the Window is displayed? This gives only the scale factor:
Copy code
window.graphicsConfiguration?.defaultTransform?.scaleX
Say I want to build a ruler composable that displays a box that's exactly 10cm long.
1
I know how to ask for DisplayMetrics on Android and how to ask the AWT Toolkit for a (wrong) value. My question is: Is there something build into SKIJA for example that does a better job on Desktop?
a
AFAIK you need to read EDID to get the physical size of the screen. And note that it is not always accurate.
s
Thank you for that hint. Looks like something that someone should build into compose-jb 😉
m
That’s how I do it but it is not perfect.
Copy code
val renderScale = AppManager.focusedWindow?.window?.graphicsConfiguration?.defaultTransform?.scaleX ?: 1.0
val resolution = Toolkit.getDefaultToolkit().screenResolution
val nativeDPI = renderScale * resolution
🙏 1
s
Thank you, that's good enough for me and results on macOS in the correct value (254 DPI). The specs say DPI is 227, but I found working with self-estimated 257 dpi turns out to result into the right length of my ruler. 🙂
u
can’t you just convert inch/cm to dp?
s
Can I? 🙂
m
You can do something like this:
Copy code
val px = LocalDensity.current.run { 1.dp.toPx() }
println("pixels: $px")
But that is just an alternative to get the
renderScale
, isn’t it?
s
Yes, that always returns 2.0 on macOS with Retina.
@uli I don't think that there is a better solution than presented by @Michael Paus
m
The solution should be improved by using the Window specific toolkit and not the default one but I don’t know how to get at that. This would be important in a multi-monitor setting.
👍 1
u
The original question was about drawing a ruler. Say you want a spacing of 1inch between ticks, then you’d just go with 160dp and the system would do the conversion to screen pixels for you
m
Who defined that 160dp maps to exactly 1inch on any screen that you may have attached to your system and with any combination of screen settings a user may choose?
👍 1
u
I would assume, it does basically, what is described above. Cause 160dp are defined to be one inch. Probably just not always exactly implemented, just as the other resolution apis above are flakey. https://en.m.wikipedia.org/wiki/Device-independent_pixel
a
@uli That is not true as the user can change scale factor on Windows/macOS/Linux(all major desktop envs), which essentially changes the DPI. For example on Windows:
Also, people have different preferences—some people prefer larger text. For this reason, Windows enables the user to change the DPI setting. For example, if the user sets the display to 144 DPI, a 72-point font is 144 pixels tall. The standard DPI settings are 100% (96 DPI), 125% (120 DPI), and 150% (144 DPI). The user can also apply a custom setting.
👍 2