Demo: use ImGui version of TreeNodeGetOpen/TreeNodeSetOpen + comments.

This commit is contained in:
ocornut
2026-02-18 16:47:30 +01:00
parent 635a5c0a12
commit 0653a0d42a
2 changed files with 17 additions and 20 deletions

View File

@@ -290,6 +290,14 @@ ImGuiDemoMarkerCallback GImGuiDemoMarkerCallback = NULL;
void* GImGuiDemoMarkerCallbackUserData = NULL; void* GImGuiDemoMarkerCallbackUserData = NULL;
#define IMGUI_DEMO_MARKER(section) do { if (GImGuiDemoMarkerCallback != NULL) GImGuiDemoMarkerCallback("imgui_demo.cpp", __LINE__, section, GImGuiDemoMarkerCallbackUserData); } while (0) #define IMGUI_DEMO_MARKER(section) do { if (GImGuiDemoMarkerCallback != NULL) GImGuiDemoMarkerCallback("imgui_demo.cpp", __LINE__, section, GImGuiDemoMarkerCallbackUserData); } while (0)
// Sneakily forward declare functions which aren't worth putting in public API yet
namespace ImGui
{
IMGUI_API void ShowFontAtlas(ImFontAtlas* atlas);
IMGUI_API bool TreeNodeGetOpen(ImGuiID storage_id);
IMGUI_API void TreeNodeSetOpen(ImGuiID storage_id, bool is_open);
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// [SECTION] Demo Window / ShowDemoWindow() // [SECTION] Demo Window / ShowDemoWindow()
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@@ -1746,9 +1754,6 @@ static void DemoWindowWidgetsDragsAndSliders()
// [SECTION] DemoWindowWidgetsFonts() // [SECTION] DemoWindowWidgetsFonts()
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Forward declare ShowFontAtlas() which isn't worth putting in public API yet
namespace ImGui { IMGUI_API void ShowFontAtlas(ImFontAtlas* atlas); }
static void DemoWindowWidgetsFonts() static void DemoWindowWidgetsFonts()
{ {
IMGUI_DEMO_MARKER("Widgets/Fonts"); IMGUI_DEMO_MARKER("Widgets/Fonts");
@@ -3079,16 +3084,6 @@ static void DemoWindowWidgetsSelectionAndMultiSelect(ImGuiDemoWindowData* demo_d
} }
} }
static bool TreeNodeGetOpen(ExampleTreeNode* node)
{
return ImGui::GetStateStorage()->GetBool((ImGuiID)node->UID);
}
static void TreeNodeSetOpen(ExampleTreeNode* node, bool open)
{
ImGui::GetStateStorage()->SetBool((ImGuiID)node->UID, open);
}
// When closing a node: 1) close and unselect all child nodes, 2) select parent if any child was selected. // When closing a node: 1) close and unselect all child nodes, 2) select parent if any child was selected.
// FIXME: This is currently handled by user logic but I'm hoping to eventually provide tree node // FIXME: This is currently handled by user logic but I'm hoping to eventually provide tree node
// features to do this automatically, e.g. a ImGuiTreeNodeFlags_AutoCloseChildNodes etc. // features to do this automatically, e.g. a ImGuiTreeNodeFlags_AutoCloseChildNodes etc.
@@ -3096,11 +3091,11 @@ static void DemoWindowWidgetsSelectionAndMultiSelect(ImGuiDemoWindowData* demo_d
{ {
// Recursive close (the test for depth == 0 is because we call this on a node that was just closed!) // Recursive close (the test for depth == 0 is because we call this on a node that was just closed!)
int unselected_count = selection->Contains((ImGuiID)node->UID) ? 1 : 0; int unselected_count = selection->Contains((ImGuiID)node->UID) ? 1 : 0;
if (depth == 0 || TreeNodeGetOpen(node)) if (depth == 0 || ImGui::TreeNodeGetOpen((ImGuiID)node->UID))
{ {
for (ExampleTreeNode* child : node->Childs) for (ExampleTreeNode* child : node->Childs)
unselected_count += TreeCloseAndUnselectChildNodes(child, selection, depth + 1); unselected_count += TreeCloseAndUnselectChildNodes(child, selection, depth + 1);
TreeNodeSetOpen(node, false); ImGui::TreeNodeSetOpen((ImGuiID)node->UID, false);
} }
// Select root node if any of its child was selected, otherwise unselect // Select root node if any of its child was selected, otherwise unselect
@@ -3134,7 +3129,7 @@ static void DemoWindowWidgetsSelectionAndMultiSelect(ImGuiDemoWindowData* demo_d
{ {
if (node->Parent != NULL) // Root node isn't visible nor selectable in our scheme if (node->Parent != NULL) // Root node isn't visible nor selectable in our scheme
selection->SetItemSelected((ImGuiID)node->UID, selected); selection->SetItemSelected((ImGuiID)node->UID, selected);
if (node->Parent == NULL || TreeNodeGetOpen(node)) if (node->Parent == NULL || ImGui::TreeNodeGetOpen((ImGuiID)node->UID))
for (ExampleTreeNode* child : node->Childs) for (ExampleTreeNode* child : node->Childs)
TreeSetAllInOpenNodes(child, selection, selected); TreeSetAllInOpenNodes(child, selection, selected);
} }
@@ -3154,7 +3149,7 @@ static void DemoWindowWidgetsSelectionAndMultiSelect(ImGuiDemoWindowData* demo_d
return NULL; return NULL;
// Recurse into childs. Query storage to tell if the node is open. // Recurse into childs. Query storage to tell if the node is open.
if (curr_node->Childs.Size > 0 && TreeNodeGetOpen(curr_node)) if (curr_node->Childs.Size > 0 && ImGui::TreeNodeGetOpen((ImGuiID)curr_node->UID))
return curr_node->Childs[0]; return curr_node->Childs[0];
// Next sibling, then into our own parent // Next sibling, then into our own parent
@@ -9463,7 +9458,7 @@ struct ExampleAppPropertyEditor
Filter.Build(); Filter.Build();
ImGui::PopItemFlag(); ImGui::PopItemFlag();
if (ImGui::BeginTable("##bg", 1, ImGuiTableFlags_RowBg)) if (ImGui::BeginTable("##list", 1, ImGuiTableFlags_RowBg))
{ {
for (ExampleTreeNode* node : root_node->Childs) for (ExampleTreeNode* node : root_node->Childs)
if (Filter.PassFilter(node->Name)) // Filter root node if (Filter.PassFilter(node->Name)) // Filter root node

View File

@@ -6726,6 +6726,8 @@ bool ImGui::TreeNodeExV(const void* ptr_id, ImGuiTreeNodeFlags flags, const char
return TreeNodeBehavior(id, flags, label, label_end); return TreeNodeBehavior(id, flags, label, label_end);
} }
// The reason those two functions are not yet in public API is because I would like to design a more feature-full and generic API for this.
// They are otherwise function (cc: #3823, #9251, #7553, #6754, #5423, #2958, #2079, #1947, #1131, #722)
bool ImGui::TreeNodeGetOpen(ImGuiID storage_id) bool ImGui::TreeNodeGetOpen(ImGuiID storage_id)
{ {
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
@@ -6733,11 +6735,11 @@ bool ImGui::TreeNodeGetOpen(ImGuiID storage_id)
return storage->GetInt(storage_id, 0) != 0; return storage->GetInt(storage_id, 0) != 0;
} }
void ImGui::TreeNodeSetOpen(ImGuiID storage_id, bool open) void ImGui::TreeNodeSetOpen(ImGuiID storage_id, bool is_open)
{ {
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
ImGuiStorage* storage = g.CurrentWindow->DC.StateStorage; ImGuiStorage* storage = g.CurrentWindow->DC.StateStorage;
storage->SetInt(storage_id, open ? 1 : 0); storage->SetInt(storage_id, is_open ? 1 : 0);
} }
bool ImGui::TreeNodeUpdateNextOpen(ImGuiID storage_id, ImGuiTreeNodeFlags flags) bool ImGui::TreeNodeUpdateNextOpen(ImGuiID storage_id, ImGuiTreeNodeFlags flags)