egui icon indicating copy to clipboard operation
egui copied to clipboard

RetainedImage slips when used in ui.horizontal

Open SeenLenz opened this issue 3 years ago • 2 comments

Describe the bug So, I'm trying to build a music player, and at the beginning of a row, I want to display a default image if there isn't one set for the song. However, I would also like to add additional options such as a hamburger menu etc, to the row. for this I need to use ui.horizontal, however, when I do this, the retained image slips into the down. I have tried doing the same thing but with the current way of displaying images and it worked perfectly fine. (also I'm aware that currently, my code is dgsht, but I'm simply working out how to use egui and what's what, in small parts of the program before actually building it)

To Reproduce Steps to reproduce the behavior: fn render_main(ctx: &egui::Context){ let song = RetainedImage::from_image_bytes("1.png",include_bytes!("1.png")).unwrap();

    CentralPanel::default().show(ctx, |ui|{

    ScrollArea::vertical().auto_shrink([false,false]).show(ui, |ui|{
        egui::Grid::new("somadse_unique_id")
        .num_columns(1)
        .spacing([40.0, 4.0])
        .min_col_width(ui.available_width())
        .min_row_height(80.)
        .striped(true)
        .show(ui, |ui|{
            for i in 1..20{20
                song.show(ui);
                ui.end_row();
            };
            });
        });
    });

}

Expected behavior The expected behavior would be that each row, has an image at the beginning followed by the rest.

Screenshots Expected: Screenshot from 2022-07-15 12-10-55

Bug: Screenshot from 2022-07-15 12-10-08

Desktop (please complete the following information):

  • Manjaro Linux x86_64 kernel: 5.15.53-1-MANJARO
  • opera
  • 89

SeenLenz avatar Jul 15 '22 10:07 SeenLenz

Please make a minimal reproduce (full code)

emilk avatar Jul 21 '22 18:07 emilk

Hi, Sorry it took this long. I made a minimal reproduce which u can simple copy+paste into: egui/examples/retanined_image:

#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release

use eframe::egui;
use egui::Vec2;
use egui_extras::RetainedImage;

fn main() {
    let options = eframe::NativeOptions {
        initial_window_size: Some(egui::vec2(500.0, 900.0)),
        ..Default::default()
    };
    eframe::run_native(
        "Show an image with eframe/egui",
        options,
        Box::new(|_cc| Box::new(MyApp::default())),
    );
}

struct MyApp {
    image: RetainedImage,
}

impl Default for MyApp {
    fn default() -> Self {
        Self {
            image: RetainedImage::from_image_bytes(
                "rust-logo-256x256.png",
                include_bytes!("rust-logo-256x256.png"),
            )
            .unwrap(),
        }
    }
}

impl eframe::App for MyApp {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        egui::CentralPanel::default().show(ctx, |ui| {
            ui.heading("This is an image:");

            egui::ScrollArea::vertical().show(ui, |ui| {
            
                egui::Grid::new("some_unique_id").striped(true).show(ui, |ui| {
                    
                    for i in 1..20{
                    
                        ui.horizontal(|ui|{
                            self.image.show(ui);
                            ui.label("asdasdajdhaskjdgaskjhasgkjashgdaskjhdg")
                            
                        });
                    
                    
                        ui.end_row();
                    
                    }
                });
            });
        });
    }
}

SeenLenz avatar Aug 22 '22 11:08 SeenLenz

I'm not sure of the internal mechanisms here but explicitly setting the interact y spacing to the height of the image before creating the horizontal layout fixes this issue, e.g.

ui.spacing_mut().interact_size.y = self.image.size().y;

ui.horizontal(|ui|{
    self.image.show(ui);
    ui.label("asdasdajdhaskjdgaskjhasgkjashgdaskjhdg")                        
});

rminderhoud avatar Aug 18 '23 21:08 rminderhoud