From ee994e20978e19d079cbfd0bd6c509a2bf154286 Mon Sep 17 00:00:00 2001 From: Sebastiano Poggi Date: Wed, 10 Apr 2024 10:49:38 +0200 Subject: [PATCH] Update tree sample to show how to do modifications The tree was remembered but not as a state, so updating it didn't work. --- .../standalone/view/component/ChipsAndTree.kt | 43 +++++++++++++------ 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/samples/standalone/src/main/kotlin/org/jetbrains/jewel/samples/standalone/view/component/ChipsAndTree.kt b/samples/standalone/src/main/kotlin/org/jetbrains/jewel/samples/standalone/view/component/ChipsAndTree.kt index 4c35bd49d..1dcb47a02 100644 --- a/samples/standalone/src/main/kotlin/org/jetbrains/jewel/samples/standalone/view/component/ChipsAndTree.kt +++ b/samples/standalone/src/main/kotlin/org/jetbrains/jewel/samples/standalone/view/component/ChipsAndTree.kt @@ -38,10 +38,12 @@ import org.jetbrains.jewel.ui.component.Chip import org.jetbrains.jewel.ui.component.CircularProgressIndicator import org.jetbrains.jewel.ui.component.GroupHeader import org.jetbrains.jewel.ui.component.LazyTree +import org.jetbrains.jewel.ui.component.OutlinedButton import org.jetbrains.jewel.ui.component.RadioButtonChip import org.jetbrains.jewel.ui.component.Text import org.jetbrains.jewel.ui.component.ToggleableChip import org.jetbrains.jewel.ui.theme.colorPalette +import kotlin.random.Random @Composable @View(title = "Chips and trees", position = 11, icon = "icons/showAsTree.svg") @@ -183,24 +185,37 @@ fun ChipsSample(modifier: Modifier = Modifier) { @Composable fun TreeSample(modifier: Modifier = Modifier) { - val tree = remember { - buildTree { - addNode("root 1") { - addLeaf("leaf 1") - addLeaf("leaf 2") - } - addNode("root 2") { - addLeaf("leaf 2.1") - addNode("node 1") { - addLeaf("subleaf 1") - addLeaf("subleaf 2") + var tree by remember { + mutableStateOf( + buildTree { + addNode("root 1") { + addLeaf("leaf 1") + addLeaf("leaf 2") + } + addNode("root 2") { + addLeaf("leaf 2.1") + addNode("node 1") { + addLeaf("subleaf 1") + addLeaf("subleaf 2") + } + } + addNode("root 3") { + addLeaf("leaf 3.1") + addLeaf("leaf 3.2") } } - addNode("root 3") { - addLeaf("leaf 3.1") - addLeaf("leaf 3.2") + ) + } + + OutlinedButton({ + tree = buildTree { + addNode("root ${Random.nextInt()}") { + addLeaf("leaf 1") + addLeaf("leaf 2") } } + }) { + Text("Update tree") } val borderColor =