Last active
January 4, 2024 03:25
-
-
Save hakanai/a9ec53c015c54b7de10729e75fa05069 to your computer and use it in GitHub Desktop.
Demonstration of recomposition not occurring if the initial window list was empty
This file contains 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
import androidx.compose.foundation.layout.* | |
import androidx.compose.material.* | |
import androidx.compose.runtime.* | |
import androidx.compose.ui.* | |
import androidx.compose.ui.window.* | |
import kotlinx.coroutines.* | |
data class SystemProfile( | |
var name: String | |
) | |
private class MyWindowState( | |
val title: String, | |
val systemProfile: SystemProfile, | |
private val close: (MyWindowState) -> Unit | |
) { | |
fun close() = close(this) | |
} | |
private class MyApplicationState { | |
val windows = mutableStateListOf<MyWindowState>() | |
fun newWindow(systemProfile: SystemProfile) { | |
windows.add( | |
MyWindowState( | |
title = "Blah title", | |
systemProfile = systemProfile, | |
close = windows::remove | |
) | |
) | |
} | |
} | |
@Composable | |
fun SpxViewer(systemProfile: SystemProfile) { | |
MaterialTheme { | |
Surface(modifier = Modifier.fillMaxSize()) { | |
Text("Details would go here.") | |
Text("systemProfile = $systemProfile") | |
} | |
} | |
} | |
fun main() = application { | |
val applicationState = remember { MyApplicationState() } | |
LaunchedEffect(Unit) { | |
// Simulate reading from file | |
delay(2000) | |
val systemProfile = SystemProfile(name = "Blah") | |
applicationState.newWindow(systemProfile) | |
} | |
// After the first call, we never get here again, because adding a window | |
// to the list doesn't trigger recomposition. | |
for (window in applicationState.windows) { | |
key(window) { | |
Window(onCloseRequest = window::close, title = window.title) { | |
SpxViewer(window.systemProfile) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment