Hi there Trying to open a dialog when pressing a ...
# compose-desktop
r
Hi there Trying to open a dialog when pressing a button (e.g.
onClick
). Although, getting the compiler error
@Composable invocations can only happen from the context of a @Composable function
since
onClick
does not seem to accept a
Composable
function as argument. What would I need to change in my code to get this work? Code:
Copy code
@Composable
private fun showDialogButton(){
    Button(onClick = {openDialog()}){
        Text("Hello Button")
    }
}

@Composable
private fun openDialog(){
    Dialog(onCloseRequest = {}, title = "Dialog"){
        Button(onClick = {}) {
            Text("Close Dialog")
        }
    }
}
m
You have to modify a mutable remembered variable that is read by your top level code and which then invokes the openDialog function inside it. It's kinda messy tbh but comes with the composable model.
r
messy indeed, but thanks for your input 🙂
z
It’s actually a lot more messy to do the other thing once your code scales and you end up with other ways of showing the dialog, and trying to synchronize all the sources of truth to figure out what the actual state of the dialog is.
E.g. when showing a dialog imperatively, who owns the state for whether the dialog should be open at any given time? What if something happens somewhere else in the app and needs to hide the dialog?
k
@mikehearn I need to learn how to read and set variables up and down the levels of a program. I also am beginning to understand the issues with global variables, but now need to learn how is best to store and transfer state, especially ways to call and re-call composables with new data, to update the GUI. It sounds simple, and I get the idea, but achieving the new call with new data is beyond me ATM! Need to do more reading... Can you please elaborate just a little on what you've said here, so I can understand what you meant? Then I can look into it. Thanks! 😎👍
m
@Kebbin I can't help you, I'm afraid. I've only read and tweaked Compose codebases, not written them. I'm waiting for an opportunity to try it out in my next GUI project but didn't start yet. And, I'm actually not convinced yet this way is genuinely better - never had the problems mentioned above with dialogs when writing GUI apps in the past e.g. hiding dialogs for reasons other than user interaction is very bad form anyway unless it's a buttonless progress dialog, as the user can end up mis-clicking. Don't worry that you're finding it hard going though. Compose can be very confusing because of how it changes the fundamental language semantics. I wouldn't dare to try and teach this to new/student programmers, for example. My advice is to experiment with introducing objects and classes again to encapsulate remembered state into widgets, rather than passing everything in the form of function parameters and captured lambda variables. See if it helps.
You can also check out the code of https://github.com/aeab13/Gitnuro/ (it's a desktop app) which has examples of dialog boxes in it
k
Thanks for your response Mike! Very well considered. I will have a look at that repo for interest. What do you use for programming your main projects then? Cheers.
m
In the past I used JavaFX. ATM I'm not doing GUI programming much, but I plan to return soon. Then I'll try out CfD and see how it goes.
😎 1