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

[Close Window] Is there a method that can close window by its own button #805

Closed
HammerBu opened this issue Oct 16, 2021 · 4 comments
Closed

Comments

@HammerBu
Copy link

When it comes an alert message or some input situation. The simplest case is that we have two buttons "yes" and "no", we can click one of them and then the window will close automatically. At the same time, parent widget will get the choice we did.

I tried to change "open" parameter but comes a reference conflict. And I tried to do something like "frame.quit" but failed. Also in the demo app, I could not find a demo to reproduce such a situation.

@parasyte
Copy link
Contributor

In my app, I use a VecDeque to hold a list of error messages and closures for what to do when one of the buttons are pressed. Here's the code. And here's where it is currently used for an example of the flexibility this kind of dynamic setup provides. There's also some useful documentation right here.

In short, you don't need to use the open method with an exclusively borrowed bool. You can use any state you want to conditionally create (and hide) a window. Even the obviously naive solution:

if self.show_error {
    egui::Window::new("Error").show(ctx, |ui| {
        if ui.button("Close").clicked() {
            self.show_error = false;
        }
    });
}

@HammerBu
Copy link
Author

In my app, I use a VecDeque to hold a list of error messages and closures for what to do when one of the buttons are pressed. Here's the code. And here's where it is currently used for an example of the flexibility this kind of dynamic setup provides. There's also some useful documentation right here.

In short, you don't need to use the open method with an exclusively borrowed bool. You can use any state you want to conditionally create (and hide) a window. Even the obviously naive solution:

if self.show_error {
    egui::Window::new("Error").show(ctx, |ui| {
        if ui.button("Close").clicked() {
            self.show_error = false;
        }
    });
}

Thank you so much! It works!

@Tudyx
Copy link

Tudyx commented Mar 22, 2023

Work also for me. The little inconvenience of this method is that the window is no longer closable by the classic cross on the to right corner. The workaround I found is using 2 booleans.

@parasyte
Copy link
Contributor

Yeah, this is for sure a limitation of the current design.

You might be able to use a temporary bool for the open() method. It's definitely more convoluted this way. I'm sure any advancements in the design to allow multiple ways to close an egui::Window would be awesome. (Maybe using a channel or something?)

let mut show_error = self.show_error;
if show_error {
    egui::Window::new("Error")
        .open(&mut show_error)
        .show(ctx, |ui| {
            if ui.button("Close").clicked() {
                self.show_error = false;
            }
        });
}
self.show_error &= show_error;

tivrfoa added a commit to tivrfoa/headlines that referenced this issue Aug 13, 2023
`toggle_config` is initialized to false, and then passed to
`.open(toggle_config)`, so the config does not appear.

This change also fixes closing the config window when
the user press Enter, see:
emilk/egui#805 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants