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

Make child's parent fit the child, in new_child #5654

Open
JerzySpendel opened this issue Jan 30, 2025 · 0 comments
Open

Make child's parent fit the child, in new_child #5654

JerzySpendel opened this issue Jan 30, 2025 · 0 comments

Comments

@JerzySpendel
Copy link
Contributor

JerzySpendel commented Jan 30, 2025

Is your feature request related to a problem? Please describe.
I use bevy and bevy_egui libraries. I have this approach that my widget-system-functions receive as an input a part of Ui (owned Ui struct) and add widgets on it. The only way to pass Ui part to the bevy system function that I could figure out, was to create an owned Ui structure by new_child and insert it to entity for later execution. The problem is that when creating an owned version of Ui I have to allocate the space manually before, for example:

        egui::Window::new("Adjust CPU Usage")       
            .open(&mut window_open.0)
            .max_size(egui::Vec2::new(300.0, 100.0))
            .show(ctx, |ui| {
                let response = ui.allocate_space(ui.available_size());
                let child_ui = ui.new_child(
                    UiBuilder::default()
                        .max_rect(egui::Rect::from_min_size(response.1.min, response.1.size())),
                );

                ui_callbacks.push(UiCallback {
                    system_id: programs_cpu_usage_system_id,
                    ui: child_ui,
                });
            });

The problem here is that in fact only a fraction of the original allocated space might get used, leaving the rest of allocated space unusable and oftentimes the parent ui too big.

Describe the solution you'd like
I would expect new_child to work similarly to for example allocate_new_ui where the parent Ui will shrink to match the size of child ui.

Describe alternatives you've considered
I considered using on of: allocate_new_ui, allocate_ui but they expect callback function. In my design I want my widget function to also be bevy systems, that look something like this:

fn total_cpu_usage(In(mut ui): In<Ui>, programs: Query<&Program>, cpu: Query<&Cpu>) {
    let total_usage = programs
        .iter()
        .map(|program| program.cpu_usage)
        .sum::<f32>();

    let bar = egui::ProgressBar::new(total_usage).show_percentage();
    ui.label(format!(
        "CPU ({:.2} GHz): ",
        cpu.get_single().unwrap().frequency
    ));
    ui.add(bar);
}

and this architecture actually works, except for the flaw that is described in first point.

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

1 participant