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.28k stars 1.6k forks source link

Grid layout breaks if it contains horizontal #3045

Open micfong-z opened 1 year ago

micfong-z commented 1 year ago

Describe the bug Grid layout show incorrect margin when ui.horizontal() is used inside as a cell.

In the following screenshot:

Screenshot 2023-06-01 at 22 25 12

My guess for the reason

Note: I have not checked or verified if this is the actual problem, and it is only my best guess.

Looking at the first row only, I'm thinking that egui may have pre-calculated the size of horizontal once and put Label 1 into the correct position, however egui falsely shifted the horizontal downwards as well to match the top of Label 1. This applies to the rest of the rows.

image

To Reproduce Here's the code used to reproduce the problem

egui::Window::new("Window").show(ctx, |ui| {
    egui::Grid::new("grid_test").show(ui, |ui| {
        ui.label("Label1");  
        ui.horizontal(|ui| {  
            ui.label("This is the longest label\nwith 4 lines\nthis is the 3rd line\nand the last line here.");
        });
        ui.end_row();

        ui.label("Label2");
        ui.horizontal(|ui| {  
            ui.label("This is a shorter label\nwith 2 lines only.");
        });
        ui.end_row();

        ui.label("Label3");
        ui.horizontal(|ui| {               
            ui.label("This is a long label\nwith 3 lines\nand here is the last line.");
        });
        ui.end_row();
    })
});

Expected behavior The grid layout should function as normal, with correct margins between each line.

Desktop information

micfong-z commented 1 year ago

Might be related to #1830, but only RetainedImage is mentioned there.

yrns commented 2 months ago

I think this is a limitation of immediate mode. Ui::horizontal assumes your content is spacing().interact_size.y tall and centers vertically based on that. If you know the height of your content you can set that value, or try Ui:horizontal_top. Ui::horizontal_centered can also work, but that eats up all available vertical height, which might also be problematic depending on your layout.

I believe this is the same issue at #1830 as you pointed out. Grid uses Ui::horizontal internally.