I fail to open a @Composable function inside the M...
# compose-desktop
x
I fail to open a @Composable function inside the MenuItem
onClick
function. Please see the snippet:
Copy code
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:
Copy code
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:
Copy code
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 concept
d
AppWindowAmbient.current
->
AppManager.focusedWindow
I think.
1
AppWindowAmbient.current
is a
@Composable
function.
x
Ok that kinda led me to the fix
changed the parameter for the function to
window: ComposableWindow
and used
AppManager.focusedWindow
now it works - still confusing cause of the many ways of having windows and appmanagers etc. 😵