Skip to content

Add some RAII-style wrappers for windows, children, tree nodes, etc. - #2197

Open
sethk wants to merge 19 commits into
ocornut:masterfrom
sethk:raii
Open

sethk wants to merge 19 commits into
ocornut:masterfrom
sethk:raii

Conversation

@sethk

@sethk sethk commented Nov 18, 2018

Copy link
Copy Markdown

As discussed in #2096, here is an additional header file with some RAII-style wrappers for common ImGui objects.

@learn-more

Copy link
Copy Markdown
Contributor

@sethk There are some generated classes that are outdated now:
PushAllowKeyboardFocus should be replaced with PushTabStop
BeginChildFrame could be replaced with BeginChild(..., ImGuiChildFlags_FrameStyle, ...);, or removed outright.
BeginPopupContextWindow does not take a boolean, but an ImGuiPopupFlags

@LRDPRDX

LRDPRDX commented Nov 4, 2026

Copy link
Copy Markdown

Nice suggestion. I've also come up with a similar idea. If you're interested here is my addition:

template<std::size_t N>
struct Color
{
    Color (const std::pair<ImGuiCol, ImVec4> (&colors)[N], bool condition = true) :
        condition(condition)
    {
        if(condition)
            for(const auto& [code, color] : colors)
                ImGui::PushStyleColor(code, color);
    }

    ~Color()
    {
        if(condition)
            ImGui::PopStyleColor(N);
    }

    bool condition { true };
};

and the usage:

    context::Color c ({
        { ImGuiCol_TitleBg,           colorByName("red") },
        { ImGuiCol_TitleBgActive,     colorByName("red") },
        { ImGuiCol_ResizeGrip,        colorByName("red") },
        { ImGuiCol_ResizeGripHovered, colorByName("redLighter") },
        { ImGuiCol_ResizeGripActive,  colorByName("redLight") },
        { ImGuiCol_Button,            colorByName("red") },
        { ImGuiCol_ButtonHovered,     colorByName("redLighter") },
        { ImGuiCol_ButtonActive,      colorByName("redLight") },
        { ImGuiCol_FrameBg,           colorByName("red") },
    }, not gui.errors.empty());

I use it mostly to highlight some windows in case of errors:

image

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.

4 participants