xetra11
11/29/2020, 2:09 PMWindow()
call. However it is not allowed due the following error:
(24, 5): Functions which invoke @Composable functions must be marked with the @Composable annotation
(25, 56): @Composable invocations can only happen from the context of a @Composable function
(32, 35): @Composable invocations can only happen from the context of a @Composable function
(33, 16): @Composable invocations can only happen from the context of a @Composable function
This is the snippet:
fun main() {
val characterState: SnapshotStateList<Character> = remember {
mutableStateListOf(
CharacterTemplate.DEFAULT_CHARACTER,
CharacterTemplate.DEFAULT_CHARACTER,
CharacterTemplate.DEFAULT_CHARACTER
)
}
val window = AppWindowAmbient.current!!.window
val file = remember { mutableStateOf(File("")) }
Window(
title = "CK3 Mod Workbench",
menuBar = MenuBar(
Menu("File", MenuItem("Exit", onClick = { AppManager.exit() })),
Menu(
"Characters",
MenuItem("Import characters", onClick = { }),
MenuItem("Dynasties", onClick = {})
),
)
) {
MaterialTheme(
colors = workbenchLightColors(),
shapes = workBenchShapes()
) {
CharacterModuleView()
}
}
}
Igor Demin
11/29/2020, 2:17 PMremember
outside Composable functions (i.e. outside Window).
Because Composable can be called multiple times, remember
helps us to execute code only once.
So just use mutableStateListOf/mutableStateOf
without remember
.xetra11
11/29/2020, 2:21 PMxetra11
11/29/2020, 2:21 PMxetra11
11/29/2020, 2:22 PMIgor Demin
11/29/2020, 2:22 PMxetra11
11/29/2020, 2:24 PMxetra11
11/29/2020, 2:24 PMval window = AppWindowAmbient.current!!.window
however this part still has issues. I have to say here that I have 0 experience in desktop application stuff. Maybe I just can't see the obviousxetra11
11/29/2020, 2:25 PMWindow
comes after itxetra11
11/29/2020, 2:26 PMMenuItem("Import characters", onClick = { }),
doesn't work either - is it Window is no @composable
? If yes - is it sufficient (and good practise) to wrap the logic into a @Composable
?Igor Demin
11/29/2020, 2:26 PMAppWindowAmbient.current
is also a Composable function. So we need to call it inside Window { }xetra11
11/29/2020, 2:29 PMxetra11
11/29/2020, 2:29 PMval fileDialog = FileDialog(window)
fileDialog.mode = FileDialog.LOAD
fileDialog.isVisible = true
file.value = File(fileDialog.directory + fileDialog.file)
xetra11
11/29/2020, 2:29 PMxetra11
11/29/2020, 2:31 PMIgor Demin
11/29/2020, 2:33 PMimport androidx.compose.desktop.AppWindow
import <http://androidx.compose.ui.window.Menu|androidx.compose.ui.window.Menu>
import androidx.compose.ui.window.MenuBar
import androidx.compose.ui.window.MenuItem
import java.awt.FileDialog
import javax.swing.SwingUtilities.invokeLater
fun main() = invokeLater {
lateinit var window: AppWindow
window = AppWindow(
menuBar = MenuBar(
Menu(
"Characters",
MenuItem("Import characters", onClick = {
val fileDialog = FileDialog(window.window)
}),
),
)
)
window.show {
// Composable content
}
}
xetra11
11/29/2020, 2:39 PM