xetra11
12/25/2020, 8:01 PMonClick
function.
Please see the snippet:
fun main() {
initializeApp()
loadProject()
Window(
title = "CK3 Mod Workbench",
menuBar = MenuBar(
Menu(
"File",
MenuItem(
"Open Project",
onClick = { loadProjectFile(AppWindowAmbient.current!!) }
),
MenuItem("Exit", onClick = { AppManager.exit() })
),
...
And here is the loadProjectFile(appWindow: AppWindow)
function:
private fun loadProjectFile(appWindow: AppWindow) {
val file = File("")
val fileChooser = JFileChooser()
fileChooser.addChoosableFileFilter(ProjectFileFilter())
when (fileChooser.showOpenDialog(appWindow.window)) {
APPROVE_OPTION -> {
val projectManager = ProjectManager()
val projectFile = fileChooser.selectedFile
val projectFromFile = Json.decodeFromString<Project>(projectFile.readText())
projectManager.loadProject(projectFromFile)
}
CANCEL_OPTION -> {
NotificationsService.warn("Cancel project file opening")
}
}
}
And I receive the classical compose error message here:
e: /home/xetra11/Development/projects/app/src/main/kotlin/main.kt: (55, 66): @Composable invocations can only happen from the context of a @Composable function
What I do not understand is why it's saying I do a @Composable
invocation? onClick
seems to have nothing to do with it as far as I can see since @Composable stuff starts at the Window.content property/lambda.
Anyway I attached @Composable
to the loadProjectFile
fun but the error still exists. I feel like I still do not fully understand the conceptDominaezzz
12/25/2020, 8:22 PMAppWindowAmbient.current
-> AppManager.focusedWindow
I think.Dominaezzz
12/25/2020, 8:23 PMAppWindowAmbient.current
is a @Composable
function.xetra11
12/25/2020, 8:29 PMxetra11
12/25/2020, 8:29 PMwindow: ComposableWindow
and used AppManager.focusedWindow
xetra11
12/25/2020, 8:30 PM