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

ComboBox menu doesn't report its actual size to its closure #4452

Open
abey79 opened this issue May 3, 2024 · 3 comments
Open

ComboBox menu doesn't report its actual size to its closure #4452

abey79 opened this issue May 3, 2024 · 3 comments
Labels
bug Something is broken egui rerun Desired for Rerun.io

Comments

@abey79
Copy link
Collaborator

abey79 commented May 3, 2024

Context

Consider the following code:

#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
#![allow(rustdoc::missing_crate_level_docs)] // it's an example

use eframe::egui;

fn main() -> Result<(), eframe::Error> {
    env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
    let options = eframe::NativeOptions {
        viewport: egui::ViewportBuilder::default().with_inner_size([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_fn);
    }
}

fn ui_fn(ui: &mut egui::Ui) {
    egui::ComboBox::from_id_source("combo")
        .selected_text("ComboBox")
        .width(100.0)
        .show_ui(ui, |ui| {
            ui.label("Hello");
            ui.label("World");
        });
}

The combobox correctly sets the width of its menu according to width(100.0).

image

Let's add some longer content:

fn ui_fn(ui: &mut egui::Ui) {
    egui::ComboBox::from_id_source("combo")
        .selected_text("ComboBox")
        .width(100.0)
        .show_ui(ui, |ui| {
            ui.label("Hello");
            ui.label("World");
            ui.label("Hellooooooooooooooooooooooooo");
        });
}

The menu automatically increases its size to accommodate the longer content. Nice.

image

Expected

The ui passed to the closure in show_ui should report its actual size, which is presumably computed from last frame content. This is useful e.g. to know the full span of the menu, should the content want to have full-span highlighting (aka rerun's ListItem).

Actual

The ui reflects the base, unexpended width.

fn ui_fn(ui: &mut egui::Ui) {
    egui::ComboBox::from_id_source("combo")
        .selected_text("ComboBox")
        .width(100.0)
        .show_ui(ui, |ui| {
            ui.ctx()
                .debug_painter()
                .debug_rect(ui.max_rect(), egui::Color32::RED, "");

            ui.label("Hello");
            ui.label("World");
            ui.label("Hellooooooooooooooooooooooooo");
        });
}
image

Workaround

Manually set the width to the desired value, which requires knowing a upper bound for the content width.

fn ui_fn(ui: &mut egui::Ui) {
    egui::ComboBox::from_id_source("combo")
        .selected_text("ComboBox")
        .width(100.0)
        .show_ui(ui, |ui| {
            ui.set_width(220.0);

            ui.ctx()
                .debug_painter()
                .debug_rect(ui.max_rect(), egui::Color32::RED, "");

            ui.label("Hello");
            ui.label("World");
            ui.label("Hellooooooooooooooooooooooooo");
        });
}
image
@abey79
Copy link
Collaborator Author

abey79 commented May 3, 2024

I realise now that this might be "working as intended". If the ui would be directly set to last frame's max width, there would be no means for the menu to ever shrink if the content eventually reduces its size.

I'm not really sure how to go about this.

@emilk
Copy link
Owner

emilk commented May 6, 2024

This is a very similar problem to:

@Myprivateclonelibrary
Copy link

Myprivateclonelibrary commented May 22, 2024

Snipaste_2024-05-22_09-44-20

Hi. When will this bug be fixed?
Maybe use Textedit

Snipaste_2024-05-22_09-55-38

Snipaste_2024-05-22_09-55-46

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is broken egui rerun Desired for Rerun.io
Projects
None yet
Development

No branches or pull requests

3 participants