https://kotlinlang.org logo
Title
d

David W

12/01/2021, 12:31 AM
Is there a way to set the background color of the native window before content is rendered? There's a white flash before the app's (dark) content appears. All of the answers I saw for this were specific to Android.
I even tried making the window not visible for a second to let the content render, but it still flashed white on making it visible...perhaps content doesn't render while it's invisible.
t

Thomas

01/15/2022, 1:52 AM
Hey, I was wondering did you find a good solution for this?
d

David W

01/15/2022, 1:53 AM
Nope, it's on my "wait and see" pile for Compose 🙂
t

Thomas

01/15/2022, 2:08 AM
Yeah I am trying to fix a few things from that pile
The JetBrains Toolbox app does not flash white so it should be possible
d

David W

01/15/2022, 2:12 AM
No, it is transparent (but with the OS frame) until everything is drawn, then it appears.
good point
t

Thomas

01/15/2022, 2:32 AM
Found the issue, you can set the background color in the Window content using
window.background =
, but at that point the window is already shown, so its too late.
The line I linked is where you need to call
this.window.background = …
which works
ComposeWindow is actually a JFrame, which has a default background color
Calling the following before creating a window also works, but not sure what it all affects:
javax.swing.UIManager.getDefaults().apply {
    put("control", javax.swing.plaf.ColorUIResource(java.awt.Color.RED))
}
I am now using the following workaround. Call it just before the Window function.
@Composable
private fun fixWhiteFlashOnStartup() {
    val background = MaterialTheme.colors.background
    remember(background) {
        UIManager.getDefaults().apply {
            put("control", ColorUIResource(background.toArgb()))
        }
    }
}
d

David W

01/15/2022, 2:45 AM
interestingly, i'm still getting the flash
I got it after trying
javax.swing.UIManager.getDefaults().apply {
    put("control", javax.swing.plaf.ColorUIResource(java.awt.Color.RED))
}
too
t

Thomas

01/15/2022, 2:46 AM
Did you call that before Window(onCloseRequest..)?
It works for me on macOS
d

David W

01/15/2022, 2:46 AM
Windows here.
t

Thomas

01/15/2022, 2:47 AM
Hmm maybe this trick does not work on Windows, will have to try that
d

David W

02/08/2022, 8:36 PM
thank you! you're the best