imgui icon indicating copy to clipboard operation
imgui copied to clipboard

Coloring Part of Window Title

Open abramann opened this issue 1 year ago • 5 comments

Is there any possible way to color a special part of window title?
Like :

ImGui::Begin("Colored #FF00FF Title ");

abramann avatar Dec 17 '22 12:12 abramann

You can modify ImGuiCol_Text:

ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(0xff, 0x00, 0xff, 0xff));
ImGui::SetNextWindowSize(ImVec2(200, 200));

ImGui::Begin("Test window");
ImGui::PopStyleColor();

ImGui::End();

averne avatar Dec 17 '22 13:12 averne

You cannot change only one part of the text color, you have to change all of it as shown by averne.

If you're willing to poke at Dear ImGui internals, you can manually draw to the title bar using PushClipRect.

Keep in mind this will not work on the docking branch. (See https://github.com/ocornut/imgui/issues/5115)

Also keep in mind that since this relies on Dear ImGui internals it might break in a future version without warning.

Here's a quick and dirty example:

// Text passed to Begin doesn't actually matter and will not be rendered because of the ##,
// just has to be unique. See FAQ for details:
// https://github.com/ocornut/imgui/blob/91b356cf8e3f56c5457100ddbe93785e54048f22/docs/FAQ.md#q-about-the-id-stack-system
ImGui::Begin("##GH-6002 Colorful Title");
ImVec2 windowStart = ImGui::GetCursorPos();
ImGuiStyle& style = ImGui::GetStyle();
const ImGuiWindow* window = ImGui::GetCurrentWindow();
const ImRect titleBarRect = window->TitleBarRect();
ImGui::PushClipRect(titleBarRect.Min, titleBarRect.Max, false);
{
    // The offsets here are based on ImGui::RenderWindowTitleBarContents (namely the pad_l calculcation)
    // This is over-simplified and does not handle ImGuiWindowFlags_NoCollapse, different
    // values of WindowMenuButtonPosition, or the close button for small windows.
    // Handling these elegantly if needed is an exercise left to the reader.
    // This also doesn't handle WindowTitleAlign
    ImGui::SetCursorPos({ style.FramePadding.x + ImGui::GetCurrentContext()->FontSize + style.ItemInnerSpacing.x, style.FramePadding.y });
    ImGui::Text("GH-6002");

    ImGui::PushStyleColor(ImGuiCol_Text, { 1.f, 0.f, 0.f, 1.f });
    ImGui::SameLine();
    ImGui::Text("Colorful");
    ImGui::PopStyleColor();

    ImGui::SameLine();
    ImGui::Text("Title");
}
ImGui::PopClipRect();

ImGui::SetCursorPos(windowStart);
ImGui::Text("This is a window with a colorful title!");
ImGui::End();

This will give you a window which looks like this:

image

If you find yourself needing rich text frequently you might consider checking out this fork: https://github.com/ocornut/imgui/pull/5288

PathogenDavid avatar Dec 18 '22 13:12 PathogenDavid

You cannot change only one part of the text color,

That’s correct. For future reference about that, linking to #902

ocornut avatar Dec 18 '22 13:12 ocornut

This I understand. Thanks everyone. Does there exist a graphic of the definitions of all the margins, spacings, and paddings that contribute to proper positioning of a UI element, such as a line of text?

JustaSimpleUser avatar Dec 27 '22 00:12 JustaSimpleUser

There’s a topic (unsure where at the moment, on my phone) where someone drew a partial one but it’s been suggested, and I agree, we should create an interactive window to visualize those values!

ocornut avatar Dec 27 '22 01:12 ocornut

Closing this, with #902 being the general/main topic for this.

ocornut avatar Mar 23 '23 17:03 ocornut