emilk / egui

egui: an easy-to-use immediate mode GUI in Rust that runs on both web and native
https://www.egui.rs/
Apache License 2.0
22.44k stars 1.6k forks source link

`menu_button` children get "flattened" #5377

Closed sdasda7777 closed 4 hours ago

sdasda7777 commented 4 hours ago

Describe the bug I noticed this bug on 0.28, but it happens on 0.29 too. Didn't seem like an exact match with anything already reported, closest would probably be #5138? Basically the menu_button children get flattened when a condition (it being empty) is triggered.

image

To Reproduce Here's a demo: to trigger the bug, just click children under one of the menu_buttons until it is empty (one will be moved to the other one upon each click), then click the other one at least once (to restore at least one child to the original menu_button).

use eframe::egui;

struct MyApp {
    vector_a: Vec<String>,
    vector_b: Vec<String>,
}

impl Default for MyApp {
    fn default() -> Self {
        Self {
            vector_a: vec!["Item A1".to_string(), "Item A2".to_string(), "Item A3".to_string()],
            vector_b: vec!["Item B1".to_string(), "Item B2".to_string(), "Item B3".to_string()],
        }
    }
}

impl eframe::App for MyApp {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        egui::CentralPanel::default().show(ctx, |ui| {
            ui.label("Move items between two lists using the menu.");

            // Create a menu bar
            egui::menu::bar(ui, |ui| {
                ui.menu_button("Move Items", |ui| {
                    // First sub-button for vector_a
                    ui.menu_button("From A to B:", |ui| {
                        if self.vector_a.iter().any(|e| ui.button(e).clicked()) {
                            self.vector_b.push(self.vector_a.pop().unwrap());
                        }
                    });

                    // Second sub-button for vector_b
                    ui.menu_button("From B to A:", |ui| {
                        if self.vector_b.iter().any(|e| ui.button(e).clicked()) {
                            self.vector_a.push(self.vector_b.pop().unwrap());
                        }
                    });
                });
            });
        });
    }
}

fn main() -> Result<(), eframe::Error> {
    eframe::run_native(
        "Simple Egui App",
        Default::default(),
        Box::new(|_cc| Ok(Box::new(MyApp::default()))),
    )
}

Expected behavior

image

Desktop (please complete the following information):

sdasda7777 commented 4 hours ago

My bad, this indeed seems to be the same issue as #5138