Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Most recently shown Modal is not always on top #5788

Closed
vkahl opened this issue Mar 13, 2025 · 2 comments · Fixed by #5800
Closed

Most recently shown Modal is not always on top #5788

vkahl opened this issue Mar 13, 2025 · 2 comments · Fixed by #5800
Labels
bug Something is broken

Comments

@vkahl
Copy link

vkahl commented Mar 13, 2025

Describe the bug

When showing two or more new Modals in one frame, the last one is not always on top (the order seems to alternate).

Expected behavior

The last Modal which is shown via call to it's show-method should always be on top as stated in the docs:

You can show multiple modals on top of each other. The topmost modal will always be the most recently shown one.

To Reproduce

Create an empty project, add eframe as dependency and copy the following source code into main.rs:

fn main() {
    eframe::run_native(
        "modal order test",
        eframe::NativeOptions::default(),
        Box::new(|_cctx| Ok(Box::new(App::default()))),
    )
    .unwrap();
}

#[derive(Default)]
struct App {
    modal_a: bool,
    modal_b: bool,
}

impl eframe::App for App {
    fn update(&mut self, ctx: &eframe::egui::Context, _frame: &mut eframe::Frame) {
        eframe::egui::CentralPanel::default().show(ctx, |ui| {
            if ui.button("Show modals").clicked() {
                self.modal_a = true;
                self.modal_b = true;
            }
        });

        if self.modal_a {
            eframe::egui::Modal::new("modal a".into()).show(ctx, |ui| {
                ui.label("A");
                if ui.button("Close").clicked() {
                    self.modal_a = false;
                }
            });
        }
        if self.modal_b {
            eframe::egui::Modal::new("modal b".into()).show(ctx, |ui| {
                ui.label("B");
                if ui.button("Close").clicked() {
                    self.modal_b = false;
                }
            });
        }
    }
}

From my understanding of the docs, modal "B" should always be on top of modal "A" after pressing the "Show modals"-button. Both Modals are newly created in the same frame, but modal "B" is shown last in the update-method. The first time I click "Show modals", it is correct, but after closing both modals and clicking "Show modals" again, "A" is on top of "B". The next time it's switched again, etc.

Desktop

  • OS: debian testing, XFCE
@vkahl vkahl added the bug Something is broken label Mar 13, 2025
@lucasmerlin
Copy link
Collaborator

Huh, when implementing modals I didn't think of opening multiple modals at once. The modal order just users the underlying layer order, and I don't think it's defined what happens when multiple layers are newly shown on the same frame.

We should at least update the documentation to clarify that opening multiple modals / layers at once will have a undefined order.

Is this something you need for your application?

@vkahl
Copy link
Author

vkahl commented Mar 13, 2025

I see, then don't worry about it! I don't need it for an application, I was just developing/experimenting with a "modal manager" which makes it convenient to spawn modals and attach callbacks to them. When trying to spawn multiple modals on top of each other using my "modal manager" I noticed this behavior. Initially I didn't know if it's my codes fault (which it turned out it wasn't), so yes, a little note about this in the docs would be appreciated.

@vkahl vkahl closed this as completed Mar 13, 2025
lucasmerlin added a commit that referenced this issue Mar 18, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
…#5800)

* Closes #5788 
* [x] I have followed the instructions in the PR template
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is broken
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants