How to properly implement a Modal

Hi all!

I tried to use Modal from iced_aw 0.7.0 like it’s described in the documentation, but it isn’t working.
The documentation tells me, it only needs two arguments (1 underlay and 1 overlay), but also talks about an “state”. I don’t know where to store, access and give that “state”-argument. So the situation now is, that modals are always open and never close.

See: Modal in iced_aw::native::modal - Rust

pub fn new(
    underlay: impl Into<Element<'a, Message, Renderer>>,
    overlay: Option<impl Into<Element<'a, Message, Renderer>>>
) -> Self

Creates a new Modal wrapping the underlying element to show some content as an overlay.

state is the content’s state, assigned at the creation of the overlying content.

It expects: * the underlay Element on which this Modal will be wrapped around. * the optional overlay Element of the Modal.

In 0.5.0 (with iced 0.9.0) it worked quite fine.

I would be glad for any advice :slight_smile:

The state argument remains from the docs of iced_aw 0.2.0 (see Modal in iced_aw::native::modal - Rust). The documentation should be updated. Did you try giving a Message to Modal::on_esc so it knows what to do on escape?

I highly recommend checking out the modal example in the iced repo:

I wrote this, so if you have any questions let me know!

Thanks for the advice.

Is there any reason, why iced_aw 0.7.0 reversed the implementation of the state from inside the modal to the programmer outside?

I’m not familiar with the iced_aw implementation unfortunately. I’d stick with the example implementation for reference.

Thanks for the advice. I tried it as in 0.2.0, but it didn’t work for me. In 0.7.0 it seems to accept only 2 widgets and no additional argument. :frowning:

And I did add the .on:esc and .on_exit.

Is Modal not a iced_aw-only widget?

Modal windows are not a iced_aw native idea, and have been implemented in various ways. iced_aw’s Modal is a helper widget to create them.

I think you misunderstood what @thunderstorm010 is saying. Here he says that the documentation is outdated and does not correspond to the function’s prototype. The second argument is no longer a closure taking a state as in 0.2.0.

Ah okay, I get that. Thank you.

To get it working with the iced_aw Modal (the only way, I get it at least) is the way in your example. To thanks for that. I’m not satisfied with it, but after all it is working atm.