bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
35.54k stars 3.52k forks source link

`\t` tab escape sequence support in text #9761

Open ickshonpe opened 1 year ago

ickshonpe commented 1 year ago

What problem does this solve or what need does it fill?

from the display_and_visibility example:

 let bottom_frame = commands.spawn(NodeBundle {
        style: Style {
            flex_direction: FlexDirection::Row,
            align_items: AlignItems::Start,
            justify_content: JustifyContent::Start,
            column_gap: Val::Px(10.),
            ..Default::default()
        },
        ..default() })
        .with_children(|builder| {
            let text_style = TextStyle {
                font: asset_server.load("fonts/FiraSans-Bold.ttf"),
                font_size: 20.0,
                color: Color::WHITE,
            };
            builder.spawn(TextBundle {
                text: Text::from_section(
                    "Display::None\nVisibility::Hidden\nVisibility::Inherited",
                    TextStyle { color: HIDDEN_COLOR, ..text_style.clone() }
                ).with_alignment(TextAlignment::Center),
                ..Default::default()
            });
            builder.spawn(TextBundle {
                text: Text::from_section(
                    "-\n-\n-",
                    TextStyle { color: Color::DARK_GRAY, ..text_style.clone() }
                ).with_alignment(TextAlignment::Center),
                ..Default::default()
            });
            builder.spawn(TextBundle::from_section(
                "The UI Node and its descendants will not be visible and will not be allotted any space in the UI layout.\nThe UI Node will not be visible but will still occupy space in the UI layout.\nThe UI node will inherit the visibility property of its parent. If it has no parent it will be visible.",
                text_style
            ));
        }).id();

In order to format the text it is displayed in three vertical columns. It's extremely unclear what this code is meant to do without running the example to see the displayed output.

A more natural way to write this would be to use tab escape sequences:

commands.spawn(
        TextBundle::from_section(
            "Display::None\t-\tThe UI Node and its descendants will not be visible and will not be allotted any space in the UI layout.\n\
            Visibility::Hidden\t-\tThe UI Node will not be visible but will still occupy space in the UI layout.\n\
            Visibility::Inherited\t-\tThe UI node will inherit the visibility property of its parent. If it has no parent it will be visible.",
        TextStyle {
            font: asset_server.load("fonts/FiraSans-Bold.ttf"),
            font_size: 20.0,
            color: Color::WHITE,
        }).with_alignment(TextAlignment::Center),
    );

What solution would you like?

It seems tricky to add tabs from within bevy_text itself and glyph_brush_layout ignores \t. Maybe the cosmic text implementation will support tabs, I'm not sure.

rparrett commented 1 year ago

Rendering tabs would be nice. But I don't know if I've ever seen an implementation that would render as expected. I think there's usually some sort of column width limitation if tabs are supported at all.

Here's a selection of examples from a few apps on this machine:

image

The bottom is cosmic-text's editor-libcosmic example, so I don't think it can save us in this instance.

ickshonpe commented 1 year ago

Yeah it's not a great example, maybe using grid would be a better approach there.

I'm starting to have some really terrible ideas about how to hack tabs into current bevy, need to find something useful to do before I waste my time implementing them.

rparrett commented 1 month ago

On Bevy main:

image

It looks like support for tabs was added in cosmic-text 0.12 https://github.com/pop-os/cosmic-text/blob/main/CHANGELOG.md#0120---2024-06-18

rparrett commented 1 month ago

I would like to test in a cosmic-text example again to see if being able to modify the tab width would get the behavior you were after.

rparrett commented 1 month ago

It does seem that this would be fixable if we exposed the tab width option for cosmic buffers.

(Text above modified to only include first tab of each line, and tab width set to 11)

image