Debug Tools: ID Stack Tool: extracted code into a StackToolGetResultAsPath() function.

This commit is contained in:
ocornut
2025-11-13 16:14:45 +01:00
parent ec6219752d
commit 5a0c67c4b8
2 changed files with 33 additions and 29 deletions

View File

@@ -17900,19 +17900,44 @@ static int StackToolFormatLevelInfo(ImGuiIDStackTool* tool, int n, bool format_f
{ {
ImGuiStackLevelInfo* info = &tool->Results[n]; ImGuiStackLevelInfo* info = &tool->Results[n];
ImGuiWindow* window = (info->DescOffset == -1 && n == 0) ? ImGui::FindWindowByID(info->ID) : NULL; ImGuiWindow* window = (info->DescOffset == -1 && n == 0) ? ImGui::FindWindowByID(info->ID) : NULL;
if (window) // Source: window name (because the root ID don't call GetID() and so doesn't get hooked) if (window) // Source: window name (because the root ID don't call GetID() and so doesn't get hooked)
return ImFormatString(buf, buf_size, format_for_ui ? "\"%s\" [window]" : "%s", ImHashSkipUncontributingPrefix(window->Name)); return ImFormatString(buf, buf_size, format_for_ui ? "\"%s\" [window]" : "%s", ImHashSkipUncontributingPrefix(window->Name));
if (info->QuerySuccess) // Source: GetID() hooks (prioritize over ItemInfo() because we frequently use patterns like: PushID(str), Button("") where they both have same id) if (info->QuerySuccess) // Source: GetID() hooks (prioritize over ItemInfo() because we frequently use patterns like: PushID(str), Button("") where they both have same id)
return ImFormatString(buf, buf_size, (format_for_ui && info->DataType == ImGuiDataType_String) ? "\"%s\"" : "%s", ImHashSkipUncontributingPrefix(&tool->ResultPathsBuf.Buf[info->DescOffset])); return ImFormatString(buf, buf_size, (format_for_ui && info->DataType == ImGuiDataType_String) ? "\"%s\"" : "%s", ImHashSkipUncontributingPrefix(&tool->ResultPathsBuf.Buf[info->DescOffset]));
if (tool->StackLevel < tool->Results.Size) // Only start using fallback below when all queries are done, so during queries we don't flickering ??? markers. if (tool->StackLevel < tool->Results.Size) // Only start using fallback below when all queries are done, so during queries we don't flickering ??? markers.
return (*buf = 0); return (*buf = 0);
#ifdef IMGUI_ENABLE_TEST_ENGINE #ifdef IMGUI_ENABLE_TEST_ENGINE
if (const char* label = ImGuiTestEngine_FindItemDebugLabel(GImGui, info->ID)) // Source: ImGuiTestEngine's ItemInfo() if (const char* label = ImGuiTestEngine_FindItemDebugLabel(GImGui, info->ID)) // Source: ImGuiTestEngine's ItemInfo()
return ImFormatString(buf, buf_size, format_for_ui ? "??? \"%s\"" : "%s", ImHashSkipUncontributingPrefix(label)); return ImFormatString(buf, buf_size, format_for_ui ? "??? \"%s\"" : "%s", ImHashSkipUncontributingPrefix(label));
#endif #endif
return ImFormatString(buf, buf_size, "???"); return ImFormatString(buf, buf_size, "???");
} }
static const char* StackToolGetResultAsPath(ImGuiIDStackTool* tool, bool hex_encode_non_ascii_chars)
{
ImGuiTextBuffer* buf = &tool->ResultFullPathBuf;
buf->resize(0);
for (int stack_n = 0; stack_n < tool->Results.Size; stack_n++)
{
char level_desc[256];
StackToolFormatLevelInfo(tool, stack_n, false, level_desc, IM_ARRAYSIZE(level_desc));
buf->append(stack_n == 0 ? "//" : "/");
for (const char* p = level_desc; *p != 0; )
{
unsigned int c;
const char* p_next = p + ImTextCharFromUtf8(&c, p, NULL);
if (c == '/')
buf->append("\\");
if (c < 256 || !hex_encode_non_ascii_chars)
buf->append(p, p_next);
else for (; p < p_next; p++)
buf->appendf("\\x%02x", (unsigned char)*p);
p = p_next;
}
}
return buf->c_str();
}
// ID Stack Tool: Display UI // ID Stack Tool: Display UI
void ImGui::ShowIDStackToolWindow(bool* p_open) void ImGui::ShowIDStackToolWindow(bool* p_open)
{ {
@@ -17927,27 +17952,7 @@ void ImGui::ShowIDStackToolWindow(bool* p_open)
// Display hovered/active status // Display hovered/active status
ImGuiIDStackTool* tool = &g.DebugIDStackTool; ImGuiIDStackTool* tool = &g.DebugIDStackTool;
const char* result_path = StackToolGetResultAsPath(tool, tool->OptHexEncodeNonAsciiChars);
// Build and display path
tool->ResultTempBuf.resize(0);
for (int stack_n = 0; stack_n < tool->Results.Size; stack_n++)
{
char level_desc[256];
StackToolFormatLevelInfo(tool, stack_n, false, level_desc, IM_ARRAYSIZE(level_desc));
tool->ResultTempBuf.append(stack_n == 0 ? "//" : "/");
for (const char* p = level_desc; *p != 0; )
{
unsigned int c;
const char* p_next = p + ImTextCharFromUtf8(&c, p, NULL);
if (c == '/')
tool->ResultTempBuf.append("\\");
if (c < 256 || !tool->OptHexEncodeNonAsciiChars)
tool->ResultTempBuf.append(p, p_next);
else for (; p < p_next; p++)
tool->ResultTempBuf.appendf("\\x%02x", (unsigned char)*p);
p = p_next;
}
}
Text("0x%08X", tool->QueryMainId); Text("0x%08X", tool->QueryMainId);
SameLine(); SameLine();
MetricsHelpMarker("Hover an item with the mouse to display elements of the ID Stack leading to the item's final ID.\nEach level of the stack correspond to a PushID() call.\nAll levels of the stack are hashed together to make the final ID of a widget (ID displayed at the bottom level of the stack).\nRead FAQ entry about the ID stack for details."); MetricsHelpMarker("Hover an item with the mouse to display elements of the ID Stack leading to the item's final ID.\nEach level of the stack correspond to a PushID() call.\nAll levels of the stack are hashed together to make the final ID of a widget (ID displayed at the bottom level of the stack).\nRead FAQ entry about the ID stack for details.");
@@ -17964,14 +17969,13 @@ void ImGui::ShowIDStackToolWindow(bool* p_open)
if (tool->OptCopyToClipboardOnCtrlC && Shortcut(ImGuiMod_Ctrl | ImGuiKey_C, ImGuiInputFlags_RouteGlobal | ImGuiInputFlags_RouteOverFocused)) if (tool->OptCopyToClipboardOnCtrlC && Shortcut(ImGuiMod_Ctrl | ImGuiKey_C, ImGuiInputFlags_RouteGlobal | ImGuiInputFlags_RouteOverFocused))
{ {
tool->CopyToClipboardLastTime = (float)g.Time; tool->CopyToClipboardLastTime = (float)g.Time;
SetClipboardText(tool->ResultTempBuf.c_str()); SetClipboardText(result_path);
} }
Text("- Path \"%s\"", tool->ResultTempBuf.c_str()); Text("- Path \"%s\"", result_path);
#ifdef IMGUI_ENABLE_TEST_ENGINE #ifdef IMGUI_ENABLE_TEST_ENGINE
Text("- Label \"%s\"", tool->QueryMainId ? ImGuiTestEngine_FindItemDebugLabel(&g, tool->QueryMainId) : ""); Text("- Label \"%s\"", tool->QueryMainId ? ImGuiTestEngine_FindItemDebugLabel(&g, tool->QueryMainId) : "");
#endif #endif
Separator(); Separator();
// Display decorated stack // Display decorated stack

View File

@@ -2140,7 +2140,7 @@ struct ImGuiIDStackTool
bool OptCopyToClipboardOnCtrlC; bool OptCopyToClipboardOnCtrlC;
float CopyToClipboardLastTime; float CopyToClipboardLastTime;
ImGuiTextBuffer ResultPathsBuf; ImGuiTextBuffer ResultPathsBuf;
ImGuiTextBuffer ResultTempBuf; ImGuiTextBuffer ResultFullPathBuf;
ImGuiIDStackTool() { memset(this, 0, sizeof(*this)); LastActiveFrame = -1; OptHexEncodeNonAsciiChars = true; CopyToClipboardLastTime = -FLT_MAX; } ImGuiIDStackTool() { memset(this, 0, sizeof(*this)); LastActiveFrame = -1; OptHexEncodeNonAsciiChars = true; CopyToClipboardLastTime = -FLT_MAX; }
}; };