Skip to content

Commit

Permalink
Disabled: nested tooltips or other non-child window within a BeginDis…
Browse files Browse the repository at this point in the history
…abled() block disable the disabled state. (#211, #7640)
  • Loading branch information
ocornut committed May 31, 2024
1 parent e47015a commit f953ebf
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ Other changes:
grab scrolls by one page, holding mouse button repeats scrolling. (#7328, #150)
- Combo: simplified Combo() API uses a list clipper (due to its api it wasn't
previously trivial before we added clipper.IncludeItemByIndex() function).
- Disabled: nested tooltips or other non-child window within a BeginDisabled()
block disable the disabled state. (#211, #7640)
- Misc: made ImGuiDir and ImGuiSortDirection stronger-typed enums.


Expand Down
40 changes: 38 additions & 2 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6648,6 +6648,11 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags)
// [EXPERIMENTAL] Skip Refresh mode
UpdateWindowSkipRefresh(window);

// Nested root windows (typically tooltips) override disabled state
if (window->RootWindow == window)
if ((window->DC.BackupItemDisabled = (g.CurrentItemFlags & ImGuiItemFlags_Disabled) != 0))
BeginDisabledOverrideReenable();

// We intentionally set g.CurrentWindow to NULL to prevent usage until when the viewport is set, then will call SetCurrentWindow()
g.CurrentWindow = NULL;

Expand Down Expand Up @@ -7259,6 +7264,8 @@ void ImGui::End()
if (!window->SkipRefresh)
PopClipRect(); // Inner window clip rectangle
PopFocusScope();
if (window->RootWindow == window && window->DC.BackupItemDisabled)
EndDisabledOverrideReenable();

if (window->SkipRefresh)
{
Expand Down Expand Up @@ -7523,7 +7530,7 @@ void ImGui::BeginDisabled(bool disabled)
}
if (was_disabled || disabled)
g.CurrentItemFlags |= ImGuiItemFlags_Disabled;
g.ItemFlagsStack.push_back(g.CurrentItemFlags);
g.ItemFlagsStack.push_back(g.CurrentItemFlags); // FIXME-OPT: can we simply skip this and use DisabledStackSize?
g.DisabledStackSize++;
}

Expand All @@ -7540,6 +7547,29 @@ void ImGui::EndDisabled()
g.Style.Alpha = g.DisabledAlphaBackup; //PopStyleVar();
}

// Could have been called BeginDisabledDisable() but it didn't want to be award nominated for most awkward function name.
// Ideally we would use a shared e.g. BeginDisabled()->BeginDisabledEx() but earlier needs to be optimal.
// The whole code for this is awkward, will reevaluate if we find a way to implement SetNextItemDisabled().
void ImGui::BeginDisabledOverrideReenable()
{
ImGuiContext& g = *GImGui;
IM_ASSERT(g.CurrentItemFlags & ImGuiItemFlags_Disabled);
g.Style.Alpha = g.DisabledAlphaBackup;
g.CurrentItemFlags &= ~ImGuiItemFlags_Disabled;
g.ItemFlagsStack.push_back(g.CurrentItemFlags);
g.DisabledStackSize++;
}

void ImGui::EndDisabledOverrideReenable()
{
ImGuiContext& g = *GImGui;
g.DisabledStackSize--;
IM_ASSERT(g.DisabledStackSize > 0);
g.ItemFlagsStack.pop_back();
g.CurrentItemFlags = g.ItemFlagsStack.back();
g.Style.Alpha = g.DisabledAlphaBackup * g.Style.DisabledAlpha;
}

void ImGui::PushTabStop(bool tab_stop)
{
PushItemFlag(ImGuiItemFlags_NoTabStop, !tab_stop);
Expand Down Expand Up @@ -9995,7 +10025,13 @@ void ImGui::ErrorCheckEndWindowRecover(ImGuiErrorLogCallback log_callback, vo
while (g.DisabledStackSize > stack_sizes->SizeOfDisabledStack) //-V1044
{
if (log_callback) log_callback(user_data, "Recovered from missing EndDisabled() in '%s'", window->Name);
EndDisabled();
if (g.CurrentItemFlags & ImGuiItemFlags_Disabled)
EndDisabled();
else
{
EndDisabledOverrideReenable();
window->DC.BackupItemDisabled = false;
}
}
while (g.ColorStack.Size > stack_sizes->SizeOfColorStack)
{
Expand Down
2 changes: 1 addition & 1 deletion imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
// Library Version
// (Integer encoded as XYYZZ for use in #if preprocessor conditionals, e.g. '#if IMGUI_VERSION_NUM >= 12345')
#define IMGUI_VERSION "1.90.8 WIP"
#define IMGUI_VERSION_NUM 19072
#define IMGUI_VERSION_NUM 19073
#define IMGUI_HAS_TABLE

/*
Expand Down
3 changes: 3 additions & 0 deletions imgui_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2472,6 +2472,7 @@ struct IMGUI_API ImGuiWindowTempData
bool NavWindowHasScrollY; // Set per window when scrolling can be used (== ScrollMax.y > 0.0f)

// Miscellaneous
bool BackupItemDisabled; // Non-child window override disabled flag
bool MenuBarAppending; // FIXME: Remove this
ImVec2 MenuBarOffset; // MenuBarOffset.x is sort of equivalent of a per-layer CursorPos.x, saved/restored as we switch to the menu bar. The only situation when MenuBarOffset.y is > 0 if when (SafeAreaPadding.y > FramePadding.y), often used on TVs.
ImGuiMenuColumns MenuColumns; // Simplified columns storage for menu items measurement
Expand Down Expand Up @@ -3127,6 +3128,8 @@ namespace ImGui
IMGUI_API void PushItemFlag(ImGuiItemFlags option, bool enabled);
IMGUI_API void PopItemFlag();
IMGUI_API const ImGuiDataVarInfo* GetStyleVarInfo(ImGuiStyleVar idx);
IMGUI_API void BeginDisabledOverrideReenable();
IMGUI_API void EndDisabledOverrideReenable();

// Logging/Capture
IMGUI_API void LogBegin(ImGuiLogType type, int auto_open_depth); // -> BeginCapture() when we design v2 api, for now stay under the radar by using the old name.
Expand Down

0 comments on commit f953ebf

Please sign in to comment.