Skip to content

TextAlignedV(): better handling of cursor position and DC boundary#9455

Open
dkosmari wants to merge 1 commit into
ocornut:masterfrom
dkosmari:upstream-text-aligned
Open

TextAlignedV(): better handling of cursor position and DC boundary#9455
dkosmari wants to merge 1 commit into
ocornut:masterfrom
dkosmari:upstream-text-aligned

Conversation

@dkosmari

Copy link
Copy Markdown

The current TextAlignedV() leaves the position and boundary in an inconsistent state, that limits its usefulness.

With this PR:

  • The widget always has width width (parameter was called size_x before), unless:
    • width == 0 means the real text width, as obtained by CalcTextSize().x. Before, size_x == 0 was giving me no rendered output, as if it was actually clipping the text to a zero-width region.
    • width < 0 means ContentRegionAvail().x.
  • Cursor position actually matches the right of the displayed text.
  • DC boundary is updated according to the actual displayed text.

Here's a test code (call test_text_aligned() from any example code), showing the bounding box, cursor position behavior, and how it behaves inside table cells:

void show_bounding_box()
{
    ImVec2 min = ImGui::GetItemRectMin();
    ImVec2 max = ImGui::GetItemRectMax();
    ImU32 col = ImGui::GetColorU32(ImVec4{1.0f, 0.0f, 0.0f, 0.5f});
    ImDrawList* draw_list = ImGui::GetCurrentWindow()->DrawList;
    draw_list->AddRect(min, max, col);
}

void test_text_aligned()
{
    static float width = 200.0f;
    static float align = 0.0f;
    if (ImGui::Begin("Test TextAligned()")) {

        ImGui::TextWrapped("Test cursor positioning and bounding box.");

        ImVec2 available = ImGui::GetContentRegionAvail();
        ImGui::SliderFloat("align", &align, 0.0f, 1.0f);
        ImGui::DragFloat("width", &width, 1.0f, -1.0f, available.x, "%.0f");
        ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {0, 0});
        if (ImGui::BeginChild("container", {},
                              ImGuiChildFlags_Borders | ImGuiChildFlags_AutoResizeY,
                              ImGuiWindowFlags_HorizontalScrollbar)) {
            ImGui::AlignTextToFramePadding();
            ImGui::TextAligned(align, width, "Irish wristwatch");
            show_bounding_box();
            ImGui::SameLine();
            ImGui::Button("##test", {10, 0});

            ImGui::Button("##test2", {10, 0});
            ImGui::SameLine();
            ImGui::TextAligned(align, width, "Rear wheel drive");
            show_bounding_box();
        }
        ImGui::EndChild();
        ImGui::PopStyleVar();

        ImGui::Separator();

        ImGui::TextWrapped("Test inside a table.");

        if (ImGui::BeginTable("table", 3,
                              ImGuiTableFlags_Borders |
                              ImGuiTableFlags_Resizable)) {

            static float apple_align  = 0.0f;
            static float banana_align = 0.5f;
            static float cherry_align = 1.0f;

            ImGui::TableNextRow(ImGuiTableRowFlags_Headers);
            ImGui::TableNextColumn();
            ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
            ImGui::SliderFloat("##apple_align", &apple_align, 0.0f, 1.0f);
            ImGui::TableNextColumn();
            ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
            ImGui::SliderFloat("##banana_align", &banana_align, 0.0f, 1.0f);
            ImGui::TableNextColumn();
            ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
            ImGui::SliderFloat("##cherry_align", &cherry_align, 0.0f, 1.0f);

            ImGui::TableNextRow();
            ImGui::TableNextColumn();
            ImGui::TextAligned(apple_align, -1.0f, "Apple");
            ImGui::TableNextColumn();
            ImGui::TextAligned(banana_align, -1.0f, "Banana");
            ImGui::TableNextColumn();
            ImGui::TextAligned(cherry_align, -1.0f, "Cherry");

            ImGui::EndTable();
        }
    }
    ImGui::End();
}

Here are images showing different outputs:

Screenshot from 2026-06-25 21-38-16 Screenshot from 2026-06-25 21-38-30 Screenshot from 2026-06-25 21-38-44 Screenshot from 2026-06-25 21-39-30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants