Should it be possible to pass data from one window...
# compose-desktop
e
Should it be possible to pass data from one window to another? My use-case: I open a new window from another and I pass data into the composable that hosts the window. However, when that data changes I get an AWT error:
java.awt.IllegalComponentStateException: The window is showing on screen
I'm guessing that compose tries to create a new window and AWT says it's already there, but all I want is the data to be passed.
d
How are you passing this data? With a shared mutable state?
e
No, it's a list of immutable elements.
d
Oh, are you calling the window function inside another window function?
e
Yes, which should be possible since both are composables. I found what is causing the issue:
Copy code
state = WindowState(size = DpSize(1000.dp, 1000.dp)),
If I pass this to the window function it breaks, but if I omit it, it works.
Copy code
rememberWindowState
Is probably the solution here
Yep, that solved it! 🙂
I'll still file a bug, because I don't think the error makes any sense. The component should be able to diff the state changes without any issues.
d
I don't think you are supposed to nest them. They might both be composables but the appliers will be different. Since the
application
applier would be for creating windows and the
Window
applier would be for creating UI elements.
#compose will have more knowledgeable people.
e
I disagree, and that is not at all how I interpret what
application
and
Window
are.
application
does not create any windows, that is what
Window
does. Not being able to nest composables is kind of against the whole point of compose IMHO.
d
Maybe
Window
should be changed from a Composable to a non Compose method, like we do with
LazyColumn
and
item
But then it will be harder to have states and listen to them inside the
application
method. I think
Window
shouldn't be nested, but it's hard to stop you from using it inside another
Composable
e
@Dragos Rachieru why don't you think windows should be nested? It's a fairly common occurrence both on desktop and Android to open windows from windows. One common example is dialogs. Not having them part of compose creates a complex situation where part of your ui is in compose and part of it is not. Windows just like views/composables tend to be hierarchal.
d
I think a better way to do this is to have a shared instance of a class that handles this and emit changes from window 1 to show window 2
d
Yes, nesting can be achieved using PopUp or Dialog. Window isn't designed for nesting I think.
d
And have the application code handle the emitted changes
e
@Dominaezzz But a dialog is a window, so I'm not sure how you can separate those two? https://developer.android.com/reference/kotlin/androidx/compose/ui/window/DialogWindowProvider I also wonder why you think it's not designed for nesting?
This seems to be the recommended path for multiple windows: https://github.com/JetBrains/compose-jb/blob/master/tutorials/Window_API_new/README.md#open-and-close-multiple-windows So, it's still in the composable tree, but captured with a mutable state. It would be interesting to hear how JB's thoughts about how they think of the Window composable. If you can compose it, people will do it. So, if it's unintentional we probably want to address that on an API level.
d
I don't think it's designed for that, mostly because I wrote it 😅.
e
LOL. I guess that answers it then 😂
Is the linked I provided above the best practice for multiple windows?
d
Yes. That, I didn't write but I do agree with it.