Animesh Sahu
11/29/2020, 10:15 AMjava.awt.Toolkit.getDefaultToolkit().screenSize
the correct way to obtain current screen dimensions?
If yes, how'd ya obtain it in K/N in the future? Otherwise what's the correct way...Igor Demin
11/29/2020, 10:39 AMAppWindowAmbient.current!!.window.graphicsConfiguration.device.displayMode.width
And we also need to listen changes of the screen size:
@Composable
private fun screenSize(): IntSize {
val window = AppWindowAmbient.current!!.window
var size by remember {
mutableStateOf(
IntSize(
window.graphicsConfiguration.device.displayMode.width,
window.graphicsConfiguration.device.displayMode.height,
)
)
}
onActive {
val listener = AWTEventListener {
size = IntSize(
window.graphicsConfiguration.device.displayMode.width,
window.graphicsConfiguration.device.displayMode.height,
)
}
Toolkit.getDefaultToolkit().addAWTEventListener(listener, AWTEvent.PAINT_EVENT_MASK)
onDispose {
Toolkit.getDefaultToolkit().removeAWTEventListener(listener)
}
}
return size
}
(listener code is from https://stackoverflow.com/questions/7455283/detect-screen-resolution-change-made-by-user-java-listener/13704277)
In K/N it will be in a completely different way 🙂 .
Maybe it is worth to provide a common API for this in the future.Animesh Sahu
11/29/2020, 1:12 PMIgor Demin
11/29/2020, 1:26 PMimport androidx.compose.desktop.AppWindow
import javax.swing.SwingUtilities.invokeLater
fun main() = invokeLater {
val window = AppWindow()
val dpi = graphicsConfiguration.defaultTransform.scaleX.toFloat()
val width = (graphicsConfiguration.device.displayMode.width / dpi).toInt()
window.setSize(width / 3, 100)
window.setWindowCentered()
window.show {
// Composable content
}
}