(After looking for an answer all of yesterday, I have decided to ask this question as I could not find it anywhere else.)
I have a small example app that I want to use in order to test the way that Compose works for Desktop Applications.
I have been able to create an application UI with the 'fun main() = application {', etc. But this, at least for the moment, seems to limit me to only the one view.
Is there a way to develop multiple different views and then change from one to the other (for a Desktop Application)?
I have tried multiple ways in order to access the other custom views that I am looking for but they are not working as intended.
My higher intention was to create an MVC Architecture for this small Kotlin project (where I could swap to many different views that hold different functionality), but I am happy to focus on the smaller question of the development of the views and the changing from one to the other, for now (hopefully I can use an example and extract the functionality later).
Example (without proper code):
View 1 - initial startup view - Displays buttons with text
View 2 - view that can be changed to (expected that the views can swap) - Displays Tables with text
Common for each - Button or Menu that can be used to swap between the different views.
I have tried (unsuccessfully) to:
- use the Composable functions, but am not able to switch views from the onClick function - error : @Composable invocations can only happen from the context of a @Composable function
- Search other tutorials that provide this way of working. I can only see single view projects/examples - nothing that would be able to change effectively. Assumed looking for the wrong search terms.
My Sample Project - Please excuse if it doesn't work, I have modified it to reduce the problem.
@OptIn(ExperimentalComposeUiApi::class)
fun main() = application {
var action by mutableStateOf("Last action: None")
Window(
onCloseRequest = ::exitApplication,
title = "TestProject",
state = WindowState(width = 1300.dp, height = 750.dp)
) {
Column() {
TextButton(onClick = {}, modifier = Modifier.padding(8.dp)) {
Text(text = "Change View")
}
}
MenuBar {
Menu("Tasks", mnemonic = 'T') {
Item(
"Create new Task",
onClick = { action = "Last action: Create new Task" },
shortcut = KeyShortcut(Key.C, ctrl = true)
)
}
}
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
Text(text = action)
}
}
}
As above in 'tried 1' - added setView() - as an example, I would for the moment assume that there are only 2 views and haven't generalised the function, in this example, to incorporate 2+ views or realistically to pass any one view at a time - setView(View):
TextButton(onClick = {setView()}, modifier = Modifier.padding(8.dp)) {
Text(text = "Change View")
}
I had seen that there were 'when' statements that involved states, but wasn't sure if that was appropriate for Desktop Applications, etc. (I had hoped that I wouldn't have to load all of the views before switching them and only relying on the one view to be used at a time) Most of the tutorials I had seen were for Android rather than custom views for Desktop.
Any help would be appreciated. Thanks (if you got this far :) )