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

Ui::vertical_centered consumes all available horizontal space. #2815

Open
a2aaron opened this issue Mar 15, 2023 · 2 comments
Open

Ui::vertical_centered consumes all available horizontal space. #2815

a2aaron opened this issue Mar 15, 2023 · 2 comments
Labels
bug Something is broken

Comments

@a2aaron
Copy link

a2aaron commented Mar 15, 2023

Describe the bug
When using ui.vertical_centered, it is impossible to stack multiple instances of the vertical group horizontally because each group seems to allocate all available horizontal space, even if the widget does not take up that much horizontal space. This causes the second vertical group to be ran off-screen and makes it hard to implement more complex layouts.

To Reproduce
Steps to reproduce the behavior:

  1. Run the following program. A window similar to the first screenshot should appear.
use eframe::egui;

fn main() -> Result<(), eframe::Error> {
    let options = eframe::NativeOptions {
        initial_window_size: Some(egui::vec2(320.0, 240.0)),
        ..Default::default()
    };
    eframe::run_native("My egui App", options, Box::new(|_cc| Box::new(MyApp)))
}

struct MyApp;

impl eframe::App for MyApp {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        egui::CentralPanel::default().show(ctx, |ui| {
            ui.horizontal(|ui| {
                ui.vertical_centered(|ui| {
                    ui.label("A");
                });
                ui.vertical_centered(|ui| {
                    ui.label("B");
                });
            });
        });
    }
}
  1. Change ui.vertical_centered to ui.vertical and rerun. A window similar to the second screenshot should appear.

Expected behavior

Screenshots
Here is the behavior of ui.vertical_centered. Notice how the label for "B" is all the way to the right and partially offscreen.
Image showing the behavior of vertical_centered. The B text is all the way to the right, partially offscreen.

Here is the behavior of ui.vertical. This is the behavior that I would expect vertical_centered to also do (or, if not that, at least have both A and B appear on screen).
Image showing the behavior of vertical. The A and B text are next to each other.

Desktop (please complete the following information):

  • OS: MacOS, Monterrey 12.6
  • Version
  • egui Version: latest commit on master (at time of writing, this is commit 59c2c4c
    Additional context
    I do not think this is an issue with the label itself requesting too much space, since similar behavior happens when creating buttons or spinners. Also, if this isn't a bug but instead intended behavior, then I think this behavior should be more clearly documented, since it seems like ui.vertical_centered_justified should do what this ui.vertical_centered currently does.
@a2aaron a2aaron added the bug Something is broken label Mar 15, 2023
@Luis-Weinzierl
Copy link

I can confirm that this also happens on Fedora Linux 36 Workstation with Wayland and Gnome 42.6, so this may be a non OS-specfic problem.

@grigorizaika
Copy link

grigorizaika commented May 25, 2023

Both ui.horizontal_centered() and ui.vertical_centered() need to be in an area with known height or width, respectively.
My guess would be that the layout needs to know where it ends to know where its center is.

If you know what the minimum size of the contents will be, put each ui.vertical_centered() inside ui.allocate_ui() (or any other method that pre-allocates some size for you). The vertical_centered layout will expand to fit the allocated area and will center inside this area.

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

No branches or pull requests

3 participants