diff --git a/packages/dear-imgui/cpp/app/src/xframes_commit.cpp b/packages/dear-imgui/cpp/app/src/xframes_commit.cpp index aa4c5f21..4006ee4d 100644 --- a/packages/dear-imgui/cpp/app/src/xframes_commit.cpp +++ b/packages/dear-imgui/cpp/app/src/xframes_commit.cpp @@ -105,14 +105,26 @@ CommitResult XFrames::ApplyCommitOperations(CommitBatch& batch) { } } operationIndex.reset(); - // Disconnect every previous Yoga owner before attaching any - // candidate child. Moving out of a removed ancestor retains the - // exact Element, Yoga node, internal subject and populated data. + // Only parents whose child list changed are re-linked. Re-linking an + // unchanged parent marks its Yoga subtree dirty and makes the next + // frame lay it out again. A child that moves leaves a parent whose + // list changes too, or one that is destroyed; both are disconnected + // before any candidate child is attached. Moving out of a removed + // ancestor retains the exact Element, Yoga node, internal subject and + // populated data. + std::unordered_set relink; + for (const auto& [parent, children] : plan.children) { + if (parent == 0) continue; + const auto previous = m_hierarchy.find(parent); + if (!m_publicationOwnedIds.contains(parent) || previous == m_hierarchy.end() || previous->second != children) + relink.insert(parent); + } for (int id : m_publicationOwnedIds) - YGNodeRemoveAllChildren(m_elements.at(id)->m_layoutNode->m_node); + if (relink.contains(id) || !plan.ownedIds.contains(id)) + YGNodeRemoveAllChildren(m_elements.at(id)->m_layoutNode->m_node); for (const auto& [parent, children] : plan.children) { m_hierarchy[parent] = children; - if (parent == 0) continue; + if (parent == 0 || !relink.contains(parent)) continue; auto* element = m_elements.at(parent).get(); for (size_t index = 0; index < children.size(); ++index) element->m_layoutNode->InsertChild(m_elements.at(children[index])->m_layoutNode.get(), index); diff --git a/packages/dear-imgui/cpp/tests/xframes_test.cpp b/packages/dear-imgui/cpp/tests/xframes_test.cpp index ba640316..122cff86 100644 --- a/packages/dear-imgui/cpp/tests/xframes_test.cpp +++ b/packages/dear-imgui/cpp/tests/xframes_test.cpp @@ -162,6 +162,25 @@ TEST_F(XFramesTest, PublicationReorderAndInsertionKeepHierarchyAndYogaConsistent EXPECT_EQ(Node(state, 1)["yogaChildren"], json::array({3, 4, 2})); EXPECT_EQ(Node(state, 4)["yogaParent"], 1); } +// Re-linking a parent marks its Yoga subtree dirty, so the next frame lays it out +// again. A publication re-links only parents whose child list changed: a sibling +// subtree that did not change keeps its layout. +TEST_F(XFramesTest, PublicationRelinksOnlyParentsWhoseChildrenChanged) { + Publish({{0, {1}}, {1, {2, 3}}, {2, {4}}, {3, {5}}, {4, {}}, {5, {}}}, + {Create(1), Create(2), Create(3), Create(4), Create(5)}); + const auto yoga = [&](int id) { return ElementAt(id)->m_layoutNode->m_node; }; + YGNodeCalculateLayout(yoga(1), 100, 100, YGDirectionLTR); + ASSERT_FALSE(YGNodeIsDirty(yoga(3))); + EXPECT_EQ(Publish({{0, {1}}, {1, {2, 3}}, {2, {6}}, {3, {5}}, {6, {}}, {5, {}}}, {Create(6)}).destroyedIds, + (std::vector{4})); + EXPECT_TRUE(YGNodeIsDirty(yoga(2))); + EXPECT_FALSE(YGNodeIsDirty(yoga(3))); + EXPECT_FALSE(YGNodeIsDirty(yoga(5))); + const auto state = xf->GetDiagnosticsState(); + EXPECT_EQ(Node(state, 2)["yogaChildren"], json::array({6})); + EXPECT_EQ(Node(state, 3)["yogaChildren"], json::array({5})); + EXPECT_EQ(Node(state, 1)["yogaChildren"], json::array({2, 3})); +} TEST_F(XFramesTest, PublicationIdenticalAndEmptyTreesAdvanceExactlyOnce) { EXPECT_EQ(Publish().nativeRevision, 1); EXPECT_EQ(Publish({{0, {1}}, {1, {}}}, {Create(1)}).nativeRevision, 2);