Is there a way to open e.g. a `Dialog` from the `o...
# compose-desktop
l
Is there a way to open e.g. a
Dialog
from the
onClick
of a
MenuItem
? Problem is that the
onClick
is not a composable function. 🤔
1
d
Yes. Use a mutablestate boolean, set it to true in
onClick
then call Dialog if boolean is true.
l
Yes, by changing a state, and have the dialog be shown based on the value of that state.
Dominic, you won this time, by a thin margin!
🥷🏼 1
😄 8
l
Thanks for the help. Appreciate! 🙏
y
At the end of the day your question is very valid/good question because it directly relates to the fundamental shift in thinking between traditional UI programming (callbacks, etc...) and state based (like compose/react, etc..). In this instance, clicking a menu item needs to change the state of the application by stating: this specific dialog window is opened. Until dismissed (Ok/Cancel/...), it stays open. When dismissed, the state changes again. This is why you need to represent this state as suggested by a mutable state boolean (or enum if you have multiple types of dialog windows that could be opened,...). Then in your code you simply do:
Copy code
if(dialogWindowOpened)
{
  // display dialog window
}
See example: https://github.com/JetBrains/compose-jb/tree/master/tutorials/Window_API_new#dialogs
👍 1
l
Thanks @Yan Pujante, I also realized that this boils down to the very fundamentals of writing the UI in compose. Quite a big part of my application needed a rewrite after that, but it feels way better now. 🙂