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

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

Open
zeozeozeo opened this issue Mar 27, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@zeozeozeo
Copy link
Contributor

gridbug.mp4

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
Copy link
Contributor Author

Here's how the attached example looks:

gridbug2.mp4

@lampsitter lampsitter added the bug Something isn't working label Mar 27, 2024
@lampsitter
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants