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
21.64k stars 1.56k forks source link

Respect leading spaces for rendering #1272

Open Elinvynia opened 2 years ago

Elinvynia commented 2 years ago

Is your feature request related to a problem? Please describe. I'm trying to align a cursor for menu entries.

                let cursor = if some_condition {
                    RichText::new("> ").color(Color32::LIGHT_GRAY).text_style(TextStyle::Monospace)
                } else {
                    RichText::new("  ").color(Color32::LIGHT_GRAY).text_style(TextStyle::Monospace)
                };

                ui.label(cursor);
                ui.label("Some Text");

Currently, the else condition never renders. There has to be an actual symbol besides just regular spaces. I'd prefer not to mess with the fonts as I like the default ones.

Describe the solution you'd like If there is a space character it was most likely put there deliberately and thus should be rendered by default.

Describe alternatives you've considered Using a custom font with some space-equivalent-yet-renderable character.

Additional context I've tried looking into the source code but unfortunately couldn't figure out where the is_empty call is made that is causing this.

Hunter522 commented 2 years ago

I came across your issue when I ran into similar problem. Was trying to get a label to always be 3 digits wide (using leading spaces) and horizontally centered on a row.

Here's a workaround I came up with that works for me:

// self.val is f32
let val_formatted = format!("{:>3}", self.val);
let text_size = WidgetText::from(val_formatted.clone())
    .into_galley(ui, None, ui.available_width(), TextStyle::Monospace)
    .size();
ui.allocate_ui_with_layout(text_size, Layout::right_to_left(), |ui| {
    ui.label(RichText::new(val_formatted).monospace());
});
emilk commented 2 years ago

This seems like a bug in the text layout code!

parasyte commented 1 year ago

FWIW, you can use the non-breaking space encoded \u{a0} to get these examples to work. E.g.:

const NBSP: &str = "\u{a0}";
RichText::new("{NBSP}{NBSP}")