lampsitter / egui_commonmark

Markdown viewer for egui
Apache License 2.0
82 stars 19 forks source link

CommonMarkViewer creates empty space above it after each update when used in a `Grid` #41

Open zeozeozeo opened 6 months ago

zeozeozeo commented 6 months ago

https://github.com/lampsitter/egui_commonmark/assets/108888572/fbc8ef9c-ed92-4ffe-9e3d-c11066ebf683

Minimal reproducible example with eframe:

use eframe::egui;
use egui_commonmark::{CommonMarkCache, CommonMarkViewer};

fn main() {
    let native_options = eframe::NativeOptions::default();
    eframe::run_native(
        "Commonmark bug",
        native_options,
        Box::new(|_cc| Box::new(App::default())),
    )
    .expect("failed to run app");
}

#[derive(Default)]
struct App {
    cache: CommonMarkCache,
    content: String,
}

impl eframe::App for App {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        egui::CentralPanel::default().show(ctx, |ui| {
            egui::Grid::new("my_grid")
                .num_columns(2)
                .striped(true)
                .show(ui, |ui| {
                    ui.label("row 1, col 1");
                    ui.label("row 1, col 2");
                    ui.end_row();
                    ui.label("row 2, col 1");
                    CommonMarkViewer::new("my_commonmark")
                        .max_image_width(Some(512))
                        .show(ui, &mut self.cache, &self.content);
                });
        });
        self.content += "a";
        ctx.request_repaint();
    }
}
zeozeozeo commented 6 months ago

Here's how the attached example looks:

https://github.com/lampsitter/egui_commonmark/assets/108888572/392da3a0-47e8-4c4b-92ba-e7a0b268c576

lampsitter commented 6 months ago

Repo without CommonMarkViewer

use eframe::egui;

fn main() {
    let native_options = eframe::NativeOptions::default();
    eframe::run_native(
        "Commonmark bug",
        native_options,
        Box::new(|_cc| Box::new(App::default())),
    )
    .expect("failed to run app");
}

#[derive(Default)]
struct App {
    content: String,
}

impl eframe::App for App {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        egui::CentralPanel::default().show(ctx, |ui| {
            egui::Grid::new("my_grid")
                .num_columns(2)
                .striped(true)
                .show(ui, |ui| {
                    ui.label("row 1, col 1");
                    ui.label("row 1, col 2");
                    ui.end_row();
                    ui.label("row 2, col 1");

                    let layout =
                        egui::Layout::left_to_right(egui::Align::BOTTOM).with_main_wrap(true);
                    ui.allocate_ui_with_layout(egui::vec2(200.0, 0.0), layout, |ui| {
                        ui.label(&self.content);
                    });
                });
        });
        self.content += "a";
        ctx.request_repaint();
    }

with_main_wrap is guilty. My naive guess is that the layout grows downwards, but the grid tries to center it, so it allocates space on both sides which is what causes the growth. I have tried different Align options but it does not appear to change anything.

I don't know whether this is a case of using egui wrong or if it's a bug in Grid.